-
Notifications
You must be signed in to change notification settings - Fork 7
/
eigen-qp.hpp
301 lines (250 loc) · 8.53 KB
/
eigen-qp.hpp
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
/*
EigenQP: Fast quadradic programming template library based on Eigen.
From https://github.com/jarredbarber/eigen-QP
MIT License
Copyright (c) 2017 Jarred Barber
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef _EIGEN_QP_H_
#define _EIGEN_QP_H_
#include <Eigen/Dense>
/**
* Solves quadradic programs with equality constraints
* using direct matrix factorization of the KKT system.
*/
namespace EigenQP
{
// Default tolerance levels specialized on types
template<typename t> t defTol();
template<>
inline double defTol<double>() { return 1E-9; }
template<>
inline float defTol<float>() { return 1E-4f; }
/*
* Solver for equality constrained problems.
* The KKT conditions are linear here, so we just
* invert with an LDLT decomposition.
*/
template<typename Scalar, int NVars=-1, int NEq=-1>
class QPEqSolver
{
private:
const int n;
const int m;
static constexpr int NWork =
((NVars == -1) || (NEq == -1)) ? -1 : (NVars+NEq);
Eigen::Matrix<Scalar,NWork,NWork> Z;
Eigen::Matrix<Scalar,NWork,1> C;
public:
QPEqSolver(int n_vars=NVars, int n_const=NEq) : n(n_vars),m(n_const),
Z(n+m,n+m), C(n+m,1)
{
Z.block(n,n,m,m).setZero();
}
void solve(Eigen::Matrix<Scalar,NVars,NVars> &Q, Eigen::Matrix<Scalar,NVars,1> &c,
Eigen::Matrix<Scalar,NEq,NVars> &A, Eigen::Matrix<Scalar,NEq,1> &b,
Eigen::Matrix<Scalar,NVars,1> &x)
{
Z.block(0,0,n,n) = Q;
Z.block(0,n,n,m) = A.adjoint();
Z.block(n,0,m,n) = A;
C.head(n) = -c;
C.tail(m) = b;
x = Z.ldlt().solve(C).head(n);
}
};
/*
* Solver for inequality constrained problems
*
* This uses a predictor-corrector interior point method from
* "Interior-Point Algorithms for Quadratic Programming" by Thomas Kruth
*
* Some small notation changes from Kruth => this code:
*
* G => Q
* g => c
* A => A.adjoint() (i.e., the constraint matrix is transposed)
* lambda => z
*/
template<typename Scalar, int NVars=-1, int NIneq=-1>
class QPIneqSolver
{
typedef Eigen::Matrix<Scalar,NVars,1> PVec;
typedef Eigen::Matrix<Scalar,NIneq,1> DVec; // Dual (i.e., Lagrange multiplier) vector
typedef Eigen::Matrix<Scalar,NVars,NVars> PMat;
private:
// Problem size
const int n;
const int m;
// Work buffers
DVec s;
DVec z;
PVec rd;
DVec rp;
DVec rs;
PVec dx;
DVec ds;
DVec dz;
PVec x;
public:
// Parameters
Scalar tolerance;
int max_iters;
QPIneqSolver(int n_vars=NVars, int n_const=NIneq) : n(n_vars),m(n_const), s(m), z(m), rd(n), rp(m), rs(m), dx(n), ds(m), dz(m)
{
tolerance = defTol<Scalar>();
max_iters = 250;
if (NVars == -1) {
x.resize(n_vars);
}
if (NIneq == -1) {
s.resize(n_const);
z.resize(n_const);
}
}
~QPIneqSolver() {}
void solve(Eigen::Matrix<Scalar,NVars,NVars> &Q, Eigen::Matrix<Scalar,NVars,1> &c,
Eigen::Matrix<Scalar,NIneq,NVars> &A, Eigen::Matrix<Scalar,NIneq,1> &b,
Eigen::Matrix<Scalar,NVars,1> &x_out)
{
const Scalar eta(0.95);
const Scalar eps = tolerance;
// Initialization
s.setOnes();
z.setOnes();
x.setZero();
// Initial residuals. Uses fact that x=0 here.
rd = c - A.adjoint()*z;
rp = s + b;
rs = (s.array()*z.array());
const Scalar ms = Scalar(1.0)/(Scalar)m;
Scalar mu = (Scalar)n/((Scalar)m); // Initial mu based on knowing that s,z are ones.
Scalar alpha;
for (int iter=0; iter < max_iters; iter++)
{
// Precompute decompositions for this iteration
Eigen::LLT<PMat> Gbar = (Q + A.adjoint()*((z.array()/s.array()).matrix().asDiagonal())*A).llt();
for (int ii=0; ii < 2; ii++)
{
// Prediction/correction step
{
auto tmp = (rs.array() - z.array()*rp.array())/s.array();
dx = -Gbar.solve(rd + A.adjoint()*tmp.matrix());
ds = A*dx - rp;
dz.array() = -(rs.array() + z.array()*ds.array())/s.array();
}
// Compute alph,mu
alpha = Scalar(1.0);
for (int jj=0; jj < m; jj++)
{
Scalar a = -z(jj)/dz(jj);
alpha = (a < alpha) && (a > 0) ? a : alpha;
a = -s(jj)/ds(jj);
alpha = (a < alpha) && (a > 0) ? a : alpha;
}
if (ii)
break; // Don't need to compute any more
// Centering
Scalar mu_aff = (s + alpha*ds).dot(z + alpha*dz)*ms;
Scalar sigma = mu_aff / mu;
sigma *= sigma*sigma;
// Corrector residual
rs.array() += ds.array()*dz.array() - sigma*mu;
}
// Step
alpha *= eta; // rescale step size
x += alpha*dx;
s += alpha*ds;
z += alpha*dz;
// Update residuals
rd = Q*x + c - A*z;
rp = s + A.adjoint()*x - b;
rs = (s.array()*z.array());
mu = s.dot(z)*ms;
// Convergence test
if ( (mu < eps) &&
(rd.norm() < eps) &&
(rs.norm() < eps) )
{
break;
}
}
x_out = x;
}
public:
EIGEN_MAKE_ALIGNED_OPERATOR_NEW
};
#if 0
/**
* General QPs with both equality and inequality constraints.
* This doesn't currently work.
*/
template<typename Scalar, int NVars=-1, int NEq=-1, int NIneq=-1>
class QPGenSolver
{
// Static size for work matrix.
static constexpr int NWork =
((NVars == -1) || (NEq == -1) || (NIneq==-1)) ? -1 : (NVars+NEq+2*NIneq);
typedef Eigen::Matrix<Scalar,NVars,1> PVec;
typedef Eigen::Matrix<Scalar,NIneq,1> DVec; // Dual (i.e., Lagrange multiplier) vector
typedef Eigen::Matrix<Scalar,NEq,1> EVec; // Dual (i.e., Lagrange multiplier) vector for equality
typedef Eigen::Matrix<Scalar,NWork,NWork> WorkBuf;
private:
// Problem size
const int n;
const int mi;
const int me;
// Work buffers
DVec s;
DVec z;
EVec y;
PVec rd;
DVec rp;
DVec rs;
EVec ry;
PVec dx;
DVec ds;
DVec dz;
EVec dy;
WorkBuf augSystem;
public:
QPGenSolver(int n_vars=NVars, int n_const_eq=NEq, int n_const_ineq=NIneq)
: n(n_vars),mi(n_const_ineq),me(n_const_eq),
s(mi), z(mi), y(me), rd(n), rp(mi), rs(mi),
ry(me), dx(n), ds(mi), dz(mi), dy(me),
augSystem(2*mi+me+n,2*mi+me+n)
{
}
~QPGenSolver() {}
void solve(Eigen::Matrix<Scalar,NVars,NVars> &Q, Eigen::Matrix<Scalar,NVars,1> &c,
Eigen::Matrix<Scalar,NIneq,NVars> &A, Eigen::Matrix<Scalar,NIneq,1> &b,
Eigen::Matrix<Scalar,NEq,NVars> &E, Eigen::Matrix<Scalar,NEq,1> &f,
Eigen::Matrix<Scalar,NVars,1> &x)
{
}
};
#endif
template<typename Scalar, int NVars, int NIneq>
void quadprog(Eigen::Matrix<Scalar,NVars,NVars> &Q, Eigen::Matrix<Scalar,NVars,1> &c,
Eigen::Matrix<Scalar,NIneq,NVars> &A, Eigen::Matrix<Scalar,NIneq,1> &b,
Eigen::Matrix<Scalar,NVars,1> &x)
{
QPIneqSolver<Scalar,NVars,NIneq> qp(c.size(),b.size());
qp.solve(Q,c,A,b,x);
}
} // End namespace
#endif