forked from robotology/osqp-eigen
-
Notifications
You must be signed in to change notification settings - Fork 3
/
OptimizerSolver.tpp
429 lines (366 loc) · 15.8 KB
/
OptimizerSolver.tpp
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
/**
* @file OptimizerSolver.tpp
* @author Giulio Romualdi
* @copyright Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
* @date 2018
*/
#include <iostream>
#include "auxil.h"
#include "scaling.h"
template<int n>
bool OSQPWrapper::OptimizerSolver::updateGradient(Eigen::Matrix<c_float, n, 1>& gradient)
{
// check if the dimension of the gradient is correct
if(gradient.rows() != m_workspace->data->n){
std::cerr << "[Optimizer Workspace] The size of the gradient must be equal to the number of the variables."
<< std::endl;
return false;
}
// update the gradient vector
if(osqp_update_lin_cost(m_workspace, gradient.data())){
std::cerr << "[Optimizer Workspace] Error when the update gradient is called."
<< std::endl;
return false;
}
return true;
}
template<int m>
bool OSQPWrapper::OptimizerSolver::updateLowerBound(Eigen::Matrix<c_float, m, 1>& lowerBound)
{
// check if the dimension of the lowerBound vector is correct
if(lowerBound.rows() != m_workspace->data->m){
std::cerr << "[Optimizer Workspace] The size of the lower bound must be equal to the number of the variables."
<< std::endl;
return false;
}
// update the lower bound vector
if(osqp_update_lower_bound(m_workspace, lowerBound.data())){
std::cerr << "[Optimizer Workspace] Error when the update lower bound is called."
<< std::endl;
return false;
}
return true;
}
template<int m>
bool OSQPWrapper::OptimizerSolver::updateUpperBound(Eigen::Matrix<c_float, m, 1>& upperBound)
{
// check if the dimension of the upperBound vector is correct
if(upperBound.rows() != m_workspace->data->m){
std::cerr << "[Optimizer Workspace] The size of the upper bound must be equal to the number of the variables."
<< std::endl;
return false;
}
// update the upper bound vector
if(osqp_update_upper_bound(m_workspace, upperBound.data())){
std::cerr << "[Optimizer Workspace] Error when the update upper bound is called."
<< std::endl;
return false;
}
return true;
}
template<int m>
bool OSQPWrapper::OptimizerSolver::updateBounds(Eigen::Matrix<c_float, m, 1>& lowerBound,
Eigen::Matrix<c_float, m, 1>& upperBound)
{
// check if the dimension of the upperBound vector is correct
if(upperBound.rows() != m_workspace->data->m){
std::cerr << "[Optimizer Workspace] The size of the upper bound must be equal to the number of the variables."
<< std::endl;
return false;
}
// check if the dimension of the lowerBound vector is correct
if(lowerBound.rows() != m_workspace->data->m){
std::cerr << "[Optimizer Workspace] The size of the lower bound must be equal to the number of the variables."
<< std::endl;
return false;
}
// update lower and upper constraints
if(osqp_update_bounds(m_workspace, lowerBound.data(), upperBound.data())){
std::cerr << "[Optimizer Workspace] Error when the update bounds is called."
<< std::endl;
return false;
}
return true;
}
template<typename T>
bool OSQPWrapper::OptimizerSolver::updateHessianMatrix(const Eigen::SparseMatrix<T> &hessianMatrix)
{
if(!m_isSolverInitialized){
std::cerr << "[updateHessianMatrix] The solver has not been initialized."
<< std::endl;
return false;
}
if(((c_int)hessianMatrix.rows() != m_workspace->data->n)||
((c_int)hessianMatrix.cols() != m_workspace->data->n)){
std::cerr << "[updateHessianMatrix] The hessian matrix has to be a nxn matrix"
<< std::endl;
return false;
}
// get the upper triangular part of the hessian matrix
Eigen::SparseMatrix<T> hessianMatrixUpperTriangular;
hessianMatrixUpperTriangular = hessianMatrix.template triangularView<Eigen::Upper>();
// compress the hessian matrix
hessianMatrixUpperTriangular.makeCompressed();
// evaluate the triplets from old and new hessian sparse matrices
std::vector<Eigen::Triplet<T>> oldHessianTriplet, newHessianTriplet;
if(!OSQPWrapper::SparseMatrixHelper::osqpSparseMatrixToTriplets(m_workspace->data->P,
oldHessianTriplet)){
std::cerr << "[updateHessianMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
if(!OSQPWrapper::SparseMatrixHelper::eigenSparseMatrixToTriplets(hessianMatrixUpperTriangular,
newHessianTriplet)){
std::cerr << "[updateHessianMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
// try to update the hessian matrix without reinitialize the solver
// according to the osqp library it can be done only if the sparsity pattern of the hessian
// matrix does not change.
std::vector<c_int> newIndices;
std::vector<c_float> newValues;
if(evaluateNewValues(oldHessianTriplet, newHessianTriplet,
newIndices, newValues)){
if(osqp_update_P(m_workspace, newValues.data(), newIndices.data(), newIndices.size() != 0)){
std::cerr << "[updateHessianMatrix] Unable to update hessian matrix."
<< std::endl;
return false;
}
}
else{
// the sparsity pattern has changed
// the optimizer solver has to be setup again
// get the primal and the dual variables
Eigen::Matrix<c_float, Eigen::Dynamic ,1> primalVariable;
Eigen::Matrix<c_float, Eigen::Dynamic ,1> dualVariable;
if(!getPrimalVariable(primalVariable)){
std::cerr << "[updateHessianMatrix] Unable to get the primal variable."
<< std::endl;
return false;
}
if(!getDualVariable(dualVariable)){
std::cerr << "[updateHessianMatrix] Unable to get the dual variable."
<< std::endl;
return false;
}
// clear old hessian matrix
m_data->clearHessianMatrix();
// set new hessian matrix
if(!m_data->setHessianMatrix(hessianMatrix)){
std::cerr << "[updateHessianMatrix] Unable to update the hessian matrix in "
<< "OptimizaroData object."
<< std::endl;
return false;
}
// clear the old solver
clearSolver();
// initialize a new solver
initSolver();
// set the old primal and dual variables
if(!setPrimalVariable(primalVariable)){
std::cerr << "[updateHessianMatrix] Unable to set the primal variable."
<< std::endl;
return false;
}
if(!setDualVariable(dualVariable)){
std::cerr << "[updateHessianMatrix] Unable to set the dual variable."
<< std::endl;
return false;
}
}
return true;
}
template<typename T>
bool OSQPWrapper::OptimizerSolver::updateLinearConstraintsMatrix(const Eigen::SparseMatrix<T> &linearConstraintsMatrix)
{
if(!m_isSolverInitialized){
std::cerr << "[updateLinearConstraintMatrix] The solver has not been initialized."
<< std::endl;
return false;
}
if(((c_int)linearConstraintsMatrix.rows() != m_workspace->data->m)||
((c_int)linearConstraintsMatrix.cols() != m_workspace->data->n)){
std::cerr << "[updateLinearConstraintMatrix] The constraints matrix has to be a mxn matrix"
<< std::endl;
return false;
}
// get the upper triangular part of the hessian matrix
Eigen::SparseMatrix<T> linearConstraintsMatrixCompressed = linearConstraintsMatrix;
// compress the hessian matrix
linearConstraintsMatrixCompressed.makeCompressed();
// evaluate the triplets from old and new hessian sparse matrices
std::vector<Eigen::Triplet<T>> oldLinearConstraintsTriplet, newLinearConstraintsTriplet;
if(!OSQPWrapper::SparseMatrixHelper::osqpSparseMatrixToTriplets(m_workspace->data->A,
oldLinearConstraintsTriplet)){
std::cerr << "[updateLinearConstraintMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
if(!OSQPWrapper::SparseMatrixHelper::eigenSparseMatrixToTriplets(linearConstraintsMatrixCompressed,
newLinearConstraintsTriplet)){
std::cerr << "[updateLinearConstraintMatrix] Unable to evaluate triplets from the old hessian matrix."
<< std::endl;
return false;
}
// try to update the linear constraints matrix without reinitialize the solver
// according to the osqp library it can be done only if the sparsity pattern of the
// matrix does not change.
std::vector<c_int> newIndices;
std::vector<c_float> newValues;
if(evaluateNewValues(oldLinearConstraintsTriplet, newLinearConstraintsTriplet,
newIndices, newValues)){
if(osqp_update_A(m_workspace, newValues.data(), newIndices.data(), newIndices.size() != 0)){
std::cerr << "[updateLinearConstraintMatrix] Unable to update linear constraints matrix."
<< std::endl;
return false;
}
}
else{
// the sparsity pattern has changed
// the optimizer solver has to be setup again
// get the primal and the dual variables
Eigen::Matrix<c_float, Eigen::Dynamic ,1> primalVariable;
Eigen::Matrix<c_float, Eigen::Dynamic ,1> dualVariable;
if(!getPrimalVariable(primalVariable)){
std::cerr << "[updateLinearConstraintMatrix] Unable to get the primal variable."
<< std::endl;
return false;
}
if(!getDualVariable(dualVariable)){
std::cerr << "[updateLinearConstraintMatrix] Unable to get the dual variable."
<< std::endl;
return false;
}
// clear old linear constraints matrix
m_data->clearLinearConstraintsMatrix();
// set new linear constraints matrix
if(!m_data->setLinearConstraintsMatrix(linearConstraintsMatrix)){
std::cerr << "[updateLinearConstraintMatrix] Unable to update the hessian matrix in "
<< "OptimizaroData object."
<< std::endl;
return false;
}
// clear the old solver
clearSolver();
// initialize a new solver
initSolver();
// set the old primal and dual variables
if(!setPrimalVariable(primalVariable)){
std::cerr << "[updateLinearConstraintMatrix] Unable to set the primal variable."
<< std::endl;
return false;
}
if(!setDualVariable(dualVariable)){
std::cerr << "[updateLinearConstraintMatrix] Unable to set the dual variable."
<< std::endl;
return false;
}
}
return true;
}
template<typename T, int n, int m>
bool OSQPWrapper::OptimizerSolver::setWarmStart(const Eigen::Matrix<T, n, 1> &primalVariable,
const Eigen::Matrix<T, m, 1> &dualVariable)
{
if(primalVariable.rows() != m_workspace->data->n){
std::cerr << "[setWarmStart] The size of the primal variable vector has to be equal to "
<< " the number of variables."
<< std::endl;
return false;
}
if(dualVariable.rows() != m_workspace->data->m){
std::cerr << "[setWarmStart] The size of the dual variable vector has to be equal to "
<< " the number of constraints."
<< std::endl;
return false;
}
Eigen::Matrix<c_float, n, 1> primalVariableCast = primalVariable.template cast <c_float>();
Eigen::Matrix<c_float, n, 1> dualVariableCast = dualVariable.template cast <c_float>();
return (osqp_warm_start(m_workspace, primalVariableCast.data(), dualVariableCast.data()) == 0);
}
template<typename T, int n>
bool OSQPWrapper::OptimizerSolver::setPrimalVariable(const Eigen::Matrix<T, n, 1> &primalVariable)
{
if(primalVariable.rows() != m_workspace->data->n){
std::cerr << "[setPrimalVariable] The size of the primal variable vector has to be equal to "
<< " the number of variables."
<< std::endl;
return false;
}
Eigen::Matrix<c_float, n, 1> primalVariableCast = primalVariable.template cast <c_float>();
return (osqp_warm_start_x(m_workspace, primalVariableCast.data()) == 0);
}
template<typename T, int m>
bool OSQPWrapper::OptimizerSolver::setDualVariable(const Eigen::Matrix<T, m, 1> &dualVariable)
{
if(dualVariable.rows() != m_workspace->data->m){
std::cerr << "[setDualVariable] The size of the dual variable vector has to be equal to "
<< " the number of constraints."
<< std::endl;
return false;
}
Eigen::Matrix<c_float, m, 1> dualVariableCast = dualVariable.template cast <c_float>();
return (osqp_warm_start_y(m_workspace, dualVariableCast.data()) == 0);
}
template<typename T, int n>
bool OSQPWrapper::OptimizerSolver::getPrimalVariable(Eigen::Matrix<T, n, 1> &primalVariable)
{
if(n == Eigen::Dynamic){
primalVariable.resize(m_workspace->data->n, 1);
}
else{
if (n != m_workspace->data->n){
std::cerr << "[getPrimalVariable] The size of the vector has to be equal to the number of "
<< "variables. (You can use an eigen dynamic vector)"
<< std::endl;
return false;
}
}
for(int i = 0; i< m_workspace->data->n; i++){
primalVariable(i,0) = (T)m_workspace->x[i];
}
return true;
}
template<typename T, int m>
bool OSQPWrapper::OptimizerSolver::getDualVariable(Eigen::Matrix<T, m, 1> &dualVariable)
{
if(m == Eigen::Dynamic){
dualVariable.resize(m_workspace->data->m, 1);
}
else{
if (m != m_workspace->data->m){
std::cerr << "[getdualVariable] The size of the vector has to be equal to the number of "
<< "constraints. (You can use an eigen dynamic vector)"
<< std::endl;
return false;
}
}
for(int i = 0; i< m_workspace->data->m; i++){
dualVariable(i,0) = (T)m_workspace->y[i];
}
return true;
}
template<typename T>
bool OSQPWrapper::OptimizerSolver::evaluateNewValues(const std::vector<Eigen::Triplet<T>> &oldMatrixTriplet,
const std::vector<Eigen::Triplet<T>> &newMatrixTriplet,
std::vector<c_int> &newIndices,
std::vector<c_float> &newValues) const
{
// check if the sparsity pattern is changed
if(newMatrixTriplet.size() == oldMatrixTriplet.size()){
for(int i = 0; i < newMatrixTriplet.size(); i++){
// check if the sparsity pattern is changed
if((newMatrixTriplet[i].row() != oldMatrixTriplet[i].row()) ||
(newMatrixTriplet[i].col() != oldMatrixTriplet[i].col()))
return false;
// check if an old value is changed
if(newMatrixTriplet[i].value() != oldMatrixTriplet[i].value()){
newValues.push_back((c_float) newMatrixTriplet[i].value());
newIndices.push_back((c_int) i);
}
}
return true;
}
return false;
}