Skip to content

Commit 26bae7a

Browse files
authored
Merge pull request #534 from JuliaControl/manual_a_bound
doc + test: algebraic bounds `â` in the pH reactor example + improve coverage
2 parents 0fd65e0 + e480201 commit 26bae7a

5 files changed

Lines changed: 85 additions & 27 deletions

File tree

‎docs/src/manual/nonlinmpc2.md‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,24 +298,26 @@ disturbed by a random-walk, instead of the measured output. Among all the settin
298298
`σR` and `σP_0`, and a past horizon `He` long enough to see the main dynamics can improve
299299
the stability on a highly nonlinear and stiff plant model like here. An exact Hessian matrix
300300
with `hessian=true` also helps for DAEs, since the dynamics are encoded in the nonlinear
301-
equality constraints. We can also bound the three estimated states to positive values since
302-
they are concentrations in mol/L:
301+
equality constraints. We can also bound the three estimated states ``\mathbf{x̂}`` to
302+
positive values since they are concentrations in mol/L, and the estimated algebraic
303+
``\mathbf{â}`` to valid pH values:
303304

304305
```@example 1
305306
nint_ym=0; nint_u=0; # disable the default stochastic model
306307
He = 8; hessian = true
307308
σQ = [0.5e-3, 0.5e-3, 0.9e-3]; σR=[0.05]; σP_0 = [0.05, 0.05, 1e-3]
308309
mhe = MovingHorizonEstimator(model; nint_ym, nint_u, He, hessian, σQ, σR, σP_0)
309310
using JuMP; unset_time_limit_sec(mhe.optim) # no wall time limit during optimization
310-
mhe = setconstraint!(mhe, x̂min=[0, 0, 0])
311+
mhe = setconstraint!(mhe, x̂min=[0, 0, 0], âmin=[1.0], âmax=[14.0])
311312
```
312313

313314
The state constraints are shown in round brackets next to the decision variables. There are
314-
27 of them (3 states × 8 datapoints in the past + 3 arrival estimates). The arrival
315-
covariance ``\mathbf{P̄}`` is constant by default for [`NonLinModelDAE`](@ref), specified by
316-
`σP_0` argument. A proper tuning of `σP_0` and `He` reduces the impact of the constant
317-
arrival approximation. We can now reproduce the last simulated scenario and see how `mhe`
318-
performs under pH and flow rate measurement noise:
315+
45 of them (3 states × 8 datapoints in the past + 3 arrival point + 2 bounds × 1 alg. var. ×
316+
8 datapoints in the past + 2 bounds × 1 arrival point). The arrival covariance ``\mathbf{P̄}``
317+
is constant by default for [`NonLinModelDAE`](@ref), specified by `σP_0` argument. A proper
318+
tuning of `σP_0` and `He` reduces the impact of the constant arrival approximation. We can
319+
now reproduce the last simulated scenario and see how `mhe` performs under pH and flow rate
320+
measurement noise:
319321

