Skip to content

Commit 9984e43

Browse files
committed
debug: specialized obj_nonlinprog! for LinModel in NonLinMPC
The two methods had different number of argument because of a refactor. It was always thus fallbacking to the one for `NonLinModel`. It works but it is less efficient.
1 parent 4ab5d3b commit 9984e43

3 files changed

Lines changed: 23 additions & 23 deletions

File tree

src/controller/execute.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ function getinfo(mpc::PredictiveController{NT}) where NT<:Real
155155
U .= U0 .+ mpc.Uop
156156
Ŷ .= Ŷ0 .+ mpc.Yop
157157
D̂ .= mpc.D̂0 + mpc.Dop
158-
J = obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
158+
J = obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ, Z̃)
159159
Ŷs = similar(mpc.Yop)
160160
predictstoch!(Ŷs, mpc, mpc.estim)
161161
info[:ΔU] = Z̃[1:mpc.Hc*model.nu]
@@ -387,32 +387,15 @@ end
387387
iszero_nc(mpc::PredictiveController) = (mpc.con.nc == 0)
388388

389389
"""
390-
obj_nonlinprog!( _ , _ , mpc::PredictiveController, model::LinModel, Ue, Ŷe, _ , Z̃)
391-
392-
Nonlinear programming objective function when `model` is a [`LinModel`](@ref).
393-
394-
The method is called by the nonlinear optimizer of [`NonLinMPC`](@ref) controllers. It can
395-
also be called on any [`PredictiveController`](@ref)s to evaluate the objective function `J`
396-
at specific `Ue`, `Ŷe` and `Z̃`, values. It does not mutate any argument.
397-
"""
398-
function obj_nonlinprog!(
399-
_, _, mpc::PredictiveController, model::LinModel, Ue, Ŷe, _ , Z̃::AbstractVector{NT}
400-
) where NT <: Real
401-
JQP = obj_quadprog(Z̃, mpc.H̃, mpc.q̃) + mpc.r[]
402-
E_JE = obj_econ(mpc, model, Ue, Ŷe)
403-
return JQP + E_JE
404-
end
405-
406-
"""
407-
obj_nonlinprog!(Ȳ, Ū, mpc::PredictiveController, model::SimModel, Ue, Ŷe, ΔŨ)
390+
obj_nonlinprog!(Ȳ, Ū, mpc::PredictiveController, model::SimModel, Ue, Ŷe, ΔŨ, Z̃)
408391
409392
Nonlinear programming objective method when `model` is not a [`LinModel`](@ref). The
410393
function `dot(x, A, x)` is a performant way of calculating `x'*A*x`. This method mutates
411394
`Ȳ` and `Ū` arguments, without assuming any initial values (it recuperates the values in
412395
`Ŷe` and `Ue` arguments).
413396
"""
414397
function obj_nonlinprog!(
415-
Ȳ, Ū, mpc::PredictiveController, model::SimModel, Ue, Ŷe, ΔŨ::AbstractVector{NT}
398+
Ȳ, Ū, mpc::PredictiveController, model::SimModel, Ue, Ŷe, ΔŨ, ::AbstractVector{NT}
416399
) where NT<:Real
417400
nu, ny = model.nu, model.ny
418401
# --- output setpoint tracking term ---
@@ -442,6 +425,23 @@ function obj_nonlinprog!(
442425
return JR̂y + JΔŨ + JR̂u + E_JE
443426
end
444427

428+
"""
429+
obj_nonlinprog!( _ , _ , mpc::PredictiveController, model::LinModel, Ue, Ŷe, ΔŨ, Z̃)
430+
431+
Nonlinear programming objective function when `model` is a [`LinModel`](@ref).
432+
433+
The method is called by the nonlinear optimizer of [`NonLinMPC`](@ref) controllers. It can
434+
also be called on any [`PredictiveController`](@ref)s to evaluate the objective function `J`
435+
at specific `Ue`, `Ŷe` and `Z̃`, values. It does not mutate any argument.
436+
"""
437+
function obj_nonlinprog!(
438+
_, _, mpc::PredictiveController, model::LinModel, Ue, Ŷe, _ , Z̃::AbstractVector{NT}
439+
) where NT <: Real
440+
JQP = obj_quadprog(Z̃, mpc.H̃, mpc.q̃) + mpc.r[]
441+
E_JE = obj_econ(mpc, model, Ue, Ŷe)
442+
return JQP + E_JE
443+
end
444+
445445
"No custom nonlinear constraints `gc` by default, return `gc` unchanged."
446446
con_custom!(gc, ::PredictiveController, _ , _, _ ) = gc
447447

src/controller/legacy.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
3232
# ---------------------- objective function -------------------------------------------
3333
function Jfunc!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq)
3434
update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
35-
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
35+
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ, Z̃)
3636
end
3737
Z̃_∇J = fill(myNaN, nZ̃) # NaN to force update_predictions! at first call
3838
∇J_context = (

src/controller/nonlinmpc.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ function addinfo!(info, mpc::NonLinMPC{NT}) where NT<:Real
585585
)
586586
function J!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq)
587587
update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
588-
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
588+
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ, Z̃)
589589
end
590590
if !isnothing(mpc.hessian)
591591
_, ∇J, ∇²J = value_gradient_and_hessian(J!, mpc.hessian, mpc.Z̃, J_cache...)
@@ -800,7 +800,7 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J
800800
geq::Vector{JNT} = zeros(JNT, neq)
801801
function J!(Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq)
802802
update_predictions!(ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
803-
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ)
803+
return obj_nonlinprog!(Ŷ0, U0, mpc, model, Ue, Ŷe, ΔŨ, Z̃)
804804
end
805805
Z̃_J = fill(myNaN, nZ̃) # NaN to force update at first call
806806
J_cache = (

0 commit comments

Comments
 (0)