-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
nlopt_solver.cc
572 lines (504 loc) · 19.9 KB
/
nlopt_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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "drake/solvers/nlopt_solver.h"
#include <algorithm>
#include <limits>
#include <list>
#include <set>
#include <stdexcept>
#include <unordered_map>
#include <vector>
#include <nlopt.hpp>
#include "drake/common/autodiff.h"
#include "drake/common/drake_assert.h"
#include "drake/common/never_destroyed.h"
#include "drake/common/text_logging.h"
#include "drake/common/unused.h"
#include "drake/math/autodiff.h"
#include "drake/solvers/mathematical_program.h"
namespace drake {
namespace solvers {
namespace {
Eigen::VectorXd MakeEigenVector(const std::vector<double>& x) {
Eigen::VectorXd xvec(x.size());
for (size_t i = 0; i < x.size(); i++) {
xvec[i] = x[i];
}
return xvec;
}
AutoDiffVecXd MakeInputAutoDiffVec(const MathematicalProgram& prog,
const Eigen::VectorXd& xvec,
const VectorXDecisionVariable& vars) {
const int num_vars = vars.rows();
auto tx = math::InitializeAutoDiff(xvec);
AutoDiffVecXd this_x(num_vars);
for (int i = 0; i < num_vars; ++i) {
this_x(i) = tx(prog.FindDecisionVariableIndex(vars(i)));
}
return this_x;
}
// This function meets the signature requirements for nlopt::vfunc as
// described in
// http://ab-initio.mit.edu/wiki/index.php/NLopt_C-plus-plus_Reference#Objective_function
// Note : NLopt uses the term "Objective" which corresponds to the Drake usage
// of "Cost".
// TODO(#2274) Fix NOLINTNEXTLINE(runtime/references).
double EvaluateCosts(const std::vector<double>& x, std::vector<double>& grad,
void* f_data) {
const MathematicalProgram* prog =
reinterpret_cast<const MathematicalProgram*>(f_data);
double cost = 0;
Eigen::VectorXd xvec = MakeEigenVector(x);
prog->EvalVisualizationCallbacks(xvec);
auto tx = math::InitializeAutoDiff(xvec);
AutoDiffVecXd ty(1);
AutoDiffVecXd this_x;
if (!grad.empty()) {
grad.assign(grad.size(), 0);
}
for (auto const& binding : prog->GetAllCosts()) {
int num_vars = binding.GetNumElements();
this_x.resize(num_vars);
for (int i = 0; i < num_vars; ++i) {
this_x(i) = tx(prog->FindDecisionVariableIndex(binding.variables()(i)));
}
binding.evaluator()->Eval(this_x, &ty);
cost += ty(0).value();
if (!grad.empty()) {
if (ty(0).derivatives().size() > 0) {
for (int j = 0; j < num_vars; ++j) {
const size_t vj_index =
prog->FindDecisionVariableIndex(binding.variables()(j));
grad[vj_index] += ty(0).derivatives()(vj_index);
}
}
}
}
return cost;
}
/// Structure to marshall data into the NLopt callback functions,
/// which take only a single pointer argument.
struct WrappedConstraint {
WrappedConstraint(const Constraint* constraint_in,
const VectorXDecisionVariable* vars_in,
const MathematicalProgram* prog_in)
: constraint(constraint_in),
vars(vars_in),
prog(prog_in),
force_bounds(false),
force_upper(false) {}
const Constraint* constraint;
const VectorXDecisionVariable* vars;
const MathematicalProgram* prog;
bool force_bounds; ///< force usage of only upper or lower bounds
bool force_upper; ///< Only used if force_bounds is set. Selects
///< which bounds are being tested (lower bound
///< vs. upper bound).
// TODO(sam.creasey) It might be desirable to have a cache for the
// result of evaluating the constraints if NLopt were being used in
// a situation where constraints were frequently being wrapped in
// such a way as to result in multiple evaluations. As this is a
// speculative case, and since NLopt's roundoff issues with
// duplicate constraints preclude it from being used in some
// scenarios, I'm not implementing such a cache at this time.
std::set<size_t> active_constraints;
};
double ApplyConstraintBounds(double result, double lb, double ub) {
// Our (Drake's) constraints are expressed in the form lb <= f(x) <=
// ub. NLopt always wants the value of a constraint expressed as
// f(x) <= 0.
//
// For upper bounds rewrite as: f(x) - ub <= 0
// For lower bounds rewrite as: -f(x) + lb <= 0
//
// If both upper and lower bounds are set, default to evaluating the
// upper bound, and switch to the lower bound only if it's being
// exceeded.
//
// See
// http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Nonlinear_constraints
// for more detail on how NLopt interprets return values.
if ((ub != std::numeric_limits<double>::infinity()) &&
((result >= lb) || (lb == ub))) {
result -= ub;
} else {
if (lb == -std::numeric_limits<double>::infinity()) {
throw std::runtime_error("Unable to handle constraint with no bounds.");
}
result *= -1;
result += lb;
}
return result;
}
// This function meets the signature of nlopt_mfunc as described in
// http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Vector-valued_constraints
void EvaluateVectorConstraint(unsigned m, double* result, unsigned n,
const double* x, double* grad, void* f_data) {
const WrappedConstraint* wrapped =
reinterpret_cast<WrappedConstraint*>(f_data);
Eigen::VectorXd xvec(n);
for (size_t i = 0; i < n; i++) {
xvec[i] = x[i];
}
// http://ab-initio.mit.edu/wiki/index.php/NLopt_Reference#Vector-valued_constraints
// explicitly tells us that it's allocated m * n array elements
// before invoking this function. It does not seem to have been
// zeroed, and not all constraints will store gradients for all
// decision variables (so don't leave junk in the other array
// elements).
if (grad) {
memset(grad, 0, sizeof(double) * m * n);
}
const Constraint* c = wrapped->constraint;
const int num_constraints = c->num_constraints();
DRAKE_ASSERT(num_constraints >= static_cast<int>(m));
DRAKE_ASSERT(wrapped->active_constraints.size() == m);
AutoDiffVecXd ty(num_constraints);
AutoDiffVecXd this_x =
MakeInputAutoDiffVec(*(wrapped->prog), xvec, *(wrapped->vars));
c->Eval(this_x, &ty);
const Eigen::VectorXd& lower_bound = c->lower_bound();
const Eigen::VectorXd& upper_bound = c->upper_bound();
size_t result_idx = 0;
for (int i = 0; i < num_constraints; i++) {
if (!wrapped->active_constraints.contains(i)) {
continue;
}
if (wrapped->force_bounds && wrapped->force_upper &&
(upper_bound(i) != std::numeric_limits<double>::infinity())) {
result[result_idx] = ApplyConstraintBounds(
ty(i).value(), -std::numeric_limits<double>::infinity(),
upper_bound(i));
} else if (wrapped->force_bounds && !wrapped->force_upper &&
(lower_bound(i) != -std::numeric_limits<double>::infinity())) {
result[result_idx] =
ApplyConstraintBounds(ty(i).value(), lower_bound(i),
std::numeric_limits<double>::infinity());
} else {
result[result_idx] =
ApplyConstraintBounds(ty(i).value(), lower_bound(i), upper_bound(i));
}
result_idx++;
DRAKE_ASSERT(result_idx <= m);
}
if (grad) {
result_idx = 0;
const int num_v_variable = wrapped->vars->rows();
std::vector<size_t> v_index(num_v_variable);
for (int i = 0; i < num_v_variable; ++i) {
v_index[i] =
wrapped->prog->FindDecisionVariableIndex((*wrapped->vars)(i));
}
for (int i = 0; i < num_constraints; i++) {
if (!wrapped->active_constraints.contains(i)) {
continue;
}
double grad_sign = 1;
if (c->upper_bound()(i) == std::numeric_limits<double>::infinity()) {
grad_sign = -1;
} else if (wrapped->force_bounds && !wrapped->force_upper) {
grad_sign = -1;
}
DRAKE_ASSERT(wrapped->vars->cols() == 1);
if (ty(i).derivatives().size() > 0) {
for (int j = 0; j < wrapped->vars->rows(); ++j) {
grad[(result_idx * n) + v_index[j]] =
ty(i).derivatives()(v_index[j]) * grad_sign;
}
}
result_idx++;
DRAKE_ASSERT(result_idx <= m);
}
DRAKE_ASSERT(result_idx == m);
}
}
template <typename C>
void WrapConstraint(const MathematicalProgram& prog, const Binding<C>& binding,
double constraint_tol, nlopt::opt* opt,
std::list<WrappedConstraint>* wrapped_list) {
// Version of the wrapped constraint which refers only to equality
// constraints (if any), and will be used with
// add_equality_mconstraint.
WrappedConstraint wrapped_eq(binding.evaluator().get(), &binding.variables(),
&prog);
// Version of the wrapped constraint which refers only to inequality
// constraints (if any), and will be used with
// add_equality_mconstraint.
WrappedConstraint wrapped_in(binding.evaluator().get(), &binding.variables(),
&prog);
bool is_pure_inequality = true;
const Eigen::VectorXd& lower_bound = binding.evaluator()->lower_bound();
const Eigen::VectorXd& upper_bound = binding.evaluator()->upper_bound();
DRAKE_ASSERT(lower_bound.size() == upper_bound.size());
for (size_t i = 0; i < static_cast<size_t>(lower_bound.size()); i++) {
if (lower_bound(i) == upper_bound(i)) {
wrapped_eq.active_constraints.insert(i);
} else {
if ((lower_bound(i) != -std::numeric_limits<double>::infinity()) &&
(upper_bound(i) != std::numeric_limits<double>::infinity())) {
is_pure_inequality = false;
}
wrapped_in.active_constraints.insert(i);
}
}
if (wrapped_eq.active_constraints.size()) {
wrapped_list->push_back(wrapped_eq);
std::vector<double> tol(wrapped_eq.active_constraints.size(),
constraint_tol);
opt->add_equality_mconstraint(EvaluateVectorConstraint,
&wrapped_list->back(), tol);
}
if (wrapped_in.active_constraints.size()) {
std::vector<double> tol(wrapped_in.active_constraints.size(),
constraint_tol);
wrapped_list->push_back(wrapped_in);
if (is_pure_inequality) {
opt->add_inequality_mconstraint(EvaluateVectorConstraint,
&wrapped_list->back(), tol);
} else {
wrapped_list->back().force_bounds = true;
wrapped_list->back().force_upper = true;
opt->add_inequality_mconstraint(EvaluateVectorConstraint,
&wrapped_list->back(), tol);
wrapped_list->push_back(wrapped_in);
wrapped_list->back().force_bounds = true;
wrapped_list->back().force_upper = false;
opt->add_inequality_mconstraint(EvaluateVectorConstraint,
&wrapped_list->back(), tol);
}
}
}
template <typename Binding>
bool IsVectorOfConstraintsSatisfiedAtSolution(
const MathematicalProgram& prog, const std::vector<Binding>& bindings,
const Eigen::Ref<const Eigen::VectorXd>& decision_variable_values,
double tol) {
for (const auto& binding : bindings) {
const Eigen::VectorXd constraint_val =
prog.EvalBinding(binding, decision_variable_values);
const int num_constraint = constraint_val.rows();
if (((constraint_val - binding.evaluator()->lower_bound()).array() <
-Eigen::ArrayXd::Constant(num_constraint, tol))
.any() ||
((constraint_val - binding.evaluator()->upper_bound()).array() >
Eigen::ArrayXd::Constant(num_constraint, tol))
.any()) {
return false;
}
}
return true;
}
template <typename T>
T GetOptionValueWithDefault(const std::unordered_map<std::string, T>& options,
const std::string& key, const T& default_value) {
auto it = options.find(key);
if (it == options.end()) {
return default_value;
}
return it->second;
}
nlopt::algorithm ParseNloptAlgorithm(const std::string& algorithm) {
// Fast path for our default value.
if (algorithm == "LD_SLSQP") {
return nlopt::algorithm::LD_SLSQP;
}
// Otherwise, scan for a match.
for (int i = 0; i < nlopt::algorithm::NUM_ALGORITHMS; ++i) {
auto c_enum = static_cast<nlopt_algorithm>(i);
auto cxx_enum = static_cast<nlopt::algorithm>(i);
if (algorithm == nlopt_algorithm_to_string(c_enum)) {
return cxx_enum;
}
}
// No match; throw a nice error message.
std::vector<std::string> known;
for (int i = 0; i < nlopt::algorithm::NUM_ALGORITHMS; ++i) {
auto c_enum = static_cast<nlopt_algorithm>(i);
known.push_back(nlopt_algorithm_to_string(c_enum));
}
throw std::logic_error(fmt::format(
"Unknown NLopt algorithm {}; valid choices are: {}",
algorithm, fmt::join(known, ", ")));
}
struct KnownOptions {
std::string algorithm{"LD_SLSQP"};
double constraint_tol{1e-6};
double xtol_rel{1e-6};
double xtol_abs{1e-6};
int max_eval{1000};
};
void Serialize(internal::SpecificOptions* archive,
// NOLINTNEXTLINE(runtime/references) to match Serialize concept.
KnownOptions& options) {
archive->Visit(MakeNameValue(NloptSolver::AlgorithmName().c_str(), // BR
&options.algorithm));
archive->Visit(MakeNameValue(NloptSolver::ConstraintToleranceName().c_str(),
&options.constraint_tol));
archive->Visit(MakeNameValue(NloptSolver::XRelativeToleranceName().c_str(),
&options.xtol_rel));
archive->Visit(MakeNameValue(NloptSolver::XAbsoluteToleranceName().c_str(),
&options.xtol_abs));
archive->Visit(MakeNameValue(NloptSolver::MaxEvalName().c_str(), // BR
&options.max_eval));
}
KnownOptions ParseOptions(internal::SpecificOptions* options) {
KnownOptions result;
options->CopyToSerializableStruct(&result);
return result;
}
} // namespace
bool NloptSolver::is_available() {
return true;
}
void NloptSolver::DoSolve2(const MathematicalProgram& prog,
const Eigen::VectorXd& initial_guess,
internal::SpecificOptions* options,
MathematicalProgramResult* result) const {
if (!prog.GetVariableScaling().empty()) {
static const logging::Warn log_once(
"NloptSolver doesn't support the feature of variable scaling.");
}
const KnownOptions parsed_options = ParseOptions(options);
const double constraint_tol = parsed_options.constraint_tol;
const int nx = prog.num_vars();
// Load the algo to use and the size.
nlopt::opt opt(ParseNloptAlgorithm(parsed_options.algorithm), nx);
std::vector<double> x(initial_guess.size());
for (size_t i = 0; i < x.size(); i++) {
if (!std::isnan(initial_guess[i])) {
x[i] = initial_guess[i];
} else {
x[i] = 0.0;
}
}
std::vector<double> xlow(nx, -std::numeric_limits<double>::infinity());
std::vector<double> xupp(nx, std::numeric_limits<double>::infinity());
for (auto const& binding : prog.bounding_box_constraints()) {
const auto& c = binding.evaluator();
const auto& lower_bound = c->lower_bound();
const auto& upper_bound = c->upper_bound();
for (int k = 0; k < static_cast<int>(binding.GetNumElements()); ++k) {
const size_t idx = prog.FindDecisionVariableIndex(binding.variables()(k));
xlow[idx] = std::max(lower_bound(k), xlow[idx]);
xupp[idx] = std::min(upper_bound(k), xupp[idx]);
if (x[idx] < xlow[idx]) {
x[idx] = xlow[idx];
}
if (x[idx] > xupp[idx]) {
x[idx] = xupp[idx];
}
}
}
opt.set_lower_bounds(xlow);
opt.set_upper_bounds(xupp);
opt.set_min_objective(EvaluateCosts, const_cast<MathematicalProgram*>(&prog));
std::list<WrappedConstraint> wrapped_vector;
// TODO(sam.creasey): Missing test coverage for generic constraints
// with >1 output.
for (const auto& c : prog.generic_constraints()) {
WrapConstraint(prog, c, constraint_tol, &opt, &wrapped_vector);
}
for (const auto& c : prog.quadratic_constraints()) {
WrapConstraint(prog, c, constraint_tol, &opt, &wrapped_vector);
}
for (const auto& c : prog.lorentz_cone_constraints()) {
WrapConstraint(prog, c, constraint_tol, &opt, &wrapped_vector);
}
for (const auto& c : prog.rotated_lorentz_cone_constraints()) {
WrapConstraint(prog, c, constraint_tol, &opt, &wrapped_vector);
}
for (const auto& c : prog.linear_equality_constraints()) {
WrapConstraint(prog, c, constraint_tol, &opt, &wrapped_vector);
}
// TODO(sam.creasey): Missing test coverage for linear constraints
// with >1 output.
for (const auto& c : prog.linear_constraints()) {
WrapConstraint(prog, c, constraint_tol, &opt, &wrapped_vector);
}
opt.set_xtol_rel(parsed_options.xtol_rel);
opt.set_xtol_abs(parsed_options.xtol_abs);
opt.set_maxeval(parsed_options.max_eval);
result->set_solution_result(SolutionResult::kSolutionFound);
NloptSolverDetails& solver_details =
result->SetSolverDetailsType<NloptSolverDetails>();
double minf = 0;
const double kUnboundedTol = -1E30;
try {
const nlopt::result nlopt_result = opt.optimize(x, minf);
solver_details.status = nlopt_result;
if (nlopt_result == nlopt::SUCCESS ||
nlopt_result == nlopt::STOPVAL_REACHED ||
nlopt_result == nlopt::XTOL_REACHED ||
nlopt_result == nlopt::FTOL_REACHED ||
nlopt_result == nlopt::MAXEVAL_REACHED ||
nlopt_result == nlopt::MAXTIME_REACHED) {
result->set_x_val(Eigen::Map<Eigen::VectorXd>(x.data(), nx));
}
switch (nlopt_result) {
case nlopt::SUCCESS:
case nlopt::STOPVAL_REACHED: {
result->set_solution_result(SolutionResult::kSolutionFound);
break;
}
case nlopt::FTOL_REACHED:
case nlopt::XTOL_REACHED: {
// Now check if the constraints are violated.
bool all_constraints_satisfied = true;
auto constraint_test = [&prog, constraint_tol,
&all_constraints_satisfied,
result](auto constraints) {
all_constraints_satisfied &= IsVectorOfConstraintsSatisfiedAtSolution(
prog, constraints, result->get_x_val(), constraint_tol);
};
constraint_test(prog.generic_constraints());
constraint_test(prog.bounding_box_constraints());
constraint_test(prog.linear_constraints());
constraint_test(prog.linear_equality_constraints());
constraint_test(prog.quadratic_constraints());
constraint_test(prog.lorentz_cone_constraints());
constraint_test(prog.rotated_lorentz_cone_constraints());
if (!all_constraints_satisfied) {
result->set_solution_result(SolutionResult::kInfeasibleConstraints);
}
break;
}
case nlopt::MAXTIME_REACHED:
case nlopt::MAXEVAL_REACHED: {
result->set_solution_result(SolutionResult::kIterationLimit);
break;
}
case nlopt::INVALID_ARGS: {
result->set_solution_result(SolutionResult::kInvalidInput);
break;
}
case nlopt::ROUNDOFF_LIMITED: {
if (minf < kUnboundedTol) {
result->set_solution_result(SolutionResult::kUnbounded);
minf = -std::numeric_limits<double>::infinity();
} else {
result->set_solution_result(SolutionResult::kSolverSpecificError);
}
break;
}
default: {
result->set_solution_result(SolutionResult::kSolverSpecificError);
}
}
} catch (std::invalid_argument&) {
result->set_solution_result(SolutionResult::kInvalidInput);
} catch (std::bad_alloc&) {
result->set_solution_result(SolutionResult::kSolverSpecificError);
} catch (nlopt::roundoff_limited&) {
if (minf < kUnboundedTol) {
result->set_solution_result(SolutionResult::kUnbounded);
minf = MathematicalProgram::kUnboundedCost;
} else {
result->set_solution_result(SolutionResult::kSolverSpecificError);
}
} catch (nlopt::forced_stop&) {
result->set_solution_result(SolutionResult::kSolverSpecificError);
} catch (std::runtime_error&) {
result->set_solution_result(SolutionResult::kSolverSpecificError);
}
result->set_optimal_cost(minf);
}
} // namespace solvers
} // namespace drake