Skip to content

Commit 095c119

Browse files
committed
clean up
1 parent a44bc93 commit 095c119

File tree

4 files changed

+14
-66
lines changed

4 files changed

+14
-66
lines changed

src/controller/explicitmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct ExplicitMPC{S<:StateEstimator} <: PredictiveController
4545
R̂u = ~iszero(Lwt) ? repeat(ru, Hp) : R̂u = Float64[]
4646
R̂y = zeros(ny* Hp) # dummy R̂y (updated just before optimization)
4747
S_Hp, T_Hp, S_Hc, T_Hc = init_ΔUtoU(nu, Hp, Hc)
48-
E, F, G, J, K, Q = init_deterpred(estim, model, Hp, Hc)
48+
E, F, G, J, K, Q = init_predmat(estim, model, Hp, Hc)
4949
_ , S̃_Hp, Ñ_Hc, Ẽ = init_defaultcon(model, Hp, Hc, C, S_Hp, S_Hc, N_Hc, E)
5050
P̃, q̃, p = init_quadprog(model, Ẽ, S̃_Hp, M_Hp, Ñ_Hc, L_Hp)
5151
Ks, Ps = init_stochpred(estim, Hp)

src/controller/linmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct LinMPC{S<:StateEstimator} <: PredictiveController
4646
R̂u = ~iszero(Lwt) ? repeat(ru, Hp) : R̂u = Float64[]
4747
R̂y = zeros(ny* Hp) # dummy R̂y (updated just before optimization)
4848
S_Hp, T_Hp, S_Hc, T_Hc = init_ΔUtoU(nu, Hp, Hc)
49-
E, F, G, J, K, Q = init_deterpred(estim, model, Hp, Hc)
49+
E, F, G, J, K, Q = init_predmat(estim, model, Hp, Hc)
5050
con, S̃_Hp, Ñ_Hc, Ẽ = init_defaultcon(model, Hp, Hc, C, S_Hp, S_Hc, N_Hc, E)
5151
P̃, q̃, p = init_quadprog(model, Ẽ, S̃_Hp, M_Hp, Ñ_Hc, L_Hp)
5252
Ks, Ps = init_stochpred(estim, Hp)

src/controller/nonlinmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct NonLinMPC{S<:StateEstimator, JEfunc<:Function} <: PredictiveController
5050
R̂u = ~iszero(Lwt) ? repeat(ru, Hp) : R̂u = Float64[]
5151
R̂y = zeros(ny* Hp) # dummy R̂y (updated just before optimization)
5252
S_Hp, T_Hp, S_Hc, T_Hc = init_ΔUtoU(nu, Hp, Hc)
53-
E, F, G, J, K, Q = init_deterpred(estim, model, Hp, Hc)
53+
E, F, G, J, K, Q = init_predmat(estim, model, Hp, Hc)
5454
con, S̃_Hp, Ñ_Hc, Ẽ = init_defaultcon(model, Hp, Hc, C, S_Hp, S_Hc, N_Hc, E)
5555
P̃, q̃, p = init_quadprog(model, Ẽ, S̃_Hp, M_Hp, Ñ_Hc, L_Hp)
5656
Ks, Ps = init_stochpred(estim, Hp)

src/predictive_control.jl

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -361,43 +361,16 @@ function validate_setpointdist(mpc::PredictiveController, ry, d, R̂y, D̂)
361361
size(D̂) (nd*Hp,) && error("D̂ size $(size(D̂)) ≠ measured dist. size × Hp ($(nd*Hp),)")
362362
end
363363

364-
"""
365-
getestimates!(mpc::PredictiveController, estim::StateEstimator) -> x̂d, x̂s, ŷ
366-
367-
Get estimator output and split `x̂` into the deterministic `x̂d` and stochastic `x̂s` states.
368-
"""
369-
function getestimates!(mpc::PredictiveController, estim::StateEstimator, ym , d)
370-
nx = estim.model.nx
371-
mpc.x̂d[:] = estim.x̂[1:nx]
372-
mpc.x̂s[:] = estim.x̂[nx+1:end]
373-
mpc.ŷ[:] = evalŷ(estim, ym, d)
374-
return mpc.x̂d, mpc.x̂s, mpc.
375-
end
376-
377-
"""
378-
getestimates!(mpc::PredictiveController, estim::InternalModel) -> x̂d, x̂s, ŷ
379-
380-
Get the internal model deterministic `estim.x̂d` and stochastic `estim.x̂s` states.
381-
"""
382-
function getestimates!(mpc::PredictiveController, estim::InternalModel, ym, d)
383-
isnothing(ym) && error("Predictive controllers with InternalModel need the measured "*
384-
"outputs ym in keyword argument to compute control actions u")
385-
mpc.x̂d[:] = estim.x̂d
386-
mpc.x̂s[:] = estim.x̂s
387-
mpc.ŷ[:] = evalŷ(estim, ym, d)
388-
return mpc.x̂d, mpc.x̂s, mpc.
389-
end
390-
391364
"""
392365
predictstoch!(mpc, estim::InternalModel, x̂s, d, ym )
393366
394367
Use current measured outputs `ym` for prediction when `estim` is a [`InternalModel`](@ref).
395368
"""
396369
function predictstoch!(mpc, estim::InternalModel, d, ym )
397-
ŷd = h(estim.model, mpc.x̂d, d - estim.model.dop) + estim.model.yop
370+
ŷd = h(estim.model, estim.x̂d, d - estim.model.dop) + estim.model.yop
398371
ŷs = zeros(estim.model.ny)
399372
ŷs[estim.i_ym] = ym - ŷd[estim.i_ym] # ŷs=0 for unmeasured outputs
400-
mpc.Ŷs[:] = mpc.Ks*mpc.x̂s + mpc.Ps*ŷs
373+
mpc.Ŷs[:] = mpc.Ks*mpc.estim.x̂s + mpc.Ps*ŷs
401374
return mpc.Ŷs
402375
end
403376
"Separate stochastic predictions are not needed if `estim` is not [`InternalModel`](@ref)."
@@ -571,7 +544,7 @@ end
571544

