Search code examples
cplexopl

keeping solution value for next iteration in opl cplex


Thank you for your answer, Alex. Your answer works correctly; But it freezes all the variables, I want it to fix only the variables that are equal to 1 for the next step. For this purpose, I have written the following code:

main {
   var source = new IloOplModelSource("subset.mod");
   var cplex = new IloCplex();
   var def = new IloOplModelDefinition(source);
   var output=0;
      for(var iter=1;iter<=5;iter++)
      {
      var opl = new IloOplModel(def,cplex);  
      var data2= new IloOplDataElements();
    data2.M=thisOplModel.s;
    data2.M.add(iter);
    data2.N=thisOplModel.N;
    data2.Links=thisOplModel.Links;   
      opl.addDataSource(data2);
      opl.generate();
      
 for (var k in data2.M){
   for (var r in data2.Links){
     if (opl.x[k][r.N]==1){ // This line is used to select variables equal to 1 
       opl.x[k][r.N].LB=output;
       opl.x[k][r.N].UB=output;
     }
   }
 }
      if (cplex.solve()) {
        writeln("ITERATION ", iter);
        writeln('\n****OBJ************');
        writeln("OBJ = " + cplex.getObjValue());
      } else {
         writeln("No solution");
      }
      opl.postProcess();            
      output=opl.x[k][r.N].solutionValue;
      writeln("x[k][1]=",opl.x[k][r.N].solutionValue);
     data2.end();
     opl.end();
    }      
    }

But this does not work, Thank you for your time.


Solution

  • My model does not freeze all variables.

    If I change sub2d.mod to

    int y[1..2][1..2]=...;
    
    execute
    {
    writeln("y=",y);
    }
    
    dvar boolean x[1..2][1..2];
    
    maximize sum(i in 1..2,j in 1..2)x[i][j];
    subject to {
      sum(i in 1..2,j in 1..2)  x[i][j]<=(sum(i in 1..2, j  in 1..2) y[i][j]) ;
      x[2][1]==(sum(i in 1..2, j  in 1..2) y[i][j]) mod 2;
    }      
    

    then the model

    int a[1..2][1..2];
    
        main {
          var source = new IloOplModelSource("sub2d.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         var output=0;
         
          for(var k=11;k<=15;k++)
          {
          var opl = new IloOplModel(def,cplex);
         
         
            
          var data2= new IloOplDataElements();
         
          data2.y=thisOplModel.a;
          data2.y[1][1]=k;
          opl.addDataSource(data2);
         
          opl.generate();
          // if k!=11 then freeze x to the output value from last time
          if (k!=11)
          {opl.x[1][1].LB=output;
          opl.x[1][1].UB=output;
          }      
    
          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
          opl.postProcess();
          output=opl.x[1][1].solutionValue;
          
          writeln("x[1][1]=",opl.x[1][1].solutionValue);
          writeln("x[2][1]=",opl.x[2][1].solutionValue);
          
          data2.end();
         opl.end();
         
         
        }  
         
        }
    

    gives

    y= [[11 0]
             [0 0]]
    OBJ = 4
    x[1][1]=1
    x[2][1]=1
    y= [[12 0]
             [0 0]]
    OBJ = 3
    x[1][1]=1
    x[2][1]=0
    y= [[13 0]
             [0 0]]
    OBJ = 4
    x[1][1]=1
    x[2][1]=1
    y= [[14 0]
             [0 0]]
    OBJ = 3
    x[1][1]=1
    x[2][1]=0
    y= [[15 0]
             [0 0]]
    OBJ = 4
    x[1][1]=1
    x[2][1]=1