Skip to content

Commit 5581d8c

Browse files
authored
Merge pull request #421 from JuliaControl/mhe_ms_stoch_defect_lin
added: stochastic defects treated as linear equality constraints in `MovingHorizonEstimator`
2 parents 6fb787d + b267dff commit 5581d8c

8 files changed

Lines changed: 233 additions & 147 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
3-
version = "2.9.3"
3+
version = "2.10.0"
44
authors = ["Francis Gagnon"]
55

66
[deps]

‎src/controller/execute.jl‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -326,38 +326,6 @@ end
326326
"Fill `Ŷs` vector with 0 values when `estim` is not an [`InternalModel`](@ref)."
327327
predictstoch!(Ŷs, ::PredictiveController, ::StateEstimator) = (Ŷs .= 0; nothing)
328328

329-
"""
330-
disturbedinput!(Û0, mpc::PredictiveController, estim::StateEstimator, U0, X̂0) -> nothing
331-
332-
Fill disturbed inputs of the augmented model `Û0` in-place with stochastic states in `X̂0`
333-
334-
Both `Û0` and `U0` variables include deviation vectors from ``k+0`` to ``k+H_p-1``. The
335-
predicted states `X̂0` include deviation vectors from ``k+1`` to ``k+H_p-1`` (the current one
336-
is stored in `estim.x̂0`).
337-
338-
This function is needed for the collocation methods that directly call the state derivative
339-
function `estim.model.f!` with the manipulated inputs augmented with the estimated
340-
disturbances at model input (see [`init_estimstoch`](@ref)). This is also needed for
341-
[`MultipleShooting`](@ref) since it calls the discrete-time update function of the
342-
deterministic model [`f!`](@ref) to treat the stochastic defects as linear equality
343-
constraints. Lastly, it's also necessary to prefill the `Û0` vector before anything else
344-
since both `û0` and `û0next` are needed at each stage with hold order `h>0`, thus potential
345-
race conditions with multi-threading.
346-
"""
347-
function disturbedinput!(Û0, mpc::PredictiveController, estim::StateEstimator, U0, X̂0)
348-
nu, nx, nx̂ = estim.model.nu, estim.model.nx, estim.nx̂
349-
Cs_u = estim.Cs_u
350-
Û0 .= U0
351-
for j=0:mpc.Hp-1
352-
xs = @views j < 1 ? estim.x̂0[(nx+1):(nx̂)] : X̂0[(nx+1+nx̂*(j-1)):(nx̂*j)]
353-
û0 = @views Û0[(1+nu*j):(nu*(j+1))]
354-
mul!(û0, Cs_u, xs, 1, 1) # û0 = u0 + Cs_u*xs
355-
end
356-
return nothing
357-
end
358-
"No input disturbances for [`InternalModel`](@ref), hence do `Û0 .= U0`."
359-
disturbedinput!(Û0, ::PredictiveController, ::InternalModel, U0, _) = (Û0 .= U0; nothing)
360-
361329
@doc raw"""
362330
linconstraint_custom!(mpc::PredictiveController, model::SimModel)
363331

‎src/controller/transcription.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ function con_nonlinprogeq!(
13471347
f_threads = transcription.f_threads
13481348
D̂0 = mpc.D̂0
13491349
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
1350-
disturbedinput!(Û0, mpc, mpc.estim, U0, X̂0_Z̃)
1350+
Û0 = disturbedinput!(Û0, mpc.estim, mpc.estim.x̂0, X̂0_Z̃, U0)
13511351
@threadsif f_threads for j=1:Hp
13521352
if j < 2
13531353
x̂d_Z̃ = @views mpc.estim.x̂0[1:nx]
@@ -1406,7 +1406,7 @@ function con_nonlinprogeq!(
14061406
nk = get_nk_mpc(model, transcription)
14071407
D̂0 = mpc.D̂0
14081408
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
1409-
disturbedinput!(Û0, mpc, mpc.estim, U0, X̂0_Z̃)
1409+
Û0 = disturbedinput!(Û0, mpc.estim, mpc.estim.x̂0, X̂0_Z̃, U0)
14101410
@threadsif f_threads for j=1:Hp
14111411
if j < 2
14121412
x̂d_Z̃ = @views mpc.estim.x̂0[1:nx]
@@ -1502,7 +1502,7 @@ function con_nonlinprogeq!(
15021502
D̂0 = mpc.D̂0
15031503
X̂0_Z̃, K_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)], Z̃[(nΔU+nX̂+1):(nΔU+nX̂+nk*Hp)]
15041504
D̂temp = mpc.buffer.D̂
1505-
disturbedinput!(Û0, mpc, mpc.estim, U0, X̂0_Z̃)
1505+
Û0 = disturbedinput!(Û0, mpc.estim, mpc.estim.x̂0, X̂0_Z̃, U0)
15061506
@threadsif f_threads for j=1:Hp
15071507
if j < 2
15081508
x̂d_Z̃ = @views mpc.estim.x̂0[1:nx]

‎src/estimator/execute.jl‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ function ĥ!(ŷ0, model::SimModel, Cs_y, x̂0, d0)
141141
return nothing
142142
end
143143

144+
"""
145+
disturbedinput!(Û0, estim::StateEstimator, x̂0, X̂0, U0) -> Û0
146+
147+
Fill disturbed inputs of the augmented model `Û0` in-place with the stochastic states in `X̂0`.
148+
149+
Both `Û0` and `U0` variables include deviation vectors from ``k+0`` to ``k+N-1``. The
150+
predicted states `X̂0` include deviation vectors from ``k+1`` to ``k+N``, and `x̂0` is the
151+
value at ``k``.
152+
153+
This function is needed for the collocation methods that directly call the state derivative
154+
function `estim.model.f!` with the manipulated inputs augmented with the estimated
155+
disturbances at model input (see [`init_estimstoch`](@ref)). This is also needed for
156+
[`MultipleShooting`](@ref) since it calls the discrete-time update function of the
157+
deterministic model [`f!`](@ref) to treat the stochastic defects as linear equality
158+
constraints. Lastly, it's also necessary to prefill the `Û0` vector before anything else
159+
since both `û0` and `û0next` are needed at each stage with hold order `h>0`, thus potential
160+
race conditions with multi-threading.
161+
"""
162+
function disturbedinput!(Û0, estim::StateEstimator, x̂0, X̂0, U0)
163+
nu, nx, nx̂ = estim.model.nu, estim.model.nx, estim.nx̂
164+
N = length(Û0) ÷ nu
165+
Cs_u = estim.Cs_u
166+
Û0 .= U0
167+
for j=0:N-1
168+
xs = @views j < 1 ? x̂0[(nx+1):(nx̂)] : X̂0[(nx+1+nx̂*(j-1)):(nx̂*j)]
169+
û0 = @views Û0[(1+nu*j):(nu*(j+1))]
170+
mul!(û0, Cs_u, xs, 1, 1) # û0 = u0 + Cs_u*xs
171+
end
172+
return Û0
173+
end
174+
144175
@doc raw"""
145176
initstate!(estim::StateEstimator, u, ym, d=[]) -> x̂
146177

