Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Storing Cplex Solution Pool in a Tuple

  • 1.  Storing Cplex Solution Pool in a Tuple

    Posted 03/09/15 11:58 AM

    Originally posted by: mikeab


    Hey Guys,

     

    So I have a pool of solutions. I need to store all these solutions into a tuple with distinct index and then store this tuple into a database table. I have the following code in my Main block:

    var nsolns = cplex.solnPoolNsolns;
    writeln("Number of Solutions = ", nsolns);
     
    for(var s = 0 ; s < nsolns ; s++){
          thisOplModel.setPoolSolution(s);
          writeln(" --------- ");
          writeln("sol = ", s, "   objective= ", cplex.getObjValue(s));
          writeln("y= ", thisOplModel.y, " ");
          thisOplModel.postProcess();
     
    }

    Where thisOplModel.y is my decision variable.

    in the above for loop there is this "s" that I use as of a counter of the solution pool index. In my post processing execute block, I like to have this counter (aka "s") as of the index of my table. so I want the output to be something like:

     

    tuple solution{

          int ind_solution;

          int y;

    }

    {solution} Model_Solution;

    Model_Solution.add( s, y );

     

    How can I do this in my CPLEX code? the "s" counter in the main block is local variable and I can not access that in my post processing execute block.

     

    Thanks

    Mike

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Storing Cplex Solution Pool in a Tuple

    Posted 03/09/15 06:02 PM

    Hi

    let me give you an example out of the game of life:

    tuple solutionpooltype
    {
     key int rank;
     int obj;
    }

    {solutionpooltype} solutionpool={};



    int n=10;
    int Half=n div 2;
    range FirstHalf = 1..Half;
    range LastHalf = n-Half+1..n;
    range States = 0..1;
    range Bord = 0..(n+1);
    range Interior = 1..n;

    range obj = 0..(n*n);

    tuple neighbors {
       int row;
       int col;
    }

    {neighbors} Neighbor =
      {<(-1),(-1)>,<(-1),0>,<(-1),1>,<0,(-1)>,<0,1>,<1,(-1)>,<1,0>,<1,1>};

    dvar int Life[Bord][Bord] in States;
    dvar int x[0..(n+2)*(n+2)-1];


    dvar int Obj in obj;

    maximize Obj;

    subject to {

    forall(i,j in Bord) Life[i][j]==x[i*(n+2)+j];


      ct1:
        Obj == sum( i , j in Bord )
          Life[i][j];
         
      forall( i , j in Interior ) {
        ct21:
          2*Life[i][j] - sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 0;
        ct22:
          3*Life[i][j] + sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 6;
        forall( ordered n1 , n2 , n3 in Neighbor ) {
          ct23:
            -Life[i][j]+Life[i+n1.row][j+n1.col]
                       +Life[i+n2.row][j+n2.col]
                       +Life[i+n3.row][j+n3.col]
            -sum( nb in Neighbor : nb!=n1 && nb!=n2 && nb!=n3 )
              Life[i+nb.row][j+nb.col] <= 2;
        }
      }
      forall( j in Bord ) {
        ct31:
          Life[0][j] == 0;
        ct32:  
          Life[j][0] == 0;
        ct33:  
          Life[j][n+1] == 0;
        ct34:  
          Life[n+1][j] == 0;
      }
      forall( i in Bord : i<n ) {
        ct41:
          Life[i][1]+Life[i+1][1]+Life[i+2][1] <= 2;
        ct42:
          Life[1][i]+Life[1][i+1]+Life[1][i+2] <= 2;
        ct43:
          Life[i][n]+Life[i+1][n]+Life[i+2][n] <= 2;
        ct44:
          Life[n][i]+Life[n][i+1]+Life[n][i+2] <= 2;
      }
      ct5:
        sum( i in FirstHalf , j in Bord )
          Life[i][j] >=
        sum( i in LastHalf , j in Bord )
          Life[i][j];
      ct6:
        sum( i in Bord , j in FirstHalf )
          Life[i][j] >=
        sum( i in Bord , j in LastHalf )
          Life[i][j];   
    }

    main {
    cplex.tilim=10;

        thisOplModel.generate();
        cplex.solve();
        if (cplex.populate()) {
          var nsolns = cplex.solnPoolNsolns;
          writeln("Number of solutions found = ",nsolns);
          writeln();
          for (var s=0; s<nsolns; s++) {
            thisOplModel.setPoolSolution(s);
            writeln("solution #", s, ": objective = ",thisOplModel.Obj);
            
            thisOplModel.solutionpool.add(s,thisOplModel.Obj);
            
          }
        }
        
        writeln("tuple set for the solution pool");
        writeln(thisOplModel.solutionpool);
    }
     

    and we get

     

    tuple set for the solution pool
     {<0 54> <1 53> <2 0> <3 53> <4 53> <5 52> <6 52> <7 54> <8 53>
         <9 54> <10 54> <11 54> <12 54> <13 54> <14 54> <15 53>
         <16 54> <17 52> <18 53> <19 54> <20 54> <21 52>}

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Storing Cplex Solution Pool in a Tuple

    Posted 03/09/15 07:32 PM

    Originally posted by: mikeab


    Hi Alex,

     

    Thanks for the reply. Well the above code works if and only if we write in the main block. My problem is not in the main block. Lets say I wanna write your codes solution into a variable that I wanna read it in a execute block. I wrote the blow code:

     

    main{
     
    thisOplModel.generate();
    cplex.populate();
    cplex.solnpoolcapacity = 5;
    cplex.PopulateLim = 4;
    writeln("Pool Capacity = ", cplex.solnpoolcapacity );
    writeln("Population Limit = ", cplex.populatelim );
     
    cplex.solve();
     
    var nsolns = cplex.solnPoolNsolns;
    writeln("Number of Solutions = ", nsolns);
    var z = new Array();
     
    for(var s = 0 ; s < nsolns ; s++){
    thisOplModel.setPoolSolution(s);
    writeln(" --------- ");
    writeln("sol = ", s, "   objective= ", cplex.getObjValue(s));
    writeln("y= ", thisOplModel.y, " ");
    z[s] = thisOplModel.y;
    writeln("z[s] = ", z[s], "   s = ", s);
    thisOplModel.Sol.add(s, cplex.getObjValue(s));
    writeln("SSSSOOOOOOl= ", thisOplModel.Sol);
    thisOplModel.postProcess();
     
    }

     

     
    execute{
    writeln("sssool = ", Sol);
    }
     

     

    Unfortunately the tuple set Sol is empty when I read run the execute block. Will you advise ?

     

    Thanks

    Mike


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Storing Cplex Solution Pool in a Tuple

    Posted 03/10/15 01:54 AM

    Hi

    I do nor reproduce your behavior.

     

    As can be seen below, if I add

    execute
    {
    writeln("post process ",solutionpool);

    }

    in the postprocess I get

     

    post process  {<0 54> <1 53> <2 0> <3 53> <4 53> <5 52> <6 52> <7 54> <8 53>
         <9 54> <10 54> <11 54> <12 54> <13 54> <14 54> <15 53>
         <16 54> <17 52> <18 53> <19 54> <20 54> <21 52>}

     

    The full model is then

     

    tuple solutionpooltype
    {
     key int rank;
     int obj;
    }

    {solutionpooltype} solutionpool={};



    int n=10;
    int Half=n div 2;
    range FirstHalf = 1..Half;
    range LastHalf = n-Half+1..n;
    range States = 0..1;
    range Bord = 0..(n+1);
    range Interior = 1..n;

    range obj = 0..(n*n);

    tuple neighbors {
       int row;
       int col;
    }

    {neighbors} Neighbor =
      {<(-1),(-1)>,<(-1),0>,<(-1),1>,<0,(-1)>,<0,1>,<1,(-1)>,<1,0>,<1,1>};

    dvar int Life[Bord][Bord] in States;
    dvar int x[0..(n+2)*(n+2)-1];


    dvar int Obj in obj;

    maximize Obj;

    subject to {

    forall(i,j in Bord) Life[i][j]==x[i*(n+2)+j];


      ct1:
        Obj == sum( i , j in Bord )
          Life[i][j];
         
      forall( i , j in Interior ) {
        ct21:
          2*Life[i][j] - sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 0;
        ct22:
          3*Life[i][j] + sum( nb in Neighbor )
            Life[i+nb.row][j+nb.col] <= 6;
        forall( ordered n1 , n2 , n3 in Neighbor ) {
          ct23:
            -Life[i][j]+Life[i+n1.row][j+n1.col]
                       +Life[i+n2.row][j+n2.col]
                       +Life[i+n3.row][j+n3.col]
            -sum( nb in Neighbor : nb!=n1 && nb!=n2 && nb!=n3 )
              Life[i+nb.row][j+nb.col] <= 2;
        }
      }
      forall( j in Bord ) {
        ct31:
          Life[0][j] == 0;
        ct32:  
          Life[j][0] == 0;
        ct33:  
          Life[j][n+1] == 0;
        ct34:  
          Life[n+1][j] == 0;
      }
      forall( i in Bord : i<n ) {
        ct41:
          Life[i][1]+Life[i+1][1]+Life[i+2][1] <= 2;
        ct42:
          Life[1][i]+Life[1][i+1]+Life[1][i+2] <= 2;
        ct43:
          Life[i][n]+Life[i+1][n]+Life[i+2][n] <= 2;
        ct44:
          Life[n][i]+Life[n][i+1]+Life[n][i+2] <= 2;
      }
      ct5:
        sum( i in FirstHalf , j in Bord )
          Life[i][j] >=
        sum( i in LastHalf , j in Bord )
          Life[i][j];
      ct6:
        sum( i in Bord , j in FirstHalf )
          Life[i][j] >=
        sum( i in Bord , j in LastHalf )
          Life[i][j];   
    }

    main {
    cplex.tilim=10;

        thisOplModel.generate();
        cplex.solve();
        if (cplex.populate()) {
          var nsolns = cplex.solnPoolNsolns;
          writeln("Number of solutions found = ",nsolns);
          writeln();
          for (var s=0; s<nsolns; s++) {
            thisOplModel.setPoolSolution(s);
            writeln("solution #", s, ": objective = ",thisOplModel.Obj);
            
            thisOplModel.solutionpool.add(s,thisOplModel.Obj);
            
          }
        }
        
        writeln("tuple set for the solution pool");
        writeln(thisOplModel.solutionpool);
        thisOplModel.postProcess();
    }

    execute
    {
    writeln("post process ",solutionpool);

    }
     

    regards

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Storing Cplex Solution Pool in a Tuple

    Posted 03/10/15 09:20 AM

    Originally posted by: mikeab


    Alex,

    This is the Log File output:

    Pool Capacity = 5
    Population Limit = 4
    Number of Solutions = 4
     --------- 
    sol = 0   objective= 3
    y=  [0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0] 
     --------- 
    sol = 1   objective= 4
    y=  [0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1] 
     --------- 
    sol = 2   objective= 3
    y=  [0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1] 
     --------- 
    sol = 3   objective= 5
    y=  [1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0] 
    Tuple Set for the Solution Pool =  {<3 5>}
    Post Process Result {}
     

     

    and this is my main block code:
     

     

    main{
     
    thisOplModel.generate();
    cplex.solnpoolcapacity = 5;
    cplex.PopulateLim = 4;
    writeln("Pool Capacity = ", cplex.solnpoolcapacity );
    writeln("Population Limit = ", cplex.populatelim );
    cplex.solve();
     
    if(cplex.populate()){
     
    var nsolns = cplex.solnPoolNsolns;
    writeln("Number of Solutions = ", nsolns);
     
     
    for(var s = 0 ; s < nsolns ; s++){
    thisOplModel.setPoolSolution(s);
    writeln(" --------- ");
    writeln("sol = ", s, "   objective= ", cplex.getObjValue(s));
    writeln("y= ", thisOplModel.y, " ");
    thisOplModel.Sol.add(s, cplex.getObjValue(s));
     
    }
     
     
    }
     
    writeln("Tuple Set for the Solution Pool = ", thisOplModel.Sol);
    thisOplModel.postProcess();
     
    }

     

    as you can see up there the Tuple Set for the Solution Pool =  {<3 5>} has only one element which is the last solution (sol #3 int solution pool with objective function value of 5 {<3 5>} but when I run the execute block right after the main block the results will be : Post Process Result {} which means the tuple set Sol is empty.

    Why is that happening ?

    Thanks for your support.

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer