Skip to main content

How to model this "nested constraint" in gurobipy?



1 comment

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Rihot,

    The probably easiest way is to use auxiliary variables \(z_{j,l}\) and auxiliary equality constraints.

    \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

    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])
    m.addConstr(z[j,l] == 0)

    Best regards, 


Please sign in to leave a comment.