Skip to content

Commit 9cd4439

Browse files
committed
- added: reduce allocations in PredictiveController with new buffers (bis)
the matrices `buffer.Ẽ` and `buffer.S̃` are added to store intermediary results
1 parent 11dc6b1 commit 9cd4439

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

src/controller/execute.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,33 +192,34 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂
192192
mul!(mpc.T_lastu0, mpc.T, mpc.estim.lastu0)
193193
ŷ, F, q̃, r = mpc.ŷ, mpc.F, mpc.q̃, mpc.r
194194
Cy, Cu = mpc.buffer.Cy, mpc.buffer.Cu
195+
M_Hp_Ẽ, L_Hp_S̃ = mpc.buffer.Ẽ, mpc.buffer.
195196
ŷ .= evaloutput(mpc.estim, d)
196-
predictstoch!(mpc, mpc.estim) # init mpc.F with Ŷs for InternalModel
197-
F .+= mpc.B
198-
mul!(F, mpc.K, mpc.estim.x̂0, 1, 1)
199-
mul!(F, mpc.V, mpc.estim.lastu0, 1, 1)
197+
predictstoch!(mpc, mpc.estim) # init F with Ŷs for InternalModel
198+
F .+= mpc.B # F = F + B
199+
mul!(F, mpc.K, mpc.estim.x̂0, 1, 1) # F = F + K*x̂0
200+
mul!(F, mpc.V, mpc.estim.lastu0, 1, 1) # F = F + V*lastu0
200201
if model.nd 0
201202
mpc.d0 .= d .- model.dop
202203
mpc.D̂0 .=.- mpc.Dop
203204
mpc.D̂e[1:model.nd] .= d
204205
mpc.D̂e[model.nd+1:end] .=
205-
mul!(F, mpc.G, mpc.d0, 1, 1)
206-
mul!(F, mpc.J, mpc.D̂0, 1, 1)
206+
mul!(F, mpc.G, mpc.d0, 1, 1) # F = F + G*d0
207+
mul!(F, mpc.J, mpc.D̂0, 1, 1) # F = F + J*D̂0
207208
end
208209
# --- output setpoint tracking term ---
209210
mpc.R̂y .= R̂y
210211
Cy .= F .- (R̂y .- mpc.Yop)
211-
M_Hp_Ẽ = mpc.weights.M_Hp*mpc.
212-
mul!(q̃, M_Hp_Ẽ', Cy) # q̃ = M_Hp*Ẽ'*Cy
212+
mul!(M_Hp_Ẽ, mpc.weights.M_Hp, mpc.)
213+
mul!(q̃, M_Hp_Ẽ', Cy) # q̃ = M_Hp*Ẽ'*Cy
213214
r .= dot(Cy, mpc.weights.M_Hp, Cy)
214215
# --- input setpoint tracking term ---
215216
mpc.R̂u .= R̂u
216217
Cu .= mpc.T_lastu0 .- (R̂u .- mpc.Uop)
217-
L_Hp_S̃ = mpc.weights.L_Hp*mpc.
218-
mul!(q̃, L_Hp_S̃', Cu, 1, 1) # q̃ = q̃ + L_Hp*S̃'*Cu
218+
mul!(L_Hp_S̃, mpc.weights.L_Hp, mpc.)
219+
mul!(q̃, L_Hp_S̃', Cu, 1, 1) # q̃ = q̃ + L_Hp*S̃'*Cu
219220
r .+= dot(Cu, mpc.weights.L_Hp, Cu)
220221
# --- finalize ---
221-
lmul!(2, q̃) # q̃ = 2*q̃
222+
lmul!(2, q̃) # q̃ = 2*q̃
222223
return nothing
223224
end
224225

@@ -230,7 +231,7 @@ Init `ŷ, F, d0, D̂0, D̂e, R̂y, R̂u` vectors when model is not a [`LinModel
230231
function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u)
231232
mul!(mpc.T_lastu0, mpc.T, mpc.estim.lastu0)
232233
mpc.ŷ .= evaloutput(mpc.estim, d)
233-
predictstoch!(mpc, mpc.estim) # init mpc.F with Ŷs for InternalModel
234+
predictstoch!(mpc, mpc.estim) # init F with Ŷs for InternalModel
234235
if model.nd 0
235236
mpc.d0 .= d .- model.dop
236237
mpc.D̂0 .=.- mpc.Dop

src/controller/explicitmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ struct ExplicitMPC{NT<:Real, SE<:StateEstimator} <: PredictiveController{NT}
6060
Uop, Yop, Dop = repeat(model.uop, Hp), repeat(model.yop, Hp), repeat(model.dop, Hp)
6161
nΔŨ = size(Ẽ, 2)
6262
ΔŨ = zeros(NT, nΔŨ)
63-
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp)
63+
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp, Hc, nϵ)
6464
mpc = new{NT, SE}(
6565
estim,
6666
ΔŨ, ŷ,

src/controller/linmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct LinMPC{
6565
Uop, Yop, Dop = repeat(model.uop, Hp), repeat(model.yop, Hp), repeat(model.dop, Hp)
6666
nΔŨ = size(Ẽ, 2)
6767
ΔŨ = zeros(NT, nΔŨ)
68-
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp)
68+
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp, Hc, nϵ)
6969
mpc = new{NT, SE, JM}(
7070
estim, optim, con,
7171
ΔŨ, ŷ,

src/controller/nonlinmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ struct NonLinMPC{
7979
test_custom_functions(NT, model, JE, gc!, nc, Uop, Yop, Dop, p)
8080
nΔŨ = size(Ẽ, 2)
8181
ΔŨ = zeros(NT, nΔŨ)
82-
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp)
82+
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp, Hc, nϵ)
8383
mpc = new{NT, SE, JM, JEfunc, GCfunc, P}(
8484
estim, optim, con,
8585
ΔŨ, ŷ,

src/predictive_control.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct PredictiveControllerBuffer{NT<:Real}
2525
::Vector{NT}
2626
Cy::Vector{NT}
2727
Cu::Vector{NT}
28+
::Matrix{NT}
29+
::Matrix{NT}
2830
empty::Vector{NT}
2931
end
3032

@@ -35,14 +37,18 @@ Create a buffer for `PredictiveController` objects.
3537
3638
The buffer is used to store intermediate results during computation without allocating.
3739
"""
38-
function PredictiveControllerBuffer{NT}(nu::Int, ny::Int, nd::Int, Hp::Int) where NT <: Real
40+
function PredictiveControllerBuffer{NT}(
41+
nu::Int, ny::Int, nd::Int, Hp::Int, Hc::Int, nϵ::Int
42+
) where NT <: Real
3943
u = Vector{NT}(undef, nu)
4044
R̂y = Vector{NT}(undef, ny*Hp)
4145
= Vector{NT}(undef, nd*Hp)
4246
Cy = Vector{NT}(undef, ny*Hp)
4347
Cu = Vector{NT}(undef, nu*Hp)
48+
= Matrix{NT}(undef, ny*Hp, nu*Hc + nϵ)
49+
= Matrix{NT}(undef, nu*Hp, nu*Hc + nϵ)
4450
empty = Vector{NT}(undef, 0)
45-
return PredictiveControllerBuffer{NT}(u, R̂y, D̂, Cy, Cu, empty)
51+
return PredictiveControllerBuffer{NT}(u, R̂y, D̂, Cy, Cu, Ẽ, S̃, empty)
4652
end
4753

4854
include("controller/construct.jl")

0 commit comments

Comments
 (0)