Skip to content

Commit b12036d

Browse files
authored
[FileFormats.MPS] allow the RHS name to be optional when reading (#2942)
1 parent d2e1523 commit b12036d

4 files changed

Lines changed: 57 additions & 18 deletions

File tree

src/FileFormats/MPS/read.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -776,19 +776,24 @@ end
776776

777777
# TODO: handle multiple RHS vectors.
778778
function parse_rhs_line(data::TempMPSModel{T}, items) where {T}
779-
if length(items) == 3
779+
if length(items) == 2
780+
# [row name] [value]
781+
parse_single_rhs(data, items[1], parse(T, items[2]), items)
782+
elseif length(items) == 3
780783
# [rhs name] [row name] [value]
781-
rhs_name, row_name, value = items
782-
parse_single_rhs(data, row_name, parse(T, value), items)
784+
parse_single_rhs(data, items[2], parse(T, items[3]), items)
785+
elseif length(items) == 4
786+
# [row name 1] [value 1] [row name 2] [value 2]
787+
parse_single_rhs(data, items[1], parse(T, items[2]), items)
788+
parse_single_rhs(data, items[3], parse(T, items[4]), items)
783789
elseif length(items) == 5
784790
# [rhs name] [row name 1] [value 1] [row name 2] [value 2]
785-
rhs_name, row_name_1, value_1, row_name_2, value_2 = items
786-
parse_single_rhs(data, row_name_1, parse(T, value_1), items)
787-
parse_single_rhs(data, row_name_2, parse(T, value_2), items)
791+
parse_single_rhs(data, items[2], parse(T, items[3]), items)
792+
parse_single_rhs(data, items[4], parse(T, items[5]), items)
788793
else
789794
_throw_parse_error(
790795
data,
791-
"Malformed RHS line: expected three or five fields.",
796+
"Malformed RHS line: expected 2, 3, 4, or 5 fields.",
792797
)
793798
end
794799
return

test/FileFormats/MPS/failing_models/rhs_malformed.mps

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ COLUMNS
66
x c 1
77
x d 1
88
RHS
9-
rhs d con 1
9+
foo
1010
ENDATA

test/FileFormats/MPS/failing_models/rhs_malformed2.mps

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/FileFormats/MPS/test_MPS.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,50 @@ function test_parse_header()
17861786
return
17871787
end
17881788

1789+
function test_issue_2941()
1790+
src = """
1791+
NAME
1792+
ROWS
1793+
E c2
1794+
E c3
1795+
E c4a
1796+
E c4b
1797+
E c5a
1798+
E c5b
1799+
COLUMNS
1800+
x c2 1.0
1801+
x c3 1.0
1802+
x c4a 1.0
1803+
x c4b 1.0
1804+
x c5a 1.0
1805+
x c5b 1.0
1806+
RHS
1807+
c2 1.1
1808+
rhs c3 1.2
1809+
c4a 1.3 c4b 1.4
1810+
rhs c5a 1.5 c5b 1.6
1811+
BOUNDS
1812+
LO bounds x 1
1813+
ENDATA
1814+
"""
1815+
model = MPS.Model()
1816+
read!(IOBuffer(src), model)
1817+
dest = MOI.Utilities.Model{Float64}()
1818+
MOI.copy_to(dest, model)
1819+
for (c_name, rhs) in [
1820+
"c2" => 1.1,
1821+
"c3" => 1.2,
1822+
"c4a" => 1.3,
1823+
"c4b" => 1.4,
1824+
"c5a" => 1.5,
1825+
"c5b" => 1.6,
1826+
]
1827+
ci = MOI.get(dest, MOI.ConstraintIndex, c_name)
1828+
@test MOI.get(dest, MOI.ConstraintSet(), ci).value == rhs
1829+
end
1830+
return
1831+
end
1832+
17891833
end # TestMPS
17901834

17911835
TestMPS.runtests()

0 commit comments

Comments
 (0)