Skip to content

Commit d9a9f3d

Browse files
committed
added: skip computations in extended_prediction when possible for NonLinMPC
1 parent e093187 commit d9a9f3d

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

src/controller/execute.jl

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,11 @@ function getinfo(mpc::PredictiveController{NT}) where NT<:Real
121121
Ȳ, Ū = similar(mpc.Yop), similar(mpc.Uop)
122122
Ŷe, Ue = Vector{NT}(undef, nŶe), Vector{NT}(undef, nUe)
123123
Ŷ0, x̂0end = predict!(Ŷ0, x̂0, x̂0next, u0, û0, mpc, model, mpc.ΔŨ)
124-
Ue, Ŷe = extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, mpc.ΔŨ)
124+
Ŷe, Ue = extended_predictions!(Ŷe, Ue, Ū, mpc, model, Ŷ0, mpc.ΔŨ)
125125
J = obj_nonlinprog!(Ȳ, Ū, mpc, model, Ue, Ŷe, mpc.ΔŨ)
126-
U =
127-
U .= @views Ue[1:end-model.nu]
128-
=
129-
Ŷ .= @views Ŷe[model.ny+1:end]
126+
U, Ŷ = Ū, Ȳ
127+
U .= mul!(U, mpc.S̃, ΔŨ) .+ mpc.T_lastu
128+
Ŷ .= Ŷ0 .+ mpc.Yop
130129
oldF = copy(mpc.F)
131130
F = predictstoch!(mpc, mpc.estim)
132131
Ŷs .= F # predictstoch! init mpc.F with Ŷs value if estim is an InternalModel
@@ -376,24 +375,38 @@ function predict!(Ŷ0, x̂0, x̂0next, u0, û0, mpc::PredictiveController, mod
376375
end
377376

378377
"""
379-
extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ) -> Ŷe, Ue
378+
extended_predictions!(Ŷe, Ue, Ū, mpc, model, Ŷ0, ΔŨ) -> Ŷe, Ue
380379
381-
Compute the extended vectors `Ue` and `Ŷe` and for the nonlinear optimization.
380+
Compute the extended vectors `Ŷe` and `Ue` for the nonlinear optimization.
382381
383-
The function mutates `Ue`, `Ŷe` and `Ū` in arguments, without assuming any initial values.
382+
The function mutates `Ŷe`, `Ue` and `Ū` in arguments, without assuming any initial values
383+
for them. Using `nocustomfcts = mpc.weights.iszero_E && mpc.con.nc == 0`, there is two
384+
special cases in which `Ŷe`, `Ue` and `Ū` are not mutated:
385+
386+
- If `mpc.weights.iszero_M_Hp[] && nocustomfcts`, the `Ŷe` vector is not computed to reduce
387+
the burden in the optimization problem.
388+
- If `mpc.weights.iszero_L_Hc[] && nocustomfcts`, the `Ue` vector is not computed for the
389+
same reason as above.
384390
"""
385-
function extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
391+
function extended_predictions!(Ŷe, Ue, Ū, mpc, model, Ŷ0, ΔŨ)
386392
ny, nu = model.ny, model.nu
387-
# --- extended manipulated inputs Ue = [U; u(k+Hp-1)] ---
388-
U =
389-
U .= mul!(U, mpc.S̃, ΔŨ) .+ mpc.T_lastu
390-
Ue[1:end-nu] .= U
391-
# u(k + Hp) = u(k + Hp - 1) since Δu(k+Hp) = 0 (because Hc ≤ Hp):
392-
Ue[end-nu+1:end] .= @views U[end-nu+1:end]
393+
nocustomfcts = (mpc.weights.iszero_E && mpc.con.nc==0)
393394
# --- extended output predictions Ŷe = [ŷ(k); Ŷ] ---
394-
Ŷe[1:ny] .= mpc.
395-
Ŷe[ny+1:end] .= Ŷ0 .+ mpc.Yop
396-
return Ue, Ŷe
395+
if !(mpc.weights.iszero_M_Hp[] && nocustomfcts)
396+
Ŷe[1:ny] .= mpc.
397+
Ŷe[ny+1:end] .= Ŷ0 .+ mpc.Yop
398+
end
399+
# --- extended manipulated inputs Ue = [U; u(k+Hp-1)] ---
400+
if !(mpc.weights.iszero_L_Hp[] && nocustomfcts)
401+
U =
402+
U .= mul!(U, mpc.S̃, ΔŨ) .+ mpc.T_lastu
403+
Ue[1:end-nu] .= U
404+
# u(k + Hp) = u(k + Hp - 1) since Δu(k+Hp) = 0 (because Hc ≤ Hp):
405+
Ue[end-nu+1:end] .= @views U[end-nu+1:end]
406+
else
407+
print("yo")
408+
end
409+
return Ŷe, Ue
397410
end
398411

399412
"""

src/controller/nonlinmpc.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
526526
u0, û0 = get_tmp(u0_cache, ΔŨ1), get_tmp(û0_cache, ΔŨ1)
527527
gc, g = get_tmp(gc_cache, ΔŨ1), get_tmp(g_cache, ΔŨ1)
528528
Ŷ0, x̂0end = predict!(Ȳ, x̂0, x̂0next, u0, û0, mpc, model, ΔŨ)
529-
Ue, Ŷe = extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
529+
Ŷe, Ue = extended_predictions!(Ŷe, Ue, Ū, mpc, model, Ŷ0, ΔŨ)
530530
ϵ = (nϵ 0) ? ΔŨ[end] : zero(T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
531531
gc = con_custom!(gc, mpc, Ue, Ŷe, ϵ)
532532
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)

0 commit comments

Comments
 (0)