I am trying to move from MathProgBase to MathOptInterface. My code works for the former package, but after I rewrote it using the latter package, I got an error saying Ipopt: Failed to construct problem
. I believe it is because I could not find the correct way to construct the non-linear model. In MathProgBase, it is
model = MathProgBase.NonlinearModel(IpoptSolver())
But I couldn’t find the corresponding one in MathOptInterface. Currently, I wrote
model = Ipopt.Optimizer()
I think it should be something like Ipopt.NLOptimizer()
instead. Just incase the other part of my code is wrong, I post all the codes below.
ipopt_gradient(x) = FiniteDiff.finite_difference_gradient(fun_objective, x)
mutable struct matching_ipopt <: MOI.AbstractNLPEvaluator
enable_hessian::Bool
end
function MOI.initialize(d::matching_ipopt, requested_features::Vector{Symbol})
for feat in requested_features
if !(feat in MOI.features_available(d))
error("Unsupported feature $feat")
end
end
end
function MOI.features_available(d::matching_ipopt)
if d.enable_hessian
return [:Grad, :Hess]
else
return [:Grad]
end
end
MOI.eval_objective(d::matching_ipopt, x) = fun_objective(x)
MOI.eval_constraint(d::matching_ipopt, g, x) = nothing
function MOI.eval_objective_gradient(d::matching_ipopt, grad_f, x)
gradient_temp = ipopt_gradient(x)
grad_f[1] = gradient_temp[1]
grad_f[2] = gradient_temp[2]
grad_f[3] = gradient_temp[3]
grad_f[4] = gradient_temp[4]
grad_f[5] = gradient_temp[5]
end
model = Ipopt.Optimizer() # How to construct the nonlinear model?
l = [-20.0, 0.0, -20.0, -20.0, -20.0]
u = [20.0, 0.0, 20.0, 20.0, 20.0]
lb = Float64[]
ub = Float64[]
start = [1.0, 1.0, 1.0, 1.0, 1.0]
block_data = MOI.NLPBlockData(MOI.NLPBoundsPair.(lb, ub), matching_ipopt(0), true)
MOI.set(model, MOI.NLPBlock(), block_data)
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.optimize!(model)