Skip to content

Commit 2408604

Browse files
committed
added: new mpc.buffer.ΔŨ buffer to reduce the allocations
1 parent 8382790 commit 2408604

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

src/controller/execute.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ See also [`LinMPC`](@ref), [`ExplicitMPC`](@ref), [`NonLinMPC`](@ref).
3535
in the future by default or ``\mathbf{d̂}(k+j)=\mathbf{d}(k)`` for ``j=1`` to ``H_p``.
3636
- `R̂y=repeat(ry, mpc.Hp)` or *`Rhaty`* : predicted output setpoints ``\mathbf{R̂_y}``, constant
3737
in the future by default or ``\mathbf{r̂_y}(k+j)=\mathbf{r_y}(k)`` for ``j=1`` to ``H_p``.
38-
- `R̂u=mpc.Uop` or *`Rhatu`* : predicted manipulated input setpoints, constant in the future
39-
by default or ``\mathbf{r̂_u}(k+j)=\mathbf{u_{op}}`` for ``j=0`` to ``H_p-1``.
38+
- `R̂u=mpc.Uop` or *`Rhatu`* : predicted manipulated input setpoints ``\mathbf{R̂_u}``, constant
39+
in the future by default or ``\mathbf{r̂_u}(k+j)=\mathbf{u_{op}}`` for ``j=0`` to ``H_p-1``.
4040
4141
# Examples
4242
```jldoctest
@@ -127,9 +127,9 @@ function getinfo(mpc::PredictiveController{NT}) where NT<:Real
127127
=
128128
Ŷ .= @views Ŷe[model.ny+1:end]
129129
oldF = copy(mpc.F)
130-
predictstoch!(mpc, mpc.estim)
131-
Ŷs .= mpc.F # predictstoch! init mpc.F with Ŷs value if estim is an InternalModel
132-
mpc.F .= oldF # restore old F value
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
133133
info[:ΔU] = mpc.ΔŨ[1:mpc.Hc*model.nu]
134134
info[] = mpc.== 1 ? mpc.ΔŨ[end] : zero(NT)
135135
info[:J] = J
@@ -479,12 +479,14 @@ solution. A failed optimization prints an `@error` log in the REPL and returns t
479479
warm-start value.
480480
"""
481481
function optim_objective!(mpc::PredictiveController{NT}) where {NT<:Real}
482-
optim = mpc.optim
483-
model = mpc.estim.model
482+
model, optim = mpc.estim.model, mpc.optim
483+
nu, Hc = model.nu, mpc.Hc
484484
ΔŨvar::Vector{JuMP.VariableRef} = optim[:ΔŨvar]
485485
# initial ΔŨ (warm-start): [Δu_{k-1}(k); Δu_{k-1}(k+1); ... ; 0_{nu × 1}; ϵ_{k-1}]
486-
ϵ0 = (mpc.== 1) ? mpc.ΔŨ[end] : empty(mpc.ΔŨ)
487-
ΔŨ0 = [mpc.ΔŨ[(model.nu+1):(mpc.Hc*model.nu)]; zeros(NT, model.nu); ϵ0]
486+
ΔŨ0 = mpc.buffer.ΔŨ
487+
ΔŨ0[1:(Hc*nu-nu)] .= @views mpc.ΔŨ[nu+1:Hc*nu]
488+
ΔŨ0[(Hc*nu-nu+1):(Hc*nu)] .= 0
489+
mpc.== 1 && (ΔŨ0[end] = mpc.ΔŨ[end])
488490
JuMP.set_start_value.(ΔŨvar, ΔŨ0)
489491
set_objective_linear_coef!(mpc, ΔŨvar)
490492
try

src/predictive_control.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ abstract type PredictiveController{NT<:Real} end
2121

2222
struct PredictiveControllerBuffer{NT<:Real}
2323
u ::Vector{NT}
24+
ΔŨ::Vector{NT}
2425
::Vector{NT}
2526
::Vector{NT}
2627
U ::Vector{NT}
@@ -39,14 +40,16 @@ The buffer is used to store intermediate results during computation without allo
3940
function PredictiveControllerBuffer{NT}(
4041
nu::Int, ny::Int, nd::Int, Hp::Int, Hc::Int, nϵ::Int
4142
) where NT <: Real
43+
nΔŨ = nu*Hc +
4244
u = Vector{NT}(undef, nu)
45+
ΔŨ = Vector{NT}(undef, nΔŨ)
4346
= Vector{NT}(undef, nd*Hp)
4447
= Vector{NT}(undef, ny*Hp)
4548
U = Vector{NT}(undef, nu*Hp)
46-
= Matrix{NT}(undef, ny*Hp, nu*Hc +)
47-
= Matrix{NT}(undef, nu*Hp, nu*Hc +)
49+
= Matrix{NT}(undef, ny*Hp, nΔŨ)
50+
= Matrix{NT}(undef, nu*Hp, nΔŨ)
4851
empty = Vector{NT}(undef, 0)
49-
return PredictiveControllerBuffer{NT}(u, D̂, Ŷ, U, Ẽ, S̃, empty)
52+
return PredictiveControllerBuffer{NT}(u, ΔŨ, D̂, Ŷ, U, Ẽ, S̃, empty)
5053
end
5154

5255
include("controller/construct.jl")

0 commit comments

Comments
 (0)