Skip to content

Commit 6272cc5

Browse files
committed
cleanup JuMP error handling and log
1 parent bde7ad6 commit 6272cc5

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

src/controller/execute.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,19 +316,19 @@ function optim_objective!(mpc::PredictiveController{NT}) where {NT<:Real}
316316
rethrow(err)
317317
end
318318
end
319-
status = termination_status(optim)
320319
ΔŨcurr, ΔŨlast = value.(ΔŨvar), ΔŨ0
321-
if !(status == OPTIMAL || status == LOCALLY_SOLVED)
322-
if isfatal(status)
320+
if !issolved(optim)
321+
status = termination_status(optim)
322+
if iserror(optim)
323323
@error("MPC terminated without solution: returning last solution shifted",
324-
status, ΔŨcurr, ΔŨlast)
324+
status)
325325
else
326326
@warn("MPC termination status not OPTIMAL or LOCALLY_SOLVED: keeping "*
327-
"solution anyway", status, ΔŨcurr, ΔŨlast)
327+
"solution anyway", status)
328328
end
329329
@debug solution_summary(optim, verbose=true)
330330
end
331-
mpc.ΔŨ[:] = isfatal(status) ? ΔŨlast : ΔŨcurr
331+
mpc.ΔŨ[:] = iserror(optim) ? ΔŨlast : ΔŨcurr
332332
return mpc.ΔŨ
333333
end
334334

src/estimator/mhe/execute.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<
5757
end
5858
end
5959
# -------- error handling -------------------------
60-
status = termination_status(optim)
6160
Z̃curr, Z̃last = value.(Z̃var), Z̃0
62-
if !(status == OPTIMAL || status == LOCALLY_SOLVED)
63-
if isfatal(status)
61+
if !issolved(optim)
62+
status = termination_status(optim)
63+
if iserror(optim)
6464
@error("MHE terminated without solution: estimation in open-loop",
6565
status)
6666
else
@@ -69,7 +69,7 @@ function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<
6969
end
7070
@debug solution_summary(optim, verbose=true)
7171
end
72-
estim.Z̃[:] = !isfatal(status) ? Z̃curr : Z̃last
72+
estim.Z̃[:] = iserror(optim) ? Z̃last : Z̃curr
7373
# --------- update estimate -----------------------
7474
estim.Ŵ[1:nŵ*Nk] = estim.Z̃[nx̂+1:nx̂+nŵ*Nk] # update Ŵ with optimum for warm-starting
7575
V̂, X̂ = predict!(V̂, X̂, estim, model, estim.Z̃)

src/general.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
"Termination status that means 'no solution available'."
2-
const FATAL_STATUSES = [
2+
const ERROR_STATUSES = [
33
INFEASIBLE, DUAL_INFEASIBLE, LOCALLY_INFEASIBLE, INFEASIBLE_OR_UNBOUNDED,
44
NUMERICAL_ERROR, INVALID_MODEL, INVALID_OPTION, INTERRUPTED,
55
OTHER_ERROR
66
]
77

8-
"Verify that the solver termination status means 'no solution available'."
9-
isfatal(status::TerminationStatusCode) = any(status .== FATAL_STATUSES)
8+
"Verify that `optim` termination status is `OPTIMAL` or `LOCALLY_SOLVED`."
9+
function issolved(optim::JuMP.GenericModel)
10+
status = termination_status(optim)
11+
return (status == OPTIMAL || status == LOCALLY_SOLVED)
12+
end
13+
14+
"Verify that `optim` termination status means 'no solution available'."
15+
function iserror(optim::JuMP.GenericModel)
16+
status = termination_status(optim)
17+
return any(status .== ERROR_STATUSES)
18+
end
1019

1120
"Evaluate the quadratic programming objective function `0.5x'*H*x + q'*x` at `x`."
1221
obj_quadprog(x, H, q) = 0.5*dot(x, H, x) + q'*x # dot(x, H, x) is faster than x'*H*x
1322

1423
"Limit the solving time to `Ts` if supported by `optim` optimizer."
15-
function limit_solve_time(optim, Ts)
24+
function limit_solve_time(optim::GenericModel, Ts)
1625
try
17-
set_time_limit_sec(optim,Ts)
26+
set_time_limit_sec(optim, Ts)
1827
catch err
1928
if isa(err, MOI.UnsupportedAttribute{MOI.TimeLimitSec})
2029
@warn "Solving time limit is not supported by the optimizer."

0 commit comments

Comments
 (0)