forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpolyhedron.cc
1309 lines (1186 loc) · 52.6 KB
/
hpolyhedron.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "drake/geometry/optimization/hpolyhedron.h"
#include <algorithm>
#include <bitset>
#include <limits>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <tuple>
#include <Eigen/Eigenvalues>
#include <drake/solvers/binding.h>
#include <fmt/format.h>
#include <libqhullcpp/Coordinates.h>
#include <libqhullcpp/Qhull.h>
#include <libqhullcpp/QhullFacet.h>
#include <libqhullcpp/QhullFacetList.h>
#include "drake/common/overloaded.h"
#include "drake/common/ssize.h"
#include "drake/geometry/optimization/affine_subspace.h"
#include "drake/geometry/optimization/vpolytope.h"
#include "drake/math/matrix_util.h"
#include "drake/math/rotation_matrix.h"
#include "drake/solvers/aggregate_costs_constraints.h"
#include "drake/solvers/constraint.h"
#include "drake/solvers/get_program_type.h"
#include "drake/solvers/gurobi_solver.h"
#include "drake/solvers/solve.h"
namespace drake {
namespace geometry {
namespace optimization {
using Eigen::MatrixXd;
using Eigen::RowVectorXd;
using Eigen::VectorXd;
using math::RigidTransformd;
using solvers::Binding;
using solvers::Constraint;
using solvers::LinearConstraint;
using solvers::LinearCost;
using solvers::MathematicalProgram;
using solvers::MatrixXDecisionVariable;
using solvers::VectorXDecisionVariable;
using symbolic::Expression;
using symbolic::Variable;
namespace {
const double kInf = std::numeric_limits<double>::infinity();
std::tuple<bool, solvers::MathematicalProgramResult> IsInfeasible(
const MathematicalProgram& prog) {
// Turn off Gurobi DualReduction to ensure that infeasible problems always
// return solvers::SolutionResult::kInfeasibleConstraints rather than
// SolutionResult::kInfeasibleOrUnbounded.
solvers::SolverOptions solver_options;
solver_options.SetOption(solvers::GurobiSolver::id(), "DualReductions", 0);
auto result = solvers::Solve(prog, std::nullopt, solver_options);
return {result.get_solution_result() ==
solvers::SolutionResult::kInfeasibleConstraints,
result};
}
/* Checks whether the constraint cᵀ x ≤ d is already implied by the linear
constraints in prog. This is done by solving a small linear program
and modifying the coefficients of `new_constraint` binding. This method may
throw a runtime error if the constraints are ill-conditioned.
@param tol. We check if the prog already implies cᵀ x ≤ d + tol. If yes then we
think this constraint cᵀ x ≤ d is redundant. Larger tol means that we are less
strict on the containment.
*/
bool IsRedundant(const Eigen::Ref<const MatrixXd>& c, double d,
solvers::MathematicalProgram* prog,
Binding<solvers::LinearConstraint>* new_constraint,
Binding<solvers::LinearCost>* program_cost_binding,
double tol) {
// Ensures that prog is an LP.
DRAKE_DEMAND(prog->GetAllConstraints().size() ==
prog->GetAllLinearConstraints().size());
DRAKE_DEMAND(prog->GetAllCosts().size() == 1 &&
prog->linear_costs()[0] == *program_cost_binding);
// This inequality ensures a bounded objective since the left hand side of
// the inequality is the same as the cost function on the next line.
new_constraint->evaluator()->UpdateCoefficients(
c, VectorXd::Constant(1, -kInf), VectorXd::Constant(1, d + 1));
program_cost_binding->evaluator()->UpdateCoefficients(-c.transpose(), 0);
// Constraints define an empty set or the current inequality of other is not
// redundant. Since we tested whether this polyhedron is empty earlier, the
// function would already have exited so this is proof that this inequality
// is irredundant.
auto [polyhedron_is_empty, result] = IsInfeasible(*prog);
if (!polyhedron_is_empty && !result.is_success()) {
throw std::runtime_error(fmt::format(
"Solver {} failed to compute the set difference; it "
"terminated with SolutionResult {}). This should only happen"
"if the problem is ill-conditioned",
result.get_solver_id().name(), result.get_solution_result()));
}
// If -result.get_optimal_cost() > other.b()(i) then the inequality is
// irredundant. Without this constant
// IrredundantBallIntersectionContainsBothOriginal fails.
return !(polyhedron_is_empty || -result.get_optimal_cost() > d + tol);
}
/* Returns a trivially-infeasible HPolyhedron of a given dimension. */
HPolyhedron ConstructInfeasibleHPolyhedron(int dimension) {
MatrixXd A_infeasible(2, dimension);
A_infeasible.setZero();
A_infeasible(0, 0) = 1;
A_infeasible(1, 0) = -1;
Eigen::Vector2d b_infeasible(-1, 0);
return HPolyhedron(A_infeasible, b_infeasible);
}
} // namespace
HPolyhedron::HPolyhedron() : ConvexSet(0, false) {}
HPolyhedron::HPolyhedron(const Eigen::Ref<const MatrixXd>& A,
const Eigen::Ref<const VectorXd>& b)
: ConvexSet(A.cols(), false), A_(A), b_(b) {
CheckInvariants();
}
HPolyhedron::HPolyhedron(const QueryObject<double>& query_object,
GeometryId geometry_id,
std::optional<FrameId> reference_frame)
: ConvexSet(3, false) {
const Shape& shape = query_object.inspector().GetShape(geometry_id);
MatrixXd A_G;
VectorXd b_G;
std::tie(A_G, b_G) = shape.Visit<std::pair<MatrixXd, VectorXd>>(overloaded{
// We only handle certain shape types.
// TODO(russt): Support [](const Convex& convex); it is already supported
// by VPolytope.
[](const Box& box) {
Eigen::Matrix<double, 6, 3> A;
A << Eigen::Matrix3d::Identity(), -Eigen::Matrix3d::Identity();
Vector6d b;
// clang-format off
b << box.width()/2.0, box.depth()/2.0, box.height()/2.0,
box.width()/2.0, box.depth()/2.0, box.height()/2.0;
// clang-format on
return std::make_pair(A, b);
},
[](const HalfSpace&) {
// z <= 0.0.
Eigen::RowVector3d A{0.0, 0.0, 1.0};
Vector1d b{0.0};
return std::make_pair(A, b);
},
[&geometry_id](const auto& unsupported) -> std::pair<MatrixXd, VectorXd> {
throw std::logic_error(fmt::format(
"{} (geometry_id={}) cannot be converted to a HPolyhedron",
unsupported, geometry_id));
}});
const RigidTransformd X_WE =
reference_frame ? query_object.GetPoseInWorld(*reference_frame)
: RigidTransformd::Identity();
const RigidTransformd& X_WG = query_object.GetPoseInWorld(geometry_id);
const RigidTransformd X_GE = X_WG.InvertAndCompose(X_WE);
// A_G*(p_GE + R_GE*p_EE_var) ≤ b_G
A_ = A_G * X_GE.rotation().matrix();
b_ = b_G - A_G * X_GE.translation();
}
HPolyhedron::HPolyhedron(const VPolytope& vpoly, double tol)
: ConvexSet(vpoly.ambient_dimension(), false) {
// First, handle the case where the VPolytope is empty.
if (vpoly.IsEmpty()) {
if (vpoly.ambient_dimension() == 0) {
throw std::runtime_error(
"Cannot convert an empty VPolytope with ambient dimension zero into "
"a HPolyhedron.");
} else {
// Just create an infeasible HPolyhedron.
*this = ConstructInfeasibleHPolyhedron(vpoly.ambient_dimension());
return;
}
}
if (vpoly.ambient_dimension() == 1) {
// In 1D, QHull doesn't work. We can simply choose the largest and smallest
// points, and add a hyperplane there.
double min_val = vpoly.vertices().minCoeff();
double max_val = vpoly.vertices().maxCoeff();
Eigen::MatrixXd A(2, 1);
Eigen::VectorXd b(2);
// x <= max_val and x >= min_val (written as -x <= -min_val)
A << 1, -1;
b << max_val, -min_val;
*this = HPolyhedron(A, b);
return;
}
// Next, handle the case where the VPolytope is not full dimensional.
const AffineSubspace affine_hull(vpoly, tol);
if (affine_hull.AffineDimension() < affine_hull.ambient_dimension()) {
// This special case avoids two QHull errors: QH6214 and QH6154.
// QH6214 is due to the VPolytope not having enough vertices to make a
// full dimensional simplex, and QH6154 is due to the VPolytope not being
// full-dimensional, because all the vertices lie along a proper affine
// subspace. We can handle both of these cases by projecting onto the
// affine hull and doing the computations there. Then, we lift back to the
// original space, and add hyperplanes to require the point lie on the
// affine subspace.
Eigen::MatrixXd points_local =
affine_hull.ToLocalCoordinates(vpoly.vertices());
// Note that QHull will not function in zero or one dimensional spaces, so
// we handle these separately here.
Eigen::MatrixXd global_A;
Eigen::VectorXd global_b;
if (affine_hull.AffineDimension() == 0) {
// If it's just a point, then the orthogonal complement constraints
// will do all the work, and we can set global_A and global_b to empty.
global_A = Eigen::MatrixXd::Zero(0, ambient_dimension());
global_b = Eigen::VectorXd::Zero(0);
} else if (affine_hull.AffineDimension() == 1) {
// In this case, we have a line, so we just pick the highest and lowest
// points on that line (w.r.t. the direction of the one vector in the
// affine basis) and put hyperplanes there.
int min_idx, max_idx;
Eigen::VectorXd point_local_vector = points_local.row(0);
point_local_vector.minCoeff(&min_idx);
point_local_vector.maxCoeff(&max_idx);
Eigen::VectorXd lower_point = vpoly.vertices().col(min_idx);
Eigen::VectorXd upper_point = vpoly.vertices().col(max_idx);
Eigen::VectorXd direction = affine_hull.basis();
global_A = Eigen::MatrixXd::Zero(2, ambient_dimension());
global_A.row(0) = -direction;
global_A.row(1) = direction;
global_b = Eigen::VectorXd::Zero(2);
global_b[0] = -lower_point.dot(direction);
global_b[1] = upper_point.dot(direction);
} else {
// Construct a new VPolytope in the local coordinates, and then try
// converting it to an HPolyhedron. This VPolytope is full dimensional
// and has enough points for QHull, so in the subsequent call of the
// VPolytope -> HPolyhedron constructor, none of this conditional block
// is considered, and it goes directly to QHull. If QHull has additional
// errors besides the dimension ones, they will be caught there.
HPolyhedron hpoly_subspace(VPolytope{points_local});
// Now, lift the HPolyhedron to the original ambient space. A point x in
// R^n is projected to P(x-d), where d is the AffineSubspace translation,
// and P is the projection matrix associated with the basis B (so that PB
// is the identity matrix, mapping points in the local coordinates of the
// AffineSubspace to themselves, where B is the matrix whose columns are
// the basis vectors). Thus, an HPolyhedron in local coordinates
// {Ay <= b : y in R^m} can be lifted to an HPolyhedron in global
// coordinates by substituting P(x-d) for y. After simplification, we have
// {APx <= b + APd : x in R^n}.
// First, we obtain the matrix P. Because it's a linear operator, we can
// compute its matrix by passing in the standard basis vectors (centered
// at the translation of the AffineSubspace).
Eigen::MatrixXd P_in =
Eigen::MatrixXd::Identity(ambient_dimension(), ambient_dimension());
Eigen::MatrixXd P = affine_hull.ToLocalCoordinates(
P_in.colwise() + affine_hull.translation());
// Now, we construct the global versions of A and b.
global_A = hpoly_subspace.A() * P;
global_b = hpoly_subspace.b() +
hpoly_subspace.A() * P * affine_hull.translation();
}
// Finally, we add additional constraints from the orthogonal complement
// basis. This ensures that the points in the new HPolyhedron lie along the
// affine hull of the VPolytope.
Eigen::MatrixXd orthogonal_complement_basis =
affine_hull.OrthogonalComplementBasis();
Eigen::MatrixXd orth_A(2 * orthogonal_complement_basis.cols(),
ambient_dimension());
orth_A << orthogonal_complement_basis.transpose(),
-orthogonal_complement_basis.transpose();
Eigen::VectorXd orth_b = orth_A * affine_hull.translation();
Eigen::MatrixXd full_A(global_A.rows() + orth_A.rows(),
ambient_dimension());
full_A << global_A, orth_A;
Eigen::VectorXd full_b(global_b.size() + orth_b.size());
full_b << global_b, orth_b;
*this = HPolyhedron(full_A, full_b);
return;
}
// Now that we know that the VPolytope is full dimensional, we can call QHull.
orgQhull::Qhull qhull;
qhull.runQhull("", vpoly.ambient_dimension(), vpoly.vertices().cols(),
vpoly.vertices().data(), "");
if (qhull.qhullStatus() != 0) {
throw std::runtime_error(
fmt::format("Qhull exited with status {} and message:\n{}",
qhull.qhullStatus(), qhull.qhullMessage()));
}
A_.resize(qhull.facetCount(), ambient_dimension());
b_.resize(qhull.facetCount());
int facet_count = 0;
for (const auto& facet : qhull.facetList()) {
A_.row(facet_count) = Eigen::Map<Eigen::RowVectorXd>(
facet.outerplane().coordinates(), facet.dimension());
b_(facet_count) = -facet.outerplane().offset();
++facet_count;
}
}
HPolyhedron::HPolyhedron(const MathematicalProgram& prog)
: ConvexSet(prog.num_vars(), false) {
// Preconditions
DRAKE_THROW_UNLESS(prog.num_vars() > 0);
DRAKE_THROW_UNLESS(prog.GetAllConstraints().size() > 0);
DRAKE_THROW_UNLESS(solvers::GetProgramType(prog) ==
solvers::ProgramType::kLP);
// Get linear equality constraints.
std::vector<Eigen::Triplet<double>> A_triplets;
std::vector<double> b;
int A_row_count = 0;
std::vector<int> unused_linear_eq_y_start_indices;
int unused_num_rows_added = 0;
solvers::internal::ParseLinearEqualityConstraints(
prog, &A_triplets, &b, &A_row_count, &unused_linear_eq_y_start_indices,
&unused_num_rows_added);
// Linear equality constraints Ax = b are parsed in the form Ax <= b. So we
// add in the reverse of the constraint, Ax >= b, encoded as -Ax <= -b. This
// is implemented by taking each triplet (i, j, v) and adding in
// (i + N, j, -v), where N is the number of rows of A before we start adding
// new triplets.
const int num_equality_triplets = A_triplets.size();
const int& num_equality_constraints = A_row_count;
for (int i = 0; i < num_equality_triplets; ++i) {
const Eigen::Triplet<double>& triplet = A_triplets.at(i);
A_triplets.emplace_back(triplet.row() + num_equality_constraints,
triplet.col(), -triplet.value());
}
// We also set b := [b; -b].
for (int i = 0; i < num_equality_constraints; ++i) {
b.push_back(-1 * b[i]);
}
A_row_count *= 2;
// Get linear inequality constraints.
std::vector<std::vector<std::pair<int, int>>>
unused_linear_constraint_dual_indices;
solvers::internal::ParseLinearConstraints(
prog, &A_triplets, &b, &A_row_count,
&unused_linear_constraint_dual_indices, &unused_num_rows_added);
// Check that none of the linear inequality constraints are trivially
// infeasible by requiring a variable be at least ∞ or at most -∞. If this is
// the case, return a trivially infeasible HPolyhedron. Constraints that
// violate this condition may not be returned by ParseLinearConstraints.
for (const auto& binding : prog.linear_constraints()) {
if (binding.evaluator()->lower_bound().maxCoeff() == kInf ||
binding.evaluator()->upper_bound().minCoeff() == -kInf) {
*this = ConstructInfeasibleHPolyhedron(prog.num_vars());
return;
}
}
// Get bounding box constraints.
VectorXd lb;
VectorXd ub;
solvers::AggregateBoundingBoxConstraints(prog, &lb, &ub);
for (int i = 0; i < lb.size(); ++i) {
// If lb[i] == Infinity or ub[i] == -Infinity, we have a trivial
// infeasibility, so we make a trivially infeasible HPolyhedron by just
// including the constraints x <= -1, x >= 0, where x is the first decision
// variable.
if (lb[i] == kInf || ub[i] == -kInf) {
*this = ConstructInfeasibleHPolyhedron(prog.num_vars());
return;
} else {
A_triplets.emplace_back(A_row_count, i, 1);
b.push_back(ub[i]);
++A_row_count;
A_triplets.emplace_back(A_row_count, i, -1);
b.push_back(-lb[i]);
++A_row_count;
}
}
// Form the A and b matrices of the HPolyhedron.
Eigen::SparseMatrix<double, Eigen::RowMajor> A_sparse(A_row_count,
prog.num_vars());
A_sparse.setFromTriplets(A_triplets.begin(), A_triplets.end());
// Identify the rows that do not contain any infinities, as the rows to keep.
// Since we have filtered out infinities that render the problem infeasible
// above, these correspond to inequalities that are trivially satisfied by all
// vectors x, and need not be included in the HPolyhedron.
std::vector<int> rows_to_keep;
rows_to_keep.reserve(A_sparse.rows());
for (int i = 0; i < ssize(b); ++i) {
if (std::abs(b[i]) < kInf) {
rows_to_keep.push_back(i);
}
}
VectorXd b_eigen(b.size());
b_eigen = VectorXd::Map(b.data(), b.size());
*this = HPolyhedron(A_sparse.toDense()(rows_to_keep, Eigen::all),
b_eigen(rows_to_keep));
}
HPolyhedron::~HPolyhedron() = default;
Hyperellipsoid HPolyhedron::MaximumVolumeInscribedEllipsoid() const {
MathematicalProgram prog;
const int N = this->ambient_dimension();
MatrixXDecisionVariable C = prog.NewSymmetricContinuousVariables(N, "C");
VectorXDecisionVariable d = prog.NewContinuousVariables(N, "d");
// Compute rowwise norms for later use in normalization of linear constraints.
MatrixXd augmented_matrix(A_.rows(), A_.cols() + b_.cols());
augmented_matrix << A_, b_;
VectorXd row_norms = augmented_matrix.rowwise().norm();
// max log det (C). This method also imposes C ≽ 0.
prog.AddMaximizeLogDeterminantCost(C.cast<Expression>());
// |aᵢC|₂ ≤ bᵢ - aᵢd, ∀i
// Add this as A_lorentz * vars + b_lorentz in the Lorentz cone constraint.
// vars = [d; C.col(0); C.col(1); ...; C.col(n-1)]
// A_lorentz = block_diagonal(-A_.row(i), A_.row(i), ..., A_.row(i))
// b_lorentz = [b_(i); 0; ...; 0]
// We also normalize each row, by dividing each instance of aᵢ and bᵢ with the
// corresponding row norm.
VectorX<symbolic::Variable> vars(C.rows() * C.cols() + d.rows());
vars.head(d.rows()) = d;
for (int i = 0; i < C.cols(); ++i) {
vars.segment(d.rows() + i * C.rows(), C.rows()) = C.col(i);
}
Eigen::MatrixXd A_lorentz =
Eigen::MatrixXd::Zero(1 + C.cols(), (1 + C.cols()) * C.rows());
Eigen::VectorXd b_lorentz = Eigen::VectorXd::Zero(1 + C.cols());
for (int i = 0; i < b_.size(); ++i) {
A_lorentz.setZero();
A_lorentz.block(0, 0, 1, A_.cols()) = -A_.row(i) / row_norms(i);
for (int j = 0; j < C.cols(); ++j) {
A_lorentz.block(j + 1, (j + 1) * C.cols(), 1, A_.cols()) =
A_.row(i) / row_norms(i);
}
b_lorentz(0) = b_(i) / row_norms(i);
prog.AddLorentzConeConstraint(A_lorentz, b_lorentz, vars);
}
auto result = solvers::Solve(prog);
if (!result.is_success()) {
throw std::runtime_error(fmt::format(
"Solver {} failed to solve the maximum inscribed ellipse problem; it "
"terminated with SolutionResult {}). Make sure that your polyhedron is "
"bounded and has an interior.",
result.get_solver_id().name(), result.get_solution_result()));
}
return Hyperellipsoid(result.GetSolution(C).inverse(), result.GetSolution(d));
}
VectorXd HPolyhedron::ChebyshevCenter() const {
MathematicalProgram prog;
VectorXDecisionVariable x = prog.NewContinuousVariables(ambient_dimension());
VectorXDecisionVariable r = prog.NewContinuousVariables<1>("r");
const double inf = std::numeric_limits<double>::infinity();
// max r
prog.AddLinearCost(Vector1d(-1.0), 0, r);
// r ≥ 0.
prog.AddBoundingBoxConstraint(0, inf, r);
// aᵢᵀ x + |aᵢ| r ≤ bᵢ.
RowVectorXd a(A_.cols() + 1);
for (int i = 0; i < A_.rows(); ++i) {
a[0] = A_.row(i).norm();
a.tail(A_.cols()) = A_.row(i);
prog.AddLinearConstraint(a, -inf, b_[i], {r, x});
}
auto result = solvers::Solve(prog);
if (!result.is_success()) {
throw std::runtime_error(fmt::format(
"Solver {} failed to solve the Chebyshev center problem; it terminated "
"with SolutionResult {}). Make sure that your polyhedron is bounded "
"and has an interior.",
result.get_solver_id().name(), result.get_solution_result()));
}
return result.GetSolution(x);
}
HPolyhedron HPolyhedron::Scale(double scale,
std::optional<Eigen::VectorXd> center) const {
DRAKE_THROW_UNLESS(scale >= 0.0);
if (center) {
DRAKE_THROW_UNLESS(center->size() == ambient_dimension());
} else {
center = ChebyshevCenter();
}
return HPolyhedron(
A_, std::pow(scale, 1.0 / ambient_dimension()) * (b_ - A_ * *center) +
A_ * *center);
}
HPolyhedron HPolyhedron::CartesianProduct(const HPolyhedron& other) const {
MatrixXd A_product = MatrixXd::Zero(A_.rows() + other.A().rows(),
A_.cols() + other.A().cols());
A_product.topLeftCorner(A_.rows(), A_.cols()) = A_;
A_product.bottomRightCorner(other.A().rows(), other.A().cols()) = other.A();
VectorXd b_product(b_.size() + other.b().size());
b_product << b_, other.b();
return {A_product, b_product};
}
HPolyhedron HPolyhedron::CartesianPower(int n) const {
MatrixXd A_power = MatrixXd::Zero(n * A_.rows(), n * A_.cols());
for (int i{0}; i < n; ++i) {
A_power.block(i * A_.rows(), i * A_.cols(), A_.rows(), A_.cols()) = A_;
}
VectorXd b_power = b_.replicate(n, 1);
return {A_power, b_power};
}
HPolyhedron HPolyhedron::Intersection(const HPolyhedron& other,
bool check_for_redundancy,
double tol) const {
DRAKE_THROW_UNLESS(ambient_dimension() == other.ambient_dimension());
if (check_for_redundancy) {
return this->DoIntersectionWithChecks(other, tol);
}
return this->DoIntersectionNoChecks(other);
}
VectorXd HPolyhedron::UniformSample(
RandomGenerator* generator,
const Eigen::Ref<const Eigen::VectorXd>& previous_sample,
const int mixing_steps,
const std::optional<Eigen::Ref<const Eigen::MatrixXd>>& subspace,
double tol) const {
DRAKE_THROW_UNLESS(mixing_steps >= 1);
if (subspace.has_value()) {
DRAKE_THROW_UNLESS(subspace->rows() == ambient_dimension());
}
std::normal_distribution<double> gaussian;
VectorXd current_sample = previous_sample;
const int sampling_dim =
subspace.has_value() ? subspace->cols() : previous_sample.rows();
VectorXd gaussian_sample(sampling_dim);
VectorXd direction(ambient_dimension());
// If a row of the A matrix is orthogonal to all columns of the basis, then
// the constraint from that row is enforced by the sample being in the column
// space of the basis. Thus, we skip that constraint when performing
// hit-and-run sampling.
std::vector<bool> skip(A_.rows(), false);
if (subspace.has_value()) {
const double squared_tol = tol * tol;
VectorXd subspace_j_squared_norm(subspace->cols());
for (int j = 0; j < subspace->cols(); ++j) {
subspace_j_squared_norm(j) = subspace->col(j).squaredNorm();
}
for (int i = 0; i < A_.rows(); ++i) {
skip[i] = true;
const double Ai_squared_norm = A_.row(i).squaredNorm();
for (int j = 0; j < subspace->cols(); ++j) {
if (std::pow(A_.row(i) * subspace->col(j), 2) >
squared_tol * Ai_squared_norm * subspace_j_squared_norm(j)) {
skip[i] = false;
break;
}
}
}
}
for (int step = 0; step < mixing_steps; ++step) {
// Choose a random direction.
for (int i = 0; i < gaussian_sample.size(); ++i) {
gaussian_sample[i] = gaussian(*generator);
}
direction =
subspace.has_value() ? *subspace * gaussian_sample : gaussian_sample;
// Find max and min θ subject to
// A(previous_sample + θ*direction) ≤ b,
// aka ∀i, θ * (A * direction)[i] ≤ (b - A * previous_sample)[i].
VectorXd line_b = b_ - A_ * current_sample;
VectorXd line_a = A_ * direction;
double theta_max = std::numeric_limits<double>::infinity();
double theta_min = -theta_max;
for (int i = 0; i < line_a.size(); ++i) {
if (skip[i]) {
continue;
} else if (line_a[i] < 0.0) {
theta_min = std::max(theta_min, line_b[i] / line_a[i]);
} else if (line_a[i] > 0.0) {
theta_max = std::min(theta_max, line_b[i] / line_a[i]);
}
}
if (std::isinf(theta_max) || std::isinf(theta_min) ||
theta_max < theta_min) {
throw std::invalid_argument(fmt::format(
"The Hit and Run algorithm failed to find a feasible point in the "
"set. The `previous_sample` must be in the set.\n"
"max(A * previous_sample - b) = {}",
(A_ * current_sample - b_).maxCoeff()));
}
// Now pick θ uniformly from [θ_min, θ_max).
std::uniform_real_distribution<double> uniform_theta(theta_min, theta_max);
const double theta = uniform_theta(*generator);
current_sample = current_sample + theta * direction;
}
// The new sample is previous_sample + θ * direction.
const double kWarnTolerance = 1e-8;
if ((current_sample - previous_sample).template lpNorm<Eigen::Infinity>() <
kWarnTolerance) {
// If the new sample is extremely close to the previous sample, the user
// may have a lower-dimensional polytope. We warn them if this happens. Note
// that we use an absolute tolerance here, since it's unclear how to compute
// an appropriate relative tolerance.
drake::log()->warn(
"The Hit and Run algorithm produced a random guess that is extremely "
"close to `previous_sample`, which could indicate that the HPolyhedron "
"being sampled is not full-dimensional. To draw samples from such an "
"HPolyhedron, please use the `subspace` argument.");
}
return current_sample;
}
// Note: This method only exists to effectively provide ChebyshevCenter(),
// which is a non-static class method, as a default argument for
// previous_sample in the UniformSample method above.
VectorXd HPolyhedron::UniformSample(
RandomGenerator* generator, const int mixing_steps,
const std::optional<Eigen::Ref<const Eigen::MatrixXd>>& subspace,
double tol) const {
VectorXd center = ChebyshevCenter();
return UniformSample(generator, center, mixing_steps, subspace, tol);
}
HPolyhedron HPolyhedron::MakeBox(const Eigen::Ref<const VectorXd>& lb,
const Eigen::Ref<const VectorXd>& ub) {
DRAKE_THROW_UNLESS(lb.size() == ub.size());
DRAKE_THROW_UNLESS((lb.array() <= ub.array()).all());
const int N = lb.size();
MatrixXd A(2 * N, N);
A << MatrixXd::Identity(N, N), -MatrixXd::Identity(N, N);
VectorXd b(2 * N);
b << ub, -lb;
return HPolyhedron(A, b);
}
HPolyhedron HPolyhedron::MakeUnitBox(int dim) {
return MakeBox(VectorXd::Constant(dim, -1.0), VectorXd::Constant(dim, 1.0));
}
HPolyhedron HPolyhedron::MakeL1Ball(const int dim) {
DRAKE_THROW_UNLESS(dim > 0);
const int size{static_cast<int>(std::pow(2, dim))};
MatrixXd A = MatrixXd::Ones(size, dim);
VectorXd b = VectorXd::Ones(size);
// L1Ball is constructed by iterating over all permutations of {± 1}ᵈⁱᵐ.
constexpr int bit_set_size = 8 * sizeof(int);
for (int row = 0; row < A.rows(); ++row) {
const std::bitset<bit_set_size> row_bits(row);
for (int col = 0; col < A.cols(); ++col) {
A(row, col) = row_bits[col] ? -1 : 1;
}
}
return {A, b};
}
std::optional<bool> HPolyhedron::DoIsBoundedShortcut() const {
if (IsEmpty()) {
// We must always test if an HPolyhedron is empty, as all subsequent tests
// depend on the HPolyhedron being nonempty. See
// BoundednessCheckEmptyEdgeCases in hpolyhedron_test.cc for examples.
return true;
}
if (A_.rows() < A_.cols()) {
// If A_ has fewer rows than columns, then either the HPolyhedron is
// unbounded, or it's empty due to a subset of the inequalities being
// mutually exclusive (and therefore bounded). Emptiness has already
// been checked above, so it's unbounded.
return false;
}
Eigen::ColPivHouseholderQR<MatrixXd> qr(A_);
if (qr.dimensionOfKernel() > 0) {
// A similar edge case to the above is possible, so once again, the
// HPolyhedron is bounded if and only if it is empty; because we already
// checked for emptiness, it's unbounded.
return false;
}
// Stiemke's theorem of alternatives says that, given A with ker(A) = {0}, we
// either have existence of x ≠ 0 such that Ax ≥ 0 or we have existence of y
// > 0 such that y^T A = 0. Since any y that verifies the second condition
// can be arbitrarily scaled, and would still pass the second condition,
// instead of asking y > 0, we can equivalently ask y ≥ 1. So boundedness
// corresponds to the following LP being feasible: find y s.t. y ≥ 1, y^T A =
// 0.
MathematicalProgram prog;
auto y = prog.NewContinuousVariables(A_.rows(), "y");
prog.AddBoundingBoxConstraint(1.0, std::numeric_limits<double>::infinity(),
y);
prog.AddLinearEqualityConstraint(A_.transpose(), VectorXd::Zero(A_.cols()),
y);
auto result = solvers::Solve(prog);
return result.is_success();
}
bool HPolyhedron::DoIsEmpty() const {
if (ambient_dimension() == 0) {
return false;
}
solvers::MathematicalProgram prog;
solvers::VectorXDecisionVariable x =
prog.NewContinuousVariables(A_.cols(), "x");
prog.AddLinearConstraint(A_, VectorXd::Constant(b_.rows(), -kInf), b_, x);
return std::get<0>(IsInfeasible(prog));
}
bool HPolyhedron::ContainedIn(const HPolyhedron& other, double tol) const {
DRAKE_THROW_UNLESS(other.A().cols() == A_.cols());
// `this` defines an empty set and therefore is contained in any `other`
// HPolyhedron.
if (DoIsEmpty()) {
return true;
}
solvers::MathematicalProgram prog;
solvers::VectorXDecisionVariable x =
prog.NewContinuousVariables(A_.cols(), "x");
prog.AddLinearConstraint(A_, VectorXd::Constant(b_.rows(), -kInf), b_, x);
Binding<solvers::LinearConstraint> redundant_constraint_binding =
prog.AddLinearConstraint(other.A().row(0), VectorXd::Constant(1, -kInf),
other.b().row(0), x);
Binding<solvers::LinearCost> program_cost_binding =
prog.AddLinearCost(-other.A().row(0), 0, x);
for (int i = 0; i < other.A().rows(); ++i) {
// If any of the constraints of `other` are irredundant then `this` is
// not contained in `other`.
if (!IsRedundant(other.A().row(i), other.b()(i), &prog,
&redundant_constraint_binding, &program_cost_binding,
tol)) {
return false;
}
}
return true;
}
HPolyhedron HPolyhedron::DoIntersectionNoChecks(
const HPolyhedron& other) const {
MatrixXd A_intersect =
MatrixXd::Zero(A_.rows() + other.A().rows(), A_.cols());
A_intersect.topRows(A_.rows()) = A_;
A_intersect.bottomRows(other.A().rows()) = other.A();
VectorXd b_intersect(b_.size() + other.b().size());
b_intersect << b_, other.b();
return {A_intersect, b_intersect};
}
HPolyhedron HPolyhedron::DoIntersectionWithChecks(const HPolyhedron& other,
double tol) const {
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> A(
A_.rows() + other.A().rows(), A_.cols());
VectorXd b(A_.rows() + other.A().rows());
A.topRows(A_.rows()) = A_;
b.head(A_.rows()) = b_;
solvers::MathematicalProgram prog;
solvers::VectorXDecisionVariable x =
prog.NewContinuousVariables(A_.cols(), "x");
prog.AddLinearConstraint(A_, VectorXd::Constant(b_.rows(), -kInf), b_, x);
auto [infeasible, result] = IsInfeasible(prog);
// `this` defines an empty set therefore any additional constraint is
// redundant.
if (infeasible) {
return {A_, b_};
}
Binding<solvers::LinearConstraint> redundant_constraint_binding =
prog.AddLinearConstraint(other.A().row(0), VectorXd::Constant(1, -kInf),
other.b().row(0), x);
Binding<solvers::LinearCost> program_cost_binding =
prog.AddLinearCost(-other.A().row(0), 0, x);
int num_kept = A_.rows();
for (int i = 0; i < other.A().rows(); ++i) {
if (!IsRedundant(other.A().row(i), other.b()(i), &prog,
&redundant_constraint_binding, &program_cost_binding,
tol)) {
A.row(num_kept) = other.A().row(i);
b.row(num_kept) = other.b().row(i);
++num_kept;
HPolyhedron maybe_empty(A.topRows(num_kept), b.topRows(num_kept));
if (maybe_empty.IsEmpty()) {
return maybe_empty;
}
}
}
return {A.topRows(num_kept), b.topRows(num_kept)};
}
HPolyhedron HPolyhedron::ReduceInequalities(double tol) const {
const std::set<int> redundant_indices = FindRedundant(tol);
const int num_vars = A_.cols();
MatrixXd A_new(A_.rows() - redundant_indices.size(), num_vars);
VectorXd b_new(A_new.rows());
int i = 0;
for (int j = 0; j < A_.rows(); ++j) {
if (!redundant_indices.contains(j)) {
A_new.row(i) = A_.row(j);
b_new.row(i) = b_.row(j);
++i;
}
}
return {A_new, b_new};
}
std::set<int> HPolyhedron::FindRedundant(double tol) const {
// This method is based on removing each constraint and solving the resulting
// LP. If the optimal cost is greater than the constraint's right hand side,
// then the constraint is redundant.
// Possible future speed up: solve duals instead of primal in parallel,
// however this would require building num_threads mathematical programs and
// may not be worth it.
std::set<int> redundant_indices;
MathematicalProgram prog;
const int num_vars = A_.cols();
const int num_cons = A_.rows();
const auto x = prog.NewContinuousVariables(num_vars, "x");
std::vector<Binding<LinearConstraint>> bindings_vec;
for (int i = 0; i < num_cons; ++i) {
bindings_vec.push_back(prog.AddLinearConstraint(
A_.row(i), -std::numeric_limits<double>::infinity(), b_[i], x));
}
auto const_binding = prog.AddLinearCost(-A_.row(0), 0, x);
for (int i = 0; i < num_cons; ++i) {
prog.RemoveConstraint(bindings_vec.at(i));
const_binding.evaluator()->UpdateCoefficients(-A_.row(i), 0);
const auto result = Solve(prog);
if ((result.is_success() && -result.get_optimal_cost() > b_[i] + tol) ||
!result.is_success()) {
// Bring back the constraint, it is not redundant.
prog.AddConstraint(bindings_vec.at(i));
} else {
redundant_indices.insert(i);
}
}
return redundant_indices;
}
namespace {
HPolyhedron MoveFaceAndCull(const Eigen::MatrixXd& A, const Eigen::VectorXd& b,
Eigen::VectorXd* face_center_distance,
std::vector<bool>* face_moved_in, int* i,
const std::vector<int>& i_cull) {
DRAKE_DEMAND(ssize(*face_moved_in) >= *i + 1);
(*face_moved_in)[*i] = true;
std::vector<int> i_not_cull;
i_not_cull.reserve(A.rows() - i_cull.size());
int num_cull_before_i = 0;
for (int j = 0; j < A.rows(); ++j) {
if (std::find(i_cull.begin(), i_cull.end(), j) == i_cull.end()) {
i_not_cull.push_back(j);
} else if (*std::find(i_cull.begin(), i_cull.end(), j) < *i) {
// Count how many indices before `i` are to be culled.
++num_cull_before_i;
}
}
// Create updated matrices/lists without the elements/rows at indices in
// `i_cull`.
MatrixXd A_new(i_not_cull.size(), A.cols());
VectorXd b_new(i_not_cull.size());
std::vector<bool> face_moved_in_new;
face_moved_in_new.reserve(i_not_cull.size());
VectorXd face_center_distance_new(i_not_cull.size());
for (int j = 0; j < ssize(i_not_cull); ++j) {
A_new.row(j) = A.row(i_not_cull[j]);
b_new(j) = b(i_not_cull[j]);
face_moved_in_new.push_back((*face_moved_in)[i_not_cull[j]]);
face_center_distance_new[j] = (*face_center_distance)[i_not_cull[j]];
}
// The face index needs to reduce because faces at lower indices have been
// removed.
*i = *i - num_cull_before_i;
HPolyhedron inbody = HPolyhedron(A_new, b_new);
*face_center_distance = face_center_distance_new;
*face_moved_in = face_moved_in_new;
return inbody;
}
} // namespace
HPolyhedron HPolyhedron::SimplifyByIncrementalFaceTranslation(
const double min_volume_ratio, const bool do_affine_transformation,
const int max_iterations, const Eigen::MatrixXd& points_to_contain,
const std::vector<HPolyhedron>& intersecting_polytopes,
const bool keep_whole_intersection, const double intersection_padding,
const int random_seed) const {
DRAKE_THROW_UNLESS(min_volume_ratio > 0);
DRAKE_THROW_UNLESS(max_iterations > 0);
DRAKE_THROW_UNLESS(intersection_padding >= 0);
const HPolyhedron circumbody = this->ReduceInequalities(0);
MatrixXd circumbody_A = circumbody.A();
VectorXd circumbody_b = circumbody.b();
DRAKE_THROW_UNLESS(!circumbody.IsEmpty());
DRAKE_THROW_UNLESS(circumbody.IsBounded());
for (int i = 0; i < points_to_contain.cols(); ++i) {
DRAKE_DEMAND(circumbody.PointInSet(points_to_contain.col(i)));
}
// Ensure rows are normalized.
for (int i = 0; i < circumbody_A.rows(); ++i) {
const double initial_row_norm = circumbody_A.row(i).norm();
circumbody_A.row(i) /= initial_row_norm;
circumbody_b(i) /= initial_row_norm;
}
// Create vector of distances from all faces to circumbody center.
const VectorXd circumbody_ellipsoid_center =
circumbody.MaximumVolumeInscribedEllipsoid().center();
VectorXd face_center_distance(circumbody_b.rows());
for (int i = 0; i < circumbody_b.rows(); ++i) {
face_center_distance(i) =
circumbody_b(i) - circumbody_A.row(i) * circumbody_ellipsoid_center;
}
// Scaling factor for face distances being translated inward.
const double face_scale_ratio =
1 - std::pow(min_volume_ratio, 1.0 / ambient_dimension());
// A multiplier for cost in LP that finds how far a face can be moved inward
// before losing an intersection. Maximizes or minimizes dot product between
// a point and the face normal, depending on `keep_whole_intersection`.
const int cost_multiplier = keep_whole_intersection ? -1 : 1;
// If scaled circumbody still intersects with a polytope in
// `intersecting_polytopes`, then we don't need to worry about losing this
// intersection in the face translation algorithm because the scaled
// circumbody will always be a subset of the inbody.
const HPolyhedron scaled_circumbody = circumbody.Scale(min_volume_ratio);
std::vector<drake::geometry::optimization::HPolyhedron>
reduced_intersecting_polytopes;
reduced_intersecting_polytopes.reserve(intersecting_polytopes.size());
for (size_t i = 0; i < intersecting_polytopes.size(); ++i) {
DRAKE_DEMAND(circumbody.IntersectsWith(intersecting_polytopes[i]));
if (keep_whole_intersection ||
!scaled_circumbody.IntersectsWith(intersecting_polytopes[i])) {
reduced_intersecting_polytopes.push_back(intersecting_polytopes[i]);
}
}
// Initialize inbody as circumbody.
HPolyhedron inbody = circumbody;
std::vector<bool> face_moved_in(inbody.b().rows(), false);
int iterations = 0;
bool any_faces_moved = true;
RandomGenerator generator(random_seed);
while (any_faces_moved && iterations < max_iterations) {
any_faces_moved = false;
// Shuffle hyperplanes.
std::vector<int> shuffle_inds(face_center_distance.size());
std::iota(shuffle_inds.begin(), shuffle_inds.end(), 0);
std::shuffle(shuffle_inds.begin(), shuffle_inds.end(), generator);
MatrixXd A_shuffled(inbody.b().size(), ambient_dimension());
VectorXd b_shuffled(inbody.b().size());
VectorXd face_center_distance_shuffled(inbody.b().size());
std::vector<bool> face_moved_in_shuffled(inbody.b().size());
for (int i_shuffle : shuffle_inds) {
A_shuffled.row(i_shuffle) = inbody.A().row(shuffle_inds[i_shuffle]);
b_shuffled(i_shuffle) = inbody.b()(shuffle_inds[i_shuffle]);
face_center_distance_shuffled(i_shuffle) =
face_center_distance(shuffle_inds[i_shuffle]);
face_moved_in_shuffled[i_shuffle] =
face_moved_in[shuffle_inds[i_shuffle]];
}
inbody = HPolyhedron(A_shuffled, b_shuffled);
face_center_distance = face_center_distance_shuffled;
face_moved_in = face_moved_in_shuffled;
int i = 0;
// Loop through remaining hyperplanes.
while (i < inbody.b().rows()) {