Skip to content

Commit f5bec2d

Browse files
committed
added: PrecompileTools to reduce package latency
1 parent 61e8c45 commit f5bec2d

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
99
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
1010
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1111
OSQP = "ab2f91bb-94b4-55e3-9ba0-7f65df51de79"
12+
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
1213

1314
[compat]
1415
ControlSystemsBase = "1"

example/juMPC.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ end
131131

132132
@time u_data, y_data, r_data, d_data = test_mpc(linModel4, nmpc)
133133

134+
#=
134135
using PlotThemes, Plots
135136
#theme(:default)
136137
theme(:dark)
@@ -151,4 +152,5 @@ pd = plot(0:N-1,d_data[1,:],label=raw"$d_1$")
151152
display(pd)
152153
display(pu)
153154
display(py)
155+
=#
154156

src/ModelPredictiveControl.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module ModelPredictiveControl
22

3+
using PrecompileTools
4+
35
using LinearAlgebra
46
using ControlSystemsBase
57
using JuMP
@@ -15,4 +17,14 @@ include("sim_model.jl")
1517
include("state_estim.jl")
1618
include("predictive_control.jl")
1719

20+
@setup_workload begin
21+
# Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the size of the
22+
# precompile file and potentially make loading faster.
23+
@compile_workload begin
24+
# all calls in this block will be precompiled, regardless of whether
25+
# they belong to your package or not (on Julia 1.8 and higher)
26+
include("precompile_calls.jl")
27+
end
28+
end
29+
1830
end

src/precompile_calls.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
sys = [ tf(1.90, [18, 1]) tf(1.90, [18, 1]);
2+
tf(-0.74,[8, 1]) tf(0.74, [8, 1]) ]
3+
Ts = 4.0
4+
model = setop!(LinModel(sys, Ts), uop=[10, 10], yop=[50, 30])
5+
y = model()
6+
7+
mpc_im = setconstraint!(LinMPC(InternalModel(model)), ŷmin=[45, -Inf])
8+
initstate!(mpc_im, model.uop, y)
9+
u = mpc_im([50, 30], ym=y)
10+
updatestate!(mpc_im, u, y)
11+
mpc_kf = setconstraint!(LinMPC(KalmanFilter(model)), ŷmin=[45, -Inf])
12+
initstate!(mpc_kf, model.uop, model())
13+
u = mpc_kf([50, 30])
14+
updatestate!(mpc_kf, u, y)
15+
mpc_ukf = setconstraint!(LinMPC(UnscentedKalmanFilter(model)), ŷmin=[45, -Inf])
16+
initstate!(mpc_ukf, model.uop, model())
17+
u = mpc_ukf([50, 30])
18+
updatestate!(mpc_ukf, u, y)
19+
mpc_skf = setconstraint!(LinMPC(SteadyKalmanFilter(model)), ŷmin=[45, -Inf])
20+
initstate!(mpc_skf, model.uop, model())
21+
u = mpc_skf([50, 30])
22+
updatestate!(mpc_skf, u, y)
23+
updatestate!(model, u)
24+
25+
f(x,u,_) = model.A*x + model.Bu*u
26+
h(x,_) = model.C*x
27+
28+
nlmodel = setop!(NonLinModel(f, h, Ts, 2, 2, 2))
29+
y = nlmodel()
30+
nmpc_im = setconstraint!(NonLinMPC(InternalModel(nlmodel)), ŷmin=[45, -Inf])
31+
initstate!(nmpc_im, nlmodel.uop, y)
32+
u = nmpc_im([50, 30], ym=y)
33+
updatestate!(nmpc_im, u, y)
34+
nmpc_ukf = setconstraint!(NonLinMPC(UnscentedKalmanFilter(nlmodel)), ŷmin=[45, -Inf])
35+
initstate!(nmpc_ukf, nlmodel.uop, y)
36+
u = nmpc_ukf([50, 30])
37+
updatestate!(nmpc_ukf, u, y)

0 commit comments

Comments
 (0)