Warning: linear constraint 0 and linear constraint 2 have the same name
AnsweredI recently switched from Python 3.12 to 3.13, both using Gurobipy 13.0.0. Models that used to build without error messages under Py3.12 now generate warnings of this kind under Py3.13:
“Warning: linear constraint 0 and linear constraint 2 have the same name”
Here is a piece of example code that reproduces the behavior:
import itertools
from gurobipy import GRB, Model
colors = ["black", "white"]
animals = ["cat", "dog"]
sizes = ["small", "large"]
model = Model("test")
x = model.addVars(colors, animals, sizes, vtype=GRB.BINARY, name="x")
model.addConstrs(
(
x[color, animal, size] <= 1
for color, animal, size in itertools.product(colors, animals, sizes)
),
"Test_constraint",
)
model.write("test-model.lp")This is what my lp file looks like when running with Python 3.12 and Gurobipy 13.0.0:
\ Model test
\ LP format - for model browsing. Use MPS format to capture full model detail.
Minimize
Subject To
Test_constraint[black,cat,small]: x[black,cat,small] <= 1
Test_constraint[black,cat,large]: x[black,cat,large] <= 1
Test_constraint[black,dog,small]: x[black,dog,small] <= 1
Test_constraint[black,dog,large]: x[black,dog,large] <= 1
Test_constraint[white,cat,small]: x[white,cat,small] <= 1
Test_constraint[white,cat,large]: x[white,cat,large] <= 1
Test_constraint[white,dog,small]: x[white,dog,small] <= 1
Test_constraint[white,dog,large]: x[white,dog,large] <= 1
Bounds
Binaries
x[black,cat,small] x[black,cat,large] x[black,dog,small]
x[black,dog,large] x[white,cat,small] x[white,cat,large]
x[white,dog,small] x[white,dog,large]
EndIf I run the same code with Python 3.13 and Gurobipy 13.0.0, this message appears:
Warning: linear constraint 0 and linear constraint 2 have the same name "Test_constraint[small]"
The lp file also looks different:
\ Model test
\ LP format - for model browsing. Use MPS format to capture full model detail.
Minimize
Subject To
Test_constraint[small]: x[black,cat,small] <= 1
Test_constraint[large]: x[black,cat,large] <= 1
Test_constraint[small]: x[black,dog,small] <= 1
Test_constraint[large]: x[black,dog,large] <= 1
Test_constraint[small]: x[white,cat,small] <= 1
Test_constraint[large]: x[white,cat,large] <= 1
Test_constraint[small]: x[white,dog,small] <= 1
Test_constraint[large]: x[white,dog,large] <= 1
Bounds
Binaries
x[black,cat,small] x[black,cat,large] x[black,dog,small]
x[black,dog,large] x[white,cat,small] x[white,cat,large]
x[white,dog,small] x[white,dog,large]
EndI am not familiar with the detailed differences in generator expressions between Python 3.12 and 3.13. The problem isn't in itertools.product, because the same warning shows up when I replace itertools.product(colors, animals, sizes) with a hardcoded list of the eight relevant tuples.
What should I change in my addConstrs call to get all three indices back in my constraint names when running Python 3.13?
-
Hi Ruud - This sounds like it might be the known bug listed here: https://docs.gurobi.com/projects/optimizer/en/current/reference/releasenotes/knownbugs.html
Can you test the workaround and see if that resolves your issue?
0 -
Thank you, my issue indeed appears to be the same as that bug. I will test the workaround and let you know if that works.
0 -
Hi Ruud,
This issue is earmarked to be fixed when we release v13.0.1 later this year.
For the record, these days we recommend that users avoid using addConstrs. The performance improvements it once offered disappeared as Python continued to evolve over the years.
- Riley
0 -
Thanks, Riley. We use both addConstr and addConstrs in our models, and only some of the addConstrs calls are prone to this issue. We've changed those with the suggested workaround and that fixes the problem for us. You can consider this ticket closed.
0
Please sign in to leave a comment.
Comments
4 comments