jump-dev / MathOptInterface.jl

A data structure for mathematical optimization problems
http://jump.dev/MathOptInterface.jl/
Other
380 stars 86 forks source link

[FileFormats.NL] read linear functions where possible #2511

Closed odow closed 1 month ago

odow commented 1 month ago

x-ref https://discourse.julialang.org/t/nlp-attribute-scalarnonlinearfunction-is-not-supported-by-the-model/110444/20?u=odow

The issue is that our reader converts everything to nonlinear, and doesn't attempt to lower back to linear if there are no nonlinear terms.

using JuMP
model = Model()
@variable(model, x)
@objective(model, Min, x)
write_to_file(model, "/tmp/nl.nl")
m = read_from_file("/tmp/nl.nl")
odow commented 1 month ago
julia> import MathOptInterface as MOI

julia> begin
           src = MOI.Utilities.Model{Float64}()
           x = MOI.add_variable(src)
           MOI.set(src, MOI.ObjectiveSense(), MOI.MIN_SENSE)
           MOI.set(src, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
           dest = MOI.FileFormats.NL.Model()
           MOI.copy_to(dest, src)
           MOI.write_to_file(dest, "/tmp/nl.nl")
           input = MOI.FileFormats.NL.Model(; use_nlp_block = false)
           MOI.read_from_file(input, "/tmp/nl.nl")
           model = MOI.Utilities.Model{Float64}()
           MOI.copy_to(model, input)
           print(model)
       end
Minimize ScalarNonlinearFunction:
 +(+(*(1.0, v[1])), +(0.0))

Subject to: