forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_attribute.cc
175 lines (161 loc) · 5.43 KB
/
program_attribute.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
#include "drake/solvers/program_attribute.h"
#include <algorithm>
#include <deque>
#include <sstream>
#include <vector>
#include <fmt/format.h>
namespace drake {
namespace solvers {
bool AreRequiredAttributesSupported(const ProgramAttributes& required,
const ProgramAttributes& supported,
std::string* unsupported_message) {
// Quick short-circuit if we're guaranteed to fail.
if ((required.size() > supported.size())
&& (unsupported_message == nullptr)) {
return false;
}
// Check required vs supported. If a mismatch is found and we don't need to
// produce a descriptive message, then we can bail immediately. Otherwise,
// tally any unsupported attributes to populate the message at the end.
std::vector<ProgramAttribute> unsupported_enums;
for (const auto& attribute : required) {
if (supported.count(attribute) == 0) {
if (unsupported_message == nullptr) {
return false;
} else {
unsupported_enums.push_back(attribute);
}
}
}
// If nothing was missing, then we're all done.
if (unsupported_enums.empty()) {
if (unsupported_message != nullptr) {
unsupported_message->clear();
}
return true;
}
// We need to produce an error message, i.e.,
// "a FooCost was declared but is not supported" or
// "a FooCost and BarCost were declared but are not supported" or
// "a FooCost, BarCost, and QuuxCost were declared but are not supported".
std::sort(unsupported_enums.begin(), unsupported_enums.end());
const int size = unsupported_enums.size();
std::string noun_phrase;
for (int i = 0; i < size; ++i) {
if (i >= 1) {
if (size == 2) {
noun_phrase += " and ";
} else if (i == (size - 1)) {
noun_phrase += ", and ";
} else {
noun_phrase += ", ";
}
}
noun_phrase += to_string(unsupported_enums[i]);
}
*unsupported_message = fmt::format(
(size == 1) ?
"a {} was declared but is not supported" :
"a {} were declared but are not supported",
noun_phrase);
return false;
}
std::string to_string(const ProgramAttribute& attr) {
switch (attr) {
case ProgramAttribute::kGenericCost:
return "GenericCost";
case ProgramAttribute::kGenericConstraint:
return "GenericConstraint";
case ProgramAttribute::kQuadraticCost:
return "QuadraticCost";
case ProgramAttribute::kQuadraticConstraint:
return "QuadraticConstraint";
case ProgramAttribute::kLinearCost:
return "LinearCost";
case ProgramAttribute::kLinearConstraint:
return "LinearConstraint";
case ProgramAttribute::kLinearEqualityConstraint:
return "LinearEqualityConstraint";
case ProgramAttribute::kLinearComplementarityConstraint:
return "LinearComplementarityConstraint";
case ProgramAttribute::kLorentzConeConstraint:
return "LorentzConeConstraint";
case ProgramAttribute::kRotatedLorentzConeConstraint:
return "RotatedLorentzConeConstraint";
case ProgramAttribute::kPositiveSemidefiniteConstraint:
return "PositiveSemidefiniteConstraint";
case ProgramAttribute::kExponentialConeConstraint:
return "ExponentialConeConstraint";
case ProgramAttribute::kL2NormCost:
return "L2NormCost";
case ProgramAttribute::kBinaryVariable:
return "BinaryVariable";
case ProgramAttribute::kCallback:
return "Callback";
}
DRAKE_UNREACHABLE();
}
std::ostream& operator<<(std::ostream& os, const ProgramAttribute& attr) {
os << to_string(attr);
return os;
}
std::string to_string(const ProgramAttributes& attrs) {
std::ostringstream result;
result << attrs;
return result.str();
}
std::ostream& operator<<(std::ostream& os, const ProgramAttributes& attrs) {
std::deque<ProgramAttribute> sorted(attrs.begin(), attrs.end());
std::sort(sorted.begin(), sorted.end());
os << "{ProgramAttributes: ";
if (sorted.empty()) {
os << "empty";
} else {
os << sorted.front();
sorted.pop_front();
for (const auto& attr : sorted) {
os << ", " << attr;
}
}
os << "}";
return os;
}
std::string to_string(const ProgramType& program_type) {
switch (program_type) {
case ProgramType::kLP:
return "linear programming";
case ProgramType::kQP:
return "quadratic programming";
case ProgramType::kSOCP:
return "second order cone programming";
case ProgramType::kSDP:
return "semidefinite programming";
case ProgramType::kGP:
return "geometric programming";
case ProgramType::kCGP:
return "conic geometric programming";
case ProgramType::kMILP:
return "mixed-integer linear programming";
case ProgramType::kMIQP:
return "mixed-integer quadratic programming";
case ProgramType::kMISOCP:
return "mixed-integer second order cone programming";
case ProgramType::kMISDP:
return "mixed-integer semidefinite programming";
case ProgramType::kQuadraticCostConicConstraint:
return "conic-constrained quadratic programming";
case ProgramType::kNLP:
return "nonlinear programming";
case ProgramType::kLCP:
return "linear complementarity programming";
case ProgramType::kUnknown:
return "uncategorized mathematical programming type";
}
DRAKE_UNREACHABLE();
}
std::ostream& operator<<(std::ostream& os, const ProgramType& program_type) {
os << to_string(program_type);
return os;
}
} // namespace solvers
} // namespace drake