问题
I have one constraint set
for(int t = 0; t < NbPeriods; t++){
for (int j =0; j < NbLocations; j++){
IloExpr Ct1(env);
for(int u = 0; u < t; u++){
Ct1 += Fortified[u][j];
}
model.add(Interdicted[t][j] <= 1 - Ct1);
}
}
after some modification I have to remove this constraint set from the model. model.remove()
is not working. How can I do it using IloRangeArray protection(env)
in this case.
回答1:
You need to define you constraints through IloConstraint
before adding to the model and save in a container (e.g.,IloConstraintArray
). Cplex removes a constraint from the model by its name not the expression. In your case,
IloConstraintArray cons_array(env);
for(int t = 0; t < NbPeriods; t++){
for (int j =0; j < NbLocations; j++){
IloExpr Ct1(env);
for(int u = 0; u < t; u++){
Ct1 += Fortified[u][j];
}
IloConstraint cons = Interdicted[t][j] <= 1 - Ct1;
model.add(cons);
cons_array.add(cons);
}
}
to remove
for (int i = 0; i < NbPeriods*NbLocations; i++)
model.remove( cons_array[i] );
You can also use cplex.exportModel("model.lp")
to export you model to a file after adding and after removing constraints and check if constrains are removed
来源:https://stackoverflow.com/questions/35307719/removing-constraint-from-a-model-in-cplex-c-concert-tech