‎src/estimator/internal_model.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ function ĥ!(ŷ0, ::InternalModel, model::NonLinModel, x̂0, d0)
191191
return h!(ŷ0, model, x̂0, d0, model.p)
192192
end
193193

194+
"No input disturbances for [`InternalModel`](@ref), return `Û0 .= U0`."
195+
disturbedinput!(Û0, ::InternalModel, _ , _ , U0) = (Û0 .= U0)
194196

195197
@doc raw"""
196198
init_internalmodel(As, Bs, Cs, Ds) -> Âs, B̂s

‎src/estimator/mhe/construct.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ struct MovingHorizonEstimator{
183183
model, transcription, He, Â, B̂u, Ĉm, B̂d, D̂dm, x̂op, f̂op, direct
184184
)
185185
ES, GS, JS, BS = init_defectmat_mhe(
186-
model, transcription, He, Â, B̂u, B̂d, x̂op, f̂op, direct
186+
model, transcription, He, Â, B̂u, B̂d, x̂op, f̂op, As, direct
187187
)
188188
# dummy values (updated just before optimization):
189189
F, fx̄ = zeros(NT, nym*He), zeros(NT, nx̂)

‎src/estimator/mhe/execute.jl‎

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function init_estimate_cov!(estim::MovingHorizonEstimator, y0m, d0, u0)
1717
estim.q̃ .= 0
1818
estim.r .= 0
1919
estim.con.FX̂ .= 0
20+
estim.con.FS .= 0
2021
if estim.direct
2122
# add y0m(-1) to the extended data window (custom NL constraints):
2223
estim.Yem[1:ny] .= y0m .+ @views yop[estim.i_ym]
@@ -422,7 +423,7 @@ function initpred!(estim::MovingHorizonEstimator{NT}, model::LinModel) where NT<
422423
nYm, nZ = estim.nym*Nk, get_nZ_mhe(estim.transcription, Nk, nx̂, nŵ)
423424
# --- truncate vectors and matrices if Nk < He ---
424425
U0, D0, Y0m = trunc_windows(estim)
425-
Ẽ, F, G, J, B, ẽx̄, Tŵ, H̃, H̃_data, q̃, Z̃var = trunc_predmat(estim, estim.transcription)
426+
Ẽ, F, G, J, B, ẽx̄, Tŵ, H̃, H̃_data, q̃, Z̃var = trunc_predmat(estim)
426427
invQ̂_Nk = trunc_cov(invQ̂_He, nx̂, Nk, estim.He)
427428
invR̂_Nk = trunc_cov(invR̂_He, nym, Nk, estim.He)
428429
# --- update F and fx̄ vectors for MHE predictions ---
@@ -652,6 +653,55 @@ function trunc_windows(estim::MovingHorizonEstimator)
652653
return U0, D0, Y0m
653654
end
654655

656+
"Truncate or extract subparts of the prediction matrices if `Nk < He`"
657+
function trunc_predmat(estim::MovingHorizonEstimator)
658+
model, transcription = estim.model, estim.transcription
659+
nŵ, nym, nε, Nk = estim.nx̂, estim.nym, estim.nε, estim.Nk[]
660+
nU, nYm, nŴ, nD = model.nu*Nk, nym*Nk, nŵ*Nk, model.nd*(Nk+1)
661+
if Nk < estim.He # avoid views since allocations only when Nk < He and we want fast mul!
662+
i_Z̃_Nk = get_i_Z̃_Nk(estim, transcription)
663+
i_Z_Nk = nε < 1 ? i_Z̃_Nk : i_Z̃_Nk[begin+1:end] .- 1
664+
Ẽ = estim.Ẽ[1:nYm, i_Z̃_Nk]
665+
G, J, B = estim.G[1:nYm, 1:nU], estim.J[1:nYm, 1:nD], estim.B[1:nYm]
666+
ẽx̄ = estim.ẽx̄[:, i_Z̃_Nk]
667+
Tŵ = estim.Tŵ[1:nŴ, i_Z_Nk]
668+
F = @views estim.F[1:nYm] # views here since they will store results
669+
H̃_data = @views estim.H̃.data[i_Z̃_Nk, i_Z̃_Nk]
670+
H̃ = @views estim.H̃[i_Z̃_Nk, i_Z̃_Nk]
671+
q̃ = @views estim.q̃[i_Z̃_Nk]
672+
Z̃var = @views estim.optim[:Z̃var][i_Z̃_Nk]
673+
else
674+
Ẽ, F, G, J, B = estim.Ẽ, estim.F, estim.G, estim.J, estim.B
675+
ẽx̄, Tŵ = estim.ẽx̄, estim.Tŵ
676+
H̃, H̃_data, q̃ = estim.H̃, estim.H̃.data, estim.q̃
677+
Z̃var = estim.optim[:Z̃var]
678+
end
679+
return Ẽ, F, G, J, B, ẽx̄, Tŵ, H̃, H̃_data, q̃, Z̃var
680+
end
681+
682+
"Extract subparts of the defect matrices if `Nk < He`"
683+
function trunc_defectmat(estim::MovingHorizonEstimator)
684+
model, transcription, con = estim.model, estim.transcription, estim.con
685+
FS = con.FS
686+
nx̂, Nk = estim.nx̂, estim.Nk[]
687+
nU, nX̂, nD = model.nu*Nk, nx̂*Nk, model.nd*(Nk+1)
688+
if Nk < estim.He # avoid views since allocations only when Nk < He and we want fast mul!
689+
i_Z̃_Nk = get_i_Z̃_Nk(estim, transcription)
690+
ẼS = con.ẼS[1:nX̂, i_Z̃_Nk]
691+
GS, JS, BS = con.GS[1:nX̂, 1:nU], con.JS[1:nX̂, 1:nD], con.BS[1:nX̂]
692+
FS = @views con.FS[1:nX̂] # views here since they will store results
693+
Aeq = @views con.Aeq[1:nX̂, i_Z̃_Nk]
694+
beq = @views con.beq[1:nX̂]
695+
Z̃var = @views estim.optim[:Z̃var][i_Z̃_Nk]
696+
else
697+
ẼS, FS, GS, JS, BS = con.ẼS, con.FS, con.GS, con.JS, con.BS
698+
Aeq = con.Aeq
699+
beq = con.beq
700+
Z̃var = estim.optim[:Z̃var]
701+
end
702+
return ẼS, FS, GS, JS, BS, Aeq, beq, Z̃var
703+
end
704+
655705
"Truncate the inverse covariance `invA_He` to the window size `Nk` if `Nk < He`."
656706
function trunc_cov(invA_He::Hermitian{<:Real, <:AbstractMatrix}, n, Nk, He)
657707
if Nk < He
@@ -931,8 +981,8 @@ function setmodel_estimator!(
931981
con.BX̂ .= BX̂
932982
# --- defect matrices ---
933983
ES, GS, JS, BS = init_defectmat_mhe(
934-
model, transcription, He, estim.Â, estim.B̂u, estim.B̂d, estim.x̂op, estim.f̂op,
935-
estim.direct
984+
model, transcription, He,
985+
estim.Â, estim.B̂u, estim.B̂d, estim.x̂op, estim.f̂op, estim.As, estim.direct
936986
)
937987
Aeq, ẼS = augmentdefect(ES, nε; slackfirst=true)
938988
con.ẼS .= ẼS

0 commit comments

Comments
 (0)