Skip to content

Commit 11dc6b1

Browse files
committed
added: reduce allocations PredictiveController with new buffer fields
1 parent 4dbd354 commit 11dc6b1

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/controller/execute.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ They are computed with these equations using in-place operations:
191191
function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂u)
192192
mul!(mpc.T_lastu0, mpc.T, mpc.estim.lastu0)
193193
ŷ, F, q̃, r = mpc.ŷ, mpc.F, mpc.q̃, mpc.r
194+
Cy, Cu = mpc.buffer.Cy, mpc.buffer.Cu
194195
ŷ .= evaloutput(mpc.estim, d)
195196
predictstoch!(mpc, mpc.estim) # init mpc.F with Ŷs for InternalModel
196197
F .+= mpc.B
@@ -206,18 +207,18 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂
206207
end
207208
# --- output setpoint tracking term ---
208209
mpc.R̂y .= R̂y
209-
Cy = F .- (R̂y .- mpc.Yop)
210+
Cy .= F .- (R̂y .- mpc.Yop)
210211
M_Hp_Ẽ = mpc.weights.M_Hp*mpc.
211-
mul!(q̃, M_Hp_Ẽ', Cy)
212+
mul!(q̃, M_Hp_Ẽ', Cy) # q̃ = M_Hp*Ẽ'*Cy
212213
r .= dot(Cy, mpc.weights.M_Hp, Cy)
213214
# --- input setpoint tracking term ---
214215
mpc.R̂u .= R̂u
215-
Cu = mpc.T_lastu0 .- (R̂u .- mpc.Uop)
216+
Cu .= mpc.T_lastu0 .- (R̂u .- mpc.Uop)
216217
L_Hp_S̃ = mpc.weights.L_Hp*mpc.
217-
mul!(q̃, L_Hp_S̃', Cu, 1, 1)
218+
mul!(q̃, L_Hp_S̃', Cu, 1, 1) # q̃ = q̃ + L_Hp*S̃'*Cu
218219
r .+= dot(Cu, mpc.weights.L_Hp, Cu)
219220
# --- finalize ---
220-
lmul!(2, q̃)
221+
lmul!(2, q̃) # q̃ = 2*q̃
221222
return nothing
222223
end
223224

src/predictive_control.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ struct PredictiveControllerBuffer{NT<:Real}
2323
u ::Vector{NT}
2424
R̂y::Vector{NT}
2525
::Vector{NT}
26+
Cy::Vector{NT}
27+
Cu::Vector{NT}
2628
empty::Vector{NT}
2729
end
2830

@@ -37,8 +39,10 @@ function PredictiveControllerBuffer{NT}(nu::Int, ny::Int, nd::Int, Hp::Int) wher
3739
u = Vector{NT}(undef, nu)
3840
R̂y = Vector{NT}(undef, ny*Hp)
3941
= Vector{NT}(undef, nd*Hp)
42+
Cy = Vector{NT}(undef, ny*Hp)
43+
Cu = Vector{NT}(undef, nu*Hp)
4044
empty = Vector{NT}(undef, 0)
41-
return PredictiveControllerBuffer{NT}(u, R̂y, D̂, empty)
45+
return PredictiveControllerBuffer{NT}(u, R̂y, D̂, Cy, Cu, empty)
4246
end
4347

4448
include("controller/construct.jl")

0 commit comments

Comments
 (0)