Skip to content

Commit 0af051b

Browse files
Merge pull request #879 from sumiya11/patch-1
Check more retcodes from NLopt
2 parents b6b117a + 4ea6adf commit 0af051b

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/OptimizationNLopt/test/runtests.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ using Test, Random
4040
@test sol.retcode == ReturnCode.Success
4141
@test 10 * sol.objective < l1
4242

43+
# XTOL_REACHED
44+
sol = solve(prob, NLopt.LD_LBFGS(), xtol_abs = 1e10)
45+
@test sol.retcode == ReturnCode.Success
46+
47+
# STOPVAL_REACHED
48+
sol = solve(prob, NLopt.LD_LBFGS(), stopval = 1e10)
49+
@test sol.retcode == ReturnCode.Success
50+
4351
prob = OptimizationProblem(optprob, x0, _p, lb = [-1.0, -1.0], ub = [0.8, 0.8])
4452
sol = solve(prob, NLopt.LD_LBFGS())
4553
@test sol.retcode == ReturnCode.Success
@@ -79,7 +87,21 @@ using Test, Random
7987
@test sol.objective < 0.8
8088
end
8189

90+
@testset "MAXTIME_REACHED" begin
91+
# without maxtime=... this will take time
92+
n = 2000
93+
A, b = rand(n, n), rand(n)
94+
system(x, p) = sum((A * x - b) .^ 2)
95+
x0 = zeros(n)
96+
__p = Float64[]
97+
optprob = OptimizationFunction((x, p) -> -system(x, p), Optimization.AutoZygote())
98+
prob = OptimizationProblem(optprob, x0, __p; sense = Optimization.MaxSense)
99+
sol = solve(prob, NLopt.Opt(:LD_LBFGS, n), maxtime = 1e-6)
100+
@test sol.retcode == ReturnCode.MaxTime
101+
end
102+
82103
@testset "constrained" begin
104+
Random.seed!(1)
83105
cons = (res, x, p) -> res .= [x[1]^2 + x[2]^2 - 1.0]
84106
x0 = zeros(2)
85107
optprob = OptimizationFunction(rosenbrock, Optimization.AutoZygote();
@@ -108,20 +130,21 @@ using Test, Random
108130
res .= [x[1]^2 + x[2]^2 - 1.0, x[2] * sin(x[1]) - x[1] - 2.0]
109131
end
110132

133+
# FTOL_REACHED
111134
optprob = OptimizationFunction(
112135
rosenbrock, Optimization.AutoForwardDiff(); cons = con2_c)
113136
Random.seed!(1)
114137
prob = OptimizationProblem(
115138
optprob, rand(2), _p, lcons = [0.0, -Inf], ucons = [0.0, 0.0])
116139
sol = solve(prob, NLopt.LD_AUGLAG(), local_method = NLopt.LD_LBFGS())
117-
# @test sol.retcode == ReturnCode.Success
140+
@test sol.retcode == ReturnCode.Success
118141
@test 10 * sol.objective < l1
119142

120143
Random.seed!(1)
121144
prob = OptimizationProblem(optprob, [0.5, 0.5], _p, lcons = [-Inf, -Inf],
122145
ucons = [0.0, 0.0], lb = [-1.0, -1.0], ub = [1.0, 1.0])
123146
sol = solve(prob, NLopt.GN_ISRES(), maxiters = 1000)
124147
@test sol.retcode == ReturnCode.MaxIters
125-
@test 10 * sol.objective < l1
148+
@test sol.objective < l1
126149
end
127150
end

src/utils.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,17 @@ function deduce_retcode(retcode::Symbol)
100100
return ReturnCode.Default
101101
elseif retcode == :Success || retcode == :EXACT_SOLUTION_LEFT ||
102102
retcode == :FLOATING_POINT_LIMIT || retcode == :true || retcode == :OPTIMAL ||
103-
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED || retcode == :SUCCESS
103+
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED ||
104+
retcode == :SUCCESS ||
105+
retcode == :STOPVAL_REACHED || retcode == :FTOL_REACHED ||
106+
retcode == :XTOL_REACHED
104107
return ReturnCode.Success
105108
elseif retcode == :Terminated
106109
return ReturnCode.Terminated
107110
elseif retcode == :MaxIters || retcode == :MAXITERS_EXCEED ||
108111
retcode == :MAXEVAL_REACHED
109112
return ReturnCode.MaxIters
110-
elseif retcode == :MaxTime || retcode == :TIME_LIMIT
113+
elseif retcode == :MaxTime || retcode == :TIME_LIMIT || retcode == :MAXTIME_REACHED
111114
return ReturnCode.MaxTime
112115
elseif retcode == :DtLessThanMin
113116
return ReturnCode.DtLessThanMin

0 commit comments

Comments
 (0)