1

I have sonthing like this:

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

I'd like to remove the extractable from the model modify this one and add again to the model the modified one. This code doesn't work... Last instruction throws an exception. What could I do? Thanks.

Roberto B
  • 43
  • 7

1 Answers1

1

It's better to use IloExp and IloConstraint for this purpose, plus don't forget to extract your new model after the change. For instance,

  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;
serge_k
  • 1,772
  • 2
  • 15
  • 21
  • Thanks, one more thing - why I have to use cplex.extract in this specific case? I have to extract also after having only removed a constr. or I have do to it only after having added somthing before solve again? Thanks a lot. – Roberto B Dec 07 '15 at 11:55
  • You need to extract the model before you call `cplex.solve()` even though one initializes cplex class with model class, e.g. `IloCplex cplex(model)` since `IloModel` and `IloCplex` are different classes. If you fail to do so the cplex will solve the initial model. – serge_k Dec 07 '15 at 13:37