Skip to content

Commit a18dfa5

Browse files
committed
rollback isfatal with termination status
1 parent a580956 commit a18dfa5

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

example/juMPC.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ initstate!(ssKalmanFilter1,[0,0],[2,1])
6464
nx = linModel4.nx
6565
kf = KalmanFilter(linModel4, σP0=10*ones(nx), σQ=0.01*ones(nx), σR=[0.1, 0.1], σQ_int=0.05*ones(2), σP0_int=10*ones(2))
6666

67-
mpc = LinMPC(kf, Hp=15, Hc=1, Mwt=[1, 1] , Nwt=[0.1, 0.1], Cwt=1e5)
67+
mpc = LinMPC(kf, Hp=15, Hc=1, Mwt=[1, 1] , Nwt=[0.1, 0.1], Cwt=1e6)
6868

6969
setconstraint!(mpc, c_umin=[0,0], c_umax=[0,0])
7070
setconstraint!(mpc, c_ŷmin=[1,1], c_ŷmax=[1,1])

src/predictive_control.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,18 +572,14 @@ function optim_objective!(mpc::LinMPC, b, q̃, p)
572572
ΔŨ0 = [lastΔŨ[(mpc.model.nu+1):(mpc.Hc*mpc.model.nu)]; zeros(mpc.model.nu)]
573573
# if soft constraints, append the last slack value ϵ_{k-1}:
574574
!isinf(mpc.C) && (ΔŨ0 = [ΔŨ0; lastΔŨ[end]])
575-
set_start_value.(ΔŨ, ΔŨ0)
575+
#set_start_value.(ΔŨ, ΔŨ0)
576576
optimize!(optmodel)
577577
status = termination_status(optmodel)
578578
if !(status == OPTIMAL || status == LOCALLY_SOLVED)
579-
@warn "MPC termination status not OPTIMAL or LOCALLY_SOLVER ($status)"
579+
@warn "MPC termination status not OPTIMAL or LOCALLY_SOLVED ($status)"
580580
@debug solution_summary(optmodel)
581581
end
582-
if has_values(optmodel)
583-
ΔŨ = value.(ΔŨ)
584-
else # if error, we take last value :
585-
ΔŨ = ΔŨ0
586-
end
582+
ΔŨ = isfatal(status) ? ΔŨ0 : value.(ΔŨ) # fatal status : use last value
587583
J = objective_value(optmodel) + p # optimal objective value by adding constant p
588584
return ΔŨ, J
589585
end
@@ -973,6 +969,16 @@ function Base.show(io::IO, mpc::PredictiveController)
973969
print(io, " $(mpc.estim.model.nd) measured disturbances d")
974970
end
975971

972+
"Verify that the solver termination status means 'no solution available'."
973+
function isfatal(status::TerminationStatusCode)
974+
fatalstatuses = [
975+
INFEASIBLE, DUAL_INFEASIBLE, LOCALLY_INFEASIBLE, INFEASIBLE_OR_UNBOUNDED,
976+
SLOW_PROGRESS, NUMERICAL_ERROR, INVALID_MODEL, INVALID_OPTION, INTERRUPTED,
977+
OTHER_ERROR
978+
]
979+
return any(status .== fatalstatuses)
980+
end
981+
976982

977983
"Functor allowing callable `PredictiveController` object as an alias for `moveinput!`."
978984
function (mpc::PredictiveController)(

0 commit comments

Comments
 (0)