forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibex_solver.cc
375 lines (330 loc) · 13.5 KB
/
ibex_solver.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "drake/solvers/ibex_solver.h"
#include <memory>
#include <utility>
#include <vector>
#include <fmt/format.h>
#include <ibex.h>
#include "drake/common/text_logging.h"
#include "drake/solvers/ibex_converter.h"
namespace drake {
namespace solvers {
using Eigen::VectorXd;
using std::shared_ptr;
using std::vector;
using symbolic::Expression;
using symbolic::Formula;
using symbolic::Variable;
namespace {
struct IbexOptions {
// Relative precision on the objective. See
// http://www.ibex-lib.org/doc/optim.html?highlight=eps#objective-precision-criteria
// for details.
double rel_eps_f{ibex::DefaultOptimizerConfig::default_rel_eps_f};
// Absolute precision on the objective. See
// http://www.ibex-lib.org/doc/optim.html?highlight=eps#objective-precision-criteria
// for details.
double abs_eps_f{ibex::DefaultOptimizerConfig::default_abs_eps_f};
// Equality relaxation value. See
// http://www.ibex-lib.org/doc/optim.html?highlight=eps#the-output-of-ibexopt
// for details.
double eps_h{ibex::NormalizedSystem::default_eps_h};
// Activate rigor mode (certify feasibility of equations). See
// http://www.ibex-lib.org/doc/optim.html?highlight=rigor#return-status for
// details.
bool rigor{false};
// Random seed (useful for reproducibility).
double random_seed{ibex::DefaultOptimizerConfig::default_random_seed};
// Precision on the variable.
double eps_x{ibex::DefaultOptimizerConfig::default_eps_x};
// Activate trace. Updates of loup/uplo are printed while minimizing.
// - 0 (by default): nothing is printed.
// - 1: prints every loup/uplo update.
// - 2: also prints each handled node.
int trace{0};
// Timeout (time in seconds). 0.0 indicates +∞.
double timeout{0.0};
};
IbexOptions ParseOptions(const SolverOptions& merged_options) {
IbexOptions options;
for (const auto& it : merged_options.GetOptionsDouble(IbexSolver::id())) {
if (it.first == "rel_eps_f") {
if (it.second <= 0) {
throw std::runtime_error{fmt::format(
"IbexSolver: '{option}' option should have a positive value but "
"'{value}' was provided.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
options.rel_eps_f = it.second;
continue;
}
if (it.first == "abs_eps_f") {
if (it.second <= 0) {
throw std::runtime_error{fmt::format(
"IbexSolver: '{option}' option should have a positive value but "
"'{value}' was provided.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
options.abs_eps_f = it.second;
continue;
}
if (it.first == "eps_h") {
if (it.second <= 0) {
throw std::runtime_error{fmt::format(
"IbexSolver: '{option}' option should have a positive value but "
"'{value}' was provided.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
options.eps_h = it.second;
continue;
}
if (it.first == "random_seed") {
options.random_seed = it.second;
continue;
}
if (it.first == "eps_x") {
if (it.second <= 0) {
throw std::runtime_error{fmt::format(
"IbexSolver: '{option}' option should have a positive value but "
"'{value}' was provided.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
options.eps_x = it.second;
continue;
}
if (it.first == "timeout") {
if (it.second < 0) {
throw std::runtime_error{fmt::format(
"IbexSolver: '{option}' option should have a non-negative value "
"but '{value}' was provided.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
options.timeout = it.second;
continue;
}
throw std::runtime_error{fmt::format(
"IbexSolver: unsupported option '{option}' with double value "
"'{value}'.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
// Handles kPrintToConsole.
if (merged_options.get_print_to_console()) {
options.trace = 1;
}
for (const auto& it : merged_options.GetOptionsInt(IbexSolver::id())) {
if (it.first == "rigor") {
options.rigor = it.second != 0;
continue;
}
if (it.first == "trace") {
const int value{it.second};
if (value < 0 || value > 2) {
throw std::runtime_error{
fmt::format("IbexSolver: cannot set IBEX Solver option 'trace' "
"to value '{value}'. It should be 0, 1, or, 2.",
fmt::arg("value", value))};
}
options.trace = value;
continue;
}
throw std::runtime_error{fmt::format(
"IbexSolver: unsupported option '{option}' with int value '{value}'.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
for (const auto& it : merged_options.GetOptionsStr(IbexSolver::id())) {
throw std::runtime_error{fmt::format(
"IbexSolver: unsupported option '{option}' with string value "
"'{value}'.",
fmt::arg("option", it.first), fmt::arg("value", it.second))};
}
return options;
}
// Adds `f` into `factory` using `ibex_converter` and `expr_ctrs`.
//
// @throw if `f` is non-atomic.
void DoAddFormula(const Formula& f,
internal::IbexConverter* const ibex_converter,
ibex::SystemFactory* const factory,
vector<internal::UniquePtrToExprCtr>* const expr_ctrs) {
internal::UniquePtrToExprCtr expr_ctr{ibex_converter->Convert(f)};
factory->add_ctr(*expr_ctr);
expr_ctrs->push_back(std::move(expr_ctr));
}
// Adds `f` into `factory` using `ibex_converter` and `expr_ctrs`.
void AddFormula(const Formula& f, internal::IbexConverter* const ibex_converter,
ibex::SystemFactory* const factory,
vector<internal::UniquePtrToExprCtr>* const expr_ctrs) {
if (symbolic::is_conjunction(f)) {
for (const auto& f_i : get_operands(f)) {
DoAddFormula(f_i, ibex_converter, factory, expr_ctrs);
}
} else {
DoAddFormula(f, ibex_converter, factory, expr_ctrs);
}
}
VectorXd ExtractMidpoints(const ibex::IntervalVector& iv) {
VectorXd v{iv.size()};
for (int i = 0; i < iv.size(); ++i) {
v[i] = iv[i].mid();
}
return v;
}
void DoOptimize(const ibex::System& sys, const IbexOptions& options,
MathematicalProgramResult* const result) {
const bool in_hc4 = !(sys.nb_ctr > 0 && sys.nb_ctr < sys.f_ctrs.image_dim());
const bool kkt = (sys.nb_ctr == 0);
ibex::DefaultOptimizerConfig config(sys, options.rel_eps_f, options.abs_eps_f,
options.eps_h, options.rigor, in_hc4, kkt,
options.random_seed, options.eps_x);
config.set_trace(options.trace);
config.set_timeout(options.timeout);
ibex::Optimizer o(config);
const ibex::Optimizer::Status status = o.optimize(sys.box);
switch (status) {
case ibex::Optimizer::INFEASIBLE:
// If no feasible point exist less than obj_init_bound. In particular,
// the function returns INFEASIBLE if the initial bound "obj_init_bound"
// is LESS than the true minimum (this case is only possible if
// goal_abs_prec and goal_rel_prec are 0). In the latter case, there may
// exist feasible points.
result->set_solution_result(SolutionResult::kInfeasibleConstraints);
break;
case ibex::Optimizer::NO_FEASIBLE_FOUND:
// If no feasible point could be found less than obj_init_bound.
// Contrary to INFEASIBLE, infeasibility is not proven here.
//
// Warning: this return value is sensitive to the abs_eps_f and
// rel_eps_f parameters. The upperbounding makes the optimizer only
// looking for points less than min { (1-rel_eps_f)*obj_init_bound,
// obj_init_bound - abs_eps_f }.
result->set_solution_result(SolutionResult::kInfeasibleConstraints);
break;
case ibex::Optimizer::UNBOUNDED_OBJ:
// If the objective function seems unbounded (tends to -oo).
result->set_solution_result(SolutionResult::kUnbounded);
break;
case ibex::Optimizer::TIME_OUT:
// Timeout.
result->set_solution_result(SolutionResult::kUnknownError);
break;
case ibex::Optimizer::UNREACHED_PREC: {
// If the search is over but the resulting interval [uplo,loup] does not
// satisfy the precision requirements. There are several possible
// reasons: the goal function may be too pessimistic or the constraints
// function may be too pessimistic with respect to the precision
// requirement (which can be too stringent). This results in tiny boxes
// that can neither be contracted nor used as new loup
// candidates. Finally, the eps_x parameter may be too large.
result->set_solution_result(SolutionResult::kUnknownError);
break;
}
case ibex::Optimizer::SUCCESS: {
// If the global minimum (with respect to the precision required) has
// been found. In particular, at least one feasible point has been
// found, less than obj_init_bound, and in the time limit.
result->set_x_val(ExtractMidpoints(o.get_loup_point()));
result->set_optimal_cost(o.get_uplo() / 2.0 +
o.get_loup() / 2.0 /* midpoint */);
result->set_solution_result(SolutionResult::kSolutionFound);
break;
}
}
}
} // namespace
bool IbexSolver::is_available() { return true; }
void IbexSolver::DoSolve(const MathematicalProgram& prog,
const VectorXd& initial_guess,
const SolverOptions& merged_options,
MathematicalProgramResult* result) const {
if (!prog.GetVariableScaling().empty()) {
static const logging::Warn log_once(
"IbexSolver doesn't support the feature of variable scaling.");
}
unused(initial_guess);
const IbexOptions options{ParseOptions(merged_options)};
// Creates a converter. Internally, it creates ibex variables.
internal::IbexConverter ibex_converter(prog.decision_variables());
ibex::SystemFactory factory;
// Prepares bounds for variables.
ibex::IntervalVector bounds(prog.num_vars());
for (const auto& b : prog.bounding_box_constraints()) {
const auto& c = b.evaluator();
const auto& lb = c->lower_bound();
const auto& ub = c->upper_bound();
for (int k = 0; k < static_cast<int>(b.GetNumElements()); ++k) {
const int index = prog.FindDecisionVariableIndex(b.variables()[k]);
bounds[index] &= ibex::Interval(lb[k], ub[k]);
}
}
// Adds ibex variables + bounds into the factory.
factory.add_var(ibex_converter.variables(), bounds);
// This expr_ctrs holds the translated ibex::ExprCtr* objects. After adding
// the ibex::Ctrs to the factory, we should not destruct them at the end of
// the loop where they are created because they are used in the system factory
// and the system.
vector<internal::UniquePtrToExprCtr> expr_ctrs;
// Add constraints.
//
// Note: Bounding-box constraints were already handled above when we
// computed bounds for variables.
for (const auto& b : prog.generic_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
for (const auto& b : prog.linear_equality_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
for (const auto& b : prog.linear_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
for (const auto& b : prog.lorentz_cone_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
for (const auto& b : prog.rotated_lorentz_cone_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
for (const auto& b : prog.exponential_cone_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
for (const auto& b : prog.linear_complementarity_constraints()) {
AddFormula(b.evaluator()->CheckSatisfied(b.variables()), &ibex_converter,
&factory, &expr_ctrs);
}
// Note: PositiveSemidefiniteConstraint and LinearMatrixInequalityConstraint
// are not supported because their symbolic evaluation is not defined.
DRAKE_DEMAND(prog.positive_semidefinite_constraints().empty());
DRAKE_DEMAND(prog.linear_matrix_inequality_constraints().empty());
// Extract costs.
vector<Expression> costs;
{
VectorX<Expression> y;
for (const auto& binding : prog.GetAllCosts()) {
const VectorXDecisionVariable& x{binding.variables()};
const auto& cost{binding.evaluator()};
cost->Eval(x, &y);
costs.push_back(y[0]);
}
}
if (costs.empty()) {
// A segmentation fault occurs when IBEX is given a problem with no cost
// functions. We add a dummy `0` cost to prevent it.
costs.push_back(Expression::Zero());
}
// Add costs into the factory.
vector<internal::UniquePtrToExprNode> expr_nodes;
for (const Expression& cost : costs) {
internal::UniquePtrToExprNode expr_node{ibex_converter.Convert(cost)};
factory.add_goal(*expr_node);
// We need to postpone the destruction of expr_node as it is
// still used inside of system_factory.
expr_nodes.push_back(std::move(expr_node));
}
ibex::System sys(factory);
DoOptimize(sys, options, result);
}
} // namespace solvers
} // namespace drake