Skip to content

new ManualEstimator tests and minor doc corrections #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/controller/explicitmpc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ The controller minimizes the following objective function at each discrete time
See [`LinMPC`](@ref) for the variable definitions. This controller does not support
constraints but the computational costs are extremely low (array division), therefore
suitable for applications that require small sample times. The keyword arguments are
identical to [`LinMPC`](@ref), except for `Cwt` and `optim` which are not supported. This
controller uses a [`SingleShooting`](@ref) transcription method.
identical to [`LinMPC`](@ref), except for `Cwt`, `transcription` and `optim`, which are not
supported. This controller uses a [`SingleShooting`](@ref) transcription method.

This method uses the default state estimator, a [`SteadyKalmanFilter`](@ref) with default
arguments. This controller is allocation-free.
Expand Down
2 changes: 1 addition & 1 deletion src/estimator/internal_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ unmeasured ``\mathbf{y^u}``. `model` evaluates the deterministic predictions
``\mathbf{ŷ_d}``, and `stoch_ym`, the stochastic predictions of the measured outputs
``\mathbf{ŷ_s^m}`` (the unmeasured ones being ``\mathbf{ŷ_s^u=0}``). The predicted outputs
sum both values : ``\mathbf{ŷ = ŷ_d + ŷ_s}``. See the Extended Help for more details. This
estimator is allocation-free is `model` simulations do not allocate.
estimator is allocation-free if `model` simulations do not allocate.

!!! warning
`InternalModel` estimator does not work if `model` is integrating or unstable. The
Expand Down
59 changes: 59 additions & 0 deletions test/3_test_predictive_control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ end
@test ym ≈ r atol=1e-2
end

@testitem "LinMPC and ManualEstimator v.s. default" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = setop!(LinModel(tf(5, [2, 1]), 3.0), yop=[10])
r = [15]
outdist = [5]
U_man, U_def = let linmodel=linmodel, r=r, outdist=outdist
mpc_man = LinMPC(ManualEstimator(linmodel))
skf = SteadyKalmanFilter(linmodel)
mpc_def = LinMPC(linmodel)
linmodel.x0 .= 0
U_man, U_def = zeros(1, 25), zeros(1, 25)
for i=1:25
ym = linmodel() - outdist
x̂ = preparestate!(skf, ym)
setstate!(mpc_man, x̂)
preparestate!(mpc_def, ym)
u_man = moveinput!(mpc_man, r)
u_def = moveinput!(mpc_def, r)
U_man[:, i], U_def[:, i] = u_man, u_def
updatestate!(skf, u_man, ym)
updatestate!(mpc_def, u_def, ym)
updatestate!(linmodel, u_man)
end
U_man, U_def
end
@test U_man ≈ U_def atol=1e-9
end

@testitem "LinMPC other methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
Expand Down Expand Up @@ -852,6 +880,37 @@ end
@test ym ≈ r atol=1e-2
end

@testitem "NonLinMPC and ManualEstimator v.s. default" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = LinModel(tf(5, [2, 1]), 3.0)
f(x,u,_,p) = p.A*x + p.Bu*u
h(x,_,p) = p.C*x
model = setop!(NonLinModel(f, h, 3.0, 1, 1, 1; solver=nothing, p=linmodel), yop=[10])
r = [15]
outdist = [5]
U_man, U_def = let model=model, r=r, outdist=outdist
nmpc_man = NonLinMPC(ManualEstimator(model), Hp=10)
ukf = UnscentedKalmanFilter(model)
nmpc_def = NonLinMPC(model, Hp=10)
model.x0 .= 0
U_man, U_def = zeros(1, 25), zeros(1, 25)
for i=1:25
ym = model() - outdist
x̂ = preparestate!(ukf, ym)
setstate!(nmpc_man, x̂)
preparestate!(nmpc_def, ym)
u_man = moveinput!(nmpc_man, r)
u_def = moveinput!(nmpc_def, r)
U_man[:, i], U_def[:, i] = u_man, u_def
updatestate!(ukf, u_man, ym)
updatestate!(nmpc_def, u_def, ym)
updatestate!(model, u_man)
end
U_man, U_def
end
@test U_man ≈ U_def atol=1e-9
end

@testitem "NonLinMPC other methods" setup=[SetupMPCtests] begin
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
linmodel = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
Expand Down