-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
mathematical_program_test.cc
4098 lines (3681 loc) · 164 KB
/
mathematical_program_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
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/solvers/mathematical_program.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <limits>
#include <map>
#include <memory>
#include <set>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "drake/common/drake_assert.h"
#include "drake/common/drake_copyable.h"
#include "drake/common/polynomial.h"
#include "drake/common/test_utilities/eigen_matrix_compare.h"
#include "drake/common/test_utilities/expect_no_throw.h"
#include "drake/common/test_utilities/expect_throws_message.h"
#include "drake/common/test_utilities/is_dynamic_castable.h"
#include "drake/common/test_utilities/symbolic_test_util.h"
#include "drake/math/matrix_util.h"
#include "drake/solvers/constraint.h"
#include "drake/solvers/decision_variable.h"
#include "drake/solvers/program_attribute.h"
#include "drake/solvers/snopt_solver.h"
#include "drake/solvers/solve.h"
#include "drake/solvers/test/generic_trivial_constraints.h"
#include "drake/solvers/test/generic_trivial_costs.h"
#include "drake/solvers/test/mathematical_program_test_util.h"
using Eigen::Dynamic;
using Eigen::Matrix;
using Eigen::Matrix2d;
using Eigen::Matrix3d;
using Eigen::Matrix4d;
using Eigen::MatrixXd;
using Eigen::Ref;
using Eigen::Vector2d;
using Eigen::Vector3d;
using Eigen::Vector4d;
using Eigen::VectorXd;
using drake::Vector1d;
using drake::solvers::internal::VecIn;
using drake::solvers::internal::VecOut;
using drake::symbolic::Expression;
using drake::symbolic::Formula;
using drake::symbolic::Variable;
using drake::symbolic::test::ExprEqual;
using drake::symbolic::test::PolyEqual;
using drake::symbolic::test::PolyNotEqual;
using std::all_of;
using std::cref;
using std::endl;
using std::is_permutation;
using std::is_same_v;
using std::make_shared;
using std::map;
using std::move;
using std::numeric_limits;
using std::pair;
using std::runtime_error;
using std::set;
using std::shared_ptr;
using std::static_pointer_cast;
using std::string;
using std::unique_ptr;
using std::vector;
namespace drake {
namespace solvers {
namespace test {
namespace {
constexpr double kNaN = std::numeric_limits<double>::quiet_NaN();
constexpr double kInf = std::numeric_limits<double>::infinity();
} // namespace
struct Movable {
Movable() = default;
Movable(Movable&&) = default;
Movable(Movable const&) = delete;
static size_t numInputs() { return 1; }
static size_t numOutputs() { return 1; }
template <typename ScalarType>
void eval(VecIn<ScalarType> const&, VecOut<ScalarType>*) const {}
};
struct Copyable {
Copyable() = default;
Copyable(Copyable&&) = delete;
Copyable(Copyable const&) = default;
static size_t numInputs() { return 1; }
static size_t numOutputs() { return 1; }
template <typename ScalarType>
void eval(VecIn<ScalarType> const&, VecOut<ScalarType>*) const {}
};
struct Unique {
Unique() = default;
Unique(Unique&&) = delete;
Unique(Unique const&) = delete;
static size_t numInputs() { return 1; }
static size_t numOutputs() { return 1; }
template <typename ScalarType>
void eval(VecIn<ScalarType> const&, VecOut<ScalarType>*) const {}
};
// Check the index, type and name etc of the newly added variables.
// This function only works if the only variables contained in @p prog are @p
// var.
template <typename ExpectedType, typename T>
void CheckAddedVariable(const MathematicalProgram& prog, const T& var, int rows,
int cols, const string& var_name, bool is_symmetric,
MathematicalProgram::VarType type_expected) {
static_assert(is_same_v<T, ExpectedType>, "Type not match");
EXPECT_EQ(var.rows(), rows);
EXPECT_EQ(var.cols(), cols);
// Checks the name of the newly added variables.
EXPECT_EQ(fmt::to_string(fmt_eigen(var)), var_name);
// Checks num_vars() function.
const int num_new_vars =
is_symmetric ? var.rows() * (var.rows() + 1) / 2 : var.size();
EXPECT_EQ(prog.num_vars(), num_new_vars);
// Checks if the newly added variable is symmetric.
EXPECT_EQ(math::IsSymmetric(var), is_symmetric);
// Checks the indices of the newly added variables.
if (is_symmetric) {
int var_count = 0;
for (int j = 0; j < var.cols(); ++j) {
for (int i = j; i < var.rows(); ++i) {
EXPECT_EQ(prog.FindDecisionVariableIndex(var(i, j)), var_count);
++var_count;
}
}
} else {
for (int i = 0; i < var.rows(); ++i) {
for (int j = 0; j < var.cols(); ++j) {
EXPECT_EQ(prog.FindDecisionVariableIndex(var(i, j)),
j * var.rows() + i);
}
}
}
// Checks the type of the newly added variables.
for (int i = 0; i < var.rows(); ++i) {
for (int j = 0; j < var.cols(); ++j) {
EXPECT_EQ(var(i, j).get_type(), type_expected);
}
}
}
template <typename Derived>
void CheckAddedIndeterminates(const MathematicalProgram& prog,
const Eigen::MatrixBase<Derived>& indeterminates,
const string& indeterminates_name) {
// Checks the name of the newly added indeterminates.
EXPECT_EQ(fmt::to_string(fmt_eigen(indeterminates)), indeterminates_name);
// Checks num_indeterminates() function.
const int num_new_indeterminates = indeterminates.size();
EXPECT_EQ(prog.num_indeterminates(), num_new_indeterminates);
// Checks the indices of the newly added indeterminates.
for (int i = 0; i < indeterminates.rows(); ++i) {
for (int j = 0; j < indeterminates.cols(); ++j) {
EXPECT_EQ(prog.FindIndeterminateIndex(indeterminates(i, j)),
j * indeterminates.rows() + i);
}
}
// Checks if the indeterminate is of type
// MathematicalProgram::VarType::CONTINUOUS variable (by default). This test
// should always be true (by defaults), but keep it to make sure everything
// works as it is supposed to be.
for (int i = 0; i < indeterminates.rows(); ++i) {
for (int j = 0; j < indeterminates.cols(); ++j) {
EXPECT_EQ(indeterminates(i, j).get_type(),
MathematicalProgram::VarType::CONTINUOUS);
}
}
}
GTEST_TEST(TestMathematicalProgram, TestConstructor) {
MathematicalProgram prog;
EXPECT_EQ(prog.initial_guess().rows(), 0);
EXPECT_EQ(prog.num_vars(), 0);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariables1) {
// Adds a dynamic-sized matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables(2, 3, "X");
CheckAddedVariable<MatrixXDecisionVariable>(
prog, X, 2, 3, "X(0,0) X(0,1) X(0,2)\nX(1,0) X(1,1) X(1,2)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariable2) {
// Adds a static-sized matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables<2, 3>("X");
CheckAddedVariable<MatrixDecisionVariable<2, 3>>(
prog, X, 2, 3, "X(0,0) X(0,1) X(0,2)\nX(1,0) X(1,1) X(1,2)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariable3) {
// Adds a dynamic-sized vector of continuous variables.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables(4, "x");
CheckAddedVariable<VectorXDecisionVariable>(
prog, x, 4, 1, "x(0)\nx(1)\nx(2)\nx(3)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariable4) {
// Adds a static-sized vector of continuous variables.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<4>("y");
CheckAddedVariable<VectorDecisionVariable<4>>(
prog, x, 4, 1, "y(0)\ny(1)\ny(2)\ny(3)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariable5) {
// Adds a static-sized matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables<2, 3>(2, 3, "Y");
CheckAddedVariable<MatrixDecisionVariable<2, 3>>(
prog, X, 2, 3, "Y(0,0) Y(0,1) Y(0,2)\nY(1,0) Y(1,1) Y(1,2)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariables6) {
// Adds a dynamic-sized matrix of continuous variables.
MathematicalProgram prog;
auto X =
prog.NewContinuousVariables<Eigen::Dynamic, Eigen::Dynamic>(2, 3, "Y");
CheckAddedVariable<MatrixXDecisionVariable>(
prog, X, 2, 3, "Y(0,0) Y(0,1) Y(0,2)\nY(1,0) Y(1,1) Y(1,2)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariables7) {
// Adds a dynamic-sized matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables<2, Eigen::Dynamic>(2, 3, "Y");
CheckAddedVariable<MatrixDecisionVariable<2, Eigen::Dynamic>>(
prog, X, 2, 3, "Y(0,0) Y(0,1) Y(0,2)\nY(1,0) Y(1,1) Y(1,2)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariables8) {
// Adds a dynamic-sized matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables<Eigen::Dynamic, 3>(2, 3, "Y");
CheckAddedVariable<MatrixDecisionVariable<Eigen::Dynamic, 3>>(
prog, X, 2, 3, "Y(0,0) Y(0,1) Y(0,2)\nY(1,0) Y(1,1) Y(1,2)", false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddContinuousVariables9) {
// Adds continuous variables with default variable name.
const std::string X_names = "X(0,0) X(0,1) X(0,2)\nX(1,0) X(1,1) X(1,2)";
MathematicalProgram prog1;
auto X1 = prog1.NewContinuousVariables(2, 3);
CheckAddedVariable<MatrixXDecisionVariable>(
prog1, X1, 2, 3, X_names, false,
MathematicalProgram::VarType::CONTINUOUS);
MathematicalProgram prog2;
auto X2 = prog2.NewContinuousVariables<Eigen::Dynamic, 3>(2, 3);
CheckAddedVariable<MatrixDecisionVariable<Eigen::Dynamic, 3>>(
prog2, X2, 2, 3, X_names, false,
MathematicalProgram::VarType::CONTINUOUS);
MathematicalProgram prog3;
auto X3 = prog3.NewContinuousVariables<2, 3>(2, 3);
CheckAddedVariable<MatrixDecisionVariable<2, 3>>(
prog3, X3, 2, 3, X_names, false,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddSymmetricVariable1) {
// Adds a static-sized symmetric matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewSymmetricContinuousVariables<3>("X");
CheckAddedVariable<MatrixDecisionVariable<3, 3>>(
prog, X, 3, 3,
"X(0,0) X(1,0) X(2,0)\nX(1,0) X(1,1) X(2,1)\nX(2,0) X(2,1) X(2,2)", true,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddSymmetricVariable2) {
// Adds a dynamic-sized symmetric matrix of continuous variables.
MathematicalProgram prog;
auto X = prog.NewSymmetricContinuousVariables(3, "X");
CheckAddedVariable<MatrixXDecisionVariable>(
prog, X, 3, 3,
"X(0,0) X(1,0) X(2,0)\nX(1,0) X(1,1) X(2,1)\nX(2,0) X(2,1) X(2,2)", true,
MathematicalProgram::VarType::CONTINUOUS);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable1) {
// Adds a dynamic-sized matrix of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables(2, 3, "B");
CheckAddedVariable<MatrixXDecisionVariable>(
prog, X, 2, 3, "B(0,0) B(0,1) B(0,2)\nB(1,0) B(1,1) B(1,2)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable2) {
// Adds a dynamic-sized matrix of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables<Eigen::Dynamic, Eigen::Dynamic>(2, 3, "B");
CheckAddedVariable<MatrixXDecisionVariable>(
prog, X, 2, 3, "B(0,0) B(0,1) B(0,2)\nB(1,0) B(1,1) B(1,2)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable3) {
// Adds dynamic-sized vector of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables(4, "B");
CheckAddedVariable<VectorXDecisionVariable>(
prog, X, 4, 1, "B(0)\nB(1)\nB(2)\nB(3)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable4) {
// Adds static-sized vector of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables<4>("B");
CheckAddedVariable<VectorDecisionVariable<4>>(
prog, X, 4, 1, "B(0)\nB(1)\nB(2)\nB(3)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable5) {
// Adds a static-sized matrix of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables<2, 3>("B");
CheckAddedVariable<MatrixDecisionVariable<2, 3>>(
prog, X, 2, 3, "B(0,0) B(0,1) B(0,2)\nB(1,0) B(1,1) B(1,2)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable6) {
// Adds a static-sized matrix of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables<2, 3>(2, 3, "B");
CheckAddedVariable<MatrixDecisionVariable<2, 3>>(
prog, X, 2, 3, "B(0,0) B(0,1) B(0,2)\nB(1,0) B(1,1) B(1,2)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable7) {
// Adds a dynamic-sized matrix of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables<2, Eigen::Dynamic>(2, 3, "B");
CheckAddedVariable<MatrixDecisionVariable<2, Eigen::Dynamic>>(
prog, X, 2, 3, "B(0,0) B(0,1) B(0,2)\nB(1,0) B(1,1) B(1,2)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddVariable, TestAddBinaryVariable8) {
// Adds a dynamic-sized matrix of binary variables.
MathematicalProgram prog;
auto X = prog.NewBinaryVariables<Eigen::Dynamic, 3>(2, 3, "B");
CheckAddedVariable<MatrixDecisionVariable<Eigen::Dynamic, 3>>(
prog, X, 2, 3, "B(0,0) B(0,1) B(0,2)\nB(1,0) B(1,1) B(1,2)", false,
MathematicalProgram::VarType::BINARY);
}
GTEST_TEST(TestAddDecisionVariables, AddDecisionVariables1) {
// Call AddVariable on an empty program.
MathematicalProgram prog;
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const Variable x2("x2", Variable::Type::BINARY);
prog.AddDecisionVariables(VectorDecisionVariable<3>(x0, x1, x2));
EXPECT_EQ(prog.num_vars(), 3);
EXPECT_EQ(prog.FindDecisionVariableIndex(x0), 0);
EXPECT_EQ(prog.FindDecisionVariableIndex(x1), 1);
EXPECT_EQ(prog.FindDecisionVariableIndex(x2), 2);
EXPECT_EQ(prog.initial_guess().rows(), 3);
EXPECT_EQ(prog.decision_variables().rows(), 3);
EXPECT_GT(
prog.required_capabilities().count(ProgramAttribute::kBinaryVariable), 0);
const auto decision_variable_index = prog.decision_variable_index();
{
const auto it = decision_variable_index.find(x0.get_id());
ASSERT_TRUE(it != decision_variable_index.end());
EXPECT_EQ(it->second, prog.FindDecisionVariableIndex(x0));
}
{
const auto it = decision_variable_index.find(x1.get_id());
ASSERT_TRUE(it != decision_variable_index.end());
EXPECT_EQ(it->second, prog.FindDecisionVariableIndex(x1));
}
{
const auto it = decision_variable_index.find(x2.get_id());
ASSERT_TRUE(it != decision_variable_index.end());
EXPECT_EQ(it->second, prog.FindDecisionVariableIndex(x2));
}
}
GTEST_TEST(TestAddDecisionVariables, AddVariable2) {
// Call AddDecisionVariables on a program that has some existing variables.
MathematicalProgram prog;
auto y = prog.NewContinuousVariables<3>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const Variable x2("x2", Variable::Type::BINARY);
prog.AddDecisionVariables(VectorDecisionVariable<3>(x0, x1, x2));
EXPECT_EQ(prog.num_vars(), 6);
EXPECT_EQ(prog.num_indeterminates(), 0);
EXPECT_EQ(prog.FindDecisionVariableIndex(x0), 3);
EXPECT_EQ(prog.FindDecisionVariableIndex(x1), 4);
EXPECT_EQ(prog.FindDecisionVariableIndex(x2), 5);
EXPECT_EQ(prog.initial_guess().rows(), 6);
}
GTEST_TEST(TestAddDecisionVariables, AddVariable3) {
// Call AddDecisionVariables on a program that has some existing variables.
// and the new variable overlap with the old variables.
MathematicalProgram prog;
auto y = prog.NewContinuousVariables<3>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
prog.AddDecisionVariables(VectorDecisionVariable<3>(x0, y(1), x1));
EXPECT_EQ(prog.num_vars(), 5);
EXPECT_EQ(prog.num_indeterminates(), 0);
EXPECT_EQ(prog.FindDecisionVariableIndex(x0), 3);
EXPECT_EQ(prog.FindDecisionVariableIndex(x1), 4);
EXPECT_EQ(prog.initial_guess().rows(), 5);
}
GTEST_TEST(TestAddDecisionVariables, AddVariableError) {
// Test the error inputs.
MathematicalProgram prog;
auto y = prog.NewContinuousVariables<3>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
// The newly added variables contain a dummy variable.
Variable dummy;
EXPECT_TRUE(dummy.is_dummy());
EXPECT_THROW(
prog.AddDecisionVariables(VectorDecisionVariable<3>(x0, x1, dummy)),
std::runtime_error);
auto z = prog.NewIndeterminates<2>("z");
// Call AddDecisionVariables on a program that has some indeterminates, and
// the new
// variables intersects with the indeterminates.
EXPECT_THROW(prog.AddDecisionVariables(VectorDecisionVariable<2>(x0, z(0))),
std::runtime_error);
// Call AddDecisionVariables with unsupported variable type.
for (symbolic::Variable::Type unsupported_type :
{symbolic::Variable::Type::BOOLEAN,
symbolic::Variable::Type::RANDOM_UNIFORM,
symbolic::Variable::Type::RANDOM_GAUSSIAN,
symbolic::Variable::Type::RANDOM_EXPONENTIAL}) {
const symbolic::Variable unsupported_var("b", unsupported_type);
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddDecisionVariables(VectorDecisionVariable<1>(unsupported_var)),
"MathematicalProgram does not support .* variables.");
}
}
GTEST_TEST(TestAddDecisionVariables, TestMatrixInput) {
// AddDecisionVariables with a matrix of variables instead of a vector.
Eigen::Matrix<symbolic::Variable, 2, 3> vars;
for (int i = 0; i < vars.rows(); ++i) {
for (int j = 0; j < vars.cols(); ++j) {
vars(i, j) = symbolic::Variable(fmt::format("x({},{})", i, j));
}
}
MathematicalProgram prog;
prog.NewContinuousVariables<1>();
const int num_existing_decision_vars = prog.num_vars();
prog.AddDecisionVariables(vars);
EXPECT_EQ(prog.num_vars(), 6 + num_existing_decision_vars);
EXPECT_EQ(prog.GetInitialGuess(vars).rows(), 2);
EXPECT_EQ(prog.GetInitialGuess(vars).cols(), 3);
EXPECT_TRUE(prog.GetInitialGuess(vars).array().isNaN().all());
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
// Make sure that the variable has been registered in prog.
EXPECT_NO_THROW(unused(prog.FindDecisionVariableIndex(vars(i, j))));
}
}
}
GTEST_TEST(NewIndeterminates, DynamicSizeMatrix) {
// Adds a dynamic-sized matrix of Indeterminates.
MathematicalProgram prog;
auto X = prog.NewIndeterminates(2, 3, "X");
static_assert(is_same_v<decltype(X), MatrixXIndeterminate>,
"should be a dynamic sized matrix");
EXPECT_EQ(X.rows(), 2);
EXPECT_EQ(X.cols(), 3);
CheckAddedIndeterminates(prog, X,
"X(0,0) X(0,1) X(0,2)\nX(1,0) X(1,1) X(1,2)");
}
GTEST_TEST(NewIndeterminates, StaticSizeMatrix) {
// Adds a static-sized matrix of Indeterminates.
MathematicalProgram prog;
auto X = prog.NewIndeterminates<2, 3>("X");
static_assert(is_same_v<decltype(X), MatrixIndeterminate<2, 3>>,
"should be a static sized matrix");
CheckAddedIndeterminates(prog, X,
"X(0,0) X(0,1) X(0,2)\nX(1,0) X(1,1) X(1,2)");
}
GTEST_TEST(NewIndeterminates, DynamicSizeVector) {
// Adds a dynamic-sized vector of Indeterminates.
MathematicalProgram prog;
auto x = prog.NewIndeterminates(4, "x");
static_assert(is_same_v<decltype(x), VectorXIndeterminate>,
"Should be a VectorXDecisionVariable object.");
EXPECT_EQ(x.rows(), 4);
CheckAddedIndeterminates(prog, x, "x(0)\nx(1)\nx(2)\nx(3)");
}
GTEST_TEST(NewIndeterminates, StaticSizeVector) {
// Adds a static-sized vector of Indeterminate variables.
MathematicalProgram prog;
auto x = prog.NewIndeterminates<4>("x");
static_assert(is_same_v<decltype(x), VectorIndeterminate<4>>,
"Should be a VectorXDecisionVariable object.");
CheckAddedIndeterminates(prog, x, "x(0)\nx(1)\nx(2)\nx(3)");
}
GTEST_TEST(TestAddIndeterminate, AddIndeterminate1) {
// Call AddIndeterminate on an empty program.
MathematicalProgram prog;
const Variable x("x", Variable::Type::CONTINUOUS);
int var_index = prog.AddIndeterminate(x);
EXPECT_EQ(prog.indeterminates().rows(), 1);
EXPECT_EQ(var_index, 0);
EXPECT_TRUE(prog.indeterminates()(0).equal_to(x));
EXPECT_EQ(prog.FindIndeterminateIndex(x), 0);
const auto it = prog.indeterminates_index().find(x.get_id());
EXPECT_TRUE(it != prog.indeterminates_index().end());
EXPECT_EQ(it->second, prog.FindIndeterminateIndex(x));
}
GTEST_TEST(TestAddIndeterminate, AddIndeterminate2) {
// Call AddIndeterminate on a program with some indeterminates
MathematicalProgram prog;
auto y = prog.NewIndeterminates<2>("y");
const Variable x("x", Variable::Type::CONTINUOUS);
int var_index = prog.AddIndeterminate(x);
EXPECT_EQ(prog.indeterminates().rows(), 3);
EXPECT_EQ(var_index, 2);
VectorIndeterminate<3> indeterminates_expected;
indeterminates_expected << y(0), y(1), x;
for (int i = 0; i < 3; ++i) {
EXPECT_TRUE(prog.indeterminates()(i).equal_to(indeterminates_expected(i)));
EXPECT_EQ(prog.FindIndeterminateIndex(indeterminates_expected(i)), i);
}
}
GTEST_TEST(TestAddIndeterminate, AddIndeterminate3) {
// prog already contains some indeterminates, and we call AddIndeterminate on
// an old indeterminate.
MathematicalProgram prog;
auto y = prog.NewIndeterminates<3>();
auto var_index = prog.AddIndeterminate(y(1));
EXPECT_EQ(var_index, prog.FindIndeterminateIndex(y(1)));
EXPECT_EQ(prog.indeterminates().size(), 3);
EXPECT_EQ(prog.indeterminates_index().size(), 3);
}
GTEST_TEST(TestAddIndeterminate, AddIndeterminateError) {
// Call with erroneous inputs.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>("x");
auto y = prog.NewIndeterminates<2>("y");
const Variable z("z", Variable::Type::BINARY);
// Call AddIndeterminate with an input that intersects with old decision
// variables.
DRAKE_EXPECT_THROWS_MESSAGE(prog.AddIndeterminate(x(0)),
".*is a decision variable.*");
// Call AddIndeterminate with an input of type BINARY.
DRAKE_EXPECT_THROWS_MESSAGE(prog.AddIndeterminate(z),
".*should be of type CONTINUOUS.*");
// Call AddIndeterminate with a dummy variable.
Variable dummy;
DRAKE_EXPECT_THROWS_MESSAGE(prog.AddIndeterminate(dummy),
".*should not be a dummy variable.*");
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVec1) {
// Call AddIndeterminates on an empty program.
MathematicalProgram prog;
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const Variable x2("x2", Variable::Type::CONTINUOUS);
prog.AddIndeterminates(VectorIndeterminate<3>(x0, x1, x2));
const VectorIndeterminate<3> indeterminates_expected(x0, x1, x2);
EXPECT_EQ(prog.indeterminates().rows(), 3);
const auto indeterminates_index = prog.indeterminates_index();
for (int i = 0; i < 3; ++i) {
EXPECT_TRUE(prog.indeterminates()(i).equal_to(indeterminates_expected(i)));
EXPECT_EQ(prog.FindIndeterminateIndex(indeterminates_expected(i)), i);
const auto it =
indeterminates_index.find(indeterminates_expected(i).get_id());
ASSERT_TRUE(it != indeterminates_index.end());
EXPECT_EQ(it->second,
prog.FindIndeterminateIndex(indeterminates_expected(i)));
}
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVec2) {
// Call AddIndeterminates on a program with some indeterminates.
MathematicalProgram prog;
auto y = prog.NewIndeterminates<2>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const Variable x2("x2", Variable::Type::CONTINUOUS);
prog.AddIndeterminates(VectorIndeterminate<3>(x0, x1, x2));
VectorIndeterminate<5> indeterminates_expected;
indeterminates_expected << y(0), y(1), x0, x1, x2;
EXPECT_EQ(prog.indeterminates().rows(), 5);
for (int i = 0; i < 5; ++i) {
EXPECT_TRUE(prog.indeterminates()(i).equal_to(indeterminates_expected(i)));
EXPECT_EQ(prog.FindIndeterminateIndex(indeterminates_expected(i)), i);
}
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVec3) {
// The program already constains some indeterminates, call AddIndeterminates()
// with some of the old indeterminates.
MathematicalProgram prog;
auto y = prog.NewIndeterminates<3>();
const symbolic::Variable x0("x0", Variable::Type::CONTINUOUS);
prog.AddIndeterminates(Vector2<symbolic::Variable>(y(1), x0));
EXPECT_EQ(prog.indeterminates().size(), 4);
EXPECT_EQ(prog.indeterminates_index().size(), 4);
EXPECT_EQ(prog.FindIndeterminateIndex(x0), 3);
EXPECT_EQ(prog.FindIndeterminateIndex(y(1)), 1);
// AddIndeterminates with duplicated variables.
const symbolic::Variable x1("x1", Variable::Type::CONTINUOUS);
prog.AddIndeterminates(Vector3<symbolic::Variable>(x1, x0, x1));
EXPECT_EQ(prog.indeterminates().size(), 5);
EXPECT_EQ(prog.indeterminates_index().size(), 5);
EXPECT_EQ(prog.FindIndeterminateIndex(x1), 4);
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVecError) {
// Call with erroneous inputs.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>("x");
auto y = prog.NewIndeterminates<2>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::BINARY);
// Call AddIndeterminates with an input that intersects with old decision
// variables.
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddIndeterminates(VectorIndeterminate<2>(x(0), x0)),
".*is a decision variable.*");
// Call AddIndeterminates with an input of type BINARY.
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddIndeterminates(VectorIndeterminate<2>(x1, x0)),
".*should be of type CONTINUOUS.*");
// Call AddIndeterminates with a dummy variable.
Variable dummy;
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddIndeterminates(VectorIndeterminate<2>(dummy, x0)),
".*should not be a dummy variable.*");
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVars1) {
// Call AddIndeterminates on an empty program.
MathematicalProgram prog;
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const Variable x2("x2", Variable::Type::CONTINUOUS);
prog.AddIndeterminates(symbolic::Variables({x0, x1, x2}));
const VectorIndeterminate<3> indeterminates_expected(x0, x1, x2);
EXPECT_EQ(prog.indeterminates().rows(), 3);
const auto indeterminates_index = prog.indeterminates_index();
for (int i = 0; i < 3; ++i) {
// the indeterminate is in the program, but in arbitrary place so we don't
// test its index.
const auto it =
indeterminates_index.find(indeterminates_expected(i).get_id());
ASSERT_TRUE(it != indeterminates_index.end());
EXPECT_EQ(it->second,
prog.FindIndeterminateIndex(indeterminates_expected(i)));
}
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVars2) {
// Call AddIndeterminates on a program with some indeterminates.
MathematicalProgram prog;
auto y = prog.NewIndeterminates<2>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const Variable x2("x2", Variable::Type::CONTINUOUS);
prog.AddIndeterminates(symbolic::Variables({x0, x1, x2}));
VectorIndeterminate<5> indeterminates_expected;
indeterminates_expected << y(0), y(1), x0, x1, x2;
EXPECT_EQ(prog.indeterminates().rows(), 5);
const auto indeterminates_index = prog.indeterminates_index();
for (int i = 0; i < 5; ++i) {
if (i < 3) {
// The variables already in the program should be in order.
EXPECT_TRUE(
prog.indeterminates()(i).equal_to(indeterminates_expected(i)));
EXPECT_EQ(prog.FindIndeterminateIndex(indeterminates_expected(i)), i);
} else {
// The remaining variables were added using an unordered set so we can't
// expect an order.
const auto it =
indeterminates_index.find(indeterminates_expected(i).get_id());
ASSERT_TRUE(it != indeterminates_index.end());
EXPECT_EQ(it->second,
prog.FindIndeterminateIndex(indeterminates_expected(i)));
}
}
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVars3) {
// Call AddIndeterminates that overlaps with the old indeterminates.
MathematicalProgram prog;
auto y = prog.NewIndeterminates<3>();
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::CONTINUOUS);
const symbolic::Variables var_set{{x0, x1, y(1)}};
prog.AddIndeterminates(var_set);
EXPECT_EQ(prog.num_indeterminates(), 5);
EXPECT_EQ(prog.indeterminates().size(), 5);
EXPECT_EQ(prog.indeterminates_index().size(), 5);
EXPECT_GT(prog.FindIndeterminateIndex(x0), 2);
EXPECT_GT(prog.FindIndeterminateIndex(x1), 2);
}
GTEST_TEST(TestAddIndeterminates, AddIndeterminatesVarsError) {
// Call with erroneous inputs.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>("x");
auto y = prog.NewIndeterminates<2>("y");
const Variable x0("x0", Variable::Type::CONTINUOUS);
const Variable x1("x1", Variable::Type::BINARY);
// Call AddIndeterminates with an input that intersects with old decision
// variables.
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddIndeterminates(symbolic::Variables({x0, x(0)})),
".*is a decision variable.*");
// Call AddIndeterminates with an input of type BINARY.
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddIndeterminates(symbolic::Variables({x0, x1})),
".*should be of type CONTINUOUS.*");
// Call AddIndeterminates with a dummy variable.
Variable dummy;
DRAKE_EXPECT_THROWS_MESSAGE(
prog.AddIndeterminates(symbolic::Variables({x0, dummy})),
".*should not be a dummy variable.*");
}
GTEST_TEST(TestAddIndeterminates, MatrixInput) {
Eigen::Matrix<symbolic::Variable, 2, 3, Eigen::RowMajor> vars;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
vars(i, j) = symbolic::Variable(fmt::format("x({},{})", i, j));
}
}
MathematicalProgram prog;
prog.NewIndeterminates<2>();
const int num_existing_indeterminates = prog.num_indeterminates();
prog.AddIndeterminates(vars);
EXPECT_EQ(prog.num_indeterminates(), num_existing_indeterminates + 6);
EXPECT_EQ(prog.num_vars(), 0);
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 3; ++j) {
EXPECT_NO_THROW(unused(prog.FindIndeterminateIndex(vars(i, j))));
}
}
}
namespace {
// Overloads to permit `ExpectBadVar` call `AddItem` for both `Cost` and
// `Constraint`.
void AddItem(MathematicalProgram* prog, const Binding<Constraint>& binding) {
prog->AddConstraint(binding);
}
void AddItem(MathematicalProgram* prog, const Binding<Cost>& binding) {
prog->AddCost(binding);
}
// Expect that adding a given constraint with bad variables (those that have
// not been added to MathematicalProgram) will throw an exception.
template <typename C, typename... Args>
void ExpectBadVar(MathematicalProgram* prog, int num_var, Args&&... args) {
using internal::CreateBinding;
auto c = make_shared<C>(std::forward<Args>(args)...);
VectorXDecisionVariable x(num_var);
for (int i = 0; i < num_var; ++i) x(i) = Variable("bad" + std::to_string(i));
// Use minimal call site (directly on adding Binding<C>).
// TODO(eric.cousineau): Check if there is a way to parse the error text to
// ensure that we are capturing the correct error.
DRAKE_EXPECT_THROWS_MESSAGE(AddItem(prog, CreateBinding(c, x)),
".*is not a decision variable.*");
}
} // namespace
GTEST_TEST(TestMathematicalProgram, TestMakePolynomial) {
MathematicalProgram prog;
const auto x = prog.NewIndeterminates<2>("x");
const auto a = prog.NewContinuousVariables<2>("a");
// A decision variable that does not belong
// to this mathematical program.
const symbolic::Variable b{"b"};
// e = a₀x₀ + (a₁ + b)x₀x₁ + (a₁b).
const Expression e{a(0) * x(0) + (a(1) + b) * x(0) * x(1) + (a(1) * b)};
const symbolic::Polynomial p{prog.MakePolynomial(e)};
// We check the constructed polynomial has the following internal mapping.
// x₀ ↦ a₀
// x₀x₁ ↦ (a₁ + b)
// 1 ↦ a₁b
const auto& coeff_map = p.monomial_to_coefficient_map();
EXPECT_EQ(coeff_map.size(), 3);
const symbolic::Monomial x0{x(0)};
const symbolic::Monomial x0x1{{{x(0), 1}, {x(1), 1}}};
const symbolic::Monomial one;
EXPECT_PRED2(ExprEqual, coeff_map.at(x0), a(0));
EXPECT_PRED2(ExprEqual, coeff_map.at(x0x1), a(1) + b);
EXPECT_PRED2(ExprEqual, coeff_map.at(one), a(1) * b);
}
GTEST_TEST(TestMathematicalProgram, TestBadBindingVariable) {
// Attempt to add a binding that does not have a valid decision variable.
MathematicalProgram prog;
const int num_var = 3;
Eigen::Matrix3d A;
A.setIdentity();
Eigen::Vector3d f, lb, ub;
f.setConstant(2);
lb.setConstant(0);
ub.setConstant(1);
Eigen::Matrix3d twiceA = 2 * A;
vector<Eigen::Ref<const MatrixXd>> F{A, twiceA};
shared_ptr<EvaluatorBase> func = MakeFunctionEvaluator(Movable());
// Test each constraint type.
ExpectBadVar<LinearConstraint>(&prog, num_var, A, lb, ub);
ExpectBadVar<LinearEqualityConstraint>(&prog, num_var, A, lb);
ExpectBadVar<BoundingBoxConstraint>(&prog, num_var, lb, ub);
ExpectBadVar<LorentzConeConstraint>(&prog, num_var, A, f);
ExpectBadVar<RotatedLorentzConeConstraint>(&prog, num_var, A, f);
ExpectBadVar<PositiveSemidefiniteConstraint>(&prog, num_var * num_var,
num_var);
ExpectBadVar<LinearMatrixInequalityConstraint>(&prog, F.size() - 1, F);
ExpectBadVar<ExponentialConeConstraint>(
&prog, num_var, Eigen::MatrixXd::Ones(3, num_var).sparseView(),
Eigen::Vector3d::Zero());
ExpectBadVar<LinearComplementarityConstraint>(&prog, num_var, A, f);
// Use this as a test for nonlinear constraints.
ExpectBadVar<EvaluatorConstraint<>>(&prog, 1, func, lb.head(1), ub.head(1));
// Test each cost type.
ExpectBadVar<LinearCost>(&prog, num_var, f);
ExpectBadVar<QuadraticCost>(&prog, num_var, A, f);
ExpectBadVar<EvaluatorCost<>>(&prog, 1, func);
}
GTEST_TEST(TestMathematicalProgram, TestAddFunction) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<1>();
Movable movable;
prog.AddCost(std::move(movable), x);
prog.AddCost(Movable(), x);
Copyable copyable;
prog.AddCost(copyable, x);
Unique unique;
prog.AddCost(cref(unique), x);
prog.AddCost(make_shared<Unique>(), x);
prog.AddCost(unique_ptr<Unique>(new Unique), x);
}
GTEST_TEST(TestMathematicalProgram, BoundingBoxTest2) {
// Test the scalar version of the bounding box constraint methods.
MathematicalProgram prog;
auto x1 = prog.NewContinuousVariables<2, 2>("x1");
MatrixXDecisionVariable x2(2, 2);
x2 = x1;
VectorDecisionVariable<4> x3;
x3 << x1.col(0), x1.col(1);
VectorXDecisionVariable x4(4);
x4 = x3;
// Six different ways to construct an equivalent constraint.
// 1. Imposes constraint on a static-sized matrix of decision variables.
// 2. Imposes constraint on a list of vectors of decision variables.
// 3. Imposes constraint on a dynamic-sized matrix of decision variables.
// 4. Imposes constraint on a static-sized vector of decision variables.
// 5. Imposes constraint on a dynamic-sized vector of decision variables.
// 6. Imposes constraint using a vector of lower/upper bound, as compared
// to the previous three cases which use a scalar lower/upper bound.
auto constraint1 = prog.AddBoundingBoxConstraint(0, 1, x1).evaluator();
auto constraint2 =
prog.AddBoundingBoxConstraint(0, 1, {x1.col(0), x1.col(1)}).evaluator();
auto constraint3 = prog.AddBoundingBoxConstraint(0, 1, x2).evaluator();
auto constraint4 = prog.AddBoundingBoxConstraint(0, 1, x3).evaluator();
auto constraint5 = prog.AddBoundingBoxConstraint(0, 1, x4).evaluator();
auto constraint6 = prog.AddBoundingBoxConstraint(Eigen::Vector4d::Zero(),
Eigen::Vector4d::Ones(), x3)
.evaluator();
// Checks that the bound variables are correct.
for (const auto& binding : prog.bounding_box_constraints()) {
EXPECT_EQ(binding.GetNumElements(), 4u);
VectorDecisionVariable<4> x_expected;
x_expected << x1(0, 0), x1(1, 0), x1(0, 1), x1(1, 1);
for (int i = 0; i < 4; ++i) {
EXPECT_EQ(binding.variables()(i), x_expected(i));
}
}
EXPECT_TRUE(
CompareMatrices(constraint1->lower_bound(), constraint2->lower_bound()));
EXPECT_TRUE(
CompareMatrices(constraint2->lower_bound(), constraint3->lower_bound()));
EXPECT_TRUE(
CompareMatrices(constraint3->lower_bound(), constraint4->lower_bound()));
EXPECT_TRUE(
CompareMatrices(constraint4->lower_bound(), constraint5->lower_bound()));
EXPECT_TRUE(
CompareMatrices(constraint5->lower_bound(), constraint6->lower_bound()));
EXPECT_TRUE(
CompareMatrices(constraint1->upper_bound(), constraint2->upper_bound()));
EXPECT_TRUE(
CompareMatrices(constraint2->upper_bound(), constraint3->upper_bound()));
EXPECT_TRUE(
CompareMatrices(constraint3->upper_bound(), constraint4->upper_bound()));
EXPECT_TRUE(
CompareMatrices(constraint4->upper_bound(), constraint5->upper_bound()));
EXPECT_TRUE(
CompareMatrices(constraint5->upper_bound(), constraint6->upper_bound()));
}
GTEST_TEST(TestMathematicalProgram, BoundingBoxTest3) {
// The bounds and variables are matrices.
MathematicalProgram prog;
auto X = prog.NewContinuousVariables(3, 2, "X");
Eigen::MatrixXd X_lo(3, 2);
X_lo << 1, 2, 3, 4, 5, 6;
// Use a row-major matrix to make sure that our code works for different types
// of matrix.
Eigen::Matrix<double, 3, 2, Eigen::RowMajor> X_up =
(X_lo.array() + 1).matrix();
auto cnstr = prog.AddBoundingBoxConstraint(X_lo, X_up, X);
EXPECT_EQ(cnstr.evaluator()->num_constraints(), 6);
std::unordered_map<symbolic::Variable, std::pair<double, double>> X_bounds;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
X_bounds.emplace(X(i, j), std::make_pair(X_lo(i, j), X_up(i, j)));
}
}
for (int i = 0; i < 6; ++i) {
EXPECT_EQ(cnstr.evaluator()->lower_bound()(i),
X_bounds.at(cnstr.variables()(i)).first);
EXPECT_EQ(cnstr.evaluator()->upper_bound()(i),
X_bounds.at(cnstr.variables()(i)).second);
}
// Now add constraint on X.topRows<2>(). It doesn't occupy contiguous memory.