-
Notifications
You must be signed in to change notification settings - Fork 0
/
qp_solver.h
325 lines (287 loc) · 9.75 KB
/
qp_solver.h
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
//
// use osqp, this is unused in ORU because we choose quadprog in final
//
#ifndef TEST_QP_SOLVER_H
#define TEST_QP_SOLVER_H
#include <numeric>
#include "osqp.h"
#include <vector>
#include <cassert>
#include <chrono>
using namespace std;
class qp_solver{
c_int dim;
c_float w_L2;
OSQPWorkspace *work;
OSQPSettings *settings;
OSQPData *data;
vector<c_float> P_x;
vector<c_int> P_i;
vector<c_int> P_p;
vector<c_float> A_x;
vector<c_int> A_i;
vector<c_int> A_p;
vector<c_float> q;
vector<c_float> l;
vector<c_float> u;
inline void destroy(){
if (data) {
if (data->A) c_free(data->A);
if (data->P) c_free(data->P);
c_free(data);
}
if (settings) c_free(settings);
}
template<typename FLOAT>
inline void init_all(const vector<FLOAT>& w, const vector<FLOAT>& H);
inline void init_all(const vector<float>& w, const vector<vector<double>>& H){
/*
* csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p)
* in OSQP,
* QP problem's "P" and "A" are represented as csc format sparse matrix
* for more detail about transform dense_matrix into csc_matrix
* see https://en.wikipedia.org/wiki/Sparse_matrix
*
* The OSQP example can be seen from here:
* https://osqp.org/docs/examples/setup-and-solve.html
*/
assert(!w.empty());
dim=w.size();
w_L2=0;
for (float i : w) {
w_L2+=i*i;
}
P_x=vector<c_float>(dim, 1.0);
c_int P_nnz = dim;
P_i=vector<c_int>(dim);
iota(P_i.begin(), P_i.end(), 0); //{0, 1, 2, ..., dim-1}
P_p=vector<c_int>(dim+1);
iota(P_p.begin(), P_p.end(), 0); //{0, 1, 2, ..., dim-1}
q=vector<c_float>(w.begin(), w.end());
for (c_float &qi:q) {
qi=-qi;
}
A_x=vector<c_float>((2+H.size())*dim);
c_int A_x_idx=0, h_idx=0;
while(A_x_idx<A_x.size()){
A_x[A_x_idx++]=1.0;
for (int i = 0; i < H.size(); ++i) {
A_x[A_x_idx++]=H[i][h_idx];
}
h_idx++;
A_x[A_x_idx++]=1.0;
}
c_int A_nnz = A_x.size();
A_i=vector<c_int>((2+H.size())*dim);
c_int A_i_idx=0, h_iidx=0;
while(A_i_idx<A_i.size()){
A_i[A_i_idx++]=0;
for (int i = 0; i < H.size(); ++i) {
A_i[A_i_idx++]=i+1;
}
A_i[A_i_idx++]=h_iidx+1+H.size();
++h_iidx;
}
A_p=vector<c_int>(dim+1);
c_int A_p_idx=0, h_pidx=0;
while(A_p_idx+1<A_p.size()){
A_p[A_p_idx+1]=A_p[A_p_idx]+2+H.size();
++A_p_idx;
}
l=vector<c_float>(dim+1+H.size(), 0);
l[0]=1.0;
for (int j = 0; j <H.size() ; ++j) {
l[j+1]=-INFINITY;
}
u=vector<c_float>(dim+1+H.size(), 1.0);
for (int j = 0; j <H.size() ; ++j) {
u[j+1]=0;
}
settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
data = (OSQPData *)c_malloc(sizeof(OSQPData));
data->n = dim;
data->m = dim+1+H.size(); //constrain number
data->P = csc_matrix(data->n, data->n, P_nnz, P_x.data(), P_i.data(), P_p.data());
data->q = q.data();
data->A = csc_matrix(data->m, data->n, A_nnz, A_x.data(), A_i.data(), A_p.data());
data->l = l.data();
data->u = u.data();
if (settings) {
osqp_set_default_settings(settings);
settings->verbose=false; // keep quiet
settings->alpha = 1.0; // Change alpha parameter
}
// Setup workspace
osqp_setup(&work, data, settings);
}
public:
qp_solver() = default;
template<typename INT>
qp_solver(INT dim);
qp_solver(const vector<double>& l_constrain, const vector<vector<double>>& H, double &ret){
// a minimize solver
dim=l_constrain.size();
P_x=vector<c_float>(dim, 0.0);
c_int P_nnz = dim;
P_i=vector<c_int>(dim);
iota(P_i.begin(), P_i.end(), 0); //{0, 1, 2, ..., dim-1}
P_p=vector<c_int>(dim+1);
iota(P_p.begin(), P_p.end(), 0); //{0, 1, 2, ..., dim-1}
q=vector<c_float>(l_constrain.begin(), l_constrain.end());
A_x=vector<c_float>((2+H.size())*dim);
c_int A_x_idx=0, h_idx=0;
while(A_x_idx<A_x.size()){
A_x[A_x_idx++]=1.0;
for (int i = 0; i < H.size(); ++i) {
A_x[A_x_idx++]=H[i][h_idx];
}
h_idx++;
A_x[A_x_idx++]=1.0;
}
c_int A_nnz = A_x.size();
A_i=vector<c_int>((2+H.size())*dim);
c_int A_i_idx=0, h_iidx=0;
while(A_i_idx<A_i.size()){
A_i[A_i_idx++]=0;
for (int i = 0; i < H.size(); ++i) {
A_i[A_i_idx++]=i+1;
}
A_i[A_i_idx++]=h_iidx+1+H.size();
++h_iidx;
}
A_p=vector<c_int>(dim+1);
c_int A_p_idx=0, h_pidx=0;
while(A_p_idx+1<A_p.size()){
A_p[A_p_idx+1]=A_p[A_p_idx]+2+H.size();
++A_p_idx;
}
l=vector<c_float>(dim+1+H.size(), 0);
l[0]=1.0;
for (int j = 0; j <H.size() ; ++j) {
l[j+1]=-INFINITY;
}
u=vector<c_float>(dim+1+H.size(), 1.0);
for (int j = 0; j <H.size() ; ++j) {
u[j+1]=0;
}
settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
data = (OSQPData *)c_malloc(sizeof(OSQPData));
data->n = dim;
data->m = dim+1+H.size(); //constrain number
data->P = csc_matrix(data->n, data->n, P_nnz, P_x.data(), P_i.data(), P_p.data());
data->q = q.data();
data->A = csc_matrix(data->m, data->n, A_nnz, A_x.data(), A_i.data(), A_p.data());
data->l = l.data();
data->u = u.data();
if (settings) {
osqp_set_default_settings(settings);
settings->verbose=false; // keep quiet
settings->alpha = 1.0; // Change alpha parameter
}
// Setup workspace
osqp_setup(&work, data, settings);
int flag = osqp_solve(work);
if(flag!=0){
ret= INFINITY;
}
ret=work->info->obj_val+w_L2;
ret>=0?sqrt(ret):0;
}
qp_solver(qp_solver &other){
if(&other!=this){
dim=other.dim;
c_int P_nnz = dim;
w_L2=other.w_L2;
P_x=other.P_x;
P_i=other.P_i;
P_p=other.P_p;
A_x=other.A_x;
c_int A_nnz = A_x.size();
A_i=other.A_i;
A_p=other.A_p;
q=other.q;
l=other.l;
u=other.u;
settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
data = (OSQPData *)c_malloc(sizeof(OSQPData));
data->n = dim;
data->m = dim+2; //constrain number
data->P = csc_matrix(data->n, data->n, P_nnz, P_x.data(), P_i.data(), P_p.data());
data->q = q.data();
data->A = csc_matrix(data->m, data->n, A_nnz, A_x.data(), A_i.data(), A_p.data());
data->l = l.data();
data->u = u.data();
if (settings) {
osqp_set_default_settings(settings);
settings->verbose=false; // keep quiet
settings->alpha = 1.0; // Change alpha parameter
}
// Setup workspace
osqp_setup(&work, data, settings);
}
}
template<typename FLOAT>
explicit qp_solver(const vector<FLOAT>& w);
template<typename FLOAT>
qp_solver(const vector<FLOAT>& w, const vector<FLOAT>& h_ij);
qp_solver(const vector<float>& w, const vector<vector<double>>& H){
init_all(w, H);
}
template<typename FLOAT>
inline void update_w(const vector<FLOAT> &w);
template<typename FLOAT>
inline void update_h(const vector<FLOAT> &h);
template<typename FLOAT>
inline FLOAT solve_update_h(const vector<FLOAT> &h);
template<typename FLOAT>
inline void update_w_h(const vector<FLOAT> &w, const vector<FLOAT>&h_ij);
template<typename FLOAT>
inline FLOAT update_w_h_solve(const vector<FLOAT> &w, const vector<FLOAT> &h);
inline c_float qp_solve(){
int flag = osqp_solve(work);
if(flag!=0){
return INFINITY;
}
c_float ret=2*work->info->obj_val+w_L2;
return ret>=0?sqrt(ret):0;
}
qp_solver& operator=(qp_solver &&other) noexcept {
if(&other!=this){
dim=other.dim;
c_int P_nnz = dim;
w_L2=other.w_L2;
P_x=other.P_x;
P_i=other.P_i;
P_p=other.P_p;
A_x=other.A_x;
c_int A_nnz = A_x.size();
A_i=other.A_i;
A_p=other.A_p;
q=other.q;
l=other.l;
u=other.u;
settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));
data = (OSQPData *)c_malloc(sizeof(OSQPData));
data->n = dim;
data->m = dim+2; //constrain number
data->P = csc_matrix(data->n, data->n, P_nnz, P_x.data(), P_i.data(), P_p.data());
data->q = q.data();
data->A = csc_matrix(data->m, data->n, A_nnz, A_x.data(), A_i.data(), A_p.data());
data->l = l.data();
data->u = u.data();
if (settings) {
osqp_set_default_settings(settings);
settings->verbose=false; // keep quiet
settings->alpha = 1.0; // Change alpha parameter
}
// Setup workspace
osqp_setup(&work, data, settings);
}
return *this;
}
~qp_solver(){
destroy();
}
};
#include "qp_solver_impl.h"
#endif //TEST_QP_SOLVER_H