Skip to content

Commit 9b81844

Browse files
committed
Add tests for new retcodes + format
1 parent 7909415 commit 9b81844

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/OptimizationNLopt/test/runtests.jl

Lines changed: 24 additions & 1 deletion
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,13 +130,14 @@ 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)

src/utils.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ 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 ||
104-
retcode == :STOPVAL_REACHED || retcode == :FTOL_REACHED || retcode == :XTOL_REACHED
103+
retcode == :LOCALLY_SOLVED || retcode == :ROUNDOFF_LIMITED ||
104+
retcode == :SUCCESS ||
105+
retcode == :STOPVAL_REACHED || retcode == :FTOL_REACHED ||
106+
retcode == :XTOL_REACHED
105107
return ReturnCode.Success
106108
elseif retcode == :Terminated
107109
return ReturnCode.Terminated

0 commit comments

Comments
 (0)