320322
```@example 1
321323
using Random

‎src/estimator/mhe/construct.jl‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ end
813813
Set the bound constraint parameters of the [`MovingHorizonEstimator`](@ref) `estim`.
814814
815815
It supports both soft and hard constraints on the estimated state ``\mathbf{x̂}``, algebraic
816-
variable ``\mathbf{â}`` (for [`NonLinModelDAE](@ref)), process noise ``\mathbf{ŵ}`` and
816+
variable ``\mathbf{â}`` (for [`NonLinModelDAE`](@ref)), process noise ``\mathbf{ŵ}`` and
817817
sensor noise ``\mathbf{v̂}``:
818818
```math
819819
\begin{alignat*}{3}
@@ -898,7 +898,7 @@ MovingHorizonEstimator estimator with a sample time Ts = 1.0 s:
898898
- `Âmin` / `Âmax` / `C_âmin` / `C_âmax` : ``\mathbf{Â_e}`` constraints `(na*(He+1),)`.
899899
- `Ŵmin` / `Ŵmax` / `C_ŵmin` / `C_ŵmax` : ``\mathbf{Ŵ}`` constraints `(nx̂*He,)`.
900900
- `V̂min` / `V̂max` / `C_v̂min` / `C_v̂max` : ``\mathbf{V̂}`` constraints `(nym*He,)`.
901-
The subscript ``\mathbf{e}`` stands for extended with the arrival estimate.
901+
The subscript ``\mathbf{e}`` stands for "extended with the arrival estimate".
902902
"""
903903
function setconstraint!(
904904
estim::MovingHorizonEstimator;
@@ -974,7 +974,7 @@ function setconstraint!(
974974
con.Âmin .= Âmin
975975
end
976976
if isnothing(Âmax) && !isnothing(âmax)
977-
size(âmax) == (na,) || throw(DimensionMismatch("âmax size must be $((nxa,))"))
977+
size(âmax) == (na,) || throw(DimensionMismatch("âmax size must be $((na,))"))
978978
for i in 1:(na*(He+1))
979979
con.Âmax[i] = âmax[(i-1) % na + 1]
980980
end
@@ -1052,12 +1052,12 @@ function setconstraint!(
10521052
if !isnothing(C_âmin)
10531053
size(C_âmin) == (nÂe,) || throw(DimensionMismatch("C_âmin size must be $((nÂe,))"))
10541054
any(C_âmin .< 0) && error("C_âmin weights should be non-negative")
1055-
con.A_âmin[:, begin] .= @. -C_âmin
1055+
con.A_Âmin[:, begin] .= @. -C_âmin
10561056
end
10571057
if !isnothing(C_âmax)
10581058
size(C_âmax) == (nÂe,) || throw(DimensionMismatch("C_âmax size must be $((nÂe,))"))
10591059
any(C_âmax .< 0) && error("C_âmax weights should be non-negative")
1060-
con.A_âmax[:, begin] .= @. -C_âmax
1060+
con.A_Âmax[:, begin] .= @. -C_âmax
10611061
end
10621062
if !isnothing(C_ŵmin)
10631063
size(C_ŵmin) == (nŵ*He,) || throw(DimensionMismatch("C_ŵmin size must be $((nŵ*He,))"))
@@ -1212,8 +1212,8 @@ function init_defaultcon_mhe(
12121212
Z̃min, Z̃max = init_boxconstraint_mhe(
12131213
model, transcription, He, nx̂, nŵ, nε,
12141214
x̂0min, x̂0max, X̂0min, X̂0max, Âmin, Âmax, Ŵmin, Ŵmax,
1215-
A_x̂min, A_x̂max, A_Âmin, A_Âmax,
1216-
C_x̂min, C_x̂max, A_Ŵmin, A_Ŵmax
1215+
A_x̂min, A_x̂max, C_x̂min, C_x̂max,
1216+
A_Âmin, A_Âmax, A_Ŵmin, A_Ŵmax
12171217
)
12181218
i_b, i_g, A, Aeq, neq = init_matconstraint_mhe(
12191219
model, transcription, Z̃min, Z̃max, nc, nε,
@@ -1403,7 +1403,7 @@ end
14031403
"""
14041404
init_boxconstraint_mhe(
14051405
model::SimModel, transcription::TranscriptionMethod, He, nx̂, nŵ, nε,
1406-
x̂0min, x̂0max, X̂0min, X̂0max, Âmin, Âmax, Ŵmin, Ŵmax,
1406+
x̂0min, x̂0max, X̂0min, X̂0max, Âmin, Âmax, Ŵmin, Ŵmax,
14071407
A_x̂min, A_x̂max, C_x̂min, C_x̂max,
14081408
A_Âmin, A_Âmax, A_Ŵmin, A_Ŵmax
14091409
) -> Z̃min, Z̃max
@@ -1412,7 +1412,7 @@ Init the decision variable box constraints `Z̃min` and `Z̃max` for [`MovingHor
14121412
"""
14131413
function init_boxconstraint_mhe(
14141414
model::SimModel{NT}, transcription::TranscriptionMethod, He, nx̂, nŵ, nε,
1415-
x̂0min, x̂0max, X̂0min, X̂0max, Âmin, Âmax, Ŵmin, Ŵmax,
1415+
x̂0min, x̂0max, X̂0min, X̂0max, Âmin, Âmax, Ŵmin, Ŵmax,
14161416
A_x̂min, A_x̂max, C_x̂min, C_x̂max,
14171417
A_Âmin, A_Âmax, A_Ŵmin, A_Ŵmax
14181418
) where {NT<:Real}

‎src/estimator/mhe/execute.jl‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,11 @@ function add_data_windows!(estim::MovingHorizonEstimator, y0m, d0, u0=estim.last
583583
return ismoving
584584
end
585585

586-
"Truncate the bounds `Bmin` and `Bmax` to the window size `Nk` if `Nk < He`."
587-
function trunc_bounds(estim::MovingHorizonEstimator{NT}, Bmin, Bmax, n) where NT<:Real
588-
He, Nk = estim.He, estim.Nk[]
589-
if Nk < He
590-
nB = n*Nk
591-
Bmin_t, Bmax_t = similar(Bmin), similar(Bmax)
586+
"Truncate the bounds `Bmin` and `Bmax` to the window size `N` if `Nk < He`."
587+
function trunc_bounds(estim::MovingHorizonEstimator, Bmin, Bmax, n, N=estim.Nk[])
588+
if estim.Nk[] < estim.He
589+
nB = n*N
590+
Bmin_t, Bmax_t = similar(Bmin), similar(Bmax)
592591
Bmin_t[1:nB] .= @views Bmin[end-nB+1:end]
593592
Bmin_t[nB+1:end] .= -Inf
594593
Bmax_t[1:nB] .= @views Bmax[end-nB+1:end]

‎src/estimator/mhe/transcription.jl‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,6 @@ function linconstraint!(
897897
model.nd > 0 && mul!(FX̂, JX̂, D0, 1, 1)
898898
# --- update b vector for linear inequality constraints ---
899899
nX̂_He, nŴ_He, nV̂_He = length(X̂0min), length(Ŵmin), length(V̂min)
900-
nx̂ = length(estim.con.x̂0min)
901900
n = 0
902901
estim.con.b[(n+1):(n+nx̂)] .= @. -estim.con.x̂0min
903902
n += nx̂
@@ -923,14 +922,15 @@ end
923922

924923
"Set `b` excluding sensor noise bounds if not a `LinModel` and non-`SingleShooting`."
925924
function linconstraint!(
926-
estim::MovingHorizonEstimator, ::SimModel, ::TranscriptionMethod
925+
estim::MovingHorizonEstimator, model::SimModel, ::TranscriptionMethod
927926
)
928-
nx̂, nŵ = estim.nx̂, estim.nx̂
927+
nx̂, nŵ, na = estim.nx̂, estim.nx̂, get_na(model)
929928
# --- truncate vector and matrices if necessary ---
930929
X̂0min, X̂0max = trunc_bounds(estim, estim.con.X̂0min, estim.con.X̂0max, nx̂)
931930
Ŵmin, Ŵmax = trunc_bounds(estim, estim.con.Ŵmin, estim.con.Ŵmax, nŵ)
931+
Âmin, Âmax = trunc_bounds(estim, estim.con.Âmin, estim.con.Âmax, na, estim.Nk[]+1)
932932
# --- update b vector for linear inequality constraints ---
933-
nx̂, nŴ_He, nX̂_He = length(estim.con.x̂0min), length(Ŵmin), length(X̂0min)
933+
nŴ_He, nX̂_He, nÂe_He = length(Ŵmin), length(X̂0min), length(Âmin)
934934
n = 0
935935
estim.con.b[(n+1):(n+nx̂)] .= @. -estim.con.x̂0min
936936
n += nx̂
@@ -940,6 +940,10 @@ function linconstraint!(
940940
n += nX̂_He
941941
estim.con.b[(n+1):(n+nX̂_He)] .= @. +X̂0max - estim.con.FX̂
942942
n += nX̂_He
943+
estim.con.b[(n+1):(n+nÂe_He)] .= @. -Âmin
944+
n += nÂe_He
945+
estim.con.b[(n+1):(n+nÂe_He)] .= @. +Âmax
946+
n += nÂe_He
943947
estim.con.b[(n+1):(n+nŴ_He)] .= @. -Ŵmin
944948
n += nŴ_He
945949
estim.con.b[(n+1):(n+nŴ_He)] .= @. +Ŵmax

‎test/2_test_state_estim.jl‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,59 @@ end
20072007
@test getinfo(mhe2)[:Ŵ] ≈ zeros(nx̂) atol=5e-2
20082008
end
20092009

2010+
@testitem "MHE constraint violation (NonLinModelDAE)" setup=[SetupMPCtests] begin
2011+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
2012+
2013+
function fq!(ẋ, res, x, a, u, d, p)
2014+
ẋ[] = -p[] * (x[] - 0.2 * u[] - 0.2 * d[])
2015+
res[] = x[] - a[]
2016+
return nothing
2017+
end
2018+
function h!(y, x, a, d, _ )
2019+
y[] = 2 * x[] + a[] + 0.1 * d[]
2020+
end
2021+
p = [0.001]
2022+
as_0, xs_0 = [-1.0], [1.0]
2023+
dae = NonLinModelDAE(fq!, h!, 1000.0, 1, 1, 1, 1, 1; p, xs_0, as_0)
2024+
2025+
transcription = TrapezoidalCollocation()
2026+
mhe2 = MovingHorizonEstimator(dae, He=1, nint_ym=0; transcription)
2027+
2028+
setconstraint!(mhe2, âmin=[1], âmax=[100])
2029+
preparestate!(mhe2, [0.0], [0.0])
2030+
updatestate!(mhe2, [10.0], [0.0], [0.0])
2031+
2032+
info = getinfo(mhe2)
2033+
@test info[:â] ≈ [1.0] atol=5e-2
2034+
2035+
setconstraint!(mhe2, âmin=[-100], âmax=[-1])
2036+
preparestate!(mhe2, [0.0], [0.0])
2037+
updatestate!(mhe2, [10.0], [0.0], [0.0])
2038+
2039+
info = getinfo(mhe2)
2040+
@test info[:â] ≈ [-1.0] atol=5e-2
2041+
2042+
transcription = OrthogonalCollocation(0, 2, roots=:gausslegendre)
2043+
mhe3 = MovingHorizonEstimator(dae; He=1, nint_ym=0, Cwt=1e4, transcription)
2044+
2045+
setconstraint!(mhe3, c_âmin=[1], c_âmax=[1])
2046+
2047+
setconstraint!(mhe3, âmin=[1], âmax=[100])
2048+
preparestate!(mhe3, [0.0], [0.0])
2049+
updatestate!(mhe3, [10.0], [0.0], [0.0])
2050+
2051+
info = getinfo(mhe3)
2052+
@test info[:â] ≈ [1.0] atol=5e-2
2053+
2054+
setconstraint!(mhe3, âmin=[-100], âmax=[-1])
2055+
preparestate!(mhe3, [0.0], [0.0])
2056+
updatestate!(mhe3, [10.0], [0.0], [0.0])
2057+
2058+
info = getinfo(mhe3)
2059+
@test info[:â] ≈ [-1.0] atol=5e-2
2060+
2061+
end
2062+
20102063
@testitem "MHE set model" setup=[SetupMPCtests] begin
20112064
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
20122065
linmodel = LinModel([0.5], [0.3], [1.0], 0, 0, 10.0)

0 commit comments

Comments
 (0)