1+ # Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the
2+ # size of the precompile file and potentially make loading faster.
3+ @setup_workload begin
4+
15sys = [
26 tf (1.90 , [1800 , 1 ]) tf (1.90 , [1800 , 1 ]);
37 tf (- 0.74 ,[800 , 1 ]) tf (0.74 , [800 , 1 ])
48]
59Ts = 400.0
6- model = setop! (LinModel (sys, Ts), uop= [10 , 10 ], yop= [50 , 30 ])
7- y = model ()
8-
9- mpc_im = setconstraint! (LinMPC (InternalModel (model)), ymin= [45 , - Inf ])
10- initstate! (mpc_im, model. uop, y)
11- preparestate! (mpc_im, [55 , 30 ])
12- mpc_im. estim ()
13- u = mpc_im ([55 , 30 ])
14- sim! (mpc_im, 2 )
15-
16- transcription = MultipleShooting ()
17- mpc_kf = setconstraint! (LinMPC (KalmanFilter (model); transcription), ymin= [45 , - Inf ])
18- initstate! (mpc_kf, model. uop, model ())
19- preparestate! (mpc_kf, [55 , 30 ])
20- mpc_kf. estim ()
21- u = mpc_kf ([55 , 30 ])
22- sim! (mpc_kf, 2 , [55 , 30 ])
23-
24- mpc_skf = setconstraint! (LinMPC (SteadyKalmanFilter (model)), ymin= [45 , - Inf ])
25- initstate! (mpc_skf, model. uop, model ())
26- preparestate! (mpc_skf, [55 , 30 ])
27- mpc_skf. estim ()
28- u = mpc_skf ([55 , 30 ])
29- sim! (mpc_skf, 2 , [55 , 30 ])
30-
31- mpc_mhe = setconstraint! (LinMPC (MovingHorizonEstimator (model, He= 2 )), ymin= [45 , - Inf ])
32- setconstraint! (mpc_mhe. estim, x̂min= [- 50 ,- 50 ,- 50 ,- 50 ], x̂max= [50 ,50 ,50 ,50 ])
33- initstate! (mpc_mhe, model. uop, model ())
34- preparestate! (mpc_mhe, [55 , 30 ])
35- mpc_mhe. estim ()
36- u = mpc_mhe ([55 , 30 ])
37- sim! (mpc_mhe, 2 , [55 , 30 ])
38-
39- mpc_man = setconstraint! (LinMPC (ManualEstimator (model)), ymin= [45 , - Inf ])
40- initstate! (mpc_man, model. uop, model ())
41- setstate! (mpc_man, ones (4 ))
42- u = mpc_man ([55 , 30 ])
43-
44- nmpc_skf = setconstraint! (NonLinMPC (SteadyKalmanFilter (model), Cwt= Inf ), ymin= [45 , - Inf ])
45- initstate! (nmpc_skf, model. uop, model ())
46- preparestate! (nmpc_skf, [55 , 30 ])
47- nmpc_skf. estim ()
48- u = nmpc_skf ([55 , 30 ])
49- sim! (nmpc_skf, 2 , [55 , 30 ])
50-
51- res = sim! (model, 2 )
52-
53- res_man = SimResult (model, res. U_data, res. Y_data; X_data= res. X_data)
54-
55- exmpc = ExplicitMPC (model)
56- initstate! (exmpc, model. uop, model ())
57- preparestate! (exmpc, [55 , 30 ])
58- exmpc. estim ()
59- u = exmpc ([55 , 30 ])
60- sim! (exmpc, 2 , [55 , 30 ])
61-
10+ sys2 = minreal (ss (sys))
6211function f! (xnext, x, u, _ , p)
6312 A, B, _ = p
6413 mul! (xnext, A , x)
@@ -70,51 +19,106 @@ function h!(y, x, _ , p)
7019 mul! (y, C, x)
7120 return nothing
7221end
73-
74- sys2 = minreal (ss (sys))
75- nlmodel = setop! (
76- NonLinModel (f!, h!, Ts, 2 , 2 , 2 , solver= RungeKutta (4 ), p= (sys2. A, sys2. B, sys2. C)),
77- uop= [10 , 10 ], yop= [50 , 30 ]
78- )
79- y = nlmodel ()
80-
81- transcription = MultipleShooting (f_threads= true )
82- nmpc_ukf = setconstraint! (NonLinMPC (
83- UnscentedKalmanFilter (nlmodel); Hp= 10 , transcription, Cwt= 1e3 ), ymin= [45 , - Inf ]
84- )
85- initstate! (nmpc_ukf, nlmodel. uop, y)
86- preparestate! (nmpc_ukf, [55 , 30 ])
87- u = nmpc_ukf ([55 , 30 ])
88- sim! (nmpc_ukf, 2 , [55 , 30 ])
89-
90- nmpc_ekf = setconstraint! (NonLinMPC (ExtendedKalmanFilter (model), Cwt= Inf ), ymin= [45 , - Inf ])
91- initstate! (nmpc_ekf, model. uop, model ())
92- preparestate! (nmpc_ekf, [55 , 30 ])
93- u = nmpc_ekf ([55 , 30 ])
94- sim! (nmpc_ekf, 2 , [55 , 30 ])
95-
96- transcription = TrapezoidalCollocation ()
97- nmpc_mhe = setconstraint! (NonLinMPC (
98- MovingHorizonEstimator (nlmodel, He= 2 ); transcription, Hp= 10 , Cwt= Inf ), ymin= [45 , - Inf ]
99- )
100- setconstraint! (nmpc_mhe. estim, x̂min= [- 50 ,- 50 ,- 50 ,- 50 ], x̂max= [50 ,50 ,50 ,50 ])
101- initstate! (nmpc_mhe, nlmodel. uop, y)
102- preparestate! (nmpc_mhe, [55 , 30 ])
103- u = nmpc_mhe ([55 , 30 ])
104- sim! (nmpc_mhe, 2 , [55 , 30 ])
22+ p = (sys2. A, sys2. B, sys2. C)
10523
10624function JE ( _ , Ŷe, _ , R̂y)
10725 Ŷ = @views Ŷe[3 : end ]
10826 Ȳ = R̂y - Ŷ
10927 return dot (Ȳ, Ȳ)
11028end
11129R̂y = repeat ([55 ; 30 ], 10 )
112- empc = setconstraint! (
113- NonLinMPC (nlmodel, Mwt= [0 , 0 ], Hp= 10 , Cwt= Inf , Ewt= 1 , JE= JE, p= R̂y), ymin= [45 , - Inf ]
114- )
115- preparestate! (empc, [55 , 30 ])
116- u = empc ()
117- sim! (empc, 2 )
118-
119- linearizemodel = linearize (nlmodel)
120- setmodel! (mpc_kf, linearizemodel)
30+
31+ # all calls in this block will be precompiled, regardless of whether
32+ # they belong to your package or not (on Julia 1.8 and higher)
33+ @compile_workload begin
34+
35+ model = setop! (LinModel (sys, Ts), uop= [10 , 10 ], yop= [50 , 30 ])
36+ y = model ()
37+
38+ mpc_im = setconstraint! (LinMPC (InternalModel (model)), ymin= [45 , - Inf ])
39+ initstate! (mpc_im, model. uop, y)
40+ preparestate! (mpc_im, [55 , 30 ])
41+ mpc_im. estim ()
42+ u = mpc_im ([55 , 30 ])
43+ sim! (mpc_im, 2 )
44+
45+ mpc_skf = setconstraint! (LinMPC (SteadyKalmanFilter (model)), ymin= [45 , - Inf ])
46+ initstate! (mpc_skf, model. uop, model ())
47+ preparestate! (mpc_skf, [55 , 30 ])
48+ mpc_skf. estim ()
49+ u = mpc_skf ([55 , 30 ])
50+ sim! (mpc_skf, 2 , [55 , 30 ])
51+
52+ transcription = MultipleShooting ()
53+ mpc_kf = setconstraint! (LinMPC (KalmanFilter (model); transcription), ymin= [45 , - Inf ])
54+ initstate! (mpc_kf, model. uop, model ())
55+ preparestate! (mpc_kf, [55 , 30 ])
56+ mpc_kf. estim ()
57+ u = mpc_kf ([55 , 30 ])
58+ sim! (mpc_kf, 2 , [55 , 30 ])
59+
60+ mhe = MovingHorizonEstimator (model, He= 2 , direct= true )
61+ mhe = setconstraint! (mhe, x̂min= [- 50 ,- 50 ,- 50 ,- 50 ], x̂max= [50 ,50 ,50 ,50 ])
62+ initstate! (mhe, model. uop, model ())
63+ preparestate! (mhe, [55 , 30 ])
64+ mhe ()
65+
66+ mpc_man = setconstraint! (LinMPC (ManualEstimator (model)), ymin= [45 , - Inf ])
67+ initstate! (mpc_man, model. uop, model ())
68+ setstate! (mpc_man, ones (4 ))
69+ mpc_man ([55 , 30 ])
70+
71+ nmpc_skf = setconstraint! (NonLinMPC (SteadyKalmanFilter (model), Cwt= Inf ), ymin= [45 , - Inf ])
72+ initstate! (nmpc_skf, model. uop, model ())
73+ preparestate! (nmpc_skf, [55 , 30 ])
74+ nmpc_skf. estim ()
75+ nmpc_skf ([55 , 30 ])
76+
77+ res = sim! (model, 2 )
78+ res_man = SimResult (model, res. U_data, res. Y_data; X_data= res. X_data)
79+
80+ exmpc = ExplicitMPC (model)
81+ initstate! (exmpc, model. uop, model ())
82+ preparestate! (exmpc, [55 , 30 ])
83+ exmpc. estim ()
84+ exmpc ([55 , 30 ])
85+
86+ nlmodel = NonLinModel (f!, h!, Ts, 2 , 2 , 2 ; solver= RungeKutta (4 ), p)
87+ nlmodel = setop! (nlmodel, uop= [10 , 10 ], yop= [50 , 30 ])
88+ y = nlmodel ()
89+
90+ transcription = MultipleShooting (f_threads= true )
91+ nmpc_ukf = setconstraint! (NonLinMPC (
92+ UnscentedKalmanFilter (nlmodel); Hp= 10 , transcription, Cwt= 1e3 ), ymin= [45 , - Inf ]
93+ )
94+ initstate! (nmpc_ukf, nlmodel. uop, y)
95+ preparestate! (nmpc_ukf, [55 , 30 ])
96+ nmpc_ukf ([55 , 30 ])
97+ sim! (nmpc_ukf, 2 , [55 , 30 ])
98+
99+ nmpc_ekf = setconstraint! (NonLinMPC (ExtendedKalmanFilter (model), Cwt= Inf ), ymin= [45 , - Inf ])
100+ initstate! (nmpc_ekf, model. uop, model ())
101+ preparestate! (nmpc_ekf, [55 , 30 ])
102+ nmpc_ekf ([55 , 30 ])
103+
104+ transcription = TrapezoidalCollocation ()
105+ nmpc_mhe = setconstraint! (NonLinMPC (
106+ MovingHorizonEstimator (nlmodel, He= 2 ); transcription, Hp= 10 , Cwt= Inf ), ymin= [45 , - Inf ]
107+ )
108+ setconstraint! (nmpc_mhe. estim, x̂min= [- 50 ,- 50 ,- 50 ,- 50 ], x̂max= [50 ,50 ,50 ,50 ])
109+ initstate! (nmpc_mhe, nlmodel. uop, y)
110+ preparestate! (nmpc_mhe, [55 , 30 ])
111+ nmpc_mhe ([55 , 30 ])
112+
113+ empc = setconstraint! (
114+ NonLinMPC (nlmodel, Mwt= [0 , 0 ], Hp= 10 , Cwt= Inf , Ewt= 1 , JE= JE, p= R̂y), ymin= [45 , - Inf ]
115+ )
116+ preparestate! (empc, [55 , 30 ])
117+ empc ()
118+
119+ linearizemodel = linearize (nlmodel)
120+ setmodel! (mpc_kf, linearizemodel)
121+
122+ end
123+
124+ end # @setup_workload
0 commit comments