-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
get_program_type_test.cc
234 lines (218 loc) · 9.36 KB
/
get_program_type_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
#include "drake/solvers/get_program_type.h"
#include <gtest/gtest.h>
namespace drake {
namespace solvers {
// We don't exhaustively test for "false positives" in these tests for
// GetProgramType(). The argument for not doing so is based on the idea that
// mathematical programs are uniquely characterized as a single type. It is
// impossible for a program that would classify as one type to ever provide a
// misclassificaiton as a different type. We rely on the sampling of
// mathematical programs to provide sufficient coverage of meaningful
// mathematical programs to render testing false positives unnecessary (as those
// candidates for false positives test positively elsewhere).
GTEST_TEST(GetProgramTypeTest, LP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddLinearConstraint(x[0] + x[1] == 1);
EXPECT_EQ(GetProgramType(prog), ProgramType::kLP);
prog.AddLinearCost(x[0] + x[1]);
EXPECT_EQ(GetProgramType(prog), ProgramType::kLP);
prog.AddLinearConstraint(x[0] >= 0);
EXPECT_EQ(GetProgramType(prog), ProgramType::kLP);
prog.AddQuadraticCost(x[0] * x[0]);
EXPECT_NE(GetProgramType(prog), ProgramType::kLP);
}
GTEST_TEST(GetProgramTypeTest, QP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddQuadraticCost(x[0] * x[0] + x[1], true);
EXPECT_EQ(GetProgramType(prog), ProgramType::kQP);
prog.AddLinearConstraint(x[0] + x[1] == 1);
EXPECT_EQ(GetProgramType(prog), ProgramType::kQP);
prog.AddLinearCost(x[0] + x[1]);
EXPECT_EQ(GetProgramType(prog), ProgramType::kQP);
// Add a non-convex quadratic cost.
auto nonconvex_cost =
prog.AddQuadraticCost(-x[1] * x[1], false /* non-convex */);
EXPECT_NE(GetProgramType(prog), ProgramType::kQP);
prog.RemoveCost(nonconvex_cost);
EXPECT_EQ(GetProgramType(prog), ProgramType::kQP);
prog.AddLorentzConeConstraint(
Vector3<symbolic::Expression>(x[0] + 2, 2 * x[0] + 1, x[0] + x[1]));
EXPECT_NE(GetProgramType(prog), ProgramType::kQP);
}
GTEST_TEST(GetProgramTypeTest, SOCP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<4>();
prog.AddLorentzConeConstraint(x.cast<symbolic::Expression>());
EXPECT_EQ(GetProgramType(prog), ProgramType::kSOCP);
prog.AddRotatedLorentzConeConstraint(x.cast<symbolic::Expression>());
EXPECT_EQ(GetProgramType(prog), ProgramType::kSOCP);
prog.AddLinearConstraint(x[0] >= 1);
EXPECT_EQ(GetProgramType(prog), ProgramType::kSOCP);
auto quadratic_cost = prog.AddQuadraticCost(x[0] * x[0]);
EXPECT_NE(GetProgramType(prog), ProgramType::kSOCP);
prog.RemoveCost(quadratic_cost);
EXPECT_EQ(GetProgramType(prog), ProgramType::kSOCP);
prog.AddPositiveSemidefiniteConstraint(x[0] * Eigen::Matrix2d::Identity() +
x[1] * Eigen::Matrix2d::Ones());
EXPECT_NE(GetProgramType(prog), ProgramType::kSOCP);
}
GTEST_TEST(GetProgramTypeTest, SDP) {
MathematicalProgram prog;
auto X = prog.NewSymmetricContinuousVariables<3>();
prog.AddPositiveSemidefiniteConstraint(X);
prog.AddLinearCost(X(0, 0) + X(1, 1));
EXPECT_EQ(GetProgramType(prog), ProgramType::kSDP);
prog.AddLorentzConeConstraint(
Vector3<symbolic::Expression>(X(0, 0) + 1, X(1, 1), X(1, 2) + 2));
EXPECT_EQ(GetProgramType(prog), ProgramType::kSDP);
auto x = prog.NewContinuousVariables<2>();
prog.AddLinearMatrixInequalityConstraint(
{Eigen::Matrix2d::Identity(), Eigen::Matrix2d::Ones(),
2 * Eigen::Matrix2d::Ones()},
x);
EXPECT_EQ(GetProgramType(prog), ProgramType::kSDP);
auto quadratic_cost = prog.AddQuadraticCost(x[0] * x[0]);
EXPECT_NE(GetProgramType(prog), ProgramType::kSDP);
}
GTEST_TEST(GetProgramTypeTest, GP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<3>();
Eigen::SparseMatrix<double> A(3, 3);
A.setIdentity();
prog.AddExponentialConeConstraint(A, Eigen::Vector3d(0, 1, 2), x);
EXPECT_EQ(GetProgramType(prog), ProgramType::kGP);
// Adding a Lorentz cone constraint, now this program cannot be modelled as
// GP, but a CGP.
prog.AddLorentzConeConstraint(x.cast<symbolic::Expression>());
EXPECT_NE(GetProgramType(prog), ProgramType::kGP);
EXPECT_EQ(GetProgramType(prog), ProgramType::kCGP);
}
GTEST_TEST(GetProgramTypeTest, NLP) {
{
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
auto quadratic_cost =
prog.AddQuadraticCost(x(0) * x(0) - x(1) * x(1), false);
EXPECT_EQ(GetProgramType(prog), ProgramType::kNLP);
}
{
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddPolynomialCost(x(0) * x(0) * x(1) + 2 * x(1));
EXPECT_EQ(GetProgramType(prog), ProgramType::kNLP);
}
{
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddConstraint(
std::make_shared<QuadraticConstraint>(Eigen::Matrix2d::Identity(),
Eigen::Vector2d(1, 0), 0, 1),
x);
EXPECT_EQ(GetProgramType(prog), ProgramType::kNLP);
auto b = prog.NewBinaryVariables<2>();
EXPECT_NE(GetProgramType(prog), ProgramType::kNLP);
}
{
// A problem with linear complementarity constraint and a cost.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddConstraint(std::make_shared<LinearComplementarityConstraint>(
Eigen::Matrix2d::Identity(), Eigen::Vector2d(1, 1)),
x);
prog.AddLinearCost(x(0) + x(1));
EXPECT_EQ(GetProgramType(prog), ProgramType::kNLP);
}
{
// A problem with linear complementarity constraint and a lorentz cone
// constraint.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<3>();
prog.AddConstraint(
std::make_shared<LinearComplementarityConstraint>(
Eigen::Matrix3d::Identity(), Eigen::Vector3d(1, 1, 0)),
x);
prog.AddLorentzConeConstraint(x);
EXPECT_EQ(GetProgramType(prog), ProgramType::kNLP);
}
}
GTEST_TEST(GetProgramTypeTest, LCP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
prog.AddLinearComplementarityConstraint(Eigen::Matrix2d::Identity(),
Eigen::Vector2d(1, 2), x);
EXPECT_EQ(GetProgramType(prog), ProgramType::kLCP);
// LCP doesn't accept linear constraint.
prog.AddLinearConstraint(x[0] + x[1] >= 1);
EXPECT_EQ(GetProgramType(prog), ProgramType::kNLP);
}
GTEST_TEST(GetProgramTypeTest, MILP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
auto b = prog.NewBinaryVariables<2>();
prog.AddLinearConstraint(x[0] + x[1] + b[1] + b[0] == 3);
prog.AddLinearCost(x[0] + x[1]);
EXPECT_EQ(GetProgramType(prog), ProgramType::kMILP);
prog.AddQuadraticCost(x[0] * x[0]);
EXPECT_NE(GetProgramType(prog), ProgramType::kMILP);
}
GTEST_TEST(GetProgramTypeTest, MIQP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
auto b = prog.NewBinaryVariables<2>();
prog.AddLinearConstraint(x[0] + x[1] + b[1] + b[0] == 3);
prog.AddLinearCost(x[0] + x[1]);
prog.AddQuadraticCost(x[0] * x[0], true);
EXPECT_EQ(GetProgramType(prog), ProgramType::kMIQP);
// Add a non-convex quadratic cost.
prog.AddQuadraticCost(-x[1] * x[1], false /* non-convex */);
EXPECT_NE(GetProgramType(prog), ProgramType::kMIQP);
}
GTEST_TEST(GetProgramTypeTest, MISOCP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
auto b = prog.NewBinaryVariables<2>();
prog.AddLinearConstraint(x[0] + x[1] + b[1] + b[0] == 3);
prog.AddLorentzConeConstraint(x.cast<symbolic::Expression>());
prog.AddLinearCost(x[0] + x[1]);
EXPECT_EQ(GetProgramType(prog), ProgramType::kMISOCP);
prog.AddPositiveSemidefiniteConstraint(x[0] * Eigen::Matrix2d::Identity() +
b[0] * Eigen::Matrix2d::Ones());
EXPECT_NE(GetProgramType(prog), ProgramType::kMISOCP);
}
GTEST_TEST(GetProgramTypeTest, MISDP) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<2>();
auto b = prog.NewBinaryVariables<2>();
prog.AddLinearConstraint(x[0] + x[1] + b[1] + b[0] == 3);
prog.AddLorentzConeConstraint(x.cast<symbolic::Expression>());
prog.AddLinearCost(x[0] + x[1]);
auto X = prog.NewSymmetricContinuousVariables<3>();
prog.AddPositiveSemidefiniteConstraint(X);
EXPECT_EQ(GetProgramType(prog), ProgramType::kMISDP);
prog.AddQuadraticCost(x[0] * x[0], true);
EXPECT_NE(GetProgramType(prog), ProgramType::kMISDP);
}
GTEST_TEST(GetProgramTypeTest, QuadraticCostConicConstraint) {
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<4>();
prog.AddLorentzConeConstraint(x.cast<symbolic::Expression>());
prog.AddRotatedLorentzConeConstraint(x.cast<symbolic::Expression>());
auto quadratic_cost = prog.AddQuadraticCost(x(0) * x(0) + x(3) * x(3), true);
EXPECT_EQ(GetProgramType(prog), ProgramType::kQuadraticCostConicConstraint);
prog.RemoveCost(quadratic_cost);
EXPECT_NE(GetProgramType(prog), ProgramType::kQuadraticCostConicConstraint);
}
GTEST_TEST(GetProgramTypeTest, Unknown) {
// Nonlinear constraint with binary variables.
MathematicalProgram prog;
auto x = prog.NewContinuousVariables<3>();
prog.AddConstraint(x[0] * x[0] * x[1] + 3 * x[1] * x[0] * x[2] == 1);
auto b = prog.NewBinaryVariables<1>();
prog.AddLinearComplementarityConstraint(Eigen::Matrix3d::Identity(),
Eigen::Vector3d::Ones(), x);
EXPECT_EQ(GetProgramType(prog), ProgramType::kUnknown);
}
} // namespace solvers
} // namespace drake