Skip to main content

Writing a max function for MTZ constraints for the CVRP

Answered

Comments

3 comments

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Magnus,

    When working with general constraints such as \(\max\), one has to introduce auxiliary variables for each general constraint term. You constraint will then look like

    \[\begin{align*}
    u_i &\leq Q - (Q - z_i - q_i)x_{1i} - \sum_{j=2,j\neq i}^{n} q_j x_{ij}\\
    z_i &= \max\{q_j, j\neq i \}
    \end{align*}\]

    In Python code this would look similar to

    for i in N:
    z = mdl.addVar(lb=-GRB.INFINITY,name="z[%d]"%(i))
    mdl.addGenConstrMax(z, [q[j] for j in N if j != i], name="max_constr[%d]"%(i))
     mdl.addConstr(u[i] <= Q - (Q - z - q[i])*x[0,i] - quicksum(q[j]*x[i,j] for j in N))

    Best regards, 
    Jaromił

    0
  • Gwyneth Butera
    • Gurobi Staff Gurobi Staff

    Please also see our Knowledge Base article How do I model logical and general expressions? for additional information.

    1
  • Magnus Mariegaard
    • Gurobi-versary
    • Conversationalist
    • First Question

    Thank you both for the quick and precise responses, that helped a lot.

    0

Please sign in to leave a comment.