Skip to content

Commit 5f7a9b6

Browse files
adds nlopt retcode handling
1 parent 9a9174e commit 5f7a9b6

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

lib/OptimizationEvolutionary/src/OptimizationEvolutionary.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function __map_optimizer_args(prob::OptimizationProblem,
2323
abstol::Union{Number, Nothing} = nothing,
2424
reltol::Union{Number, Nothing} = nothing,
2525
kwargs...)
26-
mapped_args = (;kwargs...)
26+
mapped_args = (; kwargs...)
2727

2828
if !isnothing(callback)
2929
mapped_args = (; mapped_args..., callback = callback)

lib/OptimizationEvolutionary/test/runtests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Random.seed!(1234)
2828
@test res[1]0.0625 atol=1e-5
2929
@test sol.objective < l1
3030

31-
prob = OptimizationProblem(optprob, x0, _p, lcons = [-Inf], ucons = [5.0], lb = [0.0, 1.0], ub = [Inf, Inf])
31+
prob = OptimizationProblem(optprob, x0, _p, lcons = [-Inf], ucons = [5.0],
32+
lb = [0.0, 1.0], ub = [Inf, Inf])
3233
sol = solve(prob, CMAES= 40, λ = 100))
3334
res = zeros(1)
3435
cons_circ(res, sol.u, nothing)

lib/OptimizationNLopt/src/OptimizationNLopt.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,33 @@ function __map_optimizer_args!(prob::OptimizationProblem, opt::NLopt.Opt;
7777
return nothing
7878
end
7979

80+
function __nlopt_status_to_ReturnCode(status::NLOpt.Result)
81+
if status in [
82+
NLOpt.SUCCESS,
83+
NLOpt.STOPVAL_REACHED,
84+
NLOpt.FTOL_REACHED,
85+
NLOpt.XTOL_REACHED,
86+
]
87+
return ReturnCode.Success
88+
elseif status in [
89+
NLOpt.MAXEVAL_REACHED,
90+
]
91+
return ReturnCode.MaxIters
92+
elseif status == NLOpt.MAXTIME_REACHED
93+
return ReturnCode.MaxTime
94+
elseif status in [
95+
NLOpt.OUT_OF_MEMORY,
96+
NLOpt.INVALID_ARGS,
97+
NLOpt.FAILURE,
98+
NLOpt.ROUNDOFF_LIMITED,
99+
NLOpt.FORCED_STOP,
100+
]
101+
return ReturnCode.Failure
102+
else
103+
return ReturnCode.Default
104+
end
105+
end
106+
80107
function SciMLBase.__solve(prob::OptimizationProblem,
81108
opt::Union{NLopt.Algorithm, NLopt.Opt};
82109
maxiters::Union{Number, Nothing} = nothing,
@@ -134,8 +161,9 @@ function SciMLBase.__solve(prob::OptimizationProblem,
134161
(minf, minx, ret) = NLopt.optimize(opt_setup, prob.u0)
135162
t1 = time()
136163

164+
retcode = __nlopt_status_to_ReturnCode(ret)
137165
SciMLBase.build_solution(SciMLBase.DefaultOptimizationCache(prob.f, prob.p), opt, minx,
138-
minf; original = opt_setup, retcode = ret,
166+
minf; original = opt_setup, retcode = retcode,
139167
solve_time = t1 - t0)
140168
end
141169

0 commit comments

Comments
 (0)