Skip to content

Commit e8a4f57

Browse files
authored
[FileFormats] fix reading ConstraintName of VariableIndex constraints (#2066)
1 parent 0cc1724 commit e8a4f57

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/FileFormats/MOF/read.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,10 @@ function _add_constraint(
116116
object::Object,
117117
name_map::Dict{String,MOI.VariableIndex},
118118
)
119-
index = MOI.add_constraint(
120-
model,
121-
function_to_moi(object["function"]::typeof(object), name_map),
122-
set_to_moi(object["set"]::typeof(object)),
123-
)
124-
if haskey(object, "name")
119+
f = function_to_moi(object["function"]::typeof(object), name_map)
120+
s = set_to_moi(object["set"]::typeof(object))
121+
index = MOI.add_constraint(model, f, s)
122+
if haskey(object, "name") && typeof(f) != MOI.VariableIndex
125123
MOI.set(model, MOI.ConstraintName(), index, object["name"]::String)
126124
end
127125
if haskey(object, "dual_start")

test/FileFormats/MOF/MOF.jl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,50 @@ function test_parse_int_coefficient_scalarquadraticfunction()
11571157
return
11581158
end
11591159

1160+
function test_parse_constraintname_variable()
1161+
io = IOBuffer()
1162+
print(
1163+
io,
1164+
"""{
1165+
"version": {"major": 1, "minor": 2},
1166+
"variables": [{"name": "x", "primal_start": 0.0}],
1167+
"objective": {"sense": "min", "function": {"type": "Variable", "name": "x"}},
1168+
"constraints": [{
1169+
"name": "x >= 1",
1170+
"function": {
1171+
"type": "ScalarAffineFunction",
1172+
"terms": [{"coefficient": 1, "variable": "x"}],
1173+
"constant": 0
1174+
},
1175+
"set": {"type": "GreaterThan", "lower": 1},
1176+
"primal_start": 1,
1177+
"dual_start": 0
1178+
}, {
1179+
"name": "x ∈ [0, 1]",
1180+
"function": {"type": "Variable", "name": "x"},
1181+
"set": {"type": "Interval", "lower": 0, "upper": 1}
1182+
}]
1183+
}""",
1184+
)
1185+
seekstart(io)
1186+
model = MOF.Model()
1187+
read!(io, model)
1188+
x = MOI.get(model, MOI.VariableIndex, "x")
1189+
@test MOI.get(model, MOI.NumberOfVariables()) == 1
1190+
@test MOI.get(model, MOI.VariablePrimalStart(), x) == 0.0
1191+
F, S = MOI.VariableIndex, MOI.Interval{Float64}
1192+
@test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 1
1193+
ci = first(MOI.get(model, MOI.ListOfConstraintIndices{F,S}()))
1194+
@test MOI.get(model, MOI.ConstraintFunction(), ci) == x
1195+
@test MOI.get(model, MOI.ConstraintSet(), ci) == MOI.Interval(0.0, 1.0)
1196+
F, S = MOI.ScalarAffineFunction{Float64}, MOI.GreaterThan{Float64}
1197+
@test isa(
1198+
MOI.get(model, MOI.ConstraintIndex, "x >= 1"),
1199+
MOI.ConstraintIndex{F,S},
1200+
)
1201+
return
1202+
end
1203+
11601204
function runtests()
11611205
for name in names(@__MODULE__, all = true)
11621206
if startswith("$(name)", "test_")

0 commit comments

Comments
 (0)