How to model this "nested constraint" in gurobipy?
AnsweredI have this formulation that I called it "nested constraint" (don't know if its right). What is the best way to formulate this in python gurobipy.

My current approach is that decompose these with three separate constraint below,
m.addConstrs(((gp.quicksum(y[i,j,l] for i in vertex if i!=j)) - (gp.quicksum(y[j,i,l] for i in vertex if i!=j)) == demand[l]
for j in vertex for l in customer if j==l))
m.addConstrs(((gp.quicksum(y[i,j,l] for i in vertex if i!=j)) - (gp.quicksum(y[j,i,l] for i in vertex if i!=j)) == 0
for j in customer for l in customer if j!=l))
m.addConstrs(((gp.quicksum(y[i,j,l] for i in vertex if i!=j)) - (gp.quicksum(y[j,i,l] for i in vertex if i!=j)) == -demand[l]
for j in vertex for l in customer if j==0))
0
-
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 Rihot,
The probably easiest way is to use auxiliary variables \(z_{j,l}\) and auxiliary equality constraints.
\[\begin{align*}
\sum_{i\in V}y_{ij}^l - \sum_{i \in V} y_{ji}^l &= z_{j,l} \quad \forall j, l \in V'\\
z_{j,l} &= q_l \quad \forall j,l \in V' \text{ with } j = l\\
z_{j,l} &= 0 \quad \forall j,l \in V' \text{ with } j \neq l\\
z_{j,l} &= -q_l \quad \forall j, l \in V' \text{ with } j = 0
\end{align*}\]A pseudo-code might look something like
m.addConstrs(qp.quicksum(y[i,j,l] - y[j,i,l] for i in I) == z[j,l] for j in V, for l in V)
for j in V:
for l in V:
if j == 0:
m.addConstr(z[j,l] == -q[l])
else if j == l:
m.addConstr(z[j,l] == q[l])
else:
m.addConstr(z[j,l] == 0)Best regards,
Jaromił0
Post is closed for comments.
Comments
2 comments