forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 9
/
sharded_quadratic_program_test.cc
168 lines (151 loc) · 7 KB
/
sharded_quadratic_program_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2010-2024 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "ortools/pdlp/sharded_quadratic_program.h"
#include <limits>
#include <optional>
#include "Eigen/Core"
#include "gtest/gtest.h"
#include "ortools/base/gmock.h"
#include "ortools/pdlp/quadratic_program.h"
#include "ortools/pdlp/sharder.h"
#include "ortools/pdlp/test_util.h"
namespace operations_research::pdlp {
namespace {
using ::testing::ElementsAre;
const double kInfinity = std::numeric_limits<double>::infinity();
TEST(ShardedQuadraticProgramTest, BasicTest) {
const int num_threads = 2;
const int num_shards = 10;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
const int primal_size = 2;
const int dual_size = 1;
EXPECT_EQ(sharded_qp.DualSize(), dual_size);
EXPECT_EQ(sharded_qp.PrimalSize(), primal_size);
EXPECT_THAT(ToDense(sharded_qp.TransposedConstraintMatrix()),
EigenArrayEq<double>({{1}, {1}}));
EXPECT_EQ(sharded_qp.ConstraintMatrixSharder().NumElements(), primal_size);
EXPECT_EQ(sharded_qp.DualSharder().NumElements(), dual_size);
EXPECT_EQ(sharded_qp.PrimalSharder().NumElements(), primal_size);
EXPECT_EQ(sharded_qp.TransposedConstraintMatrixSharder().NumElements(),
dual_size);
}
TEST(ShardedQuadraticProgramTest, SwapVariableBounds) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
Eigen::VectorXd new_lb{{-kInfinity, 0.0}};
Eigen::VectorXd new_ub{{0.0, 1.0}};
sharded_qp.SwapVariableBounds(new_lb, new_ub);
EXPECT_THAT(new_lb, ElementsAre(1.0, -2.0));
EXPECT_THAT(new_ub, ElementsAre(2.0, 4.0));
EXPECT_THAT(sharded_qp.Qp().variable_lower_bounds,
ElementsAre(-kInfinity, 0.0));
EXPECT_THAT(sharded_qp.Qp().variable_upper_bounds, ElementsAre(0.0, 1.0));
}
TEST(ShardedQuadraticProgramTest, SwapConstraintBounds) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
Eigen::VectorXd new_lb{{1.0}};
Eigen::VectorXd new_ub{{5.0}};
sharded_qp.SwapConstraintBounds(new_lb, new_ub);
EXPECT_THAT(new_lb, ElementsAre(-kInfinity));
EXPECT_THAT(new_ub, ElementsAre(1.0));
EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds, ElementsAre(1.0));
EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds, ElementsAre(5.0));
}
TEST(ShardedQuadraticProgramTest, SetConstraintBounds) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
sharded_qp.SetConstraintBounds(/*constraint_index=*/0, /*lower_bound=*/-1.0,
/*upper_bound=*/10.0);
EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds, ElementsAre(-1.0));
EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds, ElementsAre(10.0));
}
TEST(ShardedQuadraticProgramTest, SetConstraintLowerBound) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
sharded_qp.SetConstraintBounds(/*constraint_index=*/0, /*lower_bound=*/-1.0,
/*upper_bound=*/std::nullopt);
EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds, ElementsAre(-1.0));
EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds, ElementsAre(1.0));
}
TEST(ShardedQuadraticProgramTest, SetConstraintUpperBound) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
sharded_qp.SetConstraintBounds(/*constraint_index=*/0,
/*lower_bound=*/std::nullopt,
/*upper_bound=*/10.0);
EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds, ElementsAre(-kInfinity));
EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds, ElementsAre(10.0));
}
TEST(ShardedQuadraticProgramTest, SwapObjectiveVector) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
Eigen::VectorXd new_objective{{1.0, 2.0}};
sharded_qp.SwapObjectiveVector(new_objective);
EXPECT_THAT(new_objective, ElementsAre(-1.0, -1.0));
EXPECT_THAT(sharded_qp.Qp().objective_vector, ElementsAre(1.0, 2.0));
}
TEST(RescaleProblem, BasicTest) {
// `TestDiagonalQp1()` is:
// min 4x_1^2 + x_2^2 - x_1 - x_2 +5
// s.t. -inf <= x_1 + x_2 <=1,
// 1<=x_1<=2, -2<=x_2<=4.
// After rescaling with `row_scaling_vec` [0.5] and `col_scaling_vec` [1,0.5],
// `sharded_qp` becomes:
// min 4x_1^2 + 0.25x_2^2 - x_1 - 0.5x_2 +5
// s.t. -inf <= 0.5x_1 + 0.25x_2 <=1,
// 1<=x_1<=2, -4<=x_2<=8.
const int num_threads = 2;
const int num_shards = 10;
ShardedQuadraticProgram sharded_qp(TestDiagonalQp1(), num_threads,
num_shards);
const Eigen::VectorXd col_scaling_vec{{1, 0.5}};
const Eigen::VectorXd row_scaling_vec{{0.5}};
sharded_qp.RescaleQuadraticProgram(col_scaling_vec, row_scaling_vec);
EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds, ElementsAre(-kInfinity));
EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds, ElementsAre(0.5));
EXPECT_THAT(sharded_qp.Qp().variable_lower_bounds, ElementsAre(1, -4));
EXPECT_THAT(sharded_qp.Qp().variable_upper_bounds, ElementsAre(2, 8));
EXPECT_THAT(sharded_qp.Qp().objective_vector, ElementsAre(-1, -0.5));
EXPECT_THAT(ToDense(sharded_qp.Qp().constraint_matrix),
EigenArrayEq<double>({{0.5, 0.25}}));
EXPECT_THAT(ToDense(sharded_qp.TransposedConstraintMatrix()),
EigenArrayEq<double>({{0.5}, {0.25}}));
EXPECT_THAT(sharded_qp.Qp().objective_matrix->diagonal(),
EigenArrayEq<double>({4, 0.25}));
}
TEST(ShardedQuadraticProgramTest, ReplaceLargeConstraintBoundsWithInfinity) {
const int num_threads = 2;
const int num_shards = 2;
ShardedQuadraticProgram sharded_qp(TestLp(), num_threads, num_shards);
sharded_qp.ReplaceLargeConstraintBoundsWithInfinity(3.0);
EXPECT_THAT(sharded_qp.Qp().constraint_lower_bounds,
ElementsAre(kInfinity, -kInfinity, -kInfinity, -1));
EXPECT_THAT(sharded_qp.Qp().constraint_upper_bounds,
ElementsAre(kInfinity, kInfinity, kInfinity, 1));
}
} // namespace
} // namespace operations_research::pdlp