Skip to content

Commit

Permalink
Merge pull request #493 from SciML/nloptup
Browse files Browse the repository at this point in the history
[WIP] Fix incorrect tests and add constraints support in OptimizationNLopt
  • Loading branch information
ChrisRackauckas authored Mar 26, 2023
2 parents 88fc980 + eabd230 commit d125f25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 10 additions & 3 deletions lib/OptimizationNLopt/src/OptimizationNLopt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ function __map_optimizer_args!(prob::OptimizationProblem, opt::NLopt.Opt;
end

if prob.ub !== nothing
NLopt.upper_bounds!(opt, prob.ub)
opt.upper_bounds = prob.ub
end

if prob.lb !== nothing
NLopt.lower_bounds!(opt, prob.lb)
opt.lower_bounds = prob.lb
end

if !(isnothing(maxiters))
Expand All @@ -83,6 +83,7 @@ function __nlopt_status_to_ReturnCode(status::Symbol)
NLopt.STOPVAL_REACHED,
NLopt.FTOL_REACHED,
NLopt.XTOL_REACHED,
NLopt.ROUNDOFF_LIMITED,
])
return ReturnCode.Success
elseif status == Symbol(NLopt.MAXEVAL_REACHED)
Expand All @@ -93,7 +94,6 @@ function __nlopt_status_to_ReturnCode(status::Symbol)
NLopt.OUT_OF_MEMORY,
NLopt.INVALID_ARGS,
NLopt.FAILURE,
NLopt.ROUNDOFF_LIMITED,
NLopt.FORCED_STOP,
])
return ReturnCode.Failure
Expand Down Expand Up @@ -160,6 +160,13 @@ function SciMLBase.__solve(prob::OptimizationProblem,
t1 = time()

retcode = __nlopt_status_to_ReturnCode(ret)

if retcode == ReturnCode.Failure
@warn "NLopt failed to converge: $(ret)"
minx = fill(NaN, length(prob.u0))
minf = NaN
end

SciMLBase.build_solution(SciMLBase.DefaultOptimizationCache(prob.f, prob.p), opt, minx,
minf; original = opt_setup, retcode = retcode,
solve_time = t1 - t0)
Expand Down
8 changes: 5 additions & 3 deletions lib/OptimizationNLopt/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ using Test
sol = solve(prob, NLopt.Opt(:LN_BOBYQA, 2))
@test 10 * sol.objective < l1

prob = OptimizationProblem(optprob, x0, _p, lb = [-1.0, -1.0], ub = [0.8, 0.8])

sol = solve(prob, NLopt.Opt(:LD_LBFGS, 2))
@test 10 * sol.objective < l1

prob = OptimizationProblem(optprob, x0, lb = [-1.0, -1.0], ub = [0.8, 0.8])

sol = solve(prob, NLopt.Opt(:LD_LBFGS, 2))
@test 10 * sol.objective < l1

sol = solve(prob, NLopt.Opt(:G_MLSL_LDS, 2), local_method = NLopt.Opt(:LD_LBFGS, 2),
maxiters = 10000)
@test 10 * sol.objective < l1

prob = OptimizationProblem(optprob, x0)
prob = OptimizationProblem(optprob, x0, _p)
sol = solve(prob, NLopt.LN_BOBYQA())
@test 10 * sol.objective < l1

sol = solve(prob, NLopt.LD_LBFGS())
@test 10 * sol.objective < l1

prob = OptimizationProblem(optprob, x0, lb = [-1.0, -1.0], ub = [0.8, 0.8])
prob = OptimizationProblem(optprob, x0, _p, lb = [-1.0, -1.0], ub = [0.8, 0.8])
sol = solve(prob, NLopt.LD_LBFGS())
@test 10 * sol.objective < l1

Expand Down

0 comments on commit d125f25

Please sign in to comment.