Skip to main content

Replace automatic naming in model.addConstrs

Answered

Comments

5 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Maliheh Aramon
    • Gurobi Staff

    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
  • Maryam Azani
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Maliheh Aramon
    • Gurobi Staff

    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
  • Maryam Azani
    • Gurobi-versary
    • First Comment
    • First Question

    Thanks, something was wrong with my constraint condition. The name pattern was subscripted correctly.

    0

Post is closed for comments.