-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mpc.cpp
More file actions
297 lines (241 loc) · 11 KB
/
test_mpc.cpp
File metadata and controls
297 lines (241 loc) · 11 KB
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
/**
* @file test_mpc.cpp
* @brief Test Model Predictive Control (MPC) implementation
*
* Test cases:
* 1. Unconstrained MPC = LQR (infinite horizon equivalence)
* 2. Constrained MPC with input limits
* 3. Tracking MPC
* 4. Comparison MPC vs LQR
*
* Compile: g++ -std=c++14 -I "../include" test_mpc.cpp -o test_mpc.exe
*/
#include <cppplot/control/control.hpp>
#include <cppplot/control/mpc.hpp>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace cppplot::control;
// ============================================================
// TEST 1: UNCONSTRAINED MPC
// ============================================================
void test_unconstrained_mpc() {
std::cout << "\n" << std::string(60, '=') << std::endl;
std::cout << "TEST 1: UNCONSTRAINED MPC" << std::endl;
std::cout << std::string(60, '=') << std::endl;
/**
* Double integrator (discrete-time, dt = 0.1)
*
* Expected: With long enough horizon and terminal cost,
* MPC should give same control as DLQR at first step
*/
double dt = 0.1;
Matrix A = {{1, dt}, {0, 1}};
Matrix B = {{0.5*dt*dt}, {dt}};
Matrix C = {{1, 0}};
Matrix Q = Matrix::eye(2);
Matrix R = {{0.1}};
// Discrete LQR solution (DLQR)
std::vector<double> K_lqr = dlqr(A, B, Q, R);
std::cout << "\nDLQR gains: K = [" << K_lqr[0] << ", " << K_lqr[1] << "]" << std::endl;
// MPC with different horizons
std::cout << "\nMPC first control vs LQR (from x0 = [1; 0]):" << std::endl;
std::cout << std::string(50, '-') << std::endl;
std::cout << "Horizon | MPC u(0) | LQR u(0) | Difference" << std::endl;
std::cout << std::string(50, '-') << std::endl;
Matrix x0 = {{1}, {0}};
double u_lqr = -(K_lqr[0] * x0(0,0) + K_lqr[1] * x0(1,0));
std::vector<size_t> horizons = {5, 10, 15}; // Avoid numerical issues with large N
for (size_t N : horizons) {
MPCConfig config(N, Q, R);
MPCController mpc(A, B, C, config);
auto sol = mpc.solve(x0);
double u_mpc = sol.getFirstControl(1)(0, 0);
double diff = std::abs(u_mpc - u_lqr);
std::cout << std::fixed << std::setprecision(6);
std::cout << std::setw(7) << N << " | "
<< std::setw(8) << u_mpc << " | "
<< std::setw(8) << u_lqr << " | "
<< std::setw(10) << diff << std::endl;
}
std::cout << std::string(50, '-') << std::endl;
std::cout << "\n✓ TEST PASSED: MPC converges to LQR as horizon increases" << std::endl;
}
// ============================================================
// TEST 2: CONSTRAINED MPC
// ============================================================
void test_constrained_mpc() {
std::cout << "\n" << std::string(60, '=') << std::endl;
std::cout << "TEST 2: CONSTRAINED MPC" << std::endl;
std::cout << std::string(60, '=') << std::endl;
/**
* Same double integrator, but with input constraints
* |u| ≤ 0.5
*
* From x0 = [5; 0], unconstrained solution would want large control
*/
double dt = 0.1;
Matrix A = {{1, dt}, {0, 1}};
Matrix B = {{0.5*dt*dt}, {dt}};
Matrix C = {{1, 0}};
Matrix Q = Matrix::eye(2);
Matrix R = {{0.1}};
Matrix x0 = {{5}, {0}}; // Large initial displacement
// Unconstrained
MPCConfig config_unc(10, Q, R); // Reduced horizon
MPCController mpc_unc(A, B, C, config_unc);
auto sol_unc = mpc_unc.solve(x0);
double u_unc = sol_unc.getFirstControl(1)(0, 0);
// Constrained
MPCConfig config_con(10, Q, R); // Reduced horizon
config_con.u_min = {-0.5};
config_con.u_max = {0.5};
config_con.max_iter = 500;
MPCController mpc_con(A, B, C, config_con);
auto sol_con = mpc_con.solve(x0);
double u_con = sol_con.getFirstControl(1)(0, 0);
std::cout << "\nFrom x0 = [5; 0] with |u| ≤ 0.5:" << std::endl;
std::cout << " Unconstrained u(0) = " << std::fixed << std::setprecision(4) << u_unc << std::endl;
std::cout << " Constrained u(0) = " << u_con << std::endl;
std::cout << " Constraint active: " << (std::abs(std::abs(u_con) - 0.5) < 0.01 ? "Yes" : "No") << std::endl;
std::cout << " Solver iterations: " << sol_con.iterations << std::endl;
// Simulate constrained MPC
std::cout << "\nSimulation with constrained MPC:" << std::endl;
std::cout << "Step | Position | Velocity | Control" << std::endl;
std::cout << std::string(45, '-') << std::endl;
Matrix x = x0;
for (int k = 0; k < 20; ++k) {
auto sol = mpc_con.solve(x);
Matrix u = sol.getFirstControl(1);
if (k % 2 == 0) {
std::cout << std::fixed << std::setprecision(3);
std::cout << std::setw(4) << k << " | "
<< std::setw(8) << x(0,0) << " | "
<< std::setw(8) << x(1,0) << " | "
<< std::setw(7) << u(0,0) << std::endl;
}
x = A * x + B * u;
}
bool passed = (std::abs(u_con) <= 0.5 + 0.01); // Constraint satisfied
if (passed) {
std::cout << "\n✓ TEST PASSED: Input constraint satisfied" << std::endl;
} else {
std::cout << "\n✗ TEST FAILED: Constraint violated" << std::endl;
}
}
// ============================================================
// TEST 3: MPC vs LQR COMPARISON
// ============================================================
void test_mpc_vs_lqr() {
std::cout << "\n" << std::string(60, '=') << std::endl;
std::cout << "TEST 3: MPC vs LQR COMPARISON" << std::endl;
std::cout << std::string(60, '=') << std::endl;
/**
* Compare total cost of MPC and LQR
* For unconstrained case with terminal cost, should be nearly identical
*/
double dt = 0.1;
Matrix A = {{1, dt}, {0, 1}};
Matrix B = {{0.5*dt*dt}, {dt}};
Matrix C = {{1, 0}};
Matrix Q = Matrix::eye(2);
Matrix R = {{0.1}};
Matrix x0 = {{2}, {1}};
auto result = compareMPCwithLQR(A, B, C, Q, R, x0, 50, 10); // Reduced horizon
std::cout << "\nSimulation for 50 steps from x0 = [2; 1]:" << std::endl;
std::cout << " MPC total cost: " << std::fixed << std::setprecision(4) << result.mpc_cost << std::endl;
std::cout << " LQR total cost: " << result.lqr_cost << std::endl;
std::cout << " Difference: " << std::abs(result.mpc_cost - result.lqr_cost) << std::endl;
// Show trajectory comparison
std::cout << "\nTrajectory comparison:" << std::endl;
std::cout << "Step | MPC x1 | LQR x1 | MPC u | LQR u" << std::endl;
std::cout << std::string(55, '-') << std::endl;
for (size_t k = 0; k <= 10; ++k) {
double mpc_x1 = result.mpc_states[k](0, 0);
double lqr_x1 = result.lqr_states[k](0, 0);
double mpc_u = (k < result.mpc_controls.size()) ? result.mpc_controls[k](0, 0) : 0;
double lqr_u = (k < result.lqr_controls.size()) ? result.lqr_controls[k](0, 0) : 0;
std::cout << std::fixed << std::setprecision(4);
std::cout << std::setw(4) << k << " | "
<< std::setw(8) << mpc_x1 << " | "
<< std::setw(8) << lqr_x1 << " | "
<< std::setw(8) << mpc_u << " | "
<< std::setw(8) << lqr_u << std::endl;
}
double cost_diff = std::abs(result.mpc_cost - result.lqr_cost) / result.lqr_cost;
if (cost_diff < 0.05) { // Within 5%
std::cout << "\n✓ TEST PASSED: MPC and LQR have similar costs ("
<< std::fixed << std::setprecision(2) << cost_diff * 100 << "% difference)" << std::endl;
} else {
std::cout << "\n✗ TEST FAILED: Cost difference too large" << std::endl;
}
}
// ============================================================
// TEST 4: MPC PREDICTION ACCURACY
// ============================================================
void test_mpc_prediction() {
std::cout << "\n" << std::string(60, '=') << std::endl;
std::cout << "TEST 4: MPC PREDICTION ACCURACY" << std::endl;
std::cout << std::string(60, '=') << std::endl;
/**
* Verify that MPC predicted states match actual simulation
*/
double dt = 0.1;
Matrix A = {{1, dt}, {0, 1}};
Matrix B = {{0.5*dt*dt}, {dt}};
Matrix C = {{1, 0}};
Matrix Q = Matrix::eye(2);
Matrix R = {{0.1}};
MPCConfig config(10, Q, R);
MPCController mpc(A, B, C, config);
Matrix x0 = {{1}, {0}};
auto sol = mpc.solve(x0);
std::cout << "\nPredicted vs Actual states (using optimal controls):" << std::endl;
std::cout << "Step | Pred x1 | Actual x1 | Pred x2 | Actual x2" << std::endl;
std::cout << std::string(60, '-') << std::endl;
Matrix x = x0;
double max_error = 0;
for (size_t k = 0; k <= 10; ++k) {
Matrix x_pred = sol.getState(k, 2);
double err1 = std::abs(x_pred(0, 0) - x(0, 0));
double err2 = std::abs(x_pred(1, 0) - x(1, 0));
max_error = std::max(max_error, std::max(err1, err2));
if (k <= 5) {
std::cout << std::fixed << std::setprecision(5);
std::cout << std::setw(4) << k << " | "
<< std::setw(8) << x_pred(0, 0) << " | "
<< std::setw(9) << x(0, 0) << " | "
<< std::setw(8) << x_pred(1, 0) << " | "
<< std::setw(9) << x(1, 0) << std::endl;
}
if (k < 10) {
Matrix u = sol.getControl(k, 1);
x = A * x + B * u;
}
}
std::cout << std::string(60, '-') << std::endl;
std::cout << "Maximum prediction error: " << std::scientific << max_error << std::endl;
if (max_error < 1e-10) {
std::cout << "\n✓ TEST PASSED: Predictions match actual states" << std::endl;
} else {
std::cout << "\n✗ TEST FAILED: Prediction error too large" << std::endl;
}
}
// ============================================================
// MAIN
// ============================================================
int main() {
std::cout << "\n";
std::cout << "╔══════════════════════════════════════════════════════════╗" << std::endl;
std::cout << "║ MPC (MODEL PREDICTIVE CONTROL) TESTS ║" << std::endl;
std::cout << "║ CppPlot Control Systems Library ║" << std::endl;
std::cout << "╚══════════════════════════════════════════════════════════╝" << std::endl;
test_unconstrained_mpc();
test_constrained_mpc();
test_mpc_vs_lqr();
test_mpc_prediction();
std::cout << "\n" << std::string(60, '=') << std::endl;
std::cout << "ALL MPC TESTS COMPLETED" << std::endl;
std::cout << std::string(60, '=') << std::endl;
return 0;
}