在Gurobi C ++中从模型中删除约束

时间:2019-04-22 12:48:10

标签: c++ gurobi

我设置了一个约束

经过一些修改后,我必须从模型中删除此约束:model.addConstr(LHS10_2 <= LHS10_1)set。 model.remove()无法正常工作。我该怎么做? 感谢您的帮助。

model.remove(LHS10_2 <= LHS10_1)无法工作。

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);

        }
    }

1 个答案:

答案 0 :(得分:0)

方法GRBModel::addConstr()返回一个GRBConstr对象,您应该将其保存在局部变量中。然后,在以后的某个时间点,您可以使用GRBModel::remove()方法再次从模型中删除该特定约束,即可以执行类似的操作

// 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]);
}