Removing constraint from a model in cplex c++ concert tech

僤鯓⒐⒋嵵緔 提交于 2021-01-26 03:01:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!