I tried to initialise like this but it's wrong

GRBVar w = null;
    GRBLinExpr obj = new GRBLinExpr();
    obj.addTerm(1, w);
    model.setObjective(obj);
    model.set(GRB.IntAttr.ModelSense, GRB.MINIMIZE);

These are the two constraints

w >= xir pir - xik pik

    GRBLinExpr expr = new GRBLinExpr();
    for(int i=0; i<M; i++) {
        for(int r=0; r<4; r++) {
            expr.addTerm(pij[i][r], xij[i][r]);
        }
        for(int n=4; n<K; n++) {
            expr.addTerm(-pij[i][n], xij[i][n]);
        }
    }
    model.addConstr(w, (char) GRB.MINIMIZE, expr, "vincolo ");
    

w >= xik pik - xir pir

    expr = new GRBLinExpr();
    for(int i=0; i<M; i++) {
        for(int n=4; n<K; n++) {
            expr.addTerm(pij[i][n], xij[i][n]);
        }
        for(int r=0; r<4; r++) {
            expr.addTerm(-pij[i][r], xij[i][r]);
        }
    }
    model.addConstr(w, (char) GRB.MINIMIZE, expr, "vincolo ");
1

There are 1 answers

4
Greg Glockner On

I don't have a full program to test, but I suspect the issue is the second parameter in these two methods:

model.addConstr(w, (char) GRB.MINIMIZE, expr, "vincolo ");
// ...
model.addConstr(w, (char) GRB.MINIMIZE, expr, "vincolo ");

They should be:

model.addConstr(w, GRB.GREATER_EQUAL, expr);
// ...
model.addConstr(w, GRB.GREATER_EQUAL, expr);

Also, it's better not to label constraints alike; if you want to label them, then use a unique identifier for each instance.