572545

573546
@doc raw"""
574-
init_deterpred(estim::StateEstimator, ::LinModel, Hp, Hc) -> E, G, J, K, Q
547+
init_predmat(estim::StateEstimator, ::LinModel, Hp, Hc) -> E, G, J, K, Q
575548
576549
Construct the prediction matrices for [`LinModel`](@ref) `model`.
577550
@@ -586,8 +559,9 @@ The linear model predictions are evaluated by :
586559
where predicted outputs ``\mathbf{Ŷ}``, stochastic outputs ``\mathbf{Ŷ_s}``, and measured
587560
disturbances ``\mathbf{D̂}`` are from ``k + 1`` to ``k + H_p``. Input increments
588561
``\mathbf{ΔU}`` are from ``k`` to ``k + H_c - 1``. The vector ``\mathbf{x̂}_{k-1}(k)`` is the
589-
state estimated at the last control period. Operating points on ``\mathbf{u}``,
590-
``\mathbf{d}`` and ``\mathbf{y}`` are omitted in above equations.
562+
state estimated at the last control period. The stochastic outputs ``\mathbf{Ŷ_s = 0}`` if
563+
`estim` is not a [`InternalModel`](@ref). Operating points on ``\mathbf{u}``, ``\mathbf{d}``
564+
and ``\mathbf{y}`` are omitted in above equations.
591565
592566
# Extended Help
593567
Using the augmented matrices ``\mathbf{Â, B̂_u, Ĉ, B̂_d, D̂_d}`` in `model` and the equation
@@ -632,7 +606,7 @@ matrices are computed by :
632606
\end{aligned}
633607
```
634608
"""
635-
function init_deterpred(estim::StateEstimator, model::LinModel, Hp, Hc) #TODO: RENAME THE FUNCTION!
609+
function init_predmat(estim::StateEstimator, model::LinModel, Hp, Hc)
636610
Â, B̂u, Ĉ, B̂d, D̂d = estim.Â, estim.B̂u, estim.Ĉ, estim.B̂d, estim.D̂d
637611
nu, nx̂, ny, nd = model.nu, estim.nx̂, model.ny, model.nd
638612
# Apow 3D array : Apow[:,:,1] = A^0, Apow[:,:,2] = A^1, ...
@@ -677,7 +651,7 @@ function init_deterpred(estim::StateEstimator, model::LinModel, Hp, Hc) #TODO: R
677651
end
678652

679653
"Return empty matrices if `model` is not a [`LinModel`](@ref)"
680-
function init_deterpred(estim::StateEstimator, model::SimModel, Hp, Hc)
654+
function init_predmat(estim::StateEstimator, model::SimModel, Hp, Hc)
681655
nu, nx̂, nd = model.nu, estim.nx̂, model.nd
682656
E = zeros(0, nu*Hc)
683657
G = zeros(0, nd)
@@ -879,35 +853,6 @@ function relaxŶ(::SimModel, C, c_Ŷmin, c_Ŷmax, E)
879853
return A_Ŷmin, A_Ŷmax, Ẽ
880854
end
881855

882-
@doc raw"""
883-
init_stochpred(estim::StateEstimator, Hp) -> Ks, Ps
884-
885-
Init the stochastic prediction matrix `Ks` from `estim` estimator for predictive control.
886-
887-
``\mathbf{K_s}`` is the prediction matrix of the stochastic model (composed exclusively of
888-
integrators):
889-
```math
890-
\mathbf{Ŷ_s} = \mathbf{K_s x̂_s}(k)
891-
```
892-
The stochastic predictions ``\mathbf{Ŷ_s}`` are the integrator outputs from ``k+1`` to
893-
``k+H_p``. ``\mathbf{x̂_s}(k)`` is extracted from current estimates ``\mathbf{x̂}_{k-1}(k)``
894-
with [`getestimates!`](@ref). The method also returns an empty ``\mathbf{P_s}`` matrix, since
895-
it is useless except for [`InternalModel`](@ref) estimators.
896-
"""
897-
function init_stochpred(estim::StateEstimator, Hp)
898-
As, Cs_y = estim.As, estim.Cs_y
899-
nxs = estim.nxs
900-
Ms = Matrix{Float64}(undef, Hp*nxs, nxs)
901-
for i = 1:Hp
902-
iRow = (1:nxs) .+ nxs*(i-1)
903-
Ms[iRow, :] = As^i
904-
end
905-
Js = repeatdiag(Cs_y, Hp)
906-
Ks = Js*Ms
907-
Ps = zeros(estim.model.ny*Hp, 0)
908-
return Ks, Ps
909-
end
910-
911856
@doc raw"""
912857
init_stochpred(estim::InternalModel, Hp) -> Ks, Ps
913858
@@ -940,6 +885,9 @@ function init_stochpred(estim::InternalModel, Hp)
940885
end
941886
return Ks, Ps
942887
end
888+
"Return empty matrices if `estim` is not a [`InternalModel`](@ref)."
889+
init_stochpred(::StateEstimator, _ ) = Float64[;;], Float64[;;]
890+
943891

944892
@doc raw"""
945893
init_linconstraint(model::LinModel,

0 commit comments

Comments
 (0)