Skip to main content

How to write max function in models




  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Ankit,

    The max function works a bit different in Gurobi. It is used to define a max constraint

    \[ z = \max\{x_1,x_2,1,2\},\]

    where \(z,x_1,x_2\) are optimization variables and \(1,2\) constant values. The term \(\texttt{ct[nStage,j]-D[j]}\) describes a linear expression and not an optimization variable. Thus, in order to model the objective function, you have to introduce 2 additional auxiliary variables for each \(j\). Then, your code might look something like

    import gurobipy as gp
    from gurobipy import GRB

    m = gp.Model('test')
    # some arbitrarily picked values
    job = [1,2,3,4,5]
    nStage = 3

    ct = m.addVars(4, job, vtype=GRB.CONTINUOUS, name="ct")
    D = m.addVars(job, vtype=GRB.CONTINUOUS, name="D")
    # define auxiliary variables
    aux1 = m.addVars(job, vtype=GRB.CONTINUOUS, name="aux1")
    aux2 = m.addVars(job, vtype=GRB.CONTINUOUS, name="aux2")

    # add constraints defining ct_j - D_j = aux1_j
    m.addConstrs( (ct[nStage,j]-D[j] - aux1[j] == 0 for j in job), name="aux_constr1")
    # add constraints defining aux2_j = max(aux1_j, 0)
    m.addConstrs( (aux2[j] == gp.max_([aux1[j],0.0]) for j in job), name="aux_constr2")

    # set objective to be the sum of all aux2 variables
    m.setObjective( gp.quicksum(aux2[j] for j in job))

    Best regards,


  • Ankit khare
    First Comment
    First Question

    Thanks for the reply. It worked.

    I am facing the same problem with one constraint.

     mdl.add_constraints(ct[g,j1])>= mdl.max(ct[g-1,j1], ct[g,j]) + PT[g-1][j1-1] for f in factory for j in job for j1 in job if j1!=j for g in stage if g>1 for i in machine[g-1] ) 
    where ct is the decision variable. I tried as follows but couldn't able to run it
    First approach:
    mdl.addConstrs(ct[g,j1] >= gp.max_(ct[g-1,j1],ct[g,j]) + PT[g-1][j1-1] for f in factory for j in job for j1 in job if j1!=j for g in stage if g>1 for i in machine[g-1] )
    Second approach:
    mdl.addConstrs((aux3[g,j] == gp.max_(ct[g-1,j1],ct[g,j]) for j in job for j1 in job if j1!=j for g in stage if g>1), name="aux_constr3")
    mdl.addConstrs(ct[g,j1] >= aux3[g,j] + PT[g-1][j1-1] for f in factory for j in job for j1 in job if j1!=j for g in stage if g>1 for i in machine[g-1] )
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Ankit,

    The max_() function has to be used in an equality constraint. Thus, your second approach is the way to go. Could you provide the error message and (if possible) a minimal working example? How do you define your variables \(\texttt{ct}\)?

    Best regards,

  • Shiyang Huang

    Does Gurobi linearize max_ constraints automatically? could this lead to slower computation?

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Does Gurobi linearize max_ constraints automatically?


    could this lead to slower computation?

    Do you mean in comparison to treating the \(\max\) function as a nondifferentiable function? It is possible that there are cases where treating the \(\max\) function without linearization via additional binary variables and auxiliary constraints might be beneficial. I would say that linearization should work very well in most of cases.



Please sign in to leave a comment.