forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osqp_solver.cc
361 lines (323 loc) · 13.4 KB
/
osqp_solver.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
#include "drake/solvers/osqp_solver.h"
#include <vector>
#include <osqp.h>
#include "drake/math/eigen_sparse_triplet.h"
#include "drake/solvers/mathematical_program.h"
namespace drake {
namespace solvers {
namespace {
void ParseQuadraticCosts(const MathematicalProgram& prog,
Eigen::SparseMatrix<c_float>* P,
std::vector<c_float>* q, double* constant_cost_term) {
DRAKE_ASSERT(static_cast<int>(q->size()) == prog.num_vars());
// Loop through each quadratic costs in prog, and compute the Hessian matrix
// P, the linear cost q, and the constant cost term.
std::vector<Eigen::Triplet<c_float>> P_triplets;
for (const auto& quadratic_cost : prog.quadratic_costs()) {
const VectorXDecisionVariable& x = quadratic_cost.variables();
// x_indices are the indices of the variables x (the variables bound with
// this quadratic cost) in the program decision variables.
const std::vector<int> x_indices = prog.FindDecisionVariableIndices(x);
// Add quadratic_cost.Q to the Hessian P.
const std::vector<Eigen::Triplet<double>> Qi_triplets =
math::SparseMatrixToTriplets(quadratic_cost.evaluator()->Q());
P_triplets.reserve(P_triplets.size() + Qi_triplets.size());
for (int i = 0; i < static_cast<int>(Qi_triplets.size()); ++i) {
// Unpack the field of the triplet (for clarity below).
const int row = x_indices[Qi_triplets[i].row()];
const int col = x_indices[Qi_triplets[i].col()];
const double value = Qi_triplets[i].value();
// Since OSQP 0.6.0 the P matrix is required to be upper triangular, so
// we only add upper triangular entries to P_triplets.
if (row <= col) {
P_triplets.emplace_back(row, col, static_cast<c_float>(value));
}
}
// Add quadratic_cost.b to the linear cost term q.
for (int i = 0; i < x.rows(); ++i) {
q->at(x_indices[i]) += quadratic_cost.evaluator()->b()(i);
}
// Add quadratic_cost.c to constant term
*constant_cost_term += quadratic_cost.evaluator()->c();
}
P->resize(prog.num_vars(), prog.num_vars());
P->setFromTriplets(P_triplets.begin(), P_triplets.end());
}
void ParseLinearCosts(const MathematicalProgram& prog, std::vector<c_float>* q,
double* constant_cost_term) {
// Add the linear costs to the osqp cost.
DRAKE_ASSERT(static_cast<int>(q->size()) == prog.num_vars());
// Loop over the linear costs stored inside prog.
for (const auto& linear_cost : prog.linear_costs()) {
for (int i = 0; i < static_cast<int>(linear_cost.GetNumElements()); ++i) {
// Append the linear cost term to q.
if (linear_cost.evaluator()->a()(i) != 0) {
const int x_index =
prog.FindDecisionVariableIndex(linear_cost.variables()(i));
q->at(x_index) += linear_cost.evaluator()->a()(i);
}
}
// Add the constant cost term to constant_cost_term.
*constant_cost_term += linear_cost.evaluator()->b();
}
}
// OSQP defines its own infinity in osqp/include/glob_opts.h.
c_float ConvertInfinity(double val) {
if (std::isinf(val)) {
if (val > 0) {
return OSQP_INFTY;
}
return -OSQP_INFTY;
}
return static_cast<c_float>(val);
}
// Will call this function to parse both LinearConstraint and
// LinearEqualityConstraint.
template <typename C>
void ParseLinearConstraints(const MathematicalProgram& prog,
const std::vector<Binding<C>>& linear_constraints,
std::vector<Eigen::Triplet<c_float>>* A_triplets,
std::vector<c_float>* l, std::vector<c_float>* u,
int* num_A_rows) {
// Loop over the linear constraints, stack them to get l, u and A.
for (const auto& constraint : linear_constraints) {
const std::vector<int> x_indices =
prog.FindDecisionVariableIndices(constraint.variables());
const std::vector<Eigen::Triplet<double>> Ai_triplets =
math::SparseMatrixToTriplets(constraint.evaluator()->A());
// Append constraint.A to osqp A.
for (const auto& Ai_triplet : Ai_triplets) {
A_triplets->emplace_back(*num_A_rows + Ai_triplet.row(),
x_indices[Ai_triplet.col()],
static_cast<c_float>(Ai_triplet.value()));
}
const int num_Ai_rows = constraint.evaluator()->num_constraints();
l->reserve(l->size() + num_Ai_rows);
u->reserve(u->size() + num_Ai_rows);
for (int i = 0; i < num_Ai_rows; ++i) {
l->push_back(ConvertInfinity(constraint.evaluator()->lower_bound()(i)));
u->push_back(ConvertInfinity(constraint.evaluator()->upper_bound()(i)));
}
*num_A_rows += num_Ai_rows;
}
}
void ParseBoundingBoxConstraints(
const MathematicalProgram& prog,
std::vector<Eigen::Triplet<c_float>>* A_triplets, std::vector<c_float>* l,
std::vector<c_float>* u, int* num_A_rows) {
// Loop over the linear constraints, stack them to get l, u and A.
for (const auto& constraint : prog.bounding_box_constraints()) {
// Append constraint.A to osqp A.
for (int i = 0; i < static_cast<int>(constraint.GetNumElements()); ++i) {
A_triplets->emplace_back(
*num_A_rows + i,
prog.FindDecisionVariableIndex(constraint.variables()(i)),
static_cast<c_float>(1));
}
const int num_Ai_rows = constraint.evaluator()->num_constraints();
l->reserve(l->size() + num_Ai_rows);
u->reserve(u->size() + num_Ai_rows);
for (int i = 0; i < num_Ai_rows; ++i) {
l->push_back(ConvertInfinity(constraint.evaluator()->lower_bound()(i)));
u->push_back(ConvertInfinity(constraint.evaluator()->upper_bound()(i)));
}
*num_A_rows += num_Ai_rows;
}
}
void ParseAllLinearConstraints(const MathematicalProgram& prog,
Eigen::SparseMatrix<c_float>* A,
std::vector<c_float>* l,
std::vector<c_float>* u) {
std::vector<Eigen::Triplet<c_float>> A_triplets;
l->clear();
u->clear();
int num_A_rows = 0;
ParseLinearConstraints(prog, prog.linear_constraints(), &A_triplets, l, u,
&num_A_rows);
ParseLinearConstraints(prog, prog.linear_equality_constraints(), &A_triplets,
l, u, &num_A_rows);
ParseBoundingBoxConstraints(prog, &A_triplets, l, u, &num_A_rows);
A->resize(num_A_rows, prog.num_vars());
A->setFromTriplets(A_triplets.begin(), A_triplets.end());
}
// Convert an Eigen::SparseMatrix to csc_matrix, to be used by osqp.
// Make sure the input Eigen sparse matrix is compressed, by calling
// makeCompressed() function.
// The caller of this function is responsible for freeing the memory allocated
// here.
csc* EigenSparseToCSC(const Eigen::SparseMatrix<c_float>& mat) {
// A csc matrix is in the compressed column major.
c_float* values =
static_cast<c_float*>(c_malloc(sizeof(c_float) * mat.nonZeros()));
c_int* inner_indices =
static_cast<c_int*>(c_malloc(sizeof(c_int) * mat.nonZeros()));
c_int* outer_indices =
static_cast<c_int*>(c_malloc(sizeof(c_int) * (mat.cols() + 1)));
for (int i = 0; i < mat.nonZeros(); ++i) {
values[i] = *(mat.valuePtr() + i);
inner_indices[i] = static_cast<c_int>(*(mat.innerIndexPtr() + i));
}
for (int i = 0; i < mat.cols() + 1; ++i) {
outer_indices[i] = static_cast<c_int>(*(mat.outerIndexPtr() + i));
}
return csc_matrix(mat.rows(), mat.cols(), mat.nonZeros(), values,
inner_indices, outer_indices);
}
template <typename T1, typename T2>
void SetOsqpSolverSetting(const std::unordered_map<std::string, T1>& options,
const std::string& option_name,
T2* osqp_setting_field) {
const auto it = options.find(option_name);
if (it != options.end()) {
*osqp_setting_field = it->second;
}
}
template <typename T1, typename T2>
void SetOsqpSolverSettingWithDefaultValue(
const std::unordered_map<std::string, T1>& options,
const std::string& option_name, T2* osqp_setting_field,
const T1& default_field_value) {
const auto it = options.find(option_name);
if (it != options.end()) {
*osqp_setting_field = it->second;
} else {
*osqp_setting_field = default_field_value;
}
}
void SetOsqpSolverSettings(const SolverOptions& solver_options,
OSQPSettings* settings) {
const std::unordered_map<std::string, double>& options_double =
solver_options.GetOptionsDouble(OsqpSolver::id());
const std::unordered_map<std::string, int>& options_int =
solver_options.GetOptionsInt(OsqpSolver::id());
// TODO(hongkai.dai): Fill in all the fields defined in OSQPSettings.
SetOsqpSolverSetting(options_double, "rho", &(settings->rho));
SetOsqpSolverSetting(options_double, "sigma", &(settings->sigma));
SetOsqpSolverSetting(options_int, "scaling", &(settings->scaling));
SetOsqpSolverSetting(options_int, "max_iter", &(settings->max_iter));
SetOsqpSolverSetting(options_int, "polish_refine_iter",
&(settings->polish_refine_iter));
SetOsqpSolverSettingWithDefaultValue(options_int, "verbose",
&(settings->verbose), 0);
// Default polish to true, to get an accurate solution.
SetOsqpSolverSettingWithDefaultValue(options_int, "polish",
&(settings->polish), 1);
}
} // namespace
bool OsqpSolver::is_available() { return true; }
void OsqpSolver::DoSolve(
const MathematicalProgram& prog,
const Eigen::VectorXd& initial_guess,
const SolverOptions& merged_options,
MathematicalProgramResult* result) const {
OsqpSolverDetails& solver_details =
result->SetSolverDetailsType<OsqpSolverDetails>();
// TODO(hongkai.dai): OSQP uses initial guess to warm start.
unused(initial_guess);
// OSQP solves a convex quadratic programming problem
// min 0.5 xᵀPx + qᵀx
// s.t l ≤ Ax ≤ u
// OSQP is written in C, so this function will be in C style.
// Get the cost for the QP.
Eigen::SparseMatrix<c_float> P_sparse;
std::vector<c_float> q(prog.num_vars(), 0);
double constant_cost_term{0};
ParseQuadraticCosts(prog, &P_sparse, &q, &constant_cost_term);
ParseLinearCosts(prog, &q, &constant_cost_term);
// Parse the linear constraints.
Eigen::SparseMatrix<c_float> A_sparse;
std::vector<c_float> l, u;
ParseAllLinearConstraints(prog, &A_sparse, &l, &u);
// Now pass the constraint and cost to osqp data.
OSQPData* data = nullptr;
// Populate data.
data = static_cast<OSQPData*>(c_malloc(sizeof(OSQPData)));
data->n = prog.num_vars();
data->m = A_sparse.rows();
data->P = EigenSparseToCSC(P_sparse);
data->q = q.data();
data->A = EigenSparseToCSC(A_sparse);
data->l = l.data();
data->u = u.data();
// Define Solver settings as default.
// Problem settings
OSQPSettings* settings =
static_cast<OSQPSettings*>(c_malloc(sizeof(OSQPSettings)));
osqp_set_default_settings(settings);
SetOsqpSolverSettings(merged_options, settings);
// If any step fails, it will set the solution_result and skip other steps.
std::optional<SolutionResult> solution_result;
// Setup workspace.
OSQPWorkspace* work = nullptr;
if (!solution_result) {
const c_int osqp_setup_err = osqp_setup(&work, data, settings);
if (osqp_setup_err != 0) {
solution_result = SolutionResult::kInvalidInput;
}
}
// Solve problem.
if (!solution_result) {
DRAKE_THROW_UNLESS(work != nullptr);
const c_int osqp_solve_err = osqp_solve(work);
if (osqp_solve_err != 0) {
solution_result = SolutionResult::kInvalidInput;
}
}
// Extract results.
if (!solution_result) {
DRAKE_THROW_UNLESS(work->info != nullptr);
solver_details.iter = work->info->iter;
solver_details.status_val = work->info->status_val;
solver_details.primal_res = work->info->pri_res;
solver_details.dual_res = work->info->dua_res;
solver_details.setup_time = work->info->setup_time;
solver_details.solve_time = work->info->solve_time;
solver_details.polish_time = work->info->polish_time;
solver_details.run_time = work->info->run_time;
switch (work->info->status_val) {
case OSQP_SOLVED:
case OSQP_SOLVED_INACCURATE: {
const Eigen::Map<Eigen::Matrix<c_float, Eigen::Dynamic, 1>> osqp_sol(
work->solution->x, prog.num_vars());
result->set_x_val(osqp_sol.cast<double>());
result->set_optimal_cost(work->info->obj_val + constant_cost_term);
solution_result = SolutionResult::kSolutionFound;
break;
}
case OSQP_PRIMAL_INFEASIBLE:
case OSQP_PRIMAL_INFEASIBLE_INACCURATE: {
solution_result = SolutionResult::kInfeasibleConstraints;
result->set_optimal_cost(MathematicalProgram::kGlobalInfeasibleCost);
break;
}
case OSQP_DUAL_INFEASIBLE:
case OSQP_DUAL_INFEASIBLE_INACCURATE: {
solution_result = SolutionResult::kDualInfeasible;
break;
}
case OSQP_MAX_ITER_REACHED: {
solution_result = SolutionResult::kIterationLimit;
break;
}
default: {
solution_result = SolutionResult::kUnknownError;
break;
}
}
}
result->set_solution_result(solution_result.value());
// Clean workspace.
osqp_cleanup(work);
c_free(data->P->x);
c_free(data->P->i);
c_free(data->P->p);
c_free(data->P);
c_free(data->A->x);
c_free(data->A->i);
c_free(data->A->p);
c_free(data->A);
c_free(data);
c_free(settings);
}
} // namespace solvers
} // namespace drake