-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I add constraints that include two indices simultaneously ? #57
Comments
Hi @Amoozegar, for a continuous index you can use pandas' shift method to align the left and right hand sides of your constraint on the same index: import pandas as pd
import gurobipy as gp
from gurobipy import GRB
import gurobipy_pandas as gppd
J = 5
model = gp.Model()
F = gppd.add_vars(model, pd.RangeIndex(1, J+1), name="F")
index = pd.RangeIndex(1, J) # 1 .. J-1 index for constraints
constraints = gppd.add_constrs(
model,
F.shift(-1).loc[index], # Shift F[j+1] term onto index j
GRB.LESS_EQUAL,
F.loc[index]
) After updating the model you can check the result as follows:
|
@Amoozegar I would do it a bit differently than @simonbowly If I started with the following df/series of variables: I would do it with the following pandas
Which yields the following constraints:
|
Thanks for the response. How about formulating "Fi,j+1,z <= Fi,j,z" ? |
I think it's worth adding these to the documentation as examples. Probably a time-indexed formulation of some kind is the logical place to showcase it. |
That makes sense. |
I need to add constraints that include two indices on a same variable. Here is an example:
The text was updated successfully, but these errors were encountered: