-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathMPCUpdateMatricesTest.cpp
321 lines (258 loc) · 9.5 KB
/
MPCUpdateMatricesTest.cpp
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
/**
* @file UpdateMatricesTest.cpp
* @author Giulio Romualdi
* @copyright Released under the terms of the BSD 3-Clause License
* @date 2020
*/
// Catch2
#include <catch2/catch_test_macros.hpp>
// OsqpEigen
#include <OsqpEigen/OsqpEigen.h>
// eigen
#include <Eigen/Dense>
#include <cmath>
#include <fstream>
#include <iostream>
// colors
#define ANSI_TXT_GRN "\033[0;32m"
#define ANSI_TXT_MGT "\033[0;35m" // Magenta
#define ANSI_TXT_DFT "\033[0;0m" // Console default
#define GTEST_BOX "[ cout ] "
#define COUT_GTEST ANSI_TXT_GRN << GTEST_BOX // You could add the Default
#define COUT_GTEST_MGT COUT_GTEST << ANSI_TXT_MGT
#define T 0.1
void setDynamicsMatrices(Eigen::Matrix<c_float, 2, 2>& a,
Eigen::Matrix<c_float, 2, 1>& b,
Eigen::Matrix<c_float, 1, 2>& c,
double t)
{
double omega = 0.1132;
double alpha = 0.5 * sin(2 * M_PI * omega * t);
double beta = 2 - 1 * sin(2 * M_PI * omega * t);
a << alpha, 1, 0, alpha;
b << 0, 1;
c << beta, 0;
}
void setWeightMatrices(Eigen::DiagonalMatrix<c_float, 1>& Q, Eigen::DiagonalMatrix<c_float, 1>& R)
{
Q.diagonal() << 10;
R.diagonal() << 1;
}
void castMPCToQPHessian(const Eigen::DiagonalMatrix<c_float, 1>& Q,
const Eigen::DiagonalMatrix<c_float, 1>& R,
int mpcWindow,
int k,
Eigen::SparseMatrix<c_float>& hessianMatrix)
{
Eigen::Matrix<c_float, 2, 2> a;
Eigen::Matrix<c_float, 2, 1> b;
Eigen::Matrix<c_float, 1, 2> c;
hessianMatrix.resize(2 * (mpcWindow + 1) + 1 * mpcWindow, 2 * (mpcWindow + 1) + 1 * mpcWindow);
// populate hessian matrix
for (int i = 0; i < 2 * (mpcWindow + 1) + 1 * mpcWindow; i++)
{
double t = (k + i) * T;
setDynamicsMatrices(a, b, c, t);
if (i < 2 * (mpcWindow + 1))
{
// here the structure of the matrix c is used!
int pos = i % 2;
float value = c(pos) * Q.diagonal()[0] * c(pos);
if (value != 0)
hessianMatrix.insert(i, i) = value;
} else
{
float value = R.diagonal()[0];
if (value != 0)
hessianMatrix.insert(i, i) = value;
}
}
}
void castMPCToQPGradient(const Eigen::DiagonalMatrix<c_float, 1>& Q,
const Eigen::Matrix<c_float, 1, 1>& yRef,
int mpcWindow,
int k,
Eigen::Matrix<c_float, -1, 1>& gradient)
{
Eigen::Matrix<c_float, 2, 2> a;
Eigen::Matrix<c_float, 2, 1> b;
Eigen::Matrix<c_float, 1, 2> c;
Eigen::Matrix<c_float, 1, 1> Qy_ref;
Qy_ref = Q * (-yRef);
// populate the gradient vector
gradient = Eigen::Matrix<c_float, -1, 1>::Zero(2 * (mpcWindow + 1) + 1 * mpcWindow, 1);
for (int i = 0; i < 2 * (mpcWindow + 1); i++)
{
double t = (k + i) * T;
setDynamicsMatrices(a, b, c, t);
int pos = i % 2;
float value = Qy_ref(0, 0) * c(pos);
gradient(i, 0) = value;
}
}
void castMPCToQPConstraintMatrix(int mpcWindow,
int k,
Eigen::SparseMatrix<c_float>& constraintMatrix)
{
constraintMatrix.resize(2 * (mpcWindow + 1), 2 * (mpcWindow + 1) + 1 * mpcWindow);
// populate linear constraint matrix
for (int i = 0; i < 2 * (mpcWindow + 1); i++)
{
constraintMatrix.insert(i, i) = -1;
}
Eigen::Matrix<c_float, 2, 2> a;
Eigen::Matrix<c_float, 2, 1> b;
Eigen::Matrix<c_float, 1, 2> c;
for (int i = 0; i < mpcWindow; i++)
{
double t = (k + i) * T;
setDynamicsMatrices(a, b, c, t);
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
{
float value = a(j, k);
if (value != 0)
{
constraintMatrix.insert(2 * (i + 1) + j, 2 * i + k) = value;
}
}
}
for (int i = 0; i < mpcWindow; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 1; k++)
{
// b is constant
float value = b(j, k);
if (value != 0)
{
constraintMatrix.insert(2 * (i + 1) + j, 1 * i + k + 2 * (mpcWindow + 1))
= value;
}
}
}
void castMPCToQPConstraintVectors(const Eigen::Matrix<c_float, 2, 1>& x0,
int mpcWindow,
Eigen::Matrix<c_float, -1, 1>& lowerBound,
Eigen::Matrix<c_float, -1, 1>& upperBound)
{
// evaluate the lower and the upper equality vectors
lowerBound = Eigen::Matrix<c_float, -1, -1>::Zero(2 * (mpcWindow + 1), 1);
lowerBound.block(0, 0, 2, 1) = -x0;
upperBound = lowerBound;
}
bool updateHessianMatrix(OsqpEigen::Solver& solver,
const Eigen::DiagonalMatrix<c_float, 1>& Q,
const Eigen::DiagonalMatrix<c_float, 1>& R,
int mpcWindow,
int k)
{
Eigen::SparseMatrix<c_float> hessianMatrix;
castMPCToQPHessian(Q, R, mpcWindow, k, hessianMatrix);
if (!solver.updateHessianMatrix(hessianMatrix))
return false;
return true;
}
bool updateLinearConstraintsMatrix(OsqpEigen::Solver& solver, int mpcWindow, int k)
{
Eigen::SparseMatrix<c_float> constraintMatrix;
castMPCToQPConstraintMatrix(mpcWindow, k, constraintMatrix);
if (!solver.updateLinearConstraintsMatrix(constraintMatrix))
return false;
return true;
}
void updateConstraintVectors(const Eigen::Matrix<c_float, 2, 1>& x0,
Eigen::Matrix<c_float, -1, 1>& lowerBound,
Eigen::Matrix<c_float, -1, 1>& upperBound)
{
lowerBound.block(0, 0, 2, 1) = -x0;
upperBound.block(0, 0, 2, 1) = -x0;
}
TEST_CASE("MPCTest Update matrices")
{
// open the ofstream
std::ofstream dataStream;
dataStream.open("output.txt");
// set the preview window
int mpcWindow = 100;
// allocate the dynamics matrices
Eigen::Matrix<c_float, 2, 2> a;
Eigen::Matrix<c_float, 2, 1> b;
Eigen::Matrix<c_float, 1, 2> c;
// allocate the weight matrices
Eigen::DiagonalMatrix<c_float, 1> Q;
Eigen::DiagonalMatrix<c_float, 1> R;
// allocate the initial and the reference state space
Eigen::Matrix<c_float, 2, 1> x0;
Eigen::Matrix<c_float, 1, 1> yRef;
Eigen::Matrix<c_float, 1, 1> y;
// allocate QP problem matrices and vectors
Eigen::SparseMatrix<c_float> hessian;
Eigen::Matrix<c_float, -1, 1> gradient;
Eigen::SparseMatrix<c_float> linearMatrix;
Eigen::Matrix<c_float, -1, 1> lowerBound;
Eigen::Matrix<c_float, -1, 1> upperBound;
// set the initial and the desired states
x0 << 0, 0;
yRef << 1;
// set MPC problem quantities
setWeightMatrices(Q, R);
// cast the MPC problem as QP problem
castMPCToQPHessian(Q, R, mpcWindow, 0, hessian);
castMPCToQPGradient(Q, yRef, mpcWindow, 0, gradient);
castMPCToQPConstraintMatrix(mpcWindow, 0, linearMatrix);
castMPCToQPConstraintVectors(x0, mpcWindow, lowerBound, upperBound);
// instantiate the solver
OsqpEigen::Solver solver;
// settings
solver.settings()->setVerbosity(false);
solver.settings()->setWarmStart(true);
// set the initial data of the QP solver
solver.data()->setNumberOfVariables(2 * (mpcWindow + 1) + 1 * mpcWindow);
solver.data()->setNumberOfConstraints(2 * (mpcWindow + 1));
REQUIRE(solver.data()->setHessianMatrix(hessian));
REQUIRE(solver.data()->setGradient(gradient));
REQUIRE(solver.data()->setLinearConstraintsMatrix(linearMatrix));
REQUIRE(solver.data()->setLowerBound(lowerBound));
REQUIRE(solver.data()->setUpperBound(upperBound));
// instantiate the solver
REQUIRE(solver.initSolver());
// controller input and QPSolution vector
Eigen::Matrix<c_float, -1, 1> ctr;
Eigen::Matrix<c_float, -1, 1> QPSolution;
// number of iteration steps
int numberOfSteps = 50;
// profiling quantities
clock_t startTime, endTime;
double averageTime = 0;
for (int i = 0; i < numberOfSteps; i++)
{
startTime = clock();
setDynamicsMatrices(a, b, c, i * T);
// update the constraint bound
REQUIRE(updateHessianMatrix(solver, Q, R, mpcWindow, i));
REQUIRE(updateLinearConstraintsMatrix(solver, mpcWindow, i));
castMPCToQPGradient(Q, yRef, mpcWindow, i, gradient);
REQUIRE(solver.updateGradient(gradient));
updateConstraintVectors(x0, lowerBound, upperBound);
REQUIRE(solver.updateBounds(lowerBound, upperBound));
// solve the QP problem
REQUIRE(solver.solveProblem() == OsqpEigen::ErrorExitFlag::NoError);
// get the controller input
QPSolution = solver.getSolution();
ctr = QPSolution.block(2 * (mpcWindow + 1), 0, 1, 1);
// save data into file
auto x0Data = x0.data();
for (int j = 0; j < 2; j++)
dataStream << x0Data[j] << " ";
dataStream << std::endl;
// propagate the model
x0 = a * x0 + b * ctr;
y = c * x0;
endTime = clock();
averageTime += static_cast<double>(endTime - startTime) / CLOCKS_PER_SEC;
}
// close the stream
dataStream.close();
std::cout << COUT_GTEST_MGT << "Average time = " << averageTime / numberOfSteps << " seconds."
<< ANSI_TXT_DFT << std::endl;
}