How to do a specific constrain
AnsweredHow can i make this constrain in gurobi?

0
-
Hi,
I assume that you would like to build the constraint below using gurobipy-pandas:
\[\sum_{t^{\prime} = t} ^{t+3} x_{e, t^{\prime}, s} \leq 3, ~~ \forall e \in E, t \in \{0, \cdots, |T| - 4\}, s \in S\]
One approach that comes to my mind is to manually construct the left-hand side of the constraints for each \(e\), \(t\), \(s\) via mapping the indices of the variable series \(x\) to a function. See the script below as an example:
import gurobipy as gp
from gurobipy import GRB
import gurobipy_pandas as gppd
import pandas as pd
import numpy as np
E = range(5)
S = range(10)
T = range(6)
model = gp.Model()
x = gppd.add_vars(
model, pd.MultiIndex.from_product((E, T, S), names=("e", "t", "s")), name="x"
)
# Manually construct the LHS for each index e, t, s using the dataframe x_df
x_df = x.reset_index()
def get_lhs(index):
e, t, s = index
if t <= len(T) -4:
return x_df.query("e == @e and s == @s and t >= @t and t <= @t+3")["x"].sum()
return np.nan
# Drop the nan from LHS
LHS = x.index.map(get_lhs).dropna()
gppd.add_constrs(
model,
LHS,
GRB.LESS_EQUAL,
3,
name="c",
)Best regards,Maliheh0
Please sign in to leave a comment.
Comments
1 comment