メインコンテンツへスキップ

Adding a constraint

回答済み

コメント

7件のコメント

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    You approach looks fine. You could alternatively just sum over all students of a specific supervisor' area directly

    for s in supervisors:
    oq = get_quota(s)
    m.addConstr(gurobipy.quicksum(x[i,a] for i in students for a in supervisors[s].areas) <= oq)

    Best regards, 
    Jaromił

    0
  • Colin Layfield
    • Gurobi-versary
    • First Question
    • First Comment

    Thanks so much for your reply.

    Could I ask for advice as to how to post some additional constraints?  (Different problem).  It's a sports scheduling problem and a deviation variable needs to be calculated to determine (for the objective function calculation) how far a soft constraint has been deviated from.  See below.  Any suggestions appreciated.

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    You can use the addGenConstrAbs method to model equalities BA1 and addGenConstrMax to model qualities BA2. Note that all general constraints only accept Var objects as input. This means that you will have to introduce an auxiliary variable for every term \(g_{ij} - \sum_{s\in P}x_{ijs}\) and \(\sum_{j\in U\backslash \{i\}}(x_{ijs} + x_{jis})+1\).

    0
  • Colin Layfield
    • Gurobi-versary
    • First Question
    • First Comment

    The auxiliary variables would be the deviation variables, yes?

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    The auxiliary variables \(w_{ij}, z_{is}\) would be

    \[\begin{align*}
    w_{ij} &= g_{ij} - \sum_{s\in P}x_{ijs} \quad \forall i,j\in U, i\neq j\\
    z_{is} &= \sum_{j\in U\backslash \{i\}}(x_{ijs} + x_{jis})+1 \quad \forall i\in U, s\in P
    \end{align*}\]

    0
  • Colin Layfield
    • Gurobi-versary
    • First Question
    • First Comment

    OK, I think I see now.  The w and z variables basically correspond to the "d" ones in the original equation.

    Thanks so much for your fast replies.

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    The \(w\) and \(z\) variables don't really correspond to your \(d\) variables. They are required to work with Gurobi's general constraints. What you have to model in Gurobi is

    \[\begin{align*}
    w_{ij} &= g_{ij} - \sum_{s\in P}x_{ijs} &\forall i,j\in U, i\neq j\\
    z_{is} &= \sum_{j\in U\backslash \{i\}}(x_{ijs} + x_{jis})+1 &\forall i\in U, s\in P\\
    d_{ij} &= |w_{ij}| &\forall i,j\in U, i\neq j\\
    d_{is} &= \max(0, z_{is}) &\forall i\in U, s\in P
    \end{align*}\]
    This is because all general constraints only accept single Var objects as input.

    0

サインインしてコメントを残してください。