-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ipopt_solver_test.cc
361 lines (312 loc) · 11.5 KB
/
ipopt_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "drake/solvers/ipopt_solver.h"
#include <filesystem>
#include <gtest/gtest.h>
#include "drake/common/temp_directory.h"
#include "drake/solvers/mathematical_program.h"
#include "drake/solvers/test/linear_program_examples.h"
#include "drake/solvers/test/mathematical_program_test_util.h"
#include "drake/solvers/test/quadratic_program_examples.h"
#include "drake/solvers/test/second_order_cone_program_examples.h"
#ifdef DRAKE_IPOPT_SOLVER_TEST_HAS_IPOPT
#include <IpAlgTypes.hpp>
namespace {
constexpr int kIpoptMaxiterExceeded = Ipopt::MAXITER_EXCEEDED;
constexpr int kIpoptStopAtAcceptablePoint = Ipopt::STOP_AT_ACCEPTABLE_POINT;
constexpr int kIpoptLocalInfeasibility = Ipopt::LOCAL_INFEASIBILITY;
} // namespace
#else
namespace {
constexpr int kIpoptMaxiterExceeded = -1;
constexpr int kIpoptStopAtAcceptablePoint = -1;
constexpr int kIpoptLocalInfeasibility = -1;
} // namespace
#endif
namespace drake {
namespace solvers {
namespace test {
TEST_P(LinearProgramTest, TestLP) {
IpoptSolver solver;
prob()->RunProblem(&solver);
}
INSTANTIATE_TEST_SUITE_P(
IpoptTest, LinearProgramTest,
::testing::Combine(::testing::ValuesIn(linear_cost_form()),
::testing::ValuesIn(linear_constraint_form()),
::testing::ValuesIn(linear_problems())));
TEST_F(InfeasibleLinearProgramTest0, TestIpopt) {
prog_->SetInitialGuessForAllVariables(Eigen::Vector2d(1, 2));
IpoptSolver solver;
if (solver.available()) {
auto result = solver.Solve(*prog_, {}, {});
EXPECT_FALSE(result.is_success());
EXPECT_EQ(result.get_solution_result(),
SolutionResult::kInfeasibleConstraints);
EXPECT_EQ(result.get_solver_details<IpoptSolver>().status,
kIpoptLocalInfeasibility);
const Eigen::Vector2d x_val =
result.GetSolution(prog_->decision_variables());
EXPECT_NEAR(result.get_optimal_cost(), -x_val(0) - x_val(1), 1E-7);
}
}
TEST_F(UnboundedLinearProgramTest0, TestIpopt) {
prog_->SetInitialGuessForAllVariables(Eigen::Vector2d(1, 2));
prog_->SetSolverOption(IpoptSolver::id(), "diverging_iterates_tol", 1E3);
prog_->SetSolverOption(IpoptSolver::id(), "max_iter", 1000);
IpoptSolver solver;
if (solver.available()) {
auto result = solver.Solve(*prog_, {}, {});
EXPECT_EQ(result.get_solution_result(), SolutionResult::kUnbounded);
EXPECT_EQ(result.get_optimal_cost(),
-std::numeric_limits<double>::infinity());
}
}
TEST_F(DuplicatedVariableLinearProgramTest1, Test) {
IpoptSolver solver;
if (solver.available()) {
CheckSolution(solver);
}
}
TEST_P(QuadraticProgramTest, TestQP) {
IpoptSolver solver;
prob()->RunProblem(&solver);
}
INSTANTIATE_TEST_SUITE_P(
IpoptTest, QuadraticProgramTest,
::testing::Combine(::testing::ValuesIn(quadratic_cost_form()),
::testing::ValuesIn(linear_constraint_form()),
::testing::ValuesIn(quadratic_problems())));
GTEST_TEST(QPtest, TestUnitBallExample) {
IpoptSolver solver;
if (solver.available()) {
TestQPonUnitBallExample(solver);
}
}
class NoisyQuadraticCost {
public:
explicit NoisyQuadraticCost(const double max_noise)
: max_noise_(max_noise) {}
int numInputs() const { return 1; }
int numOutputs() const { return 1; }
template <typename T>
void eval(internal::VecIn<T> const& x, internal::VecOut<T>* y) const {
// Parabola with minimum at (-1, 1) with some deterministic noise applied to
// the input so derivatives will be correctish but not easily followable to
// the minimum.
// The sign of the noise alternates between calls. The magnitude of the
// noise increases from 0 to max_noise_ over the course of
// 2 * noise_counter_limit_ calls, after which it resets to 0.
double noise = std::pow(-1., noise_counter_) * max_noise_ * noise_counter_ /
noise_counter_limit_;
if (noise_counter_ >= 0) {
noise_counter_ = (noise_counter_ + 1) % noise_counter_limit_;
noise_counter_ *= -1;
} else {
noise_counter_ *= -1;
}
auto noisy_x = x(0) + noise;
y->resize(1);
(*y)(0) = (noisy_x + 1) * (noisy_x + 1) + 1;
}
private:
double max_noise_{};
mutable int noise_counter_{};
const int noise_counter_limit_{10};
};
GTEST_TEST(IpoptSolverTest, AcceptableResult) {
IpoptSolver solver;
SolverOptions options;
options.SetOption(IpoptSolver::id(), "tol", 1e-6);
options.SetOption(IpoptSolver::id(), "dual_inf_tol", 1e-6);
options.SetOption(IpoptSolver::id(), "max_iter", 10);
const VectorX<double> x_initial_guess = VectorX<double>::Ones(1);
if (solver.available()) {
double max_noise = 1e-2;
{
// Set up a program and give it a relatively large amount of noise for
// the specified tolerance.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables(1);
prog.AddCost(NoisyQuadraticCost(max_noise), x);
auto result = solver.Solve(prog, x_initial_guess, options);
// Expect to hit iteration limit
EXPECT_FALSE(result.is_success());
EXPECT_EQ(result.get_solution_result(), SolutionResult::kIterationLimit);
EXPECT_EQ(result.get_solver_details<IpoptSolver>().status,
kIpoptMaxiterExceeded);
}
options.SetOption(IpoptSolver::id(), "acceptable_tol", 1e-3);
options.SetOption(IpoptSolver::id(), "acceptable_dual_inf_tol", 1e-3);
options.SetOption(IpoptSolver::id(), "acceptable_iter", 3);
{
// Set up the same program, but provide acceptability criteria that
// should be feasible with even with the noise.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables(1);
prog.AddCost(NoisyQuadraticCost(max_noise), x);
auto result = solver.Solve(prog, x_initial_guess, options);
EXPECT_EQ(result.get_solver_details<IpoptSolver>().status,
kIpoptStopAtAcceptablePoint);
// Expect Ipopt's "STOP_AT_ACCEPTABLE_POINT" to be translated to success.
EXPECT_TRUE(result.is_success());
}
}
}
GTEST_TEST(IpoptSolverTest, QPDualSolution1) {
IpoptSolver solver;
TestQPDualSolution1(solver, {} /* solver_options */, 1e-5);
}
GTEST_TEST(IpoptSolverTest, QPDualSolution2) {
IpoptSolver solver;
TestQPDualSolution2(solver);
}
GTEST_TEST(IpoptSolverTest, QPDualSolution3) {
IpoptSolver solver;
TestQPDualSolution3(solver);
}
GTEST_TEST(IpoptSolverTest, EqualityConstrainedQPDualSolution1) {
IpoptSolver solver;
TestEqualityConstrainedQPDualSolution1(solver);
}
GTEST_TEST(IpoptSolverTest, EqualityConstrainedQPDualSolution2) {
IpoptSolver solver;
TestEqualityConstrainedQPDualSolution2(solver);
}
GTEST_TEST(IpoptSolverTest, LPDualSolution1) {
IpoptSolver solver;
TestLPDualSolution1(solver);
}
GTEST_TEST(IpoptSolverTest, LPDualSolution2) {
IpoptSolver solver;
TestLPDualSolution2(solver);
}
GTEST_TEST(IpoptSolverTest, EckhardtDualSolution) {
IpoptSolver solver;
TestEckhardtDualSolution(solver, Eigen::Vector3d(1., 1., 5.));
}
GTEST_TEST(IpoptSolverTest, TestNonconvexQP) {
IpoptSolver solver;
if (solver.available()) {
TestNonconvexQP(solver, false);
}
}
GTEST_TEST(IpoptSolverTest, TestL2NormCost) {
IpoptSolver solver;
TestL2NormCost(solver, 1e-6);
}
/* Tests the solver's processing of the verbosity options. With multiple ways
to request verbosity (common options and solver-specific options), we simply
apply a smoke test that none of the means causes runtime errors. Note, we
don't test the case where we configure the mathematical program itself; that
is resolved in SolverBase. We only need to test the options passed into
Solve(). The possible configurations are:
- No verbosity set at all (this is implicitly tested in all other tests).
- Common option explicitly set (on & off)
- Solver option explicitly set (on & off)
- Both options explicitly set (with all permutations of (on, on), etc.) */
GTEST_TEST(IpoptSolverTest, SolverOptionsVerbosity) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables(1);
prog.AddLinearConstraint(x(0) <= 3);
prog.AddLinearConstraint(x(0) >= -3);
prog.AddLinearCost(x(0));
IpoptSolver solver;
if (solver.is_available()) {
// Setting common options.
for (int print_to_console : {0, 1}) {
SolverOptions options;
options.SetOption(CommonSolverOption::kPrintToConsole, print_to_console);
solver.Solve(prog, {}, options);
}
// Setting solver options.
for (int print_to_console : {0, 2}) {
SolverOptions options;
options.SetOption(IpoptSolver::id(), "print_level", print_to_console);
solver.Solve(prog, {}, options);
}
// Setting both.
for (int common_print_to_console : {0, 1}) {
for (int solver_print_to_console : {0, 2}) {
SolverOptions options;
options.SetOption(CommonSolverOption::kPrintToConsole,
common_print_to_console);
options.SetOption(IpoptSolver::id(), "print_level",
solver_print_to_console);
solver.Solve(prog, {}, options);
}
}
}
}
// This is to verify we can set the print out file through CommonSolverOption.
GTEST_TEST(IpoptSolverTest, PrintToFile) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables(1);
prog.AddLinearConstraint(x(0) <= 3);
prog.AddLinearConstraint(x(0) >= -3);
prog.AddLinearCost(x(0));
const std::string filename = temp_directory() + "/ipopt.log";
EXPECT_FALSE(std::filesystem::exists({filename}));
SolverOptions solver_options;
solver_options.SetOption(CommonSolverOption::kPrintFileName, filename);
IpoptSolver solver;
if (solver.is_available()) {
const auto result = solver.Solve(prog, {}, solver_options);
EXPECT_TRUE(result.is_success());
EXPECT_TRUE(std::filesystem::exists({filename}));
}
}
TEST_P(TestEllipsoidsSeparation, TestSOCP) {
IpoptSolver ipopt_solver;
if (ipopt_solver.available()) {
SolveAndCheckSolution(ipopt_solver, {}, 1.E-8);
}
}
INSTANTIATE_TEST_SUITE_P(
IpoptSolverTest, TestEllipsoidsSeparation,
::testing::ValuesIn({EllipsoidsSeparationProblem::kProblem0,
EllipsoidsSeparationProblem::kProblem1,
EllipsoidsSeparationProblem::kProblem3}));
TEST_P(TestQPasSOCP, TestSOCP) {
IpoptSolver ipopt_solver;
if (ipopt_solver.available()) {
SolveAndCheckSolution(ipopt_solver);
}
}
INSTANTIATE_TEST_SUITE_P(IpoptSolverTest, TestQPasSOCP,
::testing::ValuesIn(GetQPasSOCPProblems()));
TEST_P(TestFindSpringEquilibrium, TestSOCP) {
IpoptSolver ipopt_solver;
if (ipopt_solver.available()) {
SolveAndCheckSolution(ipopt_solver, {}, 2E-3);
}
}
INSTANTIATE_TEST_SUITE_P(
IpoptSolverTest, TestFindSpringEquilibrium,
::testing::ValuesIn(GetFindSpringEquilibriumProblems()));
GTEST_TEST(TestSOCP, MaximizeGeometricMeanTrivialProblem1) {
MaximizeGeometricMeanTrivialProblem1 prob;
IpoptSolver solver;
if (solver.available()) {
const auto result = solver.Solve(prob.prog(), {}, {});
prob.CheckSolution(result, 4E-6);
}
}
GTEST_TEST(TestSOCP, MaximizeGeometricMeanTrivialProblem2) {
MaximizeGeometricMeanTrivialProblem2 prob;
IpoptSolver solver;
if (solver.available()) {
const auto result = solver.Solve(prob.prog(), {}, {});
prob.CheckSolution(result, 1.E-6);
}
}
GTEST_TEST(TestSOCP, SmallestEllipsoidCoveringProblem) {
IpoptSolver solver;
SolveAndCheckSmallestEllipsoidCoveringProblems(solver, {}, 1E-6);
}
GTEST_TEST(TestLP, PoorScaling) {
IpoptSolver solver;
TestLPPoorScaling1(solver, true, 1E-6);
TestLPPoorScaling2(solver, true, 1E-4);
}
} // namespace test
} // namespace solvers
} // namespace drake