model = read_from_file("my_model.nl"; use_nlp_block = false)
A JuMP Model
Maximization problem with:
Variables: 1792
Objective function type: NonlinearExpr
`NonlinearExpr`-in-`MathOptInterface.EqualTo{Float64}`: 650 constraints
`NonlinearExpr`-in-`MathOptInterface.GreaterThan{Float64}`: 43 constraints
`NonlinearExpr`-in-`MathOptInterface.LessThan{Float64}`: 423 constraints
`VariableRef`-in-`MathOptInterface.GreaterThan{Float64}`: 1763 constraints
`VariableRef`-in-`MathOptInterface.LessThan{Float64}`: 764 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.
for (F, S) in list_of_constraint_types(model)
if F == NonlinearExpr
for ci in all_constraints(model, F, S)
obj = constraint_object(ci)
@constraint(model, convert_to_quad(model, obj.func) in obj.set)
delete(model, ci)
end
end
end
function convert_to_quad(model::Model, f::NonlinearExpr)
g = convert(MOI.ScalarQuadraticFunction{Float64}, moi_function(flatten!(f)))
return jump_function(model, g)
end
But note that converting arbitrary nonlinear expressions to affine or quadratic isn’t explicitly supported by JuMP. At the moment we make a best effort, but we don’t match every case.
The real question is: why are you reading from a .nl file? What are you trying to achieve?
We are currently using IPOPT to solve our model, but it seems to be somewhat unstable. Although we have also provided initial values based on experience, it seems that there are some numerical issues with the model itself, so we want to see what the model looks like in detail.
I konw the model is bilinear, and the bilinear terms are apear in constraints.
Why I want to convert nl file?
We use JuMP modeling, and it seems only can write to nl file, I tried to convert it to mps file, but has some error.
I don’t familiar with nl file, but familiar with mps file, I want to see what’s wrong in the model.
I want to compare the IPOPT and mosek solver, but mosek can’t read my nl file directly, but in small model what you show me the example, it convert mps file and can solve.
If I convert it to mps file, I can try more solver, such as scip, gurobi, and I want to know how about my current solution.
Thanks.
So the current convert implementation in MOI can’t handle your functions. But here is a different approach that works:
using JuMP
convert_to_quad(x) = x
function convert_to_quad(x::NonlinearExpr)
head_to_operator = Dict(:+ => +, :* => *, :- => -)
return reduce(head_to_operator[x.head], convert_to_quad.(x.args))
end
model = read_from_file("/Users/Oscar/Downloads/my_model.nl"; use_nlp_block = false)
for (F, S) in list_of_constraint_types(model)
if F == NonlinearExpr
for ci in all_constraints(model, F, S)
obj = constraint_object(ci)
@constraint(model, convert_to_quad(obj.func) in obj.set)
delete(model, ci)
end
end
end
set_objective_function(model, convert_to_quad(objective_function(model)))
I think you should consider different approaches that don’t need to write the .nl file though.