From 2b437d0674e3bc400c403731bac2a6b9e6d34d0c Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 21 Aug 2024 14:28:18 +1200 Subject: [PATCH] Update --- ext/NLoptMathOptInterfaceExt.jl | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/ext/NLoptMathOptInterfaceExt.jl b/ext/NLoptMathOptInterfaceExt.jl index 9cf5536..23a2db2 100644 --- a/ext/NLoptMathOptInterfaceExt.jl +++ b/ext/NLoptMathOptInterfaceExt.jl @@ -570,13 +570,11 @@ function MOI.set( end function _fill_gradient(grad, x, f::MOI.VariableIndex) - fill!(grad, 0.0) grad[f.value] = 1.0 return end function _fill_gradient(grad, x, f::MOI.ScalarAffineFunction{Float64}) - fill!(grad, 0.0) for term in f.terms grad[term.variable.value] += term.coefficient end @@ -584,7 +582,6 @@ function _fill_gradient(grad, x, f::MOI.ScalarAffineFunction{Float64}) end function _fill_gradient(grad, x, f::MOI.ScalarQuadraticFunction{Float64}) - fill!(grad, 0.0) for term in f.affine_terms grad[term.variable.value] += term.coefficient end @@ -645,40 +642,36 @@ function _fill_jacobian(jac, x, offset, constraints::Vector) end function objective_fn(model::Optimizer, x::Vector, grad::Vector) + # The order of the conditions is important. NLP objectives override regular + # objectives. if length(grad) > 0 fill!(grad, 0.0) - end - # If we don't give any objective to NLopt, it throws the error: - # invalid NLopt arguments: NULL args to nlopt_optimize - if model.sense == MOI.FEASIBILITY_SENSE - return 0.0 - end - if length(grad) > 0 - if model.nlp_data.has_objective + if model.sense == MOI.FEASIBILITY_SENSE + # nothing + elseif model.nlp_data.has_objective MOI.eval_objective_gradient(model.nlp_data.evaluator, grad, x) elseif model.objective !== nothing _fill_gradient(grad, x, model.objective) end end - # The order of the conditions is important. NLP objectives override regular - # objectives. + if model.sense == MOI.FEASIBILITY_SENSE + return 0.0 if model.nlp_data.has_objective return MOI.eval_objective(model.nlp_data.evaluator, x) elseif model.objective !== nothing return MOI.Utilities.eval_variables(vi -> x[vi.value], model.objective) end - # No objective function set. This could happen with FEASIBILITY_SENSE. + # No ObjectiveFunction is set, but ObjectiveSense is? return 0.0 end function _initialize_options!(model::Optimizer) local_optimizer = model.options["local_optimizer"] if local_optimizer !== nothing - if local_optimizer isa Symbol - local_optimizer = NLopt.Opt(local_optimizer, num_variables) + local_optimizer = if local_optimizer isa Symbol + NLopt.Opt(local_optimizer, num_variables) else - local_optimizer = - NLopt.Opt(local_optimizer.algorithm, num_variables) + NLopt.Opt(local_optimizer.algorithm, num_variables) end NLopt.local_optimizer!(model.inner, local_optimizer) end