Skip to content

Commit 55d5a59

Browse files
committed
removed F matrix for predicting NonLinModel
1 parent fb85728 commit 55d5a59

File tree

3 files changed

+25
-23
lines changed

3 files changed

+25
-23
lines changed

src/controller/linmpc.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct LinMPC{S<:StateEstimator} <: PredictiveController
66
x̂d::Vector{Float64}
77
x̂s::Vector{Float64}
88
::Vector{Float64}
9+
Ŷs::Vector{Float64}
910
Hp::Int
1011
Hc::Int
1112
M_Hp::Diagonal{Float64, Vector{Float64}}
@@ -35,7 +36,7 @@ struct LinMPC{S<:StateEstimator} <: PredictiveController
3536
function LinMPC{S}(estim::S, Hp, Hc, Mwt, Nwt, Lwt, Cwt, ru, optim) where {S<:StateEstimator}
3637
model = estim.model
3738
nu, nxd, nxs, ny, nd = model.nu, model.nx, estim.nxs, model.ny, model.nd
38-
x̂d, x̂s, ŷ = zeros(nxd), zeros(nxs), zeros(ny)
39+
x̂d, x̂s, ŷ, Ŷs = zeros(nxd), zeros(nxs), zeros(ny), zeros(ny*Hp)
3940
validate_weights(model, Hp, Hc, Mwt, Nwt, Lwt, Cwt, ru)
4041
M_Hp = Diagonal{Float64}(repeat(Mwt, Hp))
4142
N_Hc = Diagonal{Float64}(repeat(Nwt, Hc))
@@ -55,7 +56,7 @@ struct LinMPC{S<:StateEstimator} <: PredictiveController
5556
ΔŨ = zeros(nvar)
5657
mpc = new(
5758
estim, optim, con,
58-
ΔŨ, x̂d, x̂s, ŷ,
59+
ΔŨ, x̂d, x̂s, ŷ, Ŷs,
5960
Hp, Hc,
6061
M_Hp, Ñ_Hc, L_Hp, Cwt, R̂u, R̂y,
6162
S̃_Hp, T_Hp, T_Hc,

src/controller/nonlinmpc.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct NonLinMPC{S<:StateEstimator, JEFunc<:Function} <: PredictiveController
66
x̂d::Vector{Float64}
77
x̂s::Vector{Float64}
88
::Vector{Float64}
9+
Ŷs::Vector{Float64}
910
Hp::Int
1011
Hc::Int
1112
M_Hp::Diagonal{Float64, Vector{Float64}}
@@ -39,7 +40,7 @@ struct NonLinMPC{S<:StateEstimator, JEFunc<:Function} <: PredictiveController
3940
) where {S<:StateEstimator, JEFunc<:Function}
4041
model = estim.model
4142
nu, nxd, nxs, ny, nd = model.nu, model.nx, estim.nxs, model.ny, model.nd
42-
x̂d, x̂s, ŷ = zeros(nxd), zeros(nxs), zeros(ny)
43+
x̂d, x̂s, ŷ, Ŷs = zeros(nxd), zeros(nxs), zeros(ny), zeros(ny*Hp)
4344
validate_weights(model, Hp, Hc, Mwt, Nwt, Lwt, Cwt, ru, Ewt)
4445
M_Hp = Diagonal(convert(Vector{Float64}, repeat(Mwt, Hp)))
4546
N_Hc = Diagonal(convert(Vector{Float64}, repeat(Nwt, Hc)))
@@ -59,7 +60,7 @@ struct NonLinMPC{S<:StateEstimator, JEFunc<:Function} <: PredictiveController
5960
ΔŨ = zeros(nvar)
6061
mpc = new(
6162
estim, optim, con,
62-
ΔŨ, x̂d, x̂s, ŷ,
63+
ΔŨ, x̂d, x̂s, ŷ, Ŷs,
6364
Hp, Hc,
6465
M_Hp, Ñ_Hc, L_Hp, Cwt, Ewt, JE, R̂u, R̂y,
6566
S̃_Hp, T_Hp, T_Hc,
@@ -358,14 +359,14 @@ Evaluate ``\\mathbf{Ŷ}`` when `model` is not a [`LinModel`](@ref).
358359
function evalŶ(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
359360
ΔŨ = collect(ΔŨ) # convert NTuple to Vector
360361
U0 = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0)
361-
Ŷd0 = Vector{T}(undef, model.ny*mpc.Hp)
362+
Ŷd = Vector{T}(undef, model.ny*mpc.Hp)
362363
x̂d::Vector{T} = copy(mpc.x̂d)
363364
d0 = mpc.d0
364365
for j=1:mpc.Hp
365366
u0 = U0[(1 + model.nu*(j-1)):(model.nu*j)]
366367
x̂d[:] = f(model, x̂d, u0, d0)
367368
d0 = mpc.D̂0[(1 + model.nd*(j-1)):(model.nd*j)]
368-
Ŷd0[(1 + model.ny*(j-1)):(model.ny*j)] = h(model, x̂d, d0)
369+
Ŷd[(1 + model.ny*(j-1)):(model.ny*j)] = h(model, x̂d, d0) + model.yop
369370
end
370-
return Ŷd0 + mpc.F # F = Yop + Ŷs
371+
return Ŷd + mpc.Ŷs
371372
end

src/predictive_control.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ function moveinput!(
251251
ym::Union{Vector{<:Real}, Nothing} = nothing
252252
)
253253
getestimates!(mpc, mpc.estim, ym, d)
254-
ŷs, Ŷs = predictstoch!(mpc, mpc.estim, d, ym)
255-
p = initpred!(mpc, mpc.estim.model, d, D̂, Ŷs, R̂y)
254+
predictstoch!(mpc, mpc.estim, d, ym)
255+
p = initpred!(mpc, mpc.estim.model, d, D̂, R̂y)
256256
linconstraint!(mpc, mpc.estim.model)
257257
ΔŨ, _ = optim_objective!(mpc, p)
258258
Δu = ΔŨ[1:mpc.estim.model.nu] # receding horizon principle: only Δu(k) is used (1st one)
@@ -316,11 +316,14 @@ end
316316
"""
317317
predictstoch!(mpc, estim::StateEstimator, x̂s, d, _ )
318318
319-
Predict the current `ŷs` and future `Ŷs` stochastic model outputs over `Hp`.
319+
Predict the future `Ŷs` stochastic model outputs over `Hp`.
320320
321321
See [`init_stochpred`](@ref) for details on `Ŷs` and `Ks` matrices.
322322
"""
323-
predictstoch!(mpc, estim::StateEstimator, _ , _ ) = (estim.Cs*mpc.x̂s, mpc.Ks*mpc.x̂s)
323+
function predictstoch!(mpc, estim::StateEstimator, _ , _ )
324+
mpc.Ŷs[:] = mpc.Ks*mpc.x̂s
325+
return mpc.Ŷs
326+
end
324327

325328
"""
326329
predictstoch!(mpc, estim::InternalModel, x̂s, d, ym )
@@ -331,8 +334,8 @@ function predictstoch!(mpc, estim::InternalModel, d, ym )
331334
ŷd = h(estim.model, mpc.x̂d, d - estim.model.dop) + estim.model.yop
332335
ŷs = zeros(estim.model.ny)
333336
ŷs[estim.i_ym] = ym - ŷd[estim.i_ym] # ŷs=0 for unmeasured outputs
334-
Ŷs = mpc.Ks*mpc.x̂s + mpc.Ps*ŷs
335-
return ŷs, Ŷs
337+
mpc.Ŷs[:] = mpc.Ks*mpc.x̂s + mpc.Ps*ŷs
338+
return mpc.Ŷs
336339
end
337340

338341

@@ -343,8 +346,8 @@ Init linear model prediction matrices `F`, `q̃` and `p`.
343346
344347
See [`init_deterpred`](@ref) and [`init_quadprog`](@ref) for the definition of the matrices.
345348
"""
346-
function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, Ŷs, R̂y)
347-
mpc.F[:] = mpc.Kd*mpc.x̂d + mpc.Q*mpc.estim.lastu0 + Ŷs + mpc.Yop
349+
function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y)
350+
mpc.F[:] = mpc.Kd*mpc.x̂d + mpc.Q*mpc.estim.lastu0 + mpc.Ŷs + mpc.Yop
348351
if model.nd 0
349352
mpc.d0[:], mpc.D̂0[:] = d - model.dop, D̂ - mpc.Dop
350353
mpc.F[:] = mpc.F + mpc.G*mpc.d0 + mpc.J*mpc.D̂0
@@ -365,15 +368,12 @@ end
365368
@doc raw"""
366369
initpred!(mpc::PredictiveController, model::SimModel, d, D̂, Ŷs, R̂y )
367370
368-
Init `F`, `d0` and `D̂0` prediction matrices when model is not a [`LinModel`](@ref).
371+
Init `d0` and `D̂0` prediction matrices when model is not a [`LinModel`](@ref).
369372
370-
In such a case, the constant matrix is ``\mathbf{F = Ŷ_s + Y_{op}}``, thus it
371-
incorporates the stochastic predictions and the output operating point ``\mathbf{y_{op}}``
372-
repeated over ``H_p``. `d0` and `D̂0` are the measured disturbances and its predictions
373-
without the operating points ``\mathbf{d_{op}}``.
373+
`d0` and `D̂0` are the measured disturbances and its predictions without the operating points
374+
``\mathbf{d_{op}}``.
374375
"""
375-
function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, Ŷs , R̂y )
376-
mpc.F[:] = Ŷs + mpc.Yop
376+
function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y)
377377
if model.nd 0
378378
mpc.d0[:], mpc.D̂0[:] = d - model.dop, D̂ - mpc.Dop
379379
end
@@ -600,7 +600,7 @@ function init_deterpred(model::SimModel, Hp, Hc)
600600
J = zeros(0, nd*Hp)
601601
Kd = zeros(0, nx)
602602
Q = zeros(0, nu)
603-
F = zeros(ny*Hp) # dummy value (updated just before optimization)
603+
F = zeros(0)
604604
return E, F, G, J, Kd, Q
605605
end
606606

0 commit comments

Comments
 (0)