-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
clp_solver_test.cc
225 lines (196 loc) · 6.78 KB
/
clp_solver_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "drake/solvers/clp_solver.h"
#include <gtest/gtest.h>
#include "drake/solvers/test/linear_program_examples.h"
#include "drake/solvers/test/quadratic_program_examples.h"
namespace drake {
namespace solvers {
namespace test {
TEST_P(LinearProgramTest, TestLP) {
ClpSolver solver;
prob()->RunProblem(&solver);
}
INSTANTIATE_TEST_SUITE_P(
ClpTest, LinearProgramTest,
::testing::Combine(::testing::ValuesIn(linear_cost_form()),
::testing::ValuesIn(linear_constraint_form()),
::testing::ValuesIn(linear_problems())));
TEST_F(InfeasibleLinearProgramTest0, TestInfeasible) {
ClpSolver solver;
if (solver.available()) {
auto result = solver.Solve(*prog_, {}, {});
EXPECT_EQ(result.get_solution_result(),
SolutionResult::kInfeasibleConstraints);
EXPECT_TRUE(std::isinf(result.get_optimal_cost()));
EXPECT_GT(result.get_optimal_cost(), 0.);
// This code is defined in ClpModel::status()
const int CLP_INFEASIBLE = 1;
EXPECT_EQ(result.get_solver_details<ClpSolver>().status, CLP_INFEASIBLE);
}
}
TEST_F(UnboundedLinearProgramTest0, TestUnbounded) {
ClpSolver solver;
if (solver.available()) {
auto result = solver.Solve(*prog_, {}, {});
EXPECT_FALSE(result.is_success());
EXPECT_EQ(result.get_solution_result(), SolutionResult::kUnbounded);
EXPECT_TRUE(std::isinf(result.get_optimal_cost()));
EXPECT_LT(result.get_optimal_cost(), 0.);
// This code is defined in ClpModel::status()
const int CLP_UNBOUNDED = 2;
EXPECT_EQ(result.get_solver_details<ClpSolver>().status, CLP_UNBOUNDED);
}
}
TEST_F(DuplicatedVariableLinearProgramTest1, Test) {
ClpSolver solver;
if (solver.available()) {
CheckSolution(solver);
}
}
GTEST_TEST(TestDual, DualSolution1) {
ClpSolver solver;
if (solver.available()) {
TestLPDualSolution1(solver);
}
}
GTEST_TEST(TestDual, DualSolution2) {
ClpSolver solver;
if (solver.available()) {
TestLPDualSolution2(solver);
}
}
GTEST_TEST(TestDual, DualSolution3) {
ClpSolver solver;
if (solver.available()) {
TestLPDualSolution3(solver);
}
}
GTEST_TEST(QPtest, TestUnconstrainedQP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<3>("x");
prog.AddQuadraticCost(x(0) * x(0));
ClpSolver solver;
if (solver.available()) {
auto result = solver.Solve(prog, {}, {});
EXPECT_TRUE(result.is_success());
const double tol = 1E-10;
EXPECT_NEAR(result.GetSolution(x(0)), 0, tol);
EXPECT_NEAR(result.get_optimal_cost(), 0, tol);
EXPECT_EQ(result.get_solver_details<ClpSolver>().status, 0);
}
// Add additional quadratic costs
prog.AddQuadraticCost((x(1) + x(2) - 2) * (x(1) + x(2) - 2));
if (solver.available()) {
auto result = solver.Solve(prog, {}, {});
EXPECT_TRUE(result.is_success());
const double tol = 1E-10;
EXPECT_NEAR(result.GetSolution(x(0)), 0, tol);
EXPECT_NEAR(result.GetSolution(x(1)) + result.GetSolution(x(2)), 2, tol);
EXPECT_NEAR(result.get_optimal_cost(), 0, tol);
EXPECT_EQ(result.get_solver_details<ClpSolver>().status, 0);
}
// Add linear costs.
prog.AddLinearCost(4 * x(0) + 5);
// Now the cost is (x₀ + 2)² + (x₁ + x₂ - 2)² + 1
if (solver.available()) {
auto result = solver.Solve(prog, {}, {});
EXPECT_TRUE(result.is_success());
const double tol = 1E-10;
EXPECT_NEAR(result.GetSolution(x(0)), -2, tol);
EXPECT_NEAR(result.GetSolution(x(1)) + result.GetSolution(x(2)), 2, tol);
EXPECT_NEAR(result.get_optimal_cost(), 1, tol);
EXPECT_EQ(result.get_solver_details<ClpSolver>().status, 0);
}
}
TEST_P(QuadraticProgramTest, TestQP) {
ClpSolver solver;
prob()->RunProblem(&solver);
}
INSTANTIATE_TEST_SUITE_P(
ClpTest, QuadraticProgramTest,
::testing::Combine(::testing::ValuesIn(quadratic_cost_form()),
::testing::ValuesIn(linear_constraint_form()),
::testing::ValuesIn(quadratic_problems())));
GTEST_TEST(QPtest, TestUnitBallExample) {
ClpSolver solver;
if (solver.available()) {
TestQPonUnitBallExample(solver);
}
}
GTEST_TEST(QPtest, TestInfeasible) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddQuadraticCost(x(0) * x(0) + 2 * x(1) * x(1));
prog.AddLinearConstraint(x(0) + 2 * x(1) == 2);
prog.AddLinearConstraint(x(0) >= 1);
prog.AddLinearConstraint(x(1) >= 2);
ClpSolver solver;
// The program is infeasible.
if (solver.available()) {
auto result = solver.Solve(prog, {}, {});
EXPECT_EQ(result.get_solution_result(),
SolutionResult::kInfeasibleConstraints);
EXPECT_EQ(result.get_optimal_cost(),
MathematicalProgram::kGlobalInfeasibleCost);
EXPECT_EQ(result.get_solver_details<ClpSolver>().status, 1);
}
}
GTEST_TEST(ClpSolverTest, DualSolution1) {
// Test GetDualSolution().
ClpSolver solver;
TestQPDualSolution1(solver);
}
GTEST_TEST(ClpSolverTest, DualSolution2) {
// Test GetDualSolution().
// This QP has non-zero dual solution for linear inequality constraint.
ClpSolver solver;
TestQPDualSolution2(solver);
}
GTEST_TEST(ClpSolverTest, DualSolution3) {
// Test GetDualSolution().
// This QP has non-zero dual solution for the bounding box constraint.
ClpSolver solver;
TestQPDualSolution3(solver);
}
GTEST_TEST(ClpSolverTest, EqualityConstrainedQPDualSolution1) {
ClpSolver solver;
TestEqualityConstrainedQPDualSolution1(solver);
}
GTEST_TEST(ClpSolverTest, EqualityConstrainedQPDualSolution2) {
ClpSolver solver;
TestEqualityConstrainedQPDualSolution2(solver);
}
GTEST_TEST(ClpSolverTest, TestNonconvexQP) {
ClpSolver solver;
if (solver.available()) {
TestNonconvexQP(solver, true);
}
}
// This is a code coverage test, not a functional test. If the code that
// handles verbosity options has a segfault or always throws an exception,
// then this would catch it.
GTEST_TEST(ClpSolverTest, TestVerbosity) {
LinearProgram0 example(CostForm::kSymbolic, ConstraintForm::kSymbolic);
const MathematicalProgram& prog = *example.prog();
SolverOptions options;
options.SetOption(CommonSolverOption::kPrintToConsole, 1);
ClpSolver solver;
if (solver.available()) {
// This will print stuff to the console, but we don't have any
// easy way to check that.
solver.Solve(prog, {}, options);
}
}
GTEST_TEST(ClpSolverTest, TestNumericalScaling) {
ClpSolver solver;
TestLPPoorScaling1(solver);
TestLPPoorScaling2(solver);
// Try another scaling option. Set scaling equal to 2. Somehow in CLP with
// this scaling mode, the problem is not solved successfully.
SolverOptions solver_options;
solver_options.SetOption(ClpSolver::id(), "scaling", 2);
TestLPPoorScaling1(solver, false, 1E-14, solver_options);
TestLPPoorScaling2(solver, false, 1E-4, solver_options);
}
} // namespace test
} // namespace solvers
} // namespace drake