Skip to content

Commit

Permalink
Use common retcode deduction
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhavdixit02 committed Aug 13, 2024
1 parent 2a78d49 commit 4bc4904
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 27 deletions.
27 changes: 1 addition & 26 deletions lib/OptimizationNLopt/src/OptimizationNLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,31 +125,6 @@ function __map_optimizer_args!(cache::OptimizationCache, opt::NLopt.Opt;
return nothing
end

function __nlopt_status_to_ReturnCode(status::Symbol)
if status in Symbol.([
NLopt.SUCCESS,
NLopt.STOPVAL_REACHED,
NLopt.FTOL_REACHED,
NLopt.XTOL_REACHED,
NLopt.ROUNDOFF_LIMITED
])
return ReturnCode.Success
elseif status == Symbol(NLopt.MAXEVAL_REACHED)
return ReturnCode.MaxIters
elseif status == Symbol(NLopt.MAXTIME_REACHED)
return ReturnCode.MaxTime
elseif status in Symbol.([
NLopt.OUT_OF_MEMORY,
NLopt.INVALID_ARGS,
NLopt.FAILURE,
NLopt.FORCED_STOP
])
return ReturnCode.Failure
else
return ReturnCode.Default
end
end

function SciMLBase.__solve(cache::OptimizationCache{
F,
RC,
Expand Down Expand Up @@ -258,7 +233,7 @@ function SciMLBase.__solve(cache::OptimizationCache{
t0 = time()
(minf, minx, ret) = NLopt.optimize(opt_setup, cache.u0)
t1 = time()
retcode = __nlopt_status_to_ReturnCode(ret)
retcode = deduce_retcode(ret)

if retcode == ReturnCode.Failure
@warn "NLopt failed to converge: $(ret)"
Expand Down
3 changes: 2 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ const STOP_REASON_MAP = Dict(
r"STOP: XTOL.TOO.SMALL" => ReturnCode.ConvergenceFailure,
r"STOP: TERMINATION" => ReturnCode.Terminated,
r"Optimization completed" => ReturnCode.Success,
r"Convergence achieved" => ReturnCode.Success
r"Convergence achieved" => ReturnCode.Success,
r"ROUNDOFF_LIMITED" => ReturnCode.Success
)

# Function to deduce ReturnCode from a stop_reason string using the dictionary
Expand Down

0 comments on commit 4bc4904

Please sign in to comment.