Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FileFormats.LP] more parsing issues #2223

Closed
GordStephen opened this issue Jun 23, 2023 · 5 comments · Fixed by #2225
Closed

[FileFormats.LP] more parsing issues #2223

GordStephen opened this issue Jun 23, 2023 · 5 comments · Fixed by #2225
Labels
Submodule: FileFormats About the FileFormats submodule Type: Bug

Comments

@GordStephen
Copy link

GordStephen commented Jun 23, 2023

This is on 1.18.0:

LP file:
maximize 
obj: 3 x1 + x2 + 5 x3 + x4 
subject to 
c1:  3 x1 + x2 + 2 x3 = 30 
c2:  2 x1 + x2 + 3 x3 + x4 >= 15 
c3:  2 x2 + 3 x4 <= 25 
bounds 
 -infinity <= x1 <= +infinity 
 -10 <= x2 <= 10 
 0 <= x3 <= +infinity 
 10 <= x4 <= +infinity 
end

Parsed Bounds:
0.0 <= x1 <= Inf
x2
0.0 <= x3
10.0 <= x4

So x1's lower bound is erroneously set to zero and upper bound isn't dropped (does that matter?), while x2's upper and lower bounds both get dropped.

Code to reproduce:

import MathOptInterface

const MOI = MathOptInterface

lpfile = """
maximize 
obj: 3 x1 + x2 + 5 x3 + x4 
subject to 
c1:  3 x1 + x2 + 2 x3 = 30 
c2:  2 x1 + x2 + 3 x3 + x4 >= 15 
c3:  2 x2 + 3 x4 <= 25 
bounds 
 -infinity <= x1 <= +infinity 
 -10 <= x2 <= 10 
 0 <= x3 <= +infinity 
 10 <= x4 <= +infinity 
end
"""

println("LP file:\n", lpfile, "\nParsed Bounds:")

io = IOBuffer(lpfile)
m = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_LP)
read!(io, m)

for v in MOI.get(m, MOI.ListOfVariableIndices())

    varname = MOI.get(m, MOI.VariableName(), v)
    c_lb = MOI.ConstraintIndex{MOI.VariableIndex, MOI.GreaterThan{Float64}}(v.value)
    c_ub = MOI.ConstraintIndex{MOI.VariableIndex, MOI.LessThan{Float64}}(v.value)

    if MOI.is_valid(m, c_lb)
        lb = MOI.get(m, MOI.ConstraintSet(), c_lb)
        print(lb.lower, " <= ")
    end
    
    print(varname)

    if MOI.is_valid(m, c_ub)
        ub = MOI.get(m, MOI.ConstraintSet(), c_ub)
        print(" <= ", ub.upper)
    end

    println()

end
@odow
Copy link
Member

odow commented Jun 24, 2023

😞 I think I just need to rewrite the LP reader to be a proper parser...

julia> import MathOptInterface as MOI

julia> io = IOBuffer("""
       maximize 
       subject to 
       bounds 
        -infinity <= x1 <= +infinity 
        -10 <= x2 <= 10 
        0 <= x3 <= +infinity 
        10 <= x4 <= +infinity 
       end
       """);

julia> model = MOI.FileFormats.Model(format = MOI.FileFormats.FORMAT_LP)
A .LP-file model

julia> read!(io, model)

julia> print(model)
Maximize ScalarAffineFunction{Float64}:
 0.0

Subject to:

VariableIndex-in-GreaterThan{Float64}
 x1 >= 0.0
 x3 >= 0.0
 x4 >= 10.0

VariableIndex-in-LessThan{Float64}
 x1 <= Inf

VariableIndex-in-Interval{Float64}
 x2  [-10.0, 10.0]

@odow odow added Type: Bug Submodule: FileFormats About the FileFormats submodule labels Jun 24, 2023
@odow odow changed the title More LP file parsing issues [FileFormats.LP] more parsing issues Jun 24, 2023
@odow
Copy link
Member

odow commented Jun 24, 2023

To clarify, the only issue was that the lower bound of x1 was treated as 0 instead of -inf, because we didn't handle the case in which a free variable was provided with -infinity <= x <= infinity instead of the explicit syntax x free.

x2 was added as an interval constraint.

@odow
Copy link
Member

odow commented Jun 24, 2023

Fix is here: #2225

@GordStephen
Copy link
Author

Ok, makes sense! Sorry for the user error with x2. Didn’t realize print was available either.

@odow
Copy link
Member

odow commented Jun 24, 2023

LP files have a default lower bound of 0 for a variable. So if a variable is not supplied bounds, the it is x >= 0. So the bug was interpreting -inf <= x <= inf as being the same as not supplying bound haha.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Submodule: FileFormats About the FileFormats submodule Type: Bug
2 participants