Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 21, 2024
1 parent 71e53bb commit 2b437d0
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions ext/NLoptMathOptInterfaceExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,21 +570,18 @@ 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
return
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2b437d0

Please sign in to comment.