index constraint
回答済みHelloo,
I have 2 binary variables Z[i,j,e] and u[i,j,e],
I want to express this constraint :
Z[i,j]+u[i,j] <= 1 (regardless of e)
I dont know how to express it in Gurobipy,
Thank you !!
-
Hi Rayan,
Let's say i, j, e, come from sets I, J and E respectively. If I understand correctly you are wanting to model constraints of the form
\[z_{ije} + u_{ije'} \leq 1, \quad \forall i \in I, j \in J, e \in E, e' \in E\]
If you have defined z and u like so
z = model.addVars(I,J,E, ...)
u = model.addVars(I,J,E, ...)then the you can add the constraints like so
model.addConstrs(
z[i,j,e] + u[i,j,e2] <= 1
for i in I
for j in J
for e in E
for e2 in E
)However you can likely make this formulation stronger if both z and u variables are binary. Consider a set of binary variables, x, and a set of binary variables y and the constraints
\[x_m + y_n \leq 1, \quad \forall m \in M, n \in N\]
If you think of each variable as representing a node in a graph, and an edge exists between nodes if they linked by the above constraints, then what you have is a complete bipartite graph. One set of variables in a solution must always be zero. The moment any x variable has value 1, all y variables must be 0. The moment any y variable has value 1, all x variables must be zero.
We can therefore introduce a binary variable w which is used to decide which set will be 0. We can then replace the constraints above with the following
\begin{align}
x_m \leq w, \quad \forall m \in M\\
y_n \leq 1-w, \quad \forall n \in N
\end{align}which is likely to run much faster.
You have this exact situation for each specific i and j, and so you can introduce binary variables w
w = model.addVars(I,J, vtype="B")
and use the following constraints instead of the ones above
model.addConstrs(
z[i,j,e] <= w[i,j]
for i in I
for j in J
for e in E
)
model.addConstrs(
u[i,j,e] + w[i,j] <= 1
for i in I
for j in J
for e in E
)If your z and u variables are not binary then this is all irrelevant though.
- Riley
0 -
Thank youu !!
0
サインインしてコメントを残してください。
コメント
2件のコメント