Repository navigation
Expand file tree
/
Copy pathprecompile.jl
More file actions
111 lines (93 loc) · 3.21 KB
/
Copy pathprecompile.jl
File metadata and controls
111 lines (93 loc) · 3.21 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
# Putting some things in `@setup_workload` instead of `@compile_workload` can reduce the
# size of the precompile file and potentially make loading faster.
@setup_workload begin
sys = [
tf(1.90, [1800, 1]) tf(1.90, [1800, 1]);
tf(-0.74,[800, 1]) tf(0.74, [800, 1])
]
Ts = 400.0
sys2 = minreal(ss(sys))
function f1!(xnext, x, u, _ , p)
A, B, _ = p
mul!(xnext, A , x)
mul!(xnext, B, u, 1, 1)
return nothing
end
function h1!(y, x, _ , p)
_, _, C = p
mul!(y, C, x)
return nothing
end
p = (sys2.A, sys2.B, sys2.C)
function fq_dae!(ẋ, res, x, a, u, _ , _ )
ẋ[1] = -0.005*(x[1] - 0.2*u[1])
res .= (x .- a)
return nothing
end
function h_dae!(y, x, a, _ , _ )
y .= 2 .* x .+ a
end
function JE( _ , Ŷe, _ , R̂y , _ )
Ŷ = @views Ŷe[3:end]
Ȳ = R̂y - Ŷ
return dot(Ȳ, Ȳ)
end
R̂y = repeat([55; 30], 3)
# all calls in this block will be precompiled, regardless of whether
# they belong to your package or not (on Julia 1.8 and higher)
@compile_workload begin
model = setop!(LinModel(sys, Ts), uop=[10, 10], yop=[50, 30])
y = model()
mpc_skf = setconstraint!(LinMPC(SteadyKalmanFilter(model)), ymin=[45, -Inf])
initstate!(mpc_skf, model.uop, model())
preparestate!(mpc_skf, [55, 30])
mpc_skf.estim()
u = mpc_skf([55, 30])
sim!(mpc_skf, 2, [55, 30])
transcription = MultipleShooting()
mpc_kf = setconstraint!(LinMPC(KalmanFilter(model); transcription), ymin=[45, -Inf])
initstate!(mpc_kf, model.uop, model())
preparestate!(mpc_kf, [55, 30])
mpc_kf.estim()
u = mpc_kf([55, 30])
sim!(mpc_kf, 2, [55, 30])
mpc_im = setconstraint!(LinMPC(InternalModel(model)), ymin=[45, -Inf])
initstate!(mpc_im, model.uop, y)
preparestate!(mpc_im, [55, 30])
mpc_im.estim()
u = mpc_im([55, 30])
mhe = MovingHorizonEstimator(model, He=2)
mhe = setconstraint!(mhe, x̂min=[-50,-50,-50,-50], x̂max=[50,50,50,50])
initstate!(mhe, model.uop, model())
preparestate!(mhe, [55, 30])
mhe()
res = sim!(model, 2)
res_man = SimResult(model, res.U_data, res.Y_data; X_data=res.X_data)
exmpc = ExplicitMPC(model)
initstate!(exmpc, model.uop, model())
preparestate!(exmpc, [55, 30])
exmpc.estim()
exmpc([55, 30])
nlmodel = NonLinModel(f1!, h1!, Ts, 2, 2, 2; solver=RungeKutta(4), p)
nlmodel = setop!(nlmodel, uop=[10, 10], yop=[50, 30])
y = nlmodel()
transcription = MultipleShooting(f_threads=true)
nmpc_ukf = setconstraint!(
NonLinMPC(nlmodel; Hp=3, Mwt=[0, 0], Ewt=1, JE=JE, p=R̂y, transcription),
ymin=[45, -Inf]
)
sim!(nmpc_ukf, 2, [55, 30])
linearizemodel = linearize(nlmodel)
setmodel!(mpc_kf, linearizemodel)
transcription = OrthogonalCollocation(0, 2)
daemodel = NonLinModelDAE(fq_dae!, h_dae!, 100.0, 1, 1, 1, 1; transcription)
sim!(daemodel, 2, [10])
mhe_dae = MovingHorizonEstimator(daemodel, He=2, direct=false, hessian=true)
setconstraint!(mhe_dae, x̂min=[-50, 50], x̂max=[50, 50])
nmpc_dae = NonLinMPC(mhe_dae, Hp=3, hessian=true)
setconstraint!(nmpc_dae, ymin=[-50], ymax=[50])
preparestate!(nmpc_dae, [5])
moveinput!(nmpc_dae, [0.1])
updatestate!(nmpc_dae, [2], [5])
end
end # @setup_workload