Skip to main content

How to write a maximize constraint using gurobipy

Answered

Comments

3 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 why not try our AI Gurobot?.
  • Jaromił Najman
    • Gurobi Staff

    Hi Ying,

    Your first try was almost successful. Please note that the max_() only accepts single Variables and constants as arguments but the term \(\texttt{(b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M)}\) is a LinExpr. Thus, in order to make your first approach work, you can add auxiliary variables for each of the terms as

    N = [1,2,3]
    V = [4,5,6]

    a = m.addVars(N,V,name="a")
    b = m.addVars(N,V,name="b")
    T_ij = m.addVars(N,N,name="T_")
    x = m.addVars(N,N,V,name="x")
    aux = m.addVars(N,N,V,name="aux")
    M = 10000

    for i in N:
    for j in N:
    for k in V:
    m.addConstr(aux[i,j,k] == (b[i,k] + T_ij[i,j] - ( 1-x[i,j,k] )*M))
    m.addConstr(a[j,k] == max_( aux[i,j,k], 0) )

    The same applies to your third approach because the max_() is just a shortcut for the addGenConstrMax() function.

    Your 2nd approach should work as it is, but I would recommend using one of the \(\texttt{max}\) functions.

    Best regards,
    Jaromił

    0
  • YING SIMA
    • Gurobi-versary
    • First Comment
    • First Question

    Hi Jaromił,

     

    I see! That's why I couldn't manage it correctly.

    Thank you so much for your solution and help!!

     

    Best regards,

    Ying

     

     

     

    0

Post is closed for comments.