Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 101 additions & 97 deletions src/precompile.jl
Original file line number Diff line number Diff line change
@@ -1,64 +1,13 @@
# 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
model = setop!(LinModel(sys, Ts), uop=[10, 10], yop=[50, 30])
y = model()

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])
sim!(mpc_im, 2)

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_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])

mpc_mhe = setconstraint!(LinMPC(MovingHorizonEstimator(model, He=2)), ymin=[45, -Inf])
setconstraint!(mpc_mhe.estim, x̂min=[-50,-50,-50,-50], x̂max=[50,50,50,50])
initstate!(mpc_mhe, model.uop, model())
preparestate!(mpc_mhe, [55, 30])
mpc_mhe.estim()
u = mpc_mhe([55, 30])
sim!(mpc_mhe, 2, [55, 30])

mpc_man = setconstraint!(LinMPC(ManualEstimator(model)), ymin=[45, -Inf])
initstate!(mpc_man, model.uop, model())
setstate!(mpc_man, ones(4))
u = mpc_man([55, 30])

nmpc_skf = setconstraint!(NonLinMPC(SteadyKalmanFilter(model), Cwt=Inf), ymin=[45, -Inf])
initstate!(nmpc_skf, model.uop, model())
preparestate!(nmpc_skf, [55, 30])
nmpc_skf.estim()
u = nmpc_skf([55, 30])
sim!(nmpc_skf, 2, [55, 30])

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()
u = exmpc([55, 30])
sim!(exmpc, 2, [55, 30])

sys2 = minreal(ss(sys))
function f!(xnext, x, u, _ , p)
A, B, _ = p
mul!(xnext, A , x)
Expand All @@ -70,51 +19,106 @@ function h!(y, x, _ , p)
mul!(y, C, x)
return nothing
end

sys2 = minreal(ss(sys))
nlmodel = setop!(
NonLinModel(f!, h!, Ts, 2, 2, 2, solver=RungeKutta(4), p=(sys2.A, sys2.B, sys2.C)),
uop=[10, 10], yop=[50, 30]
)
y = nlmodel()

transcription = MultipleShooting(f_threads=true)
nmpc_ukf = setconstraint!(NonLinMPC(
UnscentedKalmanFilter(nlmodel); Hp=10, transcription, Cwt=1e3), ymin=[45, -Inf]
)
initstate!(nmpc_ukf, nlmodel.uop, y)
preparestate!(nmpc_ukf, [55, 30])
u = nmpc_ukf([55, 30])
sim!(nmpc_ukf, 2, [55, 30])

nmpc_ekf = setconstraint!(NonLinMPC(ExtendedKalmanFilter(model), Cwt=Inf), ymin=[45, -Inf])
initstate!(nmpc_ekf, model.uop, model())
preparestate!(nmpc_ekf, [55, 30])
u = nmpc_ekf([55, 30])
sim!(nmpc_ekf, 2, [55, 30])

transcription = TrapezoidalCollocation()
nmpc_mhe = setconstraint!(NonLinMPC(
MovingHorizonEstimator(nlmodel, He=2); transcription, Hp=10, Cwt=Inf), ymin=[45, -Inf]
)
setconstraint!(nmpc_mhe.estim, x̂min=[-50,-50,-50,-50], x̂max=[50,50,50,50])
initstate!(nmpc_mhe, nlmodel.uop, y)
preparestate!(nmpc_mhe, [55, 30])
u = nmpc_mhe([55, 30])
sim!(nmpc_mhe, 2, [55, 30])
p = (sys2.A, sys2.B, sys2.C)

function JE( _ , Ŷe, _ , R̂y)
Ŷ = @views Ŷe[3:end]
Ȳ = R̂y - Ŷ
return dot(Ȳ, Ȳ)
end
R̂y = repeat([55; 30], 10)
empc = setconstraint!(
NonLinMPC(nlmodel, Mwt=[0, 0], Hp=10, Cwt=Inf, Ewt=1, JE=JE, p=R̂y), ymin=[45, -Inf]
)
preparestate!(empc, [55, 30])
u = empc()
sim!(empc, 2)

linearizemodel = linearize(nlmodel)
setmodel!(mpc_kf, linearizemodel)

# 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_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])
sim!(mpc_im, 2)

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])

mhe = MovingHorizonEstimator(model, He=2, direct=true)
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()

mpc_man = setconstraint!(LinMPC(ManualEstimator(model)), ymin=[45, -Inf])
initstate!(mpc_man, model.uop, model())
setstate!(mpc_man, ones(4))
mpc_man([55, 30])

nmpc_skf = setconstraint!(NonLinMPC(SteadyKalmanFilter(model), Cwt=Inf), ymin=[45, -Inf])
initstate!(nmpc_skf, model.uop, model())
preparestate!(nmpc_skf, [55, 30])
nmpc_skf.estim()
nmpc_skf([55, 30])

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(f!, h!, 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(
UnscentedKalmanFilter(nlmodel); Hp=10, transcription, Cwt=1e3), ymin=[45, -Inf]
)
initstate!(nmpc_ukf, nlmodel.uop, y)
preparestate!(nmpc_ukf, [55, 30])
nmpc_ukf([55, 30])
sim!(nmpc_ukf, 2, [55, 30])

nmpc_ekf = setconstraint!(NonLinMPC(ExtendedKalmanFilter(model), Cwt=Inf), ymin=[45, -Inf])
initstate!(nmpc_ekf, model.uop, model())
preparestate!(nmpc_ekf, [55, 30])
nmpc_ekf([55, 30])

transcription = TrapezoidalCollocation()
nmpc_mhe = setconstraint!(NonLinMPC(
MovingHorizonEstimator(nlmodel, He=2); transcription, Hp=10, Cwt=Inf), ymin=[45, -Inf]
)
setconstraint!(nmpc_mhe.estim, x̂min=[-50,-50,-50,-50], x̂max=[50,50,50,50])
initstate!(nmpc_mhe, nlmodel.uop, y)
preparestate!(nmpc_mhe, [55, 30])
nmpc_mhe([55, 30])

empc = setconstraint!(
NonLinMPC(nlmodel, Mwt=[0, 0], Hp=10, Cwt=Inf, Ewt=1, JE=JE, p=R̂y), ymin=[45, -Inf]
)
preparestate!(empc, [55, 30])
empc()

linearizemodel = linearize(nlmodel)
setmodel!(mpc_kf, linearizemodel)

end

end # @setup_workload