Betty
Betty

Reputation: 3

Removing constraint from a model in gurobi c++

I have one constraint set

after some modification I have to remove this constraint:model.addConstr(LHS10_2 <= LHS10_1)set from the model. model.remove() is not working. How can I do it? Thank for your help.

model.remove(LHS10_2 <= LHS10_1) can not work.

for (i = 1; i <= ULD; i++)
    {
        for (j = 1; j <= station; j++)
        {
            GRBLinExpr LHS10_1 = 0;//自載飛航節線
            GRBLinExpr LHS10_2 = 0;//他航載飛航節線
            for (k = 2; k <= load; k++)
            {
                if (k == 2 || k == 3 || k == 7)
                {
                    for (l = 1; l <= (node - 2); l++)
                    {
                        for (m = 1; m <= Num_FAn[j][k][l]; m++)
                        {
                            LHS10_1 += p*X[i][j][k][l][FSAn[j][k][l][m]][FLAn[j][k][l][m]][FNAn[j][k][l][m]];
                        }
                    }
                }
                if (k == 4 || k == 5)
                {
                    for (l = 1; l <= (node - 2); l++)
                    {
                        for (m = 1; m <= Num_FAn[j][k][l]; m++)
                        {
                            LHS10_2 += X[i][j][k][l][FSAn[j][k][l][m]][FLAn[j][k][l][m]][FNAn[j][k][l][m]];
                        }
                    }
                }
            }
            model.addConstr(LHS10_2 <= LHS10_1);

        }
    }

Upvotes: 0

Views: 1067

Answers (1)

Robert
Robert

Reputation: 595

The method GRBModel::addConstr() returns an GRBConstr object that you should save in a local variable. Then at a later point in time, you can use the GRBModel::remove() method to delete that particular constraint from the model again, i.e., you could do something like

// Array to hold added constraint objects
GRBConstr* c = new GRBConstr[nConstr];
for (int k = 0; k < nConstr, ++k) {
    // Create expressions LHS10_2 and LHS10_1 as needed
    // [...]
    // Add k-th constraint, grap object for later removal from model
    c[k] = model.addConstr(LHS10_2 <= LHS10_1);
}
// Do some stuff, optimize, etc.
// [...]
// now delete unwanted constraints from model
for (int k = 0; k < nConstr, ++k) {
    model.remove(c[k]);
}

Upvotes: 1

Related Questions