Skip to main content

How to model this "nested constraint" in gurobipy?

Answered

Comments

2 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Jaromił Najman
    • Gurobi Staff

    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.