Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check more retcodes from NLopt #879

Merged
merged 3 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/OptimizationNLopt/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ using Test, Random
@test sol.retcode == ReturnCode.Success
@test 10 * sol.objective < l1

# XTOL_REACHED
sol = solve(prob, NLopt.LD_LBFGS(), xtol_abs = 1e10)
@test sol.retcode == ReturnCode.Success

# STOPVAL_REACHED
sol = solve(prob, NLopt.LD_LBFGS(), stopval = 1e10)
@test sol.retcode == ReturnCode.Success

prob = OptimizationProblem(optprob, x0, _p, lb = [-1.0, -1.0], ub = [0.8, 0.8])
sol = solve(prob, NLopt.LD_LBFGS())
@test sol.retcode == ReturnCode.Success
Expand Down Expand Up @@ -79,7 +87,21 @@ using Test, Random
@test sol.objective < 0.8
end

@testset "MAXTIME_REACHED" begin
# without maxtime=... this will take time
n = 2000
A, b = rand(n, n), rand(n)
system(x, p) = sum((A * x - b) .^ 2)
x0 = zeros(n)
__p = Float64[]
optprob = OptimizationFunction((x, p) -> -system(x, p), Optimization.AutoZygote())
prob = OptimizationProblem(optprob, x0, __p; sense = Optimization.MaxSense)
sol = solve(prob, NLopt.Opt(:LD_LBFGS, n), maxtime = 1e-6)
@test sol.retcode == ReturnCode.MaxTime
end

@testset "constrained" begin
Random.seed!(1)
cons = (res, x, p) -> res .= [x[1]^2 + x[2]^2 - 1.0]
x0 = zeros(2)
optprob = OptimizationFunction(rosenbrock, Optimization.AutoZygote();
Expand Down Expand Up @@ -108,13 +130,14 @@ using Test, Random
res .= [x[1]^2 + x[2]^2 - 1.0, x[2] * sin(x[1]) - x[1] - 2.0]
end

# FTOL_REACHED
optprob = OptimizationFunction(
rosenbrock, Optimization.AutoForwardDiff(); cons = con2_c)
Random.seed!(1)
prob = OptimizationProblem(
optprob, rand(2), _p, lcons = [0.0, -Inf], ucons = [0.0, 0.0])
sol = solve(prob, NLopt.LD_AUGLAG(), local_method = NLopt.LD_LBFGS())
# @test sol.retcode == ReturnCode.Success
@test sol.retcode == ReturnCode.Success
@test 10 * sol.objective < l1

Random.seed!(1)
Expand Down
7 changes: 5 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ function deduce_retcode(retcode::Symbol)
return ReturnCode.Default
elseif retcode == :Success || retcode == :EXACT_SOLUTION_LEFT ||
retcode == :FLOATING_POINT_LIMIT || retcode == :true || retcode == :OPTIMAL ||
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED || retcode == :SUCCESS
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED ||
retcode == :SUCCESS ||
retcode == :STOPVAL_REACHED || retcode == :FTOL_REACHED ||
retcode == :XTOL_REACHED
return ReturnCode.Success
elseif retcode == :Terminated
return ReturnCode.Terminated
elseif retcode == :MaxIters || retcode == :MAXITERS_EXCEED ||
retcode == :MAXEVAL_REACHED
return ReturnCode.MaxIters
elseif retcode == :MaxTime || retcode == :TIME_LIMIT
elseif retcode == :MaxTime || retcode == :TIME_LIMIT || retcode == :MAXTIME_REACHED
return ReturnCode.MaxTime
elseif retcode == :DtLessThanMin
return ReturnCode.DtLessThanMin
Expand Down
Loading