Skip to content
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: 6 additions & 0 deletions src/FileFormats/MPS/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,9 @@ function _collect_coefficients(
for index in MOI.get(model, MOI.ListOfConstraintIndices{F,S}())
row_name = MOI.get(model, MOI.ConstraintName(), index)
func = MOI.get(model, MOI.ConstraintFunction(), index)
if !iszero(func.constant)
throw(MOI.ScalarFunctionConstantNotZero{T,F,S}(func.constant))
end
_extract_terms(var_to_column, coefficients, row_name, func)
end
return
Expand All @@ -281,6 +284,9 @@ function _collect_indicator(
func = MOI.get(model, MOI.ConstraintFunction(), index)
funcs = MOI.Utilities.eachscalar(func)
z = convert(MOI.VariableIndex, funcs[1])
if !iszero(func.constants[2])
throw(MOI.ScalarFunctionConstantNotZero{T,F,S}(func.constants[2]))
end
_extract_terms(var_to_column, coefficients, row_name, funcs[2])
condition = _activation_condition(S)
var_name = _var_name(model, z, var_to_column[z], options.generic_names)
Expand Down
58 changes: 52 additions & 6 deletions test/FileFormats/MPS/test_MPS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ import MathOptInterface.FileFormats: MPS
import OrderedCollections: OrderedDict

function runtests()
for name in names(@__MODULE__; all = true)
if startswith("$(name)", "test_")
@testset "$name" begin
getfield(@__MODULE__, name)()
end
end
is_test(name) = startswith("$name", "test_")
@testset "$name" for name in filter(is_test, names(@__MODULE__; all = true))
getfield(@__MODULE__, name)()
end
return
end
Expand Down Expand Up @@ -1926,6 +1923,55 @@ function test_semi_reading_2()
return
end

function test_scalar_function_constant_not_zero()
for input in [
"""
variables: x
c: 1.0 * x + 2.0 >= 3.0
""",
"""
variables: x
c: 1.0 * x * x + 2.0 >= 3.0
""",
"""
variables: x
c: 1.0 * x + 2.0 <= 3.0
""",
"""
variables: x
c: 1.0 * x * x + 2.0 <= 3.0
""",
"""
variables: x
c: 1.0 * x + 2.0 == 3.0
""",
"""
variables: x
c: 1.0 * x * x + 2.0 == 3.0
""",
"""
variables: x, z
c: [z, 1.0 * x + 2.0] in Indicator{ACTIVATE_ON_ONE}(LessThan(1.0))
z in ZeroOne()
""",
"""
variables: x, z
c: [z, 1.0 * x + 2.0] in Indicator{ACTIVATE_ON_ONE}(GreaterThan(1.0))
z in ZeroOne()
""",
"""
variables: x, z
c: [z, 1.0 * x + 2.0] in Indicator{ACTIVATE_ON_ONE}(EqualTo(1.0))
z in ZeroOne()
""",
]
model = MPS.Model()
MOI.Utilities.loadfromstring!(model, input)
@test_throws(MOI.ScalarFunctionConstantNotZero, sprint(write, model))
end
return
end

end # TestMPS

TestMPS.runtests()
Loading