Defining set of decision variables with partially common index sets in gurobi pandas
回答済みGood day,
I want to define the following binary decision variables using the pandas dataframe: x_{ik}, y_{ikn}, z_{kn} and w_{is}.
As we see, while index k is the same for first three variables, index n is only applicable for the second and third decision variables and index s is used only in the fourth decision variable. How can I leverage gurobi-pandas to define these variables using pandas multiindex within the same dataframe?
Appreciate your help with a small example in advance.
Best,
Vusal
-
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 withgppd.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
サインインしてコメントを残してください。
コメント
1件のコメント