Skip to content

Commit 1e1fe40

Browse files
committed
added an option to stop mpc if the solution is not optimal
1 parent 3c8cca8 commit 1e1fe40

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/Base.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ type MPCSettings
342342
printLevel::Int64
343343
expandGoal::Bool # bool to indicate if the goal needs to be expanded
344344
enlargeGoalTolFactor::Int64 # scaling factor to enlare the goal
345+
onlyOptimal::Bool
345346
end
346347

347348
function MPCSettings()
@@ -357,7 +358,8 @@ function MPCSettings()
357358
true,
358359
2,
359360
true,
360-
2
361+
2,
362+
false
361363
)
362364
end
363365

@@ -855,7 +857,7 @@ function postProcess!(n;kwargs...)
855857
n.r.ocp.tst = n.r.ocp.tctr
856858
end
857859

858-
if n.r.ocp.status==:Optimal || (n.s.mpc.on && isequal(n.mpc.v.evalNum,1))
860+
if n.r.ocp.status==:Optimal || (!n.s.mpc.onlyOptimal && n.s.mpc.on && isequal(n.mpc.v.evalNum,1))
859861
if (!isequal(n.r.ocp.status,:Optimal) && n.s.mpc.on && isequal(n.mpc.v.evalNum,1))
860862
warn("There is no previous :Optimal solution to use since isequal(n.mpc.v.evalNum,1). \n
861863
Attemting to extract: ",n.r.ocp.status," solution. \n
@@ -871,7 +873,7 @@ function postProcess!(n;kwargs...)
871873
n.r.ocp.U[:,ctr] = getvalue(n.r.ocp.u[:,ctr])
872874
end
873875

874-
elseif n.s.mpc.on && n.s.mpc.lastOptimal
876+
elseif n.s.mpc.on && n.s.mpc.lastOptimal && !n.s.mpc.onlyOptimal
875877
if !n.s.ocp.save
876878
error("This functionality currently needs to have n.s.save==true")
877879
end
@@ -910,6 +912,10 @@ function postProcess!(n;kwargs...)
910912
n.r.ocp.U[:,ctr] = n.r.ocp.dfs[optIdx][n.ocp.control.name[ctr]][timeIdx:end-1]
911913
end
912914
end
915+
else
916+
warn("The solution is not Optimal \n
917+
Setting: n.f.mpc.simFailed = [true, n.r.ocp.status] ")
918+
n.f.mpc.simFailed = [true, n.r.ocp.status]
913919
end
914920
if n.s.ocp.evalConstraints && n.r.ocp.status!=:Error # note may want to remove the && arg
915921
evalConstraints!(n)

src/MPC_Module.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ function defineMPC!(n;
117117
goal=n.ocp.XF,
118118
goalTol=0.1*abs.(n.ocp.X0 - n.ocp.XF),
119119
lastOptimal::Bool=true,
120-
printLevel::Int64=2)
120+
printLevel::Int64=2,
121+
onlyOptimal::Bool=false)
121122
n.s.mpc.on = true
122123
n.mpc::MPC = MPC()
123124
n.s.mpc.mode = mode
@@ -132,6 +133,7 @@ function defineMPC!(n;
132133
n.mpc.v.goalTol = goalTol
133134
n.s.mpc.lastOptimal = lastOptimal
134135
n.s.mpc.printLevel = printLevel
136+
n.s.mpc.onlyOptimal = onlyOptimal
135137

136138
n.f.mpc.simFailed[1] = false # for some reason it is getting defined as 0.0, not false during initialization
137139
n.f.mpc.defined = true

0 commit comments

Comments
 (0)