Skip to content

Commit ca79234

Browse files
authored
Fix and test setting local_optimizer (#241)
1 parent a7eb503 commit ca79234

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

ext/NLoptMathOptInterfaceExt.jl

+2
Original file line numberDiff line numberDiff line change
@@ -786,9 +786,11 @@ end
786786
function _initialize_options!(model::Optimizer)
787787
local_optimizer = model.options["local_optimizer"]
788788
if local_optimizer !== nothing
789+
num_variables = length(model.starting_values)
789790
local_optimizer = if local_optimizer isa Symbol
790791
NLopt.Opt(local_optimizer, num_variables)
791792
else
793+
@assert local_optimizer isa NLopt.Opt
792794
NLopt.Opt(local_optimizer.algorithm, num_variables)
793795
end
794796
NLopt.local_optimizer!(model.inner, local_optimizer)

test/MOI_wrapper.jl

+58
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,64 @@ function test_list_of_constraint_attributes_set()
156156
return
157157
end
158158

159+
function test_raw_optimizer_attribute_in_optimize()
160+
model = NLopt.Optimizer()
161+
x = MOI.add_variables(model, 2)
162+
f = (x[1] - 2.0) * (x[1] - 2.0) + (x[2] + 1.0)^2# * (x[2] + 1)
163+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
164+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
165+
for (k, v) in (
166+
"algorithm" => :LD_SLSQP,
167+
"stopval" => 1.0,
168+
"ftol_rel" => 1e-6,
169+
"ftol_abs" => 1e-6,
170+
"xtol_rel" => 1e-6,
171+
"xtol_abs" => 1e-6,
172+
"maxeval" => 100,
173+
"maxtime" => 60.0,
174+
"initial_step" => [0.1, 0.1],
175+
"population" => 10,
176+
"seed" => 1234,
177+
"vector_storage" => 3,
178+
)
179+
attr = MOI.RawOptimizerAttribute(k)
180+
MOI.set(model, attr, v)
181+
end
182+
MOI.optimize!(model)
183+
@test (MOI.get.(model, MOI.VariablePrimal(), x), [2.0, -1.0]; atol = 1e-4)
184+
return
185+
end
186+
187+
function test_local_optimizer_Symbol()
188+
model = NLopt.Optimizer()
189+
x = MOI.add_variables(model, 2)
190+
f = (x[1] - 2.0) * (x[1] - 2.0) + (x[2] + 1.0) * (x[2] + 1.0)
191+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
192+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
193+
MOI.set(model, MOI.RawOptimizerAttribute("algorithm"), :AUGLAG)
194+
attr = MOI.RawOptimizerAttribute("local_optimizer")
195+
@test MOI.get(model, attr) === nothing
196+
MOI.set(model, attr, :LD_SLSQP)
197+
MOI.optimize!(model)
198+
@test MOI.get(model, MOI.TerminationStatus()) isa MOI.TerminationStatusCode
199+
return
200+
end
201+
202+
function test_local_optimizer_Opt()
203+
model = NLopt.Optimizer()
204+
x = MOI.add_variables(model, 2)
205+
f = (x[1] - 2.0) * (x[1] - 2.0) + (x[2] + 1.0) * (x[2] + 1.0)
206+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
207+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
208+
MOI.set(model, MOI.RawOptimizerAttribute("algorithm"), :GD_MLSL)
209+
attr = MOI.RawOptimizerAttribute("local_optimizer")
210+
@test MOI.get(model, attr) === nothing
211+
MOI.set(model, attr, NLopt.Opt(:LD_MMA, 2))
212+
MOI.optimize!(model)
213+
@test MOI.get(model, MOI.TerminationStatus()) isa MOI.TerminationStatusCode
214+
return
215+
end
216+
159217
function test_get_objective_function()
160218
model = NLopt.Optimizer()
161219
x = MOI.add_variable(model)

0 commit comments

Comments
 (0)