Skip to content

[FileFormats.LP] fix reading models with default bounds #2121

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

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/FileFormats/LP/LP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,6 @@ function _parse_section(
if _is_less_than(tokens[2])
# name <= bound
ub = rhs
# LP files have default lower bounds of 0, unless the upper
# bound is less than 0.
lb = ub > 0.0 ? 0.0 : -Inf
elseif _is_greater_than(tokens[2])
# name >= bound
lb = rhs
Expand All @@ -800,9 +797,6 @@ function _parse_section(
elseif _is_greater_than(tokens[2])
# bound >= name
ub = lhs
# LP files have default lower bounds of 0, unless the upper
# bound is less than 0.
lb = ub > 0.0 ? 0.0 : -Inf
elseif _is_equal_to(tokens[2])
lb = ub = lhs
else
Expand Down
84 changes: 79 additions & 5 deletions test/FileFormats/LP/LP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,8 @@ function test_read_model1_tricky()
@test occursin("Var4 >= 5.5", file)
@test occursin("V3 >= -3", file)
@test occursin("V5 = 1", file)
@test occursin("0 <= V2 <= 3", file)
@test occursin("V2 <= 3", file)
@test occursin("V2 >= 0", file)
@test occursin("0 <= V7 <= 1", file)
@test occursin("0 <= V8 <= 1", file)
@test occursin("V6 free", file)
Expand Down Expand Up @@ -717,15 +718,15 @@ end
function test_wrong_way_bounds()
for (case, result) in [
"x >= 2" => "x >= 2",
"x <= 2" => "0 <= x <= 2",
"x <= 2" => "x <= 2\nx >= 0",
"x == 2" => "x = 2",
"x > 2" => "x >= 2",
"x < 2" => "0 <= x <= 2",
"x < 2" => "x <= 2\nx >= 0",
"x = 2" => "x = 2",
"2 >= x" => "0 <= x <= 2",
"2 >= x" => "x <= 2\nx >= 0",
"2 <= x" => "x >= 2",
"2 == x" => "x = 2",
"2 > x" => "0 <= x <= 2",
"2 > x" => "x <= 2\nx >= 0",
"2 < x" => "x >= 2",
"2 = x" => "x = 2",
]
Expand Down Expand Up @@ -758,6 +759,79 @@ function test_variable_coefficient_variable()
return
end

function _test_round_trip(bound, needle)
model = MOI.FileFormats.LP.Model()
io = IOBuffer()
write(io, "Minimize\nobj: x\nBounds\n$bound\nEnd")
seekstart(io)
read!(io, model)
seekstart(io)
write(io, model)
seekstart(io)
file = read(io, String)
@test occursin(needle, file)
return
end

function test_reading_bounds()
# Test lower bound
_test_round_trip("x >= 1", "Bounds\nx >= 1\nEnd")
_test_round_trip("x >= 0", "Bounds\nx >= 0\nEnd")
_test_round_trip("x >= -1", "Bounds\nx >= -1\nEnd")
_test_round_trip("x > 1", "Bounds\nx >= 1\nEnd")
_test_round_trip("x > 0", "Bounds\nx >= 0\nEnd")
_test_round_trip("x > -1", "Bounds\nx >= -1\nEnd")
# Test reversed lower bound
_test_round_trip("1 <= x", "Bounds\nx >= 1\nEnd")
_test_round_trip("0 <= x", "Bounds\nx >= 0\nEnd")
_test_round_trip("-1 <= x", "Bounds\nx >= -1\nEnd")
_test_round_trip("1 < x", "Bounds\nx >= 1\nEnd")
_test_round_trip("0 < x", "Bounds\nx >= 0\nEnd")
_test_round_trip("-1 < x", "Bounds\nx >= -1\nEnd")
# Test upper bound
_test_round_trip("x <= 1", "Bounds\nx <= 1\nx >= 0\nEnd")
_test_round_trip("x <= 0", "Bounds\nx <= 0\nx >= 0\nEnd")
_test_round_trip("x <= -1", "Bounds\nx <= -1\nEnd")
_test_round_trip("x < 1", "Bounds\nx <= 1\nx >= 0\nEnd")
_test_round_trip("x < 0", "Bounds\nx <= 0\nx >= 0\nEnd")
_test_round_trip("x < -1", "Bounds\nx <= -1\nEnd")
# Test reversed upper bound
_test_round_trip("1 >= x", "Bounds\nx <= 1\nx >= 0\nEnd")
_test_round_trip("0 >= x", "Bounds\nx <= 0\nx >= 0\nEnd")
_test_round_trip("-1 >= x", "Bounds\nx <= -1\nEnd")
_test_round_trip("1 > x", "Bounds\nx <= 1\nx >= 0\nEnd")
_test_round_trip("0 > x", "Bounds\nx <= 0\nx >= 0\nEnd")
_test_round_trip("-1 > x", "Bounds\nx <= -1\nEnd")
# Test equality
_test_round_trip("x == 1", "Bounds\nx = 1\nEnd")
_test_round_trip("x == 0", "Bounds\nx = 0\nEnd")
_test_round_trip("x == -1", "Bounds\nx = -1\nEnd")
_test_round_trip("1 = x", "Bounds\nx = 1\nEnd")
_test_round_trip("0 = x", "Bounds\nx = 0\nEnd")
_test_round_trip("-1 = x", "Bounds\nx = -1\nEnd")
# Test interval
_test_round_trip("0 <= x <= 1", "Bounds\n0 <= x <= 1\nEnd")
_test_round_trip("-1 <= x <= 1", "Bounds\n-1 <= x <= 1\nEnd")
_test_round_trip("-2 <= x <= -1", "Bounds\n-2 <= x <= -1\nEnd")
# Test reversed interval
_test_round_trip("1 >= x >= 0", "Bounds\n0 <= x <= 1\nEnd")
_test_round_trip("1 >= x >= -1", "Bounds\n-1 <= x <= 1\nEnd")
_test_round_trip("-1 >= x >= -2", "Bounds\n-2 <= x <= -1\nEnd")
# Test double-sided equality
_test_round_trip("1 <= x <= 1", "Bounds\nx = 1\nEnd")
_test_round_trip("0 <= x <= 0", "Bounds\nx = 0\nEnd")
_test_round_trip("-2 <= x <= -2", "Bounds\nx = -2\nEnd")
# Test upper then lower
_test_round_trip("x <= 1\nx >= 0", "Bounds\nx <= 1\nx >= 0\nEnd")
_test_round_trip("x <= 2\nx >= 1", "Bounds\nx <= 2\nx >= 1\nEnd")
_test_round_trip("x <= 2\nx >= -1", "Bounds\nx <= 2\nx >= -1\nEnd")
# Test lower then upper
_test_round_trip("x >= 0\nx <= 1", "Bounds\nx <= 1\nx >= 0\nEnd")
_test_round_trip("x >= 1\nx <= 2", "Bounds\nx <= 2\nx >= 1\nEnd")
_test_round_trip("x >= -1\nx <= 2", "Bounds\nx <= 2\nx >= -1\nEnd")
return
end

function runtests()
for name in names(@__MODULE__, all = true)
if startswith("$(name)", "test_")
Expand Down