Decision Optimization

 View Only
  • 1.  CPLEX C++ make a deep copy of an IloModel

    Posted Tue June 28, 2022 04:36 PM
    Hello, I have a problem with the following code:


    I think, that I create a deep copy of model in model1. model has the expected solution (X = 5, Y = 0) and objective 10. Then I create the deep copy of model and store it in model1. model1 has the expected solution (X=4, Y=1) and objective 9. 
    After the my problem begins: again I want to solve the model instance and I get the solution (X=5, Y=0), which is correct but now the objective is 9?
    why does this occur?
    thank you in advance!



        IloEnv env;

        IloModel model(env);

        IloNumVar X(env, 0, IloInfinity, ILOFLOAT);

        IloNumVar Y(env, 0, IloInfinity, ILOFLOAT);

        model.add(X + Y <= 5);

        model.add(IloMaximize(env, 2*X + Y));

        IloCplex mycplex(env);

        mycplex.extract(model);

        IloBool feasible = mycplex.solve();

        cout << mycplex.getValue(X) << "\n";

        if(feasible == IloTrue){

            cout << mycplex.getObjValue() << "\n";

        }

        IloModel model1(env);

        for (IloModel::Iterator it(model); it.ok(); ++it)

            model1.add(*it);

        model1.add(X <= 4);

        IloCplex mycplex1(env);

        mycplex1.extract(model1);

        IloBool feasible1 = mycplex1.solve();

        cout << mycplex1.getValue(X) << "\n";

        if(feasible1 == IloTrue){

            cout << mycplex1.getObjValue() << "\n";

        }

        IloCplex mycplex2(env);

        mycplex2.extract(model);

        IloBool feasible2 = mycplex2.solve();

        cout << mycplex2.getValue(X) << "\n";

        if(feasible2 == IloTrue){

            cout << mycplex1.getObjValue() << "\n";

        }




    ------------------------------
    Marco Reclik
    ------------------------------

    #DecisionOptimization


  • 2.  RE: CPLEX C++ make a deep copy of an IloModel

    IBM Champion
    Posted Tue June 28, 2022 07:15 PM
    Your very last output line calls getObjValue() on mycplex1, not on mycplex2.

    ------------------------------
    Paul Rubin
    Professor Emeritus
    Michigan State University
    ------------------------------