I tried the following without apparently working:
IloSolutionArray soluciones(env);
IloSolution sol(env);
- To store the solution in the array for a worker (once cp.next() found it)
sol.store(cp);
soluciones.add(sol);
- To restore the solution in cp and keep searching from that solution forward:
cp.restore(soluciones[Worker]);
cp.startNewSearch(); //maybe not correct to continue the search?
However, the search starts from the beginning. Any ide why?
------------------------------
javier rodrigo
------------------------------
Original Message:
Sent: Wed January 26, 2022 03:41 AM
From: ALEX FLEISCHER
Subject: Storing solution and Restoring
Hi,
what I wrote in OPL can also be done in C++
CPOptimizer knows the solution but also the tree and some context so this loop is better than getting a solution and then warm starting from that solution.
By the way sometimes hybrid approaches between MIP and CP can help.
Hybrid between CPO and CPLEX in OPL in How to with OPL
------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
Original Message:
Sent: Wed January 26, 2022 03:28 AM
From: javier rodrigo
Subject: Storing solution and Restoring
Thanks Alex. I guess my question is then, is it the same to use the store method to save the latest solution found for later on continue with the search than to save only the solution of final variables? I mean, when using the store method does cplex save ONLY these final variables or as well more information for continuing faster with the search like branches already visited, intermediate variables and so on.
Thanks.
Note: i am using C++ API
------------------------------
javier rodrigo
Original Message:
Sent: Wed January 26, 2022 03:10 AM
From: ALEX FLEISCHER
Subject: Storing solution and Restoring
Hi,
you can save solution one by one in an array.
Let me show that out of the example from Easy Optimization
Get several solutions with Constraint Programming
using CP;int nbKids=300;float costBus40=500;float costBus30=400;int nbsolmax=100;int nbBus40array[1..nbsolmax];int nbBus30array[1..nbsolmax]; dvar int+ nbBus40;dvar int+ nbBus30; minimize costBus40*nbBus40 +nbBus30*costBus30; subject to{ 40*nbBus40+nbBus30*30>=nbKids;}execute{ writeln("nbBus40 = ",nbBus40," and nbBus30 = ",nbBus30," and the cost is ",costBus40*nbBus40 +nbBus30*costBus30);}main{ nbsol=0;cp.param.SearchType=24;cp.param.workers=1;thisOplModel.generate();cp.startNewSearch();while(cp.next()) { thisOplModel.postProcess(); nbsol++; thisOplModel.nbBus40array[nbsol]=thisOplModel.nbBus40; thisOplModel.nbBus30array[nbsol]=thisOplModel.nbBus30;}for(var i=1;i<=nbsol;i++){writeln("solution ",i); writeln(thisOplModel.nbBus40array[i]);writeln(thisOplModel.nbBus30array[i]);}}
which gives
nbBus40 = 0 and nbBus30 = 10 and the cost is 4000nbBus40 = 3 and nbBus30 = 6 and the cost is 3900nbBus40 = 6 and nbBus30 = 2 and the cost is 3800solution 1010solution 236solution 362
------------------------------
[Alex] [Fleischer]
[EMEA CPLEX Optimization Technical Sales]
[IBM]
Original Message:
Sent: Tue January 25, 2022 03:26 PM
From: javier rodrigo
Subject: Storing solution and Restoring
I just realized cp.getStatus() is just returning an integer (1 Feasible, 2 Optimal, 2 Unfeasible)... I don´t know why I wasn't getting it before.
My questions for how to store and restore solutions in an solution arrays still stand anyway
Thanks for the tip Paul
------------------------------
javier rodrigo
Original Message:
Sent: Tue January 25, 2022 03:17 PM
From: javier rodrigo
Subject: Storing solution and Restoring
Thanks Paul, but the solution could be Optimal as well in which case I do not want to store it as I am not going to use later on. If I do env.out() << cp.getStatus() I get Optimal or Feasible print out but I don´t know I cannot compare it. Must be a problem of types I guess
------------------------------
javier rodrigo
Original Message:
Sent: Tue January 25, 2022 03:10 PM
From: Paul Rubin
Subject: Storing solution and Restoring
CPLEX has a "getStatus()" method, but I do not believe that CP Optimizer does. Any solution returned by CPO is feasible, so you might want to just store the most recently found solution inside the "while (cp.next())" loop. Once the loop exits, you either have a solution (in which case the model is known feasible) or you do not.
------------------------------
Paul Rubin
Professor Emeritus
Michigan State University
Original Message:
Sent: Tue January 25, 2022 02:27 PM
From: javier rodrigo
Subject: Storing solution and Restoring
Hello. I am using C++ API. I have defined a model that I am runnig within a loop modifying the objective (actually is a multiobjective and I am changing one of them). The pseudo code is the following where I have comment what I don´t know how to do it. Hope is more or less clear... thanks... again.
Worker=0;IloNumExprArray objArray(env);objArray.add(obj1); //defined specially for worker=0objArray.add(obj2); //obj are IloIntExprobjArray.add(obj3);IloObjective obj(env);obj = IloMinimize(env, IloStaticLex(env, objArray));model.add(obj);IloCP cp(model);while (!searchFinished) { cp.startNewSearch(); cp.setParameter(IloCP::TimeLimit, 20); while (cp.next()) { //print intermediate solutions until time out } if (cp.getStatus() == "Feasible") { //THIS LINE IS NOT WORKING //store the solution for later use in an solution array. DONT KNOW HOW TO DO IT } model.remove(obj); obj.end(); Worker++; obj1 = Expression for new worker objArray[0]=obj1; obj = IloMinimize(env, IloStaticLex(env, objArray)); model.add(obj); if (Worker is last one) { Worker = first with feasible (not optimal solution) //restore last solution found for Worker from solution array. DONT KNOW HOW TO DO IT } if (criteria to end loop) searchFinished=true;}
------------------------------
javier rodrigo
------------------------------
#DecisionOptimization