Skip to content

Commit 6c61fd0

Browse files
Add error message when NLopt AUGLAG algorithms are used without local_method
Fixes #1020 NLopt's AUGLAG algorithms require a local optimization method to be specified. Previously, using them without a local_method would result in undefined behavior (returning wrong results like [0.0, 0.0] with Inf objective). This commit adds a clear error message when AUGLAG algorithms are used without specifying a local_method parameter, helping users understand the correct usage. Changes: - Add check in __map_optimizer_args! to detect AUGLAG algorithms without local_method - Provide helpful error message with usage example - Add test cases to verify the error is thrown correctly 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b5b4dc6 commit 6c61fd0

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/OptimizationNLopt/src/OptimizationNLopt.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ function __map_optimizer_args!(cache::OptimizationCache, opt::NLopt.Opt;
6868
local_maxtime::Union{Number, Nothing} = nothing,
6969
local_options::Union{NamedTuple, Nothing} = nothing,
7070
kwargs...)
71+
72+
# Check if AUGLAG algorithm requires local_method
73+
alg_str = string(opt.algorithm)
74+
if occursin("AUGLAG", alg_str) && local_method === nothing
75+
error("NLopt.$(opt.algorithm) requires a local optimization method. " *
76+
"Please specify a local_method, e.g., solve(prob, NLopt.$(opt.algorithm)(); " *
77+
"local_method = NLopt.LN_NELDERMEAD())")
78+
end
79+
7180
if local_method !== nothing
7281
if isa(local_method, NLopt.Opt)
7382
if ndims(local_method) != length(cache.u0)

lib/OptimizationNLopt/test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ using Test, Random
149149
# @test sol.retcode == ReturnCode.Success
150150
@test 10 * sol.objective < l1
151151

152+
# Test that AUGLAG without local_method throws an error
153+
@test_throws ErrorException solve(prob, NLopt.LN_AUGLAG())
154+
@test_throws ErrorException solve(prob, NLopt.LD_AUGLAG())
155+
152156
function con2_c(res, x, p)
153157
res .= [x[1]^2 + x[2]^2 - 1.0, x[2] * sin(x[1]) - x[1] - 2.0]
154158
end

0 commit comments

Comments
 (0)