Skip to main content

Defining set of decision variables with partially common index sets in gurobi pandas

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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.
  • Riley Clement
    • Gurobi Staff

    Hi Vusal,

    If the variables have different indexes then it doesn't make sense to have them in the same dataframe, however this does not mean you cannot use gurobipy-pandas.

    Let's start with a simple case, using just the x and z variables from your example.  We can create a pandas.Series for each of the variables (a dataframe isn't necessary here):

    import gurobipy as gp
    from gurobipy import GRB
    import gurobipy_pandas as gppd
    import pandas as pd

    i = (1,2)
    k = (1,2,3)
    n = ("a","b")

    model = gp.Model()

    x = gppd.add_vars(model, pd.MultiIndex.from_product((i,k), names=("i", "k")), vtype=GRB.BINARY, name="x")
    z = gppd.add_vars(model, pd.MultiIndex.from_product((k,n), names=("k", "n")), vtype=GRB.BINARY, name="z")

    Then let's say you wanted to add constraints of the form:

    \[ \sum_{k} C_k x_{i,k} z_{k,n} \geq R_{i,n}, \quad \forall i, n\]

    for some constant vectors C and R.

    import numpy as np
    C = pd.Series(
        np.random.randint(1,10, size=len(k)),
        index=pd.Index(k, name="k"),
    )
    R = pd.Series(
        np.random.randint(1,10, size=len(i)*len(n)),
        pd.MultiIndex.from_product((i,n), names=("i", "n")),
    )


    Adding the constraints can be concisely done with

    gppd.add_constrs(model, (C*x*z).groupby(["i", "n"]).sum(), ">=", R)

    The advantage of using gurobipy-pandas is this will be an efficient way of building the model.

    The disadvantage is that it may be less readable for those unfamiliar with this style.

    - Riley

    0

Post is closed for comments.