Constraint on decision variables in a multi period scenario
AnsweredI have the following parameters:
period = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
facility = ['f1', 'f2', 'f3', 'f4', 'f5']
I have declared a binary decision variable as follows:
X_f = m.addVars(facility, period, vtype=GRB.BINARY, name="X_f")
In a multi period facility location problem, I have the following constraints:
- for every facility, if X_f have the boolean value of 0 in period 0, it remains 0 for all of the periods
- for every facility, if X_f has the boolean value 1 in period 0, then it remains 1 for all of the periods
Basically, it means that once a facility has been opened in period 0, it should not be closed; and if a facility is closed in period 0, then it should remain closed.
I tried to implement this as follows:
for f in facility:However, I get the error
if X_f[f, 0].X == 0:
m.addConstrs((X_f[f, t+1] == 0 for t in range(0, 11)))
if X_f[f, 0].X == 1:
m.addConstrs((X_f[f, t+1] == 1 for t in range(0, 11)))
m.optimize()
AttributeError: Index out of range for attribute 'X'
I understand this is due to the fact that X_f[f, 0].X only has value after the optimization has run.
Is there another way to implement this constraint in a multi period setting?
-
Official comment
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. -
Hi,
You are correct that you cannot access the X attribute before running the optimization. Instead, you can use indicator constraints to model something like this.
This article contains additional information on how to model conditional constraints.
Silke
0
Post is closed for comments.
Comments
2 comments