@@ -494,10 +494,9 @@ If first warm-starts the solver with [`set_warmstart!`](@ref). It then calls
494494"""
495495function optim_objective! (mpc:: PredictiveController{NT} ) where {NT<: Real }
496496 model, optim = mpc. estim. model, mpc. optim
497- nu, Hc = model. nu, mpc. Hc
498497 Z̃var:: Vector{JuMP.VariableRef} = optim[:Z̃var ]
499498 Z̃s = set_warmstart! (mpc, mpc. transcription, Z̃var)
500- set_objective_linear_coef! (mpc, Z̃var)
499+ set_objective_linear_coef! (mpc, model, Z̃var)
501500 try
502501 JuMP. optimize! (optim)
503502 catch err
@@ -534,8 +533,14 @@ function optim_objective!(mpc::PredictiveController{NT}) where {NT<:Real}
534533 return mpc. Z̃
535534end
536535
537- " By default, no need to modify the objective function."
538- set_objective_linear_coef! (:: PredictiveController , _ ) = nothing
536+ " By default, no need to update the objective function."
537+ set_objective_linear_coef! (:: PredictiveController , :: SimModel , _ ) = nothing
538+
539+ " Update the linear coefficients of the quadratic objective with `mpc.q̃` if applicable."
540+ function set_objective_linear_coef! (mpc:: PredictiveController , :: LinModel , Z̃var)
541+ mpc. weights. iszero_E && JuMP. set_objective_coefficient (mpc. optim, Z̃var, mpc. q̃)
542+ return nothing
543+ end
539544
540545"""
541546 preparestate!(mpc::PredictiveController, ym, d=[]) -> x̂
@@ -794,9 +799,15 @@ function setmodel_controller!(mpc::PredictiveController, uop_old, x̂op_old)
794799 JuMP. delete (optim, optim[:linconstrainteq ])
795800 JuMP. unregister (optim, :linconstrainteq )
796801 @constraint (optim, linconstrainteq, con. Aeq* Z̃var .== con. beq)
797- set_objective_hessian! (mpc, Z̃var)
802+ set_objective_hessian! (mpc, model, Z̃var)
798803 return nothing
799804end
800805
801- " No need to set the objective Hessian by default (only needed for quadratic optimization)."
802- set_objective_hessian! (:: PredictiveController , _ ) = nothing
806+ " No need to set the objective Hessian by default (only needed for quadratic objective)."
807+ set_objective_hessian! (:: PredictiveController , :: SimModel , _ ) = nothing
808+
809+ " Set the objective Hessian with `mpc.H̃` if the objective is quadratic."
810+ function set_objective_hessian! (mpc:: PredictiveController , :: LinModel , Z̃var)
811+ mpc. weights. iszero_E && @objective (mpc. optim, Min, obj_quadprog (Z̃var, mpc. H̃, mpc. q̃))
812+ return nothing
813+ end
0 commit comments