我有这样的声音:
IloExtractable extractable(env);
...
extractable = model.add( x + y <= 4);
...
model.remove(extractable);
IloExpr soft_expr(extractable.asConstraint());
IloNumVar v = IloNumVar(env, 0.0, +IloInfinity,ILOFLOAT);
soft_expr += v;
model.add(soft_expr);
我想从模型中删除可提取物修改此项并将模板再次添加到修改过的模型中。 此代码不起作用...最后一条指令抛出异常。 我能做什么? 感谢。
答案 0 :(得分:1)
为此目的最好使用IloExp
和IloConstraint
,另外不要忘记在更改后提取新模型。例如,
IloExpr con = x[0] + x[1];
IloConstraint cons = con == 3 ;
model.add( cons );
cplex.solve();
IloNumArray vals(env);
cplex.getValues(vals, x);
cplex.exportModel("./model1.lp"); // to check out
cplex.out() << "Solution status " << cplex.getStatus() << endl;
cplex.out() << "Objective value " << cplex.getObjValue() << endl;
cplex.out() << "Solution is: " << vals << endl;
//--------------------------//
model.remove( cons );
IloNumVar v = IloNumVar(env, 0.0, +IloInfinity,ILOFLOAT);
cons = con + v == 3; // your new constraint
model.add( cons );
cplex.extract(model);
cplex.solve();
cplex.getValues(vals, x);
cplex.exportModel("./model2.lp");
cplex.out() << "Solution status " << cplex.getStatus() << endl;
cplex.out() << "Objective value " << cplex.getObjValue() << endl;
cplex.out() << "Solution is: " << vals << endl;