Skip to content

Commit b172642

Browse files
authored
Fix setting names that are too long (#580)
1 parent 9bafe7d commit b172642

File tree

3 files changed

+63
-23
lines changed

3 files changed

+63
-23
lines changed

src/MOI_wrapper/MOI_indicator_constraint.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,12 @@ function MOI.set(
183183
_update_if_necessary(model, check_attribute_change = false)
184184
info = _info(model, c)
185185
info.name = name
186-
if !isempty(name)
186+
if length(name) <= GRB_MAX_NAMELEN
187187
row = Cint(_info(model, c).row - 1)
188188
ret = GRBsetstrattrelement(model, "GenConstrName", row, name)
189189
_check_ret(model, ret)
190+
_require_update(model, attribute_change = true)
190191
end
191-
_require_update(model, attribute_change = true)
192192
return
193193
end
194194

src/MOI_wrapper/MOI_wrapper.jl

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,9 +1101,13 @@ function MOI.set(
11011101
)
11021102
info = _info(model, v)
11031103
info.name = name
1104-
ret = GRBsetstrattrelement(model, "VarName", Cint(info.column) - 1, name)
1105-
_check_ret(model, ret)
1106-
_require_update(model, attribute_change = true)
1104+
if length(name) <= GRB_MAX_NAMELEN
1105+
# Gurobi has a maximum name limit of GRB_MAX_NAMELEN characters.
1106+
col = c_column(model, v)
1107+
ret = GRBsetstrattrelement(model, "VarName", col, name)
1108+
_check_ret(model, ret)
1109+
_require_update(model, attribute_change = true)
1110+
end
11071111
model.name_to_variable = nothing
11081112
return
11091113
end
@@ -2256,9 +2260,10 @@ function MOI.set(
22562260
)
22572261
info = _info(model, c)
22582262
info.name = name
2259-
if !isempty(name)
2260-
ret =
2261-
GRBsetstrattrelement(model, "ConstrName", Cint(info.row - 1), name)
2263+
if length(name) <= GRB_MAX_NAMELEN
2264+
# Gurobi has a maximum name limit of GRB_MAX_NAMELEN characters.
2265+
row = Cint(info.row - 1)
2266+
ret = GRBsetstrattrelement(model, "ConstrName", row, name)
22622267
_check_ret(model, ret)
22632268
_require_update(model, attribute_change = true)
22642269
end
@@ -3348,8 +3353,12 @@ function MOI.get(model::Optimizer, ::MOI.Name)
33483353
return unsafe_string(valueP[])
33493354
end
33503355

3351-
function MOI.set(model::Optimizer, ::MOI.Name, name::String)
3356+
function MOI.set(model::Optimizer, attr::MOI.Name, name::String)
33523357
ret = GRBsetstrattr(model, "ModelName", name)
3358+
if ret == GRB_ERROR_INVALID_ARGUMENT
3359+
msg = "Name too long (maximum name length is $GRB_MAX_NAMELEN characters)"
3360+
throw(MOI.SetAttributeNotAllowed(attr, msg))
3361+
end
33533362
_check_ret(model, ret)
33543363
_require_update(model, attribute_change = true)
33553364
return
@@ -4444,21 +4453,13 @@ function MOI.set(
44444453
name::String,
44454454
)
44464455
info = _info(model, c)
4447-
if !isempty(info.name) && model.name_to_constraint_index !== nothing
4448-
delete!(model.name_to_constraint_index, info.name)
4449-
end
4450-
_update_if_necessary(model)
4451-
ret = GRBsetstrattrelement(model, "QCName", Cint(info.row - 1), name)
4452-
_check_ret(model, ret)
4453-
_require_update(model, attribute_change = true)
44544456
info.name = name
4455-
if model.name_to_constraint_index === nothing || isempty(name)
4456-
return
4457-
end
4458-
if haskey(model.name_to_constraint_index, name)
4459-
model.name_to_constraint_index = nothing
4460-
else
4461-
model.name_to_constraint_index[c] = name
4457+
_update_if_necessary(model)
4458+
if length(name) <= GRB_MAX_NAMELEN
4459+
ret = GRBsetstrattrelement(model, "QCName", Cint(info.row - 1), name)
4460+
_check_ret(model, ret)
4461+
_require_update(model; attribute_change = true)
44624462
end
4463+
model.name_to_constraint_index = nothing
44634464
return
44644465
end

test/MOI/MOI_wrapper.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,45 @@ function test_primal_feasible_status()
977977
return
978978
end
979979

980+
function test_ModelName_too_long()
981+
model = Gurobi.Optimizer(GRB_ENV)
982+
@test_throws(
983+
MOI.SetAttributeNotAllowed{MOI.Name},
984+
MOI.set(model, MOI.Name(), "a"^(GRB_MAX_NAMELEN + 1)),
985+
)
986+
name = "a"^GRB_MAX_NAMELEN
987+
MOI.set(model, MOI.Name(), name)
988+
@test MOI.get(model, MOI.Name()) == name
989+
return
990+
end
991+
992+
function test_VarName_too_long()
993+
model = Gurobi.Optimizer(GRB_ENV)
994+
x = MOI.add_variable(model)
995+
name = "a"^256
996+
MOI.set(model, MOI.VariableName(), x, name)
997+
@test MOI.get(model, MOI.VariableName(), x) == name
998+
MOI.set(model, MOI.VariableName(), x, "x")
999+
@test MOI.get(model, MOI.VariableName(), x) == "x"
1000+
MOI.set(model, MOI.VariableName(), x, "")
1001+
@test MOI.get(model, MOI.VariableName(), x) == ""
1002+
return
1003+
end
1004+
1005+
function test_ConstrName_too_long()
1006+
model = Gurobi.Optimizer(GRB_ENV)
1007+
x = MOI.add_variable(model)
1008+
c = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0))
1009+
name = "a"^256
1010+
MOI.set(model, MOI.ConstraintName(), c, name)
1011+
@test MOI.get(model, MOI.ConstraintName(), c) == name
1012+
MOI.set(model, MOI.ConstraintName(), c, "c")
1013+
@test MOI.get(model, MOI.ConstraintName(), c) == "c"
1014+
MOI.set(model, MOI.ConstraintName(), c, "")
1015+
@test MOI.get(model, MOI.ConstraintName(), c) == ""
1016+
return
1017+
end
1018+
9801019
end # TestMOIWrapper
9811020

9821021
TestMOIWrapper.runtests()

0 commit comments

Comments
 (0)