forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_program_type.cc
279 lines (260 loc) · 11.6 KB
/
get_program_type.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
#include "drake/solvers/get_program_type.h"
#include <initializer_list>
#include <vector>
#include "drake/common/never_destroyed.h"
#include "drake/solvers/program_attribute.h"
namespace drake {
namespace solvers {
namespace {
// The requirements of a type of optimization program based on its attributes.
// The requirements are that the program attributes
// 1. is a subset of @p acceptable_attributes.
// 2. must include all of @p must_include_attributes.
// 3. must include at least one of @p must_include_one_of.
// If must_include_one_of is empty, then we ignore this condition.
struct Requirements {
ProgramAttributes acceptable;
ProgramAttributes must_include;
ProgramAttributes must_include_one_of;
};
// Check if @p program_attributes satisfies @p requirements.
bool SatisfiesProgramType(const Requirements& requirements,
const ProgramAttributes& program_attributes) {
// Check if program_attributes is a subset of acceptable_attributes
for (const auto attribute : program_attributes) {
if (!requirements.acceptable.contains(attribute)) {
return false;
}
}
// Check if program_attributes include must_include_attributes
for (const auto& must_include_attr : requirements.must_include) {
if (!program_attributes.contains(must_include_attr)) {
return false;
}
}
bool include_one_of = requirements.must_include_one_of.empty() ? true : false;
for (const auto& include : requirements.must_include_one_of) {
if (program_attributes.contains(include)) {
include_one_of = true;
break;
}
}
if (!include_one_of) {
return false;
}
return true;
}
const Requirements& GetRequirementsLP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
ProgramAttributes attributes{std::initializer_list<ProgramAttribute>{
ProgramAttribute::kLinearCost, ProgramAttribute::kLinearConstraint,
ProgramAttribute::kLinearEqualityConstraint}};
return Requirements{.acceptable = attributes,
.must_include = {},
.must_include_one_of = attributes};
}()};
return requirements.access();
}
const Requirements& GetRequirementsQP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
const Requirements& lp_requirements = GetRequirementsLP();
ProgramAttributes acceptable = lp_requirements.acceptable;
acceptable.emplace(ProgramAttribute::kQuadraticCost);
return Requirements{.acceptable = acceptable,
.must_include = {ProgramAttribute::kQuadraticCost},
.must_include_one_of = {}};
}()};
return requirements.access();
}
const Requirements& GetRequirementsSOCP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
const Requirements& lp_requirements = GetRequirementsLP();
ProgramAttributes acceptable = lp_requirements.acceptable;
acceptable.emplace(ProgramAttribute::kLorentzConeConstraint);
acceptable.emplace(ProgramAttribute::kRotatedLorentzConeConstraint);
return Requirements{.acceptable = acceptable,
.must_include = {},
.must_include_one_of = {
ProgramAttribute::kLorentzConeConstraint,
ProgramAttribute::kRotatedLorentzConeConstraint}};
}()};
return requirements.access();
}
const Requirements& GetRequirementsSDP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
const Requirements& socp_requirements = GetRequirementsSOCP();
ProgramAttributes acceptable = socp_requirements.acceptable;
acceptable.emplace(ProgramAttribute::kPositiveSemidefiniteConstraint);
return Requirements{
.acceptable = acceptable,
.must_include = {ProgramAttribute::kPositiveSemidefiniteConstraint},
.must_include_one_of = {}};
}()};
return requirements.access();
}
const Requirements& GetRequirementsGP() {
static const drake::never_destroyed<Requirements> requirements{Requirements{
.acceptable = {ProgramAttribute::kLinearCost,
ProgramAttribute::kExponentialConeConstraint},
.must_include = {ProgramAttribute::kExponentialConeConstraint},
.must_include_one_of = {}}};
return requirements.access();
}
const Requirements& GetRequirementsCGP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
const Requirements& sdp_requirements = GetRequirementsSDP();
ProgramAttributes acceptable = sdp_requirements.acceptable;
acceptable.emplace(ProgramAttribute::kExponentialConeConstraint);
ProgramAttributes must_include_one_of = sdp_requirements.acceptable;
must_include_one_of.erase(ProgramAttribute::kLinearCost);
return Requirements{
.acceptable = acceptable,
.must_include = {ProgramAttribute::kExponentialConeConstraint},
.must_include_one_of = must_include_one_of};
}()};
return requirements.access();
}
const Requirements& GetRequirementsQuadraticCostConicProgram() {
static const drake::never_destroyed<Requirements> requirements{[]() {
const Requirements& cgp_requirements = GetRequirementsCGP();
ProgramAttributes acceptable = cgp_requirements.acceptable;
acceptable.emplace(ProgramAttribute::kQuadraticCost);
return Requirements{.acceptable = acceptable,
.must_include = {ProgramAttribute::kQuadraticCost},
.must_include_one_of = {
// All nonlinear convex conic constraints.
ProgramAttribute::kLorentzConeConstraint,
ProgramAttribute::kRotatedLorentzConeConstraint,
ProgramAttribute::kPositiveSemidefiniteConstraint,
ProgramAttribute::kExponentialConeConstraint}};
}()};
return requirements.access();
}
const Requirements& GetRequirementsLCP() {
static const drake::never_destroyed<Requirements> requirements{Requirements{
.acceptable = {ProgramAttribute::kLinearComplementarityConstraint},
.must_include = {ProgramAttribute::kLinearComplementarityConstraint},
.must_include_one_of = {}}};
return requirements.access();
}
// Returns the requirements of a mixed-integer optimization program.
// Append kBinaryVariable to acceptable and must_include.
// Retain must_include_one_of.
Requirements MixedIntegerRequirements(
const Requirements& continuous_requirements) {
Requirements mip_requirements = continuous_requirements;
mip_requirements.acceptable.emplace(ProgramAttribute::kBinaryVariable);
mip_requirements.must_include.emplace(ProgramAttribute::kBinaryVariable);
return mip_requirements;
}
const Requirements& GetRequirementsMILP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
return MixedIntegerRequirements(GetRequirementsLP());
}()};
return requirements.access();
}
const Requirements& GetRequirementsMIQP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
return MixedIntegerRequirements(GetRequirementsQP());
}()};
return requirements.access();
}
const Requirements& GetRequirementsMISOCP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
return MixedIntegerRequirements(GetRequirementsSOCP());
}()};
return requirements.access();
}
const Requirements& GetRequirementsMISDP() {
static const drake::never_destroyed<Requirements> requirements{[]() {
return MixedIntegerRequirements(GetRequirementsSDP());
}()};
return requirements.access();
}
bool AllQuadraticCostsConvex(
const std::vector<Binding<QuadraticCost>>& quadratic_costs) {
return std::all_of(quadratic_costs.begin(), quadratic_costs.end(),
[](const Binding<QuadraticCost>& quadratic_cost) {
return quadratic_cost.evaluator()->is_convex();
});
}
bool IsNLP(const MathematicalProgram& prog) {
// A program is a nonlinear program (NLP) if it satisfies all the following
// conditions:
// 1. It has no binary variables.
// 2. It is not an LCP (A program with only linear complementarity
// constraints).
// 3. It has at least one of : generic costs, generic constraints, non-convex
// quadratic cost, linear complementarity constraints, quadratic constraints.
const bool has_generic_cost =
prog.required_capabilities().contains(ProgramAttribute::kGenericCost);
const bool has_nonconvex_quadratic_cost =
!AllQuadraticCostsConvex(prog.quadratic_costs());
const bool has_generic_constraint = prog.required_capabilities().contains(
ProgramAttribute::kGenericConstraint);
const bool has_quadratic_constraint =
prog.required_capabilities().count(
ProgramAttribute::kQuadraticConstraint) > 0;
const bool no_binary_variable = prog.required_capabilities().count(
ProgramAttribute::kBinaryVariable) == 0;
const bool has_linear_complementarity_constraint =
prog.required_capabilities().count(
ProgramAttribute::kLinearComplementarityConstraint) > 0;
const bool is_LCP =
SatisfiesProgramType(GetRequirementsLCP(), prog.required_capabilities());
return no_binary_variable && !is_LCP &&
(has_generic_cost || has_nonconvex_quadratic_cost ||
has_generic_constraint || has_quadratic_constraint ||
has_linear_complementarity_constraint);
}
} // namespace
ProgramType GetProgramType(const MathematicalProgram& prog) {
if (SatisfiesProgramType(GetRequirementsLP(), prog.required_capabilities())) {
return ProgramType::kLP;
} else if (SatisfiesProgramType(GetRequirementsQP(),
prog.required_capabilities()) &&
AllQuadraticCostsConvex(prog.quadratic_costs())) {
return ProgramType::kQP;
} else if (SatisfiesProgramType(GetRequirementsSOCP(),
prog.required_capabilities())) {
return ProgramType::kSOCP;
} else if (SatisfiesProgramType(GetRequirementsSDP(),
prog.required_capabilities())) {
return ProgramType::kSDP;
} else if (SatisfiesProgramType(GetRequirementsGP(),
prog.required_capabilities())) {
// TODO(hongkai.dai): support more general type of geometric programming,
// with constraints on posynomials.
return ProgramType::kGP;
} else if (SatisfiesProgramType(GetRequirementsCGP(),
prog.required_capabilities())) {
return ProgramType::kCGP;
} else if (SatisfiesProgramType(GetRequirementsMILP(),
prog.required_capabilities())) {
return ProgramType::kMILP;
} else if (SatisfiesProgramType(GetRequirementsMIQP(),
prog.required_capabilities()) &&
AllQuadraticCostsConvex(prog.quadratic_costs())) {
return ProgramType::kMIQP;
} else if (SatisfiesProgramType(GetRequirementsMISOCP(),
prog.required_capabilities())) {
return ProgramType::kMISOCP;
} else if (SatisfiesProgramType(GetRequirementsMISDP(),
prog.required_capabilities())) {
return ProgramType::kMISDP;
} else if (SatisfiesProgramType(GetRequirementsQuadraticCostConicProgram(),
prog.required_capabilities()) &&
AllQuadraticCostsConvex(prog.quadratic_costs())) {
return ProgramType::kQuadraticCostConicConstraint;
} else if (SatisfiesProgramType(GetRequirementsLCP(),
prog.required_capabilities())) {
return ProgramType::kLCP;
} else if (IsNLP(prog)) {
return ProgramType::kNLP;
} else {
return ProgramType::kUnknown;
}
DRAKE_UNREACHABLE();
}
} // namespace solvers
} // namespace drake