Skip to main content

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

Ongoing

Comments

2 comments

  • 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}.$$

    0
  • Nicolò Angeli
    Gurobi-versary
    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();

    expr.addTerm(L_op[i][j],z_opw[i][j][0]);

    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++) {

    model.addGenConstrMax();

    }

    // Constraint 3: t_walk_o1 = m_o1 for all o

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

    expr = new GRBLinExpr();

    expr.addTerm(1,m_o1[i]);

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

    }

    0

Please sign in to leave a comment.