forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint.cc
426 lines (366 loc) · 14 KB
/
constraint.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
#include "drake/solvers/constraint.h"
#include <cmath>
#include <limits>
#include <set>
#include <unordered_map>
#include "drake/math/matrix_util.h"
#include "drake/solvers/symbolic_extraction.h"
using std::abs;
namespace drake {
namespace solvers {
namespace {
// Returns `True` if lb is -∞. Otherwise returns a symbolic formula `lb <= e`.
symbolic::Formula MakeLowerBound(const double lb,
const symbolic::Expression& e) {
if (lb == -std::numeric_limits<double>::infinity()) {
return symbolic::Formula::True();
} else {
return lb <= e;
}
}
// Returns `True` if ub is ∞. Otherwise returns a symbolic formula `e <= ub`.
symbolic::Formula MakeUpperBound(const symbolic::Expression& e,
const double ub) {
if (ub == std::numeric_limits<double>::infinity()) {
return symbolic::Formula::True();
} else {
return e <= ub;
}
}
} // namespace
symbolic::Formula Constraint::DoCheckSatisfied(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x) const {
VectorX<symbolic::Expression> y(num_constraints());
DoEval(x, &y);
symbolic::Formula f{symbolic::Formula::True()};
for (int i = 0; i < num_constraints(); ++i) {
// Add lbᵢ ≤ yᵢ ≤ ubᵢ.
f = f && MakeLowerBound(lower_bound_[i], y[i]) &&
MakeUpperBound(y[i], upper_bound_[i]);
}
return f;
}
template <typename DerivedX, typename ScalarY>
void QuadraticConstraint::DoEvalGeneric(const Eigen::MatrixBase<DerivedX>& x,
VectorX<ScalarY>* y) const {
y->resize(num_constraints());
*y = .5 * x.transpose() * Q_ * x + b_.transpose() * x;
}
void QuadraticConstraint::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void QuadraticConstraint::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void QuadraticConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
template <typename DerivedX, typename ScalarY>
void LorentzConeConstraint::DoEvalGeneric(const Eigen::MatrixBase<DerivedX>& x,
VectorX<ScalarY>* y) const {
const VectorX<ScalarY> z = A_dense_ * x.template cast<ScalarY>() + b_;
y->resize(num_constraints());
(*y)(0) = z(0);
(*y)(1) = pow(z(0), 2) - z.tail(z.size() - 1).squaredNorm();
}
void LorentzConeConstraint::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void LorentzConeConstraint::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void LorentzConeConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
template <typename DerivedX, typename ScalarY>
void RotatedLorentzConeConstraint::DoEvalGeneric(
const Eigen::MatrixBase<DerivedX>& x, VectorX<ScalarY>* y) const {
const VectorX<ScalarY> z = A_dense_ * x.template cast<ScalarY>() + b_;
y->resize(num_constraints());
(*y)(0) = z(0);
(*y)(1) = z(1);
(*y)(2) = z(0) * z(1) - z.tail(z.size() - 2).squaredNorm();
}
void RotatedLorentzConeConstraint::DoEval(
const Eigen::Ref<const Eigen::VectorXd>& x, Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void RotatedLorentzConeConstraint::DoEval(
const Eigen::Ref<const AutoDiffVecXd>& x, AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void RotatedLorentzConeConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
template <typename DerivedX, typename ScalarY>
void LinearConstraint::DoEvalGeneric(const Eigen::MatrixBase<DerivedX>& x,
VectorX<ScalarY>* y) const {
y->resize(num_constraints());
(*y) = A_ * x.template cast<ScalarY>();
}
void LinearConstraint::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void LinearConstraint::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void LinearConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
template <typename DerivedX, typename ScalarY>
void BoundingBoxConstraint::DoEvalGeneric(const Eigen::MatrixBase<DerivedX>& x,
VectorX<ScalarY>* y) const {
y->resize(num_constraints());
(*y) = x.template cast<ScalarY>();
}
void BoundingBoxConstraint::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void BoundingBoxConstraint::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void BoundingBoxConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
template <typename DerivedX, typename ScalarY>
void LinearComplementarityConstraint::DoEvalGeneric(
const Eigen::MatrixBase<DerivedX>& x, VectorX<ScalarY>* y) const {
y->resize(num_constraints());
(*y) = (M_ * x.template cast<ScalarY>()) + q_;
}
void LinearComplementarityConstraint::DoEval(
const Eigen::Ref<const Eigen::VectorXd>& x, Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void LinearComplementarityConstraint::DoEval(
const Eigen::Ref<const AutoDiffVecXd>& x, AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void LinearComplementarityConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
bool LinearComplementarityConstraint::DoCheckSatisfied(
const Eigen::Ref<const Eigen::VectorXd>& x, const double tol) const {
// Check: x >= 0 && Mx + q >= 0 && x'(Mx + q) == 0
Eigen::VectorXd y(num_constraints());
DoEval(x, &y);
return (x.array() > -tol).all() && (y.array() > -tol).all() &&
(abs(x.dot(y)) < tol);
}
bool LinearComplementarityConstraint::DoCheckSatisfied(
const Eigen::Ref<const AutoDiffVecXd>& x, const double tol) const {
AutoDiffVecXd y(num_constraints());
DoEval(x, &y);
return (x.array() > -tol).all() && (y.array() > -tol).all() &&
(abs(x.dot(y)) < tol);
}
symbolic::Formula LinearComplementarityConstraint::DoCheckSatisfied(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x) const {
VectorX<symbolic::Expression> y(num_constraints());
DoEval(x, &y);
symbolic::Formula f{symbolic::Formula::True()};
// 1. Mx + q >= 0
for (VectorX<symbolic::Expression>::Index i = 0; i < y.size(); ++i) {
f = f && (y(i) >= 0);
}
// 2. x >= 0
for (VectorX<symbolic::Expression>::Index i = 0; i < x.size(); ++i) {
f = f && (x(i) >= 0);
}
// 3. x'(Mx + q) == 0
f = f && (x.dot(y) == 0);
return f;
}
void PositiveSemidefiniteConstraint::DoEval(
const Eigen::Ref<const Eigen::VectorXd>& x, Eigen::VectorXd* y) const {
DRAKE_ASSERT(x.rows() == num_constraints() * num_constraints());
Eigen::MatrixXd S(num_constraints(), num_constraints());
for (int i = 0; i < num_constraints(); ++i) {
S.col(i) = x.segment(i * num_constraints(), num_constraints());
}
DRAKE_ASSERT(S.rows() == num_constraints());
// This uses the lower diagonal part of S to compute the eigen values.
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigen_solver(S);
*y = eigen_solver.eigenvalues();
}
void PositiveSemidefiniteConstraint::DoEval(
const Eigen::Ref<const AutoDiffVecXd>&, AutoDiffVecXd*) const {
throw std::logic_error(
"The Eval function for positive semidefinite constraint is not defined, "
"since the eigen solver does not work for AutoDiffScalar.");
}
void PositiveSemidefiniteConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>&,
VectorX<symbolic::Expression>*) const {
throw std::logic_error(
"The Eval function for positive semidefinite constraint is not defined, "
"since the eigen solver does not work for symbolic::Expression.");
}
void LinearMatrixInequalityConstraint::DoEval(
const Eigen::Ref<const Eigen::VectorXd>& x, Eigen::VectorXd* y) const {
DRAKE_ASSERT(x.rows() == static_cast<int>(F_.size()) - 1);
Eigen::MatrixXd S = F_[0];
for (int i = 1; i < static_cast<int>(F_.size()); ++i) {
S += x(i - 1) * F_[i];
}
Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> eigen_solver(S);
*y = eigen_solver.eigenvalues();
}
void LinearMatrixInequalityConstraint::DoEval(
const Eigen::Ref<const AutoDiffVecXd>&, AutoDiffVecXd*) const {
throw std::logic_error(
"The Eval function for positive semidefinite constraint is not defined, "
"since the eigen solver does not work for AutoDiffScalar.");
}
void LinearMatrixInequalityConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>&,
VectorX<symbolic::Expression>*) const {
throw std::logic_error(
"The Eval function for positive semidefinite constraint is not defined, "
"since the eigen solver does not work for symbolic::Expression.");
}
LinearMatrixInequalityConstraint::LinearMatrixInequalityConstraint(
const std::vector<Eigen::Ref<const Eigen::MatrixXd>>& F,
double symmetry_tolerance)
: Constraint(F.empty() ? 0 : F.front().rows(),
F.empty() ? 0 : F.size() - 1),
F_(F.begin(), F.end()),
matrix_rows_(F.empty() ? 0 : F.front().rows()) {
DRAKE_DEMAND(!F.empty());
set_bounds(Eigen::VectorXd::Zero(matrix_rows_),
Eigen::VectorXd::Constant(
matrix_rows_, std::numeric_limits<double>::infinity()));
for (const auto& Fi : F) {
DRAKE_ASSERT(Fi.rows() == matrix_rows_);
DRAKE_ASSERT(math::IsSymmetric(Fi, symmetry_tolerance));
}
}
ExpressionConstraint::ExpressionConstraint(
const Eigen::Ref<const VectorX<symbolic::Expression>>& v,
const Eigen::Ref<const Eigen::VectorXd>& lb,
const Eigen::Ref<const Eigen::VectorXd>& ub)
: Constraint(v.rows(), GetDistinctVariables(v).size(), lb, ub),
expressions_(v) {
// Setup map_var_to_index_ and vars_ so that
// map_var_to_index_[vars_(i).get_id()] = i.
for (int i = 0; i < expressions_.size(); ++i) {
internal::ExtractAndAppendVariablesFromExpression(expressions_(i), &vars_,
&map_var_to_index_);
}
derivatives_ = symbolic::Jacobian(expressions_, vars_);
// Setup the environment.
for (int i = 0; i < vars_.size(); i++) {
environment_.insert(vars_[i], 0.0);
}
}
void ExpressionConstraint::DoEval(const Eigen::Ref<const Eigen::VectorXd>& x,
Eigen::VectorXd* y) const {
DRAKE_DEMAND(x.rows() == vars_.rows());
// Set environment with current x values.
for (int i = 0; i < vars_.size(); i++) {
environment_[vars_[i]] = x(map_var_to_index_.at(vars_[i].get_id()));
}
// Evaluate into the output, y.
y->resize(num_constraints());
for (int i = 0; i < num_constraints(); i++) {
(*y)[i] = expressions_[i].Evaluate(environment_);
}
}
void ExpressionConstraint::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DRAKE_DEMAND(x.rows() == vars_.rows());
// Set environment with current x values.
for (int i = 0; i < vars_.size(); i++) {
environment_[vars_[i]] = x(map_var_to_index_.at(vars_[i].get_id())).value();
}
// Evaluate value and derivatives into the output, y.
// Using ∂yᵢ/∂zⱼ = ∑ₖ ∂fᵢ/∂xₖ ∂xₖ/∂zⱼ.
y->resize(num_constraints());
Eigen::VectorXd dyidx(x.size());
for (int i = 0; i < num_constraints(); i++) {
(*y)[i].value() = expressions_[i].Evaluate(environment_);
for (int k = 0; k < x.size(); k++) {
dyidx[k] = derivatives_(i, k).Evaluate(environment_);
}
(*y)[i].derivatives().resize(x(0).derivatives().size());
for (int j = 0; j < x(0).derivatives().size(); j++) {
(*y)[i].derivatives()[j] = 0.0;
for (int k = 0; k < x.size(); k++) {
(*y)[i].derivatives()[j] += dyidx[k] * x(k).derivatives()[j];
}
}
}
}
void ExpressionConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DRAKE_DEMAND(x.rows() == vars_.rows());
symbolic::Substitution subst;
for (int i = 0; i < vars_.size(); ++i) {
if (!vars_[i].equal_to(x[i])) {
subst.emplace(vars_[i], x[i]);
}
}
y->resize(num_constraints());
if (subst.empty()) {
*y = expressions_;
} else {
for (int i = 0; i < num_constraints(); ++i) {
(*y)[i] = expressions_[i].Substitute(subst);
}
}
}
ExponentialConeConstraint::ExponentialConeConstraint(
const Eigen::Ref<const Eigen::SparseMatrix<double>>& A,
const Eigen::Ref<const Eigen::Vector3d>& b)
: Constraint(
2, A.cols(), Eigen::Vector2d::Zero(),
Eigen::Vector2d::Constant(std::numeric_limits<double>::infinity())),
A_{A},
b_{b} {
DRAKE_DEMAND(A.rows() == 3);
}
template <typename DerivedX, typename ScalarY>
void ExponentialConeConstraint::DoEvalGeneric(
const Eigen::MatrixBase<DerivedX>& x, VectorX<ScalarY>* y) const {
y->resize(num_constraints());
Vector3<ScalarY> z = A_ * x.template cast<ScalarY>() + b_;
using std::exp;
(*y)(0) = z(0) - z(1) * exp(z(2) / z(1));
(*y)(1) = z(1);
}
void ExponentialConeConstraint::DoEval(
const Eigen::Ref<const Eigen::VectorXd>& x, Eigen::VectorXd* y) const {
DoEvalGeneric(x, y);
}
void ExponentialConeConstraint::DoEval(const Eigen::Ref<const AutoDiffVecXd>& x,
AutoDiffVecXd* y) const {
DoEvalGeneric(x, y);
}
void ExponentialConeConstraint::DoEval(
const Eigen::Ref<const VectorX<symbolic::Variable>>& x,
VectorX<symbolic::Expression>* y) const {
DoEvalGeneric(x, y);
}
} // namespace solvers
} // namespace drake