Skip to content

Commit cc6437e

Browse files
authored
[FileFormats.MPS] fix writing objective constant in MAX_SENSE (#2778)
1 parent f68a7a0 commit cc6437e

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/FileFormats/MPS/MPS.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ end
451451
function _extract_terms_objective(model, var_to_column, coefficients, flip_obj)
452452
obj_func = _get_objective(model)
453453
_extract_terms(var_to_column, coefficients, "OBJ", obj_func, flip_obj)
454-
return obj_func.constant
454+
return flip_obj ? -obj_func.constant : obj_func.constant
455455
end
456456

457457
function _var_name(

test/FileFormats/MPS/MPS.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,6 +1610,57 @@ function test_int_round_trip()
16101610
return
16111611
end
16121612

1613+
function test_obj_constant_min()
1614+
model = MOI.FileFormats.MPS.Model()
1615+
x = MOI.add_variable(model)
1616+
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
1617+
f = 1.0 * x + 2.0
1618+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
1619+
io = IOBuffer()
1620+
write(io, model)
1621+
dest = MOI.FileFormats.MPS.Model()
1622+
seekstart(io)
1623+
read!(io, dest)
1624+
g = MOI.get(dest, MOI.ObjectiveFunction{typeof(f)}())
1625+
@test g.constant == 2.0
1626+
@test MOI.get(dest, MOI.ObjectiveSense()) == MOI.MIN_SENSE
1627+
return
1628+
end
1629+
1630+
function test_obj_constant_max_to_min()
1631+
model = MOI.FileFormats.MPS.Model()
1632+
x = MOI.add_variable(model)
1633+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
1634+
f = 1.0 * x + 2.0
1635+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
1636+
io = IOBuffer()
1637+
write(io, model)
1638+
dest = MOI.FileFormats.MPS.Model()
1639+
seekstart(io)
1640+
read!(io, dest)
1641+
g = MOI.get(dest, MOI.ObjectiveFunction{typeof(f)}())
1642+
@test g.constant == -2.0
1643+
@test MOI.get(dest, MOI.ObjectiveSense()) == MOI.MIN_SENSE
1644+
return
1645+
end
1646+
1647+
function test_obj_constant_max_to_max()
1648+
model = MOI.FileFormats.MPS.Model(; print_objsense = true)
1649+
x = MOI.add_variable(model)
1650+
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
1651+
f = 1.0 * x + 2.0
1652+
MOI.set(model, MOI.ObjectiveFunction{typeof(f)}(), f)
1653+
io = IOBuffer()
1654+
write(io, model)
1655+
dest = MOI.FileFormats.MPS.Model()
1656+
seekstart(io)
1657+
read!(io, dest)
1658+
g = MOI.get(dest, MOI.ObjectiveFunction{typeof(f)}())
1659+
@test g.constant == 2.0
1660+
@test MOI.get(dest, MOI.ObjectiveSense()) == MOI.MAX_SENSE
1661+
return
1662+
end
1663+
16131664
end # TestMPS
16141665

16151666
TestMPS.runtests()

0 commit comments

Comments
 (0)