Decision Optimization

 View Only
Expand all | Collapse all

Storing solution and Restoring

  • 1.  Storing solution and Restoring

    Posted Tue January 25, 2022 02:27 PM
    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=0
    objArray.add(obj2); //obj are IloIntExpr
    objArray.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
    ------------------------------


  • 2.  RE: Storing solution and Restoring

    Posted Tue January 25, 2022 03:10 PM
    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
    ------------------------------



  • 3.  RE: Storing solution and Restoring

    Posted Tue January 25, 2022 03:17 PM
    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
    ------------------------------



  • 4.  RE: Storing solution and Restoring

    Posted Tue January 25, 2022 03:26 PM
    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
    ------------------------------



  • 5.  RE: Storing solution and Restoring

    Posted Wed January 26, 2022 03:11 AM
    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 4000
    nbBus40 = 3 and nbBus30 = 6 and the cost is 3900
    nbBus40 = 6 and nbBus30 = 2 and the cost is 3800
    solution 1
    0
    10
    solution 2
    3
    6
    solution 3
    6
    2
    


    ------------------------------
    [Alex] [Fleischer]
    [EMEA CPLEX Optimization Technical Sales]
    [IBM]
    ------------------------------



  • 6.  RE: Storing solution and Restoring

    Posted Wed January 26, 2022 03:28 AM
    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
    ------------------------------



  • 7.  RE: Storing solution and Restoring

    Posted Wed January 26, 2022 03:42 AM
    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]
    ------------------------------



  • 8.  RE: Storing solution and Restoring

    Posted Wed January 26, 2022 01:30 PM
    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
    ------------------------------



  • 9.  RE: Storing solution and Restoring

    Posted Thu January 27, 2022 04:10 AM
    Dear Javier,

    Did you consider using the setStartingPoint method as described in https://www.ibm.com/docs/en/icos/20.1.0?topic=methods-setstartingpoint-method
    I hope this helps,
    Cheers,

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 10.  RE: Storing solution and Restoring

    Posted Thu January 27, 2022 11:26 AM
    Thanks, yes I know that solution but I was hoping that the store and restore methods could go one step further and save variables as well as other useful information for the algorith to continue with the search once restored.

    However, I am not able to make them work...

    ------------------------------
    javier rodrigo
    ------------------------------



  • 11.  RE: Storing solution and Restoring

    Posted Thu January 27, 2022 11:44 AM
    Dear Javier,

    Indeed, the startNewSearch() function without starting point starts a new search completely from scratch.
    The setStartingPoint(solution), IMHO, should do exactly what you wish: allow startNewSearch() to use the given solution as a starting point and search for improving solutions. Please let us know if this way of doing things is a problem for  your application. 
    Cheers,

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 12.  RE: Storing solution and Restoring

    Posted Thu January 27, 2022 12:13 PM
    with the setStartingPoint works but what are the store and restore methods for?

    ------------------------------
    javier rodrigo
    ------------------------------



  • 13.  RE: Storing solution and Restoring

    Posted Thu January 27, 2022 02:43 PM
    Dear Javier,

    The store method allows you to export assignments found by the solver (thus later, you can pass them to setStartingPont).
    The restore  method allows you to import assignments and then see if they honor the model constraints (restore will return true). It also allows you to introspect the solver state using solver.getMin(x) or solver.getMax(x) to see how the restored assignment impacts the domain of other variables (e.g x not in the restored assignment).
    I hope it helps.

    Cheers.

    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 14.  RE: Storing solution and Restoring

    Posted Thu January 27, 2022 03:33 PM