Skip to main content

How to implement a max over constraint considering an array of variables




  • Silke Horn
    Gurobi Staff Gurobi Staff

    You can use a max general constraint for this. Such a max constraint states that one variable should equal the maximum of a set of other variables (and a constant).

    Note that since the arguments for the max constraint need to be variables and not linear expressions you need to define some auxiliary variables $$\mathrm{aux}_{op} = L_{op} \ast z_{op1}.$$ Then you can define $$m_{o1} = \max_{p}(\mathrm{aux}_{op})$$ and finally $$t_{o1}^{walk} \leq m_{o1}.$$

  • Nicolò Angeli
    First Comment
    First Question

    Thank you for your help, I tried to structure the code following the structure you provided, although I do not know how to fill the arguments of the addGenConstrMax(). I leave here the code snippet

    // Constraint 1: aux_ow1 = L_op * z_op1 for all o, for all p

    GRBVar[][] aux_op1 = new GRBVar[O][P];

    for (int i=0; i<O;i++) {

    for (int j=1; j<P; j++) {

    expr = new GRBLinExpr();


    model.addConstr(expr, GRB.EQUAL,aux_op1[i][j], "constraint8["+i+"]["+j+"]");



    // Constraint 2: m_o1 = max over p (aux_op1) for all o

    GRBVar[] m_o1 = new GRBVar[O];

    for (int i=0; i<O;i++) {



    // Constraint 3: t_walk_o1 = m_o1 for all o

    for (int i=0; i<O;i++) {

    expr = new GRBLinExpr();


    model.addConstr(expr, GRB.EQUAL,t_walk_ow[i][0], "constraint8["+i+"]");



Please sign in to leave a comment.