Skip to content

Commit 2845aaa

Browse files
authored
Merge pull request #157 from JuliaControl/cleanup_predictstoch
changed: simpler code for stochastic predictions of `InternalModel`
2 parents d57c13e + aff9e52 commit 2845aaa

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/controller/execute.jl

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,7 @@ function getinfo(mpc::PredictiveController{NT}) where NT<:Real
126126
U, Ŷ = Ū, Ȳ
127127
U .= mul!(U, mpc.S̃, mpc.ΔŨ) .+ mpc.T_lastu
128128
Ŷ .= Ŷ0 .+ mpc.Yop
129-
oldF = copy(mpc.F)
130-
F = predictstoch!(mpc, mpc.estim)
131-
Ŷs .= F # predictstoch! init mpc.F with Ŷs value if estim is an InternalModel
132-
F .= oldF # restore old F value
129+
predictstoch!(Ŷs, mpc, mpc.estim)
133130
info[:ΔU] = mpc.ΔŨ[1:mpc.Hc*model.nu]
134131
info[] = mpc.== 1 ? mpc.ΔŨ[end] : zero(NT)
135132
info[:J] = J
@@ -231,7 +228,7 @@ function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂
231228
end
232229

233230
"""
234-
initpred_common!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u) -> F
231+
initpred_common!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u) -> mpc.F
235232
236233
Common computations of `initpred!` for all types of [`SimModel`](@ref).
237234
@@ -251,23 +248,22 @@ function initpred_common!(mpc::PredictiveController, model::SimModel, d, D̂, R
251248
end
252249
mpc.R̂y .= R̂y
253250
mpc.R̂u .= R̂u
254-
F = predictstoch!(mpc, mpc.estim) # init mpc.F with Ŷs for InternalModel
255-
return F
251+
predictstoch!(mpc.F, mpc, mpc.estim)
252+
return mpc.F
256253
end
257254

258255
@doc raw"""
259-
predictstoch!(mpc::PredictiveController, estim::InternalModel) -> F
256+
predictstoch!(Ŷs, mpc::PredictiveController, estim::InternalModel) -> nothing
260257
261-
Init `mpc.F` vector with ``\mathbf{F = Ŷ_s}`` when `estim` is an [`InternalModel`](@ref).
258+
Fill `Ŷs` in-place with stochastic predictions if `estim` is an [`InternalModel`](@ref).
262259
"""
263-
function predictstoch!(mpc::PredictiveController, estim::InternalModel)
264-
Ŷs = mpc.F
260+
function predictstoch!(Ŷs, mpc::PredictiveController, estim::InternalModel)
265261
mul!(Ŷs, mpc.Ks, estim.x̂s)
266262
mul!(Ŷs, mpc.Ps, estim.ŷs, 1, 1)
267-
return mpc.F
263+
return nothing
268264
end
269-
"Separate stochastic predictions are not needed if `estim` is not [`InternalModel`](@ref)."
270-
predictstoch!(mpc::PredictiveController, ::StateEstimator) = (mpc.F .= 0)
265+
"Fill `Ŷs` vector with 0 values when `estim` is not an [`InternalModel`](@ref)."
266+
predictstoch!(Ŷs, mpc::PredictiveController, ::StateEstimator) = (Ŷs .= 0; nothing)
271267

272268
@doc raw"""
273269
linconstraint!(mpc::PredictiveController, model::LinModel)

0 commit comments

Comments
 (0)