jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.24k stars 396 forks source link

Why I can't read nl file with other solver #3637

Closed rlacjfjin closed 11 months ago

rlacjfjin commented 11 months ago

As the title suggests, I am currently using the write_to_file function from JuMP to create the my_model.nl file. I then want to solve this model using other solvers. Here are my attempts and results:

  1. scip -f my_model.nl - No issues.

  2. ipopt my_model.nl - No issues.

  3. mosek my_model.nl - Error encountered: Reading started. MOSEK error 1101 (MSK_RES_ERR_MPS_INV_FIELD): Field number 1 is invalid.

  4. Using amplpy in Python to read my_model.nl:

    ampl = AMPL()
    ampl.read("my_model.nl")
    Also encountered an error:
    Error:
    my_model.nl
    line 1 offset 3
    syntax error
    context:  g3  >>> 1  <<< 1 0

My model may be used JuMP expression,NLexpression,constraint,NLconstraint functions, and my_model.nl file is:

g3 1 1 0
 2 2 1 0 0 0
 0 1
 0 0
 0 0 0
 0 0 0 1
 0 0 0 0 0
 4 2
 0 0
 0 0 0 0 0
C0
n0
C1
n0
O0 1
n0
x2
0 0
1 0
r
2 7
1 15
b
0 1 10
0 1 10
k1
2
J0 2
0 2
1 3
J1 2
0 3
1 4
G0 2
0 1
1 2

I want to do:

  1. convert the nl file to mps file if possible.
    • I tried using JuMP read nl file and write_to_file("my_model.mps"), It has some error with: MathOptInterface.UnsupportedAttribute{MathOptInterface.NLPBlock}: Attribute MathOptInterface.NLPBlock() is not supported by the model.

Can you help me? Thanks.

odow commented 11 months ago

MSK_RES_ERR_MPS_INV_FIELD

This suggests that you are telling Mosek to read it as a MPS file. You probably need to pass additional flags to ask it to read a .nl file.

Using amplpy in Python to read my_model.nl

I assume that amp.read is for .mod and .dat files. You probably need different syntax to read a .nl file into AMPL.

convert the nl file to mps file if possible.

You cannot do this. MPS does not support nonlinear, which is what

MathOptInterface.UnsupportedAttribute{MathOptInterface.NLPBlock}: Attribute MathOptInterface.NLPBlock() is not supported by the model.

is telling you.

odow commented 11 months ago

Here's what I think you're after:

julia> using JuMP

julia> function convert_to_quad(model::Model, f::NonlinearExpr)
           g = convert(MOI.ScalarQuadraticFunction{Float64}, moi_function(f))
           return jump_function(model, g)
       end
convert_to_quad (generic function with 1 method)

julia> model = read_from_file("/tmp/model.nl"; use_nlp_block = false)
A JuMP Model
Maximization problem with:
Variables: 2
Objective function type: NonlinearExpr
`NonlinearExpr`-in-`MathOptInterface.GreaterThan{Float64}`: 1 constraint
`NonlinearExpr`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`VariableRef`-in-`MathOptInterface.GreaterThan{Float64}`: 2 constraints
`VariableRef`-in-`MathOptInterface.LessThan{Float64}`: 2 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> 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

julia> set_objective_function(model, convert_to_quad(model, objective_function(model)))

julia> model
A JuMP Model
Maximization problem with:
Variables: 2
Objective function type: QuadExpr
`QuadExpr`-in-`MathOptInterface.GreaterThan{Float64}`: 1 constraint
`QuadExpr`-in-`MathOptInterface.LessThan{Float64}`: 1 constraint
`VariableRef`-in-`MathOptInterface.GreaterThan{Float64}`: 2 constraints
`VariableRef`-in-`MathOptInterface.LessThan{Float64}`: 2 constraints
Model mode: AUTOMATIC
CachingOptimizer state: NO_OPTIMIZER
Solver name: No optimizer attached.

julia> write_to_file(model, "model.mps")

shell> cat model.mps
NAME          
ROWS
 N  OBJ
 L  c1_1
 G  c1
COLUMNS
    x1        c1_1      3
    x1        c1        2
    x1        OBJ       -1
    x2        c1_1      4
    x2        c1        3
    x2        OBJ       -2
RHS
    rhs       c1_1      15
    rhs       c1        7
RANGES
BOUNDS
 LO bounds    x1        1
 UP bounds    x1        10
 LO bounds    x2        1
 UP bounds    x2        10
QUADOBJ
QCMATRIX   c1_1
QCMATRIX   c1
ENDATA
odow commented 11 months ago

Closing because this is not a bug in JuMP.

If you have follow-up questions, please post on the community forum, https://jump.dev/forum, and we can discuss there :smile: