Skip to content

Commit 21f1bea

Browse files
Merge pull request #949 from SciML/fix-nlopt-dual-ftol-rel
Add support for dual_ftol_rel parameter in NLopt
2 parents 0d180a8 + a344318 commit 21f1bea

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/OptimizationNLopt/src/OptimizationNLopt.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function __map_optimizer_args!(cache::OptimizationCache, opt::NLopt.Opt;
7575

7676
if !isnothing(local_options)
7777
for j in Dict(pairs(local_options))
78-
eval(Meta.parse("NLopt." * string(j.first) * "!"))(local_meth, j.second)
78+
NLopt.nlopt_set_param(opt, j.first, j.second)
7979
end
8080
end
8181

@@ -93,7 +93,7 @@ function __map_optimizer_args!(cache::OptimizationCache, opt::NLopt.Opt;
9393
# add optimiser options from kwargs
9494
for j in kwargs
9595
if j.first != :cons_tol
96-
eval(Meta.parse("NLopt." * string(j.first) * "!"))(opt, j.second)
96+
NLopt.nlopt_set_param(opt, j.first, j.second)
9797
end
9898
end
9999

lib/OptimizationNLopt/test/runtests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ using Test, Random
100100
@test sol.retcode == ReturnCode.MaxTime
101101
end
102102

103+
@testset "dual_ftol_rel parameter" begin
104+
# Test that dual_ftol_rel parameter can be passed to NLopt without errors
105+
# This parameter is specific to MMA/CCSA algorithms for dual optimization tolerance
106+
x0_test = zeros(2)
107+
optprob = OptimizationFunction(rosenbrock, Optimization.AutoZygote())
108+
prob = OptimizationProblem(optprob, x0_test, _p)
109+
110+
# Test with NLopt.Opt interface
111+
opt = NLopt.Opt(:LD_MMA, 2)
112+
# This should not throw an error - the PR fixed the UndefVarError
113+
sol = solve(prob, opt, dual_ftol_rel = 1e-6, maxiters = 100)
114+
@test sol.retcode [ReturnCode.Success, ReturnCode.MaxIters]
115+
116+
# Test with direct algorithm interface
117+
sol = solve(prob, NLopt.LD_MMA(), dual_ftol_rel = 1e-5, maxiters = 100)
118+
@test sol.retcode [ReturnCode.Success, ReturnCode.MaxIters]
119+
120+
# Verify it works with other solver options
121+
sol = solve(prob, NLopt.LD_MMA(), dual_ftol_rel = 1e-4, ftol_rel = 1e-6,
122+
xtol_rel = 1e-6, maxiters = 100)
123+
@test sol.retcode [ReturnCode.Success, ReturnCode.MaxIters]
124+
end
125+
103126
@testset "constrained" begin
104127
Random.seed!(1)
105128
cons = (res, x, p) -> res .= [x[1]^2 + x[2]^2 - 1.0]

0 commit comments

Comments
 (0)