1
- # ---------------------- Unit tests -------------------------------------------------------
2
- const UNIT_MPC = SUITE[" unit tests" ][" PredictiveController" ]
1
+ # -----------------------------------------------------------------------------------------
2
+ # ---------------------- UNIT TESTS -------------------------------------------------------
3
+ # -----------------------------------------------------------------------------------------
4
+ const UNIT_MPC = SUITE[" UNIT TESTS" ][" PredictiveController" ]
3
5
4
6
linmpc_ss = LinMPC (
5
7
linmodel, transcription= SingleShooting (),
@@ -10,7 +12,7 @@ linmpc_ms = LinMPC(
10
12
Mwt= [1 , 1 ], Nwt= [0.1 , 0.1 ], Lwt= [0.1 , 0.1 ], Hp= 10
11
13
)
12
14
13
- samples, evals, seconds = 500 , 1 , 60
15
+ samples, evals, seconds = 5000 , 1 , 60
14
16
UNIT_MPC[" LinMPC" ][" moveinput!" ][" SingleShooting" ] =
15
17
@benchmarkable (
16
18
moveinput! ($ linmpc_ss, $ y, $ d),
@@ -49,7 +51,7 @@ nmpc_nonlin_ms = NonLinMPC(
49
51
Mwt= [1 , 1 ], Nwt= [0.1 , 0.1 ], Lwt= [0.1 , 0.1 ], Hp= 10
50
52
)
51
53
52
- samples, evals, seconds = 500 , 1 , 60
54
+ samples, evals, seconds = 5000 , 1 , 60
53
55
UNIT_MPC[" NonLinMPC" ][" moveinput!" ][" LinModel" ][" SingleShooting" ] =
54
56
@benchmarkable (
55
57
moveinput! ($ nmpc_lin_ss, $ y, $ d),
@@ -75,16 +77,14 @@ UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["MultipleShooting"] =
75
77
samples= samples, evals= evals, seconds= seconds
76
78
)
77
79
80
+ # # ----------------------------------------------------------------------------------------
81
+ # # ---------------------- CASE STUDIES ----------------------------------------------------
82
+ # # ----------------------------------------------------------------------------------------
83
+ const CASE_MPC = SUITE[" CASE STUDIES" ][" PredictiveController" ]
78
84
79
- # # ---------------------- Case studies ----------------------------------------------------
80
- const CASE_MPC = SUITE[" case studies" ][" PredictiveController" ]
81
-
82
- # # ----------------- Case study: CSTR without feedforward ------------------------
83
- G = [ tf (1.90 , [18 , 1 ]) tf (1.90 , [18 , 1 ]);
84
- tf (- 0.74 ,[8 , 1 ]) tf (0.74 , [8 , 1 ]) ]
85
- uop, yop = [20 , 20 ], [50 , 30 ]
86
- model = setop! (LinModel (G, 2.0 ); uop, yop)
87
- plant = setop! (LinModel (G, 2.0 ); uop, yop)
85
+ # # ----------------- Case study: CSTR without feedforward ---------------------------------
86
+ model = CSTR_model
87
+ plant = deepcopy (model)
88
88
plant. A[diagind (plant. A)] .- = 0.1 # plant-model mismatch
89
89
function test_mpc (mpc, plant)
90
90
plant. x0 .= 0 ; y = plant ()
@@ -135,7 +135,7 @@ transcription = MultipleShooting()
135
135
mpc_ipopt_ms = setconstraint! (LinMPC (model; optim, transcription), ymin= [45 , - Inf ])
136
136
JuMP. unset_time_limit_sec (mpc_ipopt_ms. optim)
137
137
138
- samples, evals = 500 , 1
138
+ samples, evals = 5000 , 1
139
139
CASE_MPC[" CSTR" ][" LinMPC" ][" Without feedforward" ][" OSQP" ][" SingleShooting" ] =
140
140
@benchmarkable (test_mpc ($ mpc_osqp_ss, $ plant);
141
141
samples= samples, evals= evals
@@ -158,8 +158,7 @@ CASE_MPC["CSTR"]["LinMPC"]["Without feedforward"]["Ipopt"]["MultipleShooting"] =
158
158
)
159
159
160
160
# # ----------------- Case study: CSTR with feedforward -------------------------
161
- model_d = LinModel ([G G[1 : 2 , 2 ]], 2.0 ; i_d= [3 ])
162
- model_d = setop! (model_d; uop, yop, dop= [20 ])
161
+ model_d = CSTR_model_d
163
162
function test_mpc_d (mpc_d, plant)
164
163
plant. x0 .= 0 ; y = plant (); d = [20 ]
165
164
initstate! (mpc_d, plant. uop, y, d)
@@ -209,7 +208,7 @@ transcription = MultipleShooting()
209
208
mpc_d_ipopt_ms = setconstraint! (LinMPC (model_d; optim, transcription), ymin= [45 , - Inf ])
210
209
JuMP. unset_time_limit_sec (mpc_d_ipopt_ms. optim)
211
210
212
- samples, evals = 500 , 1
211
+ samples, evals = 5000 , 1
213
212
CASE_MPC[" CSTR" ][" LinMPC" ][" With feedforward" ][" OSQP" ][" SingleShooting" ] =
214
213
@benchmarkable (test_mpc_d ($ mpc_d_osqp_ss, $ plant);
215
214
samples= samples, evals= evals
@@ -233,21 +232,11 @@ CASE_MPC["CSTR"]["LinMPC"]["With feedforward"]["Ipopt"]["MultipleShooting"] =
233
232
234
233
235
234
# ----------------- Case study: Pendulum noneconomic -----------------------------
236
- function f! (ẋ, x, u, _ , p)
237
- g, L, K, m = p # [m/s²], [m], [kg/s], [kg]
238
- θ, ω = x[1 ], x[2 ] # [rad], [rad/s]
239
- τ = u[1 ] # [Nm]
240
- ẋ[1 ] = ω
241
- ẋ[2 ] = - g/ L* sin (θ) - K/ m* ω + τ/ m/ L^ 2
242
- end
243
- h! (y, x, _ , _ ) = (y[1 ] = 180 / π* x[1 ]) # [°]
244
- p = [9.8 , 0.4 , 1.2 , 0.3 ]
245
- nu = 1 ; nx = 2 ; ny = 1 ; Ts = 0.1
246
- model = NonLinModel (f!, h!, Ts, nu, nx, ny; p)
235
+ model, p = pendulum_model, pendulum_p
247
236
σQ = [0.1 , 1.0 ]; σR= [5.0 ]; nint_u= [1 ]; σQint_u= [0.1 ]
248
237
estim = UnscentedKalmanFilter (model; σQ, σR, nint_u, σQint_u)
249
- p_plant = copy (p); p_plant[ 3 ] = 1.25 * p[ 3 ]
250
- plant = NonLinModel (f!, h!, Ts, nu, nx, ny; p = p_plant)
238
+ plant = deepcopy (model)
239
+ plant. p[ 3 ] = 1.25 * p[ 3 ] # plant-model mismatch
251
240
N = 35 ; u = [0.5 ];
252
241
253
242
Hp, Hc, Mwt, Nwt, Cwt = 20 , 2 , [0.5 ], [2.5 ], Inf
@@ -285,7 +274,7 @@ JuMP.unset_time_limit_sec(nmpc_madnlp_ss.optim)
285
274
# MadNLP_QNopt = MadNLP.QuasiNewtonOptions(; max_history=42)
286
275
# JuMP.set_attribute(nmpc_madnlp_ms.optim, "quasi_newton_options", MadNLP_QNopt)
287
276
288
- samples, evals, seconds = 50 , 1 , 15 * 60
277
+ samples, evals, seconds = 100 , 1 , 15 * 60
289
278
CASE_MPC[" Pendulum" ][" NonLinMPC" ][" Noneconomic" ][" Ipopt" ][" SingleShooting" ] =
290
279
@benchmarkable (
291
280
sim! ($ nmpc_ipopt_ss, $ N, $ ry; plant= $ plant, x_0= $ x_0, x̂_0= $ x̂_0),
@@ -303,10 +292,9 @@ CASE_MPC["Pendulum"]["NonLinMPC"]["Noneconomic"]["MadNLP"]["SingleShooting"] =
303
292
)
304
293
305
294
# ----------------- Case study: Pendulum economic --------------------------------
306
- h2! (y, x, _ , _ ) = (y[1 ] = 180 / π* x[1 ]; y[2 ]= x[2 ])
307
- nu, nx, ny = 1 , 2 , 2
308
- model2 = NonLinModel (f!, h2!, Ts, nu, nx, ny; p)
309
- plant2 = NonLinModel (f!, h2!, Ts, nu, nx, ny; p= p_plant)
295
+ model2, p = pendulum_model2, pendulum_p2
296
+ plant2 = deepcopy (model2)
297
+ plant2. p[3 ] = 1.25 * p[3 ] # plant-model mismatch
310
298
estim2 = UnscentedKalmanFilter (model2; σQ, σR, nint_u, σQint_u, i_ym= [1 ])
311
299
function JE (UE, ŶE, _ , p)
312
300
Ts = p
@@ -336,7 +324,7 @@ JuMP.unset_time_limit_sec(empc_madnlp_ss.optim)
336
324
337
325
# TODO : test EMPC with MadNLP and MultipleShooting, see comment above.
338
326
339
- samples, evals, seconds = 50 , 1 , 15 * 60
327
+ samples, evals, seconds = 100 , 1 , 15 * 60
340
328
CASE_MPC[" Pendulum" ][" NonLinMPC" ][" Economic" ][" Ipopt" ][" SingleShooting" ] =
341
329
@benchmarkable (
342
330
sim! ($ empc_ipopt_ss, $ N, $ ry; plant= $ plant2, x_0= $ x_0, x̂_0= $ x̂_0),
@@ -388,7 +376,7 @@ JuMP.unset_time_limit_sec(nmpc2_ipopt_ms.optim)
388
376
# TODO : test custom constraints with MadNLP and SingleShooting, see comment above.
389
377
# TODO : test custom constraints with MadNLP and MultipleShooting, see comment above.
390
378
391
- samples, evals, seconds = 50 , 1 , 15 * 60
379
+ samples, evals, seconds = 100 , 1 , 15 * 60
392
380
CASE_MPC[" Pendulum" ][" NonLinMPC" ][" Custom constraints" ][" Ipopt" ][" SingleShooting" ] =
393
381
@benchmarkable (
394
382
sim! ($ nmpc2_ipopt_ss, $ N, $ ry; plant= $ plant2, x_0= $ x_0, x̂_0= $ x̂_0),
@@ -458,7 +446,7 @@ mpc3_ipopt_ms = LinMPC(kf; Hp, Hc, Mwt, Nwt, Cwt, optim, transcription)
458
446
mpc3_ipopt_ms = setconstraint! (mpc3_ipopt_ms; umin, umax)
459
447
JuMP. unset_time_limit_sec (mpc3_ipopt_ms. optim)
460
448
461
- samples, evals = 500 , 1
449
+ samples, evals = 5000 , 1
462
450
CASE_MPC[" Pendulum" ][" LinMPC" ][" Successive linearization" ][" OSQP" ][" SingleShooting" ] =
463
451
@benchmarkable (
464
452
sim2! ($ mpc3_osqp_ss, $ model, $ N, $ ry, $ plant, $ x_0, $ x̂_0, $ y_step),
0 commit comments