Skip to main content

Writing a max function for MTZ constraints for the CVRP

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three 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 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

    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

Post is closed for comments.