1- # ---------------------- Unit tests -------------------------------------------------------
2- const UNIT_MPC = SUITE[" unit tests" ][" PredictiveController" ]
1+ # -----------------------------------------------------------------------------------------
2+ # ---------------------- UNIT TESTS -------------------------------------------------------
3+ # -----------------------------------------------------------------------------------------
4+ const UNIT_MPC = SUITE[" UNIT TESTS" ][" PredictiveController" ]
35
46linmpc_ss = LinMPC (
57 linmodel, transcription= SingleShooting (),
@@ -10,7 +12,7 @@ linmpc_ms = LinMPC(
1012 Mwt= [1 , 1 ], Nwt= [0.1 , 0.1 ], Lwt= [0.1 , 0.1 ], Hp= 10
1113)
1214
13- samples, evals, seconds = 500 , 1 , 60
15+ samples, evals, seconds = 5000 , 1 , 60
1416UNIT_MPC[" LinMPC" ][" moveinput!" ][" SingleShooting" ] =
1517 @benchmarkable (
1618 moveinput! ($ linmpc_ss, $ y, $ d),
@@ -49,7 +51,7 @@ nmpc_nonlin_ms = NonLinMPC(
4951 Mwt= [1 , 1 ], Nwt= [0.1 , 0.1 ], Lwt= [0.1 , 0.1 ], Hp= 10
5052)
5153
52- samples, evals, seconds = 500 , 1 , 60
54+ samples, evals, seconds = 5000 , 1 , 60
5355UNIT_MPC[" NonLinMPC" ][" moveinput!" ][" LinModel" ][" SingleShooting" ] =
5456 @benchmarkable (
5557 moveinput! ($ nmpc_lin_ss, $ y, $ d),
@@ -75,16 +77,14 @@ UNIT_MPC["NonLinMPC"]["moveinput!"]["NonLinModel"]["MultipleShooting"] =
7577 samples= samples, evals= evals, seconds= seconds
7678 )
7779
80+ # # ----------------------------------------------------------------------------------------
81+ # # ---------------------- CASE STUDIES ----------------------------------------------------
82+ # # ----------------------------------------------------------------------------------------
83+ const CASE_MPC = SUITE[" CASE STUDIES" ][" PredictiveController" ]
7884
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)
8888plant. A[diagind (plant. A)] .- = 0.1 # plant-model mismatch
8989function test_mpc (mpc, plant)
9090 plant. x0 .= 0 ; y = plant ()
@@ -135,7 +135,7 @@ transcription = MultipleShooting()
135135mpc_ipopt_ms = setconstraint! (LinMPC (model; optim, transcription), ymin= [45 , - Inf ])
136136JuMP. unset_time_limit_sec (mpc_ipopt_ms. optim)
137137
138- samples, evals = 500 , 1
138+ samples, evals = 5000 , 1
139139CASE_MPC[" CSTR" ][" LinMPC" ][" Without feedforward" ][" OSQP" ][" SingleShooting" ] =
140140 @benchmarkable (test_mpc ($ mpc_osqp_ss, $ plant);
141141 samples= samples, evals= evals
@@ -158,8 +158,7 @@ CASE_MPC["CSTR"]["LinMPC"]["Without feedforward"]["Ipopt"]["MultipleShooting"] =
158158 )
159159
160160# # ----------------- 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
163162function test_mpc_d (mpc_d, plant)
164163 plant. x0 .= 0 ; y = plant (); d = [20 ]
165164 initstate! (mpc_d, plant. uop, y, d)
@@ -209,7 +208,7 @@ transcription = MultipleShooting()
209208mpc_d_ipopt_ms = setconstraint! (LinMPC (model_d; optim, transcription), ymin= [45 , - Inf ])
210209JuMP. unset_time_limit_sec (mpc_d_ipopt_ms. optim)
211210
212- samples, evals = 500 , 1
211+ samples, evals = 5000 , 1
213212CASE_MPC[" CSTR" ][" LinMPC" ][" With feedforward" ][" OSQP" ][" SingleShooting" ] =
214213 @benchmarkable (test_mpc_d ($ mpc_d_osqp_ss, $ plant);
215214 samples= samples, evals= evals
@@ -233,21 +232,11 @@ CASE_MPC["CSTR"]["LinMPC"]["With feedforward"]["Ipopt"]["MultipleShooting"] =
233232
234233
235234# ----------------- 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
247236σQ = [0.1 , 1.0 ]; σR= [5.0 ]; nint_u= [1 ]; σQint_u= [0.1 ]
248237estim = 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
251240N = 35 ; u = [0.5 ];
252241
253242Hp, Hc, Mwt, Nwt, Cwt = 20 , 2 , [0.5 ], [2.5 ], Inf
@@ -285,7 +274,7 @@ JuMP.unset_time_limit_sec(nmpc_madnlp_ss.optim)
285274# MadNLP_QNopt = MadNLP.QuasiNewtonOptions(; max_history=42)
286275# JuMP.set_attribute(nmpc_madnlp_ms.optim, "quasi_newton_options", MadNLP_QNopt)
287276
288- samples, evals, seconds = 50 , 1 , 15 * 60
277+ samples, evals, seconds = 100 , 1 , 15 * 60
289278CASE_MPC[" Pendulum" ][" NonLinMPC" ][" Noneconomic" ][" Ipopt" ][" SingleShooting" ] =
290279 @benchmarkable (
291280 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"] =
303292 )
304293
305294# ----------------- 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
310298estim2 = UnscentedKalmanFilter (model2; σQ, σR, nint_u, σQint_u, i_ym= [1 ])
311299function JE (UE, ŶE, _ , p)
312300 Ts = p
@@ -336,7 +324,7 @@ JuMP.unset_time_limit_sec(empc_madnlp_ss.optim)
336324
337325# TODO : test EMPC with MadNLP and MultipleShooting, see comment above.
338326
339- samples, evals, seconds = 50 , 1 , 15 * 60
327+ samples, evals, seconds = 100 , 1 , 15 * 60
340328CASE_MPC[" Pendulum" ][" NonLinMPC" ][" Economic" ][" Ipopt" ][" SingleShooting" ] =
341329 @benchmarkable (
342330 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)
388376# TODO : test custom constraints with MadNLP and SingleShooting, see comment above.
389377# TODO : test custom constraints with MadNLP and MultipleShooting, see comment above.
390378
391- samples, evals, seconds = 50 , 1 , 15 * 60
379+ samples, evals, seconds = 100 , 1 , 15 * 60
392380CASE_MPC[" Pendulum" ][" NonLinMPC" ][" Custom constraints" ][" Ipopt" ][" SingleShooting" ] =
393381 @benchmarkable (
394382 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)
458446mpc3_ipopt_ms = setconstraint! (mpc3_ipopt_ms; umin, umax)
459447JuMP. unset_time_limit_sec (mpc3_ipopt_ms. optim)
460448
461- samples, evals = 500 , 1
449+ samples, evals = 5000 , 1
462450CASE_MPC[" Pendulum" ][" LinMPC" ][" Successive linearization" ][" OSQP" ][" SingleShooting" ] =
463451 @benchmarkable (
464452 sim2! ($ mpc3_osqp_ss, $ model, $ N, $ ry, $ plant, $ x_0, $ x̂_0, $ y_step),
0 commit comments