I strongly encourage you to use JuMP instead of MOI directly (or other Julia packages like Optim, GalacticOptim, etc.).
The MathOptInterface API is complex to implement, and even more so for nonlinear problems.
You had a few things missing:
- You did not check
requested_featuresto see what Ipopt asked for (it asked for:grad) - Thus
features_availableshould return[:grad], and you needed to implementeval_objective_gradient - You need to set an
ObjectiveSense
Here’s is the example working, but again, I would encourage you to use JuMP instead.
using Ipopt
const MOI = Ipopt.MOI
struct dumbEval <: MOI.AbstractNLPEvaluator end
MOI.initialize(::dumbEval, ::Vector{Symbol}) = nothing
MOI.features_available(::dumbEval) = [:Grad]
MOI.eval_objective(::dumbEval, ::Vector{Float64}) = 1.0
function MOI.eval_objective_gradient(
::dumbEval,
g::Vector{Float64},
x::Vector{Float64},
)
g .= 0.0
return
end
o = Ipopt.Optimizer()
x = MOI.add_variable(o)
MOI.set(o, MOI.NLPBlock(), MOI.NLPBlockData([], dumbEval(), true))
MOI.set(o, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.optimize!(o)