Skip to content

Commit d69d915

Browse files
Merge pull request #474 from ValentinKaisermayer/vk-nl-op-retcode
Adds nlopt retcode handling
2 parents 9a9174e + d98f926 commit d69d915

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-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: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ function __map_optimizer_args!(prob::OptimizationProblem, opt::NLopt.Opt;
7777
return nothing
7878
end
7979

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

162+
retcode = __nlopt_status_to_ReturnCode(ret)
137163
SciMLBase.build_solution(SciMLBase.DefaultOptimizationCache(prob.f, prob.p), opt, minx,
138-
minf; original = opt_setup, retcode = ret,
164+
minf; original = opt_setup, retcode = retcode,
139165
solve_time = t1 - t0)
140166
end
141167

0 commit comments

Comments
 (0)