From eabd2304ccf1b719b10f1bf2862f6e634fd2d004 Mon Sep 17 00:00:00 2001 From: Vaibhav Dixit Date: Tue, 21 Mar 2023 21:01:21 +0530 Subject: [PATCH] Move roundoff_limited to success and pass `p` in tests --- lib/OptimizationNLopt/src/OptimizationNLopt.jl | 13 ++++++++++--- lib/OptimizationNLopt/test/runtests.jl | 8 +++++--- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/OptimizationNLopt/src/OptimizationNLopt.jl b/lib/OptimizationNLopt/src/OptimizationNLopt.jl index 73f80349b..f4cd6b3e0 100644 --- a/lib/OptimizationNLopt/src/OptimizationNLopt.jl +++ b/lib/OptimizationNLopt/src/OptimizationNLopt.jl @@ -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)) @@ -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) @@ -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 @@ -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) diff --git a/lib/OptimizationNLopt/test/runtests.jl b/lib/OptimizationNLopt/test/runtests.jl index beef90b15..31c08b076 100644 --- a/lib/OptimizationNLopt/test/runtests.jl +++ b/lib/OptimizationNLopt/test/runtests.jl @@ -18,10 +18,12 @@ 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 @@ -29,14 +31,14 @@ using Test 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