Replace automatic naming in model.addConstrs
AnsweredHi,
I am trying to code the constraint names based on iterations and a condition which should replace the name automatically created by Gurobi. However, the naming does not replace the automatic name. I have the code below:
LP.addConstrs((I[month, item] >= safety_stock[item] - M * (1 - i[month,item])
for month in months
for item in items
if month >= months[a+1]), name = "bigM1(%s,%s)" %(month,item))
the outcome is as follows where the name in paranthesis is the correct name and the one in bracket is the one created automatically which I want to remove.

-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Maryam,
You are using the method Model.addConstrs() to add multiple constraints into the model. As explained in the documentation, the argument \(\texttt{name}\) is the name pattern. Since each constraint should have a unique name, the name pattern will be subscripted by the index of the generator expression which is \((\texttt{month}, \texttt{item})\) in your code. You cannot name all your \(\texttt{BigM1}\) constraints the same, so the outcome you see is expected.
Best regards,
Maliheh
0 -
Hi Maliheh,
The issue is that the name pattern is subscripted without considering the if condition in my constraint. I need the name to follow the same conditions as in my constraint (if month >= months[a+1]) and therefore skipping some month in the months. Is there any solution available for this situation?
Best,
0 -
Hi Maryam,
As I explained in my previous message, the name pattern will be subscripted by the index of the generator expression. Since the generator expression respects the conditional statements, the naming pattern does so.
I created this small example where you can copy and run on your machine to see it for yourself.
import gurobipy as gp
from gurobipy import GRB
if __name__ == "__main__":
m = gp.Model("test")
months, items = ["Dec", "Jan", "Feb", "Mar", "Apr", "May"], [
"item1",
"item2",
"item3",
]
x = m.addVars(months, items, name="x")
month_prefix, item_prefix = "Dec", "item1"
m.addConstrs(
(
x[month, item] <= 1
for month in months
for item in items
if month in ["Jan", "Mar", "May"]
),
name=f"x_{month_prefix}_{item_prefix}",
)
m.update()
# Get constraint names
for c in m.getConstrs():
print(c.ConstrName)If you run the code snippet above, the output will be:x_Dec_item1[Jan,item1]
x_Dec_item1[Jan,item2]
x_Dec_item1[Jan,item3]
x_Dec_item1[Mar,item1]
x_Dec_item1[Mar,item2]
x_Dec_item1[Mar,item3]
x_Dec_item1[May,item1]
x_Dec_item1[May,item2]
x_Dec_item1[May,item3]As seen above, the naming pattern does respect the condition \(\texttt{if month in ["Jan", "Mar", "May"]}\).
If you see otherwise in your code, please post a minimal reproducible example here with the input data such that I can run it and clearly explain what you see and what you are expecting to see.
Best regards,
Maliheh
0 -
Thanks, something was wrong with my constraint condition. The name pattern was subscripted correctly.
0
Post is closed for comments.
Comments
5 comments