Skip to content

Commit 61ab8a9

Browse files
committed
debug: only update objective linear coef before solving LinMPC
1 parent 36618eb commit 61ab8a9

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

docs/src/public/predictive_control.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ over the prediction horizon ``H_p`` are defined as:
2525
\end{bmatrix} \: , \quad
2626
\mathbf{Ŷ} = \begin{bmatrix}
2727
\mathbf{ŷ}(k+1) \\ \mathbf{ŷ}(k+2) \\ \vdots \\ \mathbf{ŷ}(k+H_p)
28-
\end{bmatrix} \: \text{and} \quad
28+
\end{bmatrix} \quad \text{and} \quad
2929
\mathbf{R̂_y} = \begin{bmatrix}
3030
\mathbf{r̂_y}(k+1) \\ \mathbf{r̂_y}(k+2) \\ \vdots \\ \mathbf{r̂_y}(k+H_p)
3131
\end{bmatrix}
@@ -36,7 +36,7 @@ The vectors for the manipulated input ``\mathbf{u}`` are shifted by one time ste
3636
```math
3737
\mathbf{U} = \begin{bmatrix}
3838
\mathbf{u}(k+0) \\ \mathbf{u}(k+1) \\ \vdots \\ \mathbf{u}(k+H_p-1)
39-
\end{bmatrix} \: \text{and} \quad
39+
\end{bmatrix} \quad \text{and} \quad
4040
\mathbf{R̂_u} = \begin{bmatrix}
4141
\mathbf{r_u} \\ \mathbf{r_u} \\ \vdots \\ \mathbf{r_u}
4242
\end{bmatrix}

example/juMPC.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ using Preferences
1010
set_preferences!(ModelPredictiveControl, "precompile_workload" => false; force=true)
1111

1212

13-
#using JuMP, DAQP
13+
using JuMP, DAQP
1414
#using JuMP, HiGHS
1515
using JuMP, Ipopt
1616
using LinearAlgebra

src/controller/linmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ function init_optimization!(mpc::LinMPC)
217217
return nothing
218218
end
219219

220-
"Set quadratic programming `q̃` vector just before optimization."
221-
function set_objective!(mpc::LinMPC, ΔŨ)
222-
set_objective_function(mpc.optim, obj_quadprog(ΔŨ, mpc.P̃, mpc.q̃))
220+
"For [`LinMPC`](@ref), set the QP linear coefficient `q̃` just before optimization."
221+
function set_objective_linear_coef!(mpc::LinMPC, ΔŨvar)
222+
set_objective_coefficient.(mpc.optim, ΔŨvar, mpc.)
223223
return nothing
224224
end

src/predictive_control.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -525,18 +525,18 @@ function optim_objective!(mpc::PredictiveController)
525525
# if soft constraints, append the last slack value ϵ_{k-1}:
526526
!isinf(mpc.C) && (ΔŨ0 = [ΔŨ0; lastΔŨ[end]])
527527
set_start_value.(ΔŨvar, ΔŨ0)
528-
set_objective!(mpc, ΔŨvar)
529-
#try
528+
set_objective_linear_coef!(mpc, ΔŨvar)
529+
try
530530
optimize!(optim)
531-
#catch err
532-
# if isa(err, MOI.UnsupportedAttribute{MOI.VariablePrimalStart})
533-
# # reset_optimizer to unset warm-start, set_start_value.(nothing) seems buggy
534-
# MOIU.reset_optimizer(optim)
535-
# optimize!(optim)
536-
# else
537-
# rethrow(err)
538-
# end
539-
#end
531+
catch err
532+
if isa(err, MOI.UnsupportedAttribute{MOI.VariablePrimalStart})
533+
# reset_optimizer to unset warm-start, set_start_value.(nothing) seems buggy
534+
MOIU.reset_optimizer(optim)
535+
optimize!(optim)
536+
else
537+
rethrow(err)
538+
end
539+
end
540540
status = termination_status(optim)
541541
if !(status == OPTIMAL || status == LOCALLY_SOLVED)
542542
@warn "MPC termination status not OPTIMAL or LOCALLY_SOLVED ($status)"
@@ -546,8 +546,8 @@ function optim_objective!(mpc::PredictiveController)
546546
return mpc.ΔŨ
547547
end
548548

549-
"By default, no change to the objective function."
550-
set_objective!(::PredictiveController, _ ) = nothing
549+
"By default, no need to modify the objective function."
550+
set_objective_linear_coef!(::PredictiveController, _ ) = nothing
551551

552552
@doc raw"""
553553
init_ΔUtoU(nu, Hp, Hc, C, c_Umin, c_Umax)

0 commit comments

Comments
 (0)