Skip to main content

Groupby

Answered

Comments

2 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 why not try our AI Gurobot?.
  • Maliheh Aramon
    • Gurobi Staff

    Hi Othman, 

    You can use the tupledict.sum() method to find the sum of the values associated with keys that match the specified pattern. Using your example, the following code snippet shows a minimal example of how to add constraints limiting the total supply per period. 

    data = {
    "day": ["1st of January", "2nd of January", "10th of January"],
    "period": [1, 1, 2],
    }
    df = pandas.DataFrame.from_dict(data)

    model = gurobipy.Model("model")
    supply = model.addVars(df["day"], vtype=GRB.BINARY, name="supply")

    for period in df["period"].unique():
    # Select the days associated with each period
    days_per_period = df[df["period"] == period]["day"]
    model.addConstr(
    supply.sum(days_per_period) <= 2,
    name=f"supply_limit_per_period_{period}",
    )

    Best regards,

    Maliheh

    0

Post is closed for comments.