mathematical-optimizationcplexopldocplex

Dynamic Tuple Data Addition Within a Loop in the Main Block using OPL CPLEX


"How can I add a row of tuple data (Links) in each iteration within a loop in the main block in OPL CPLEX?" In this code, I want to add one row from the tuple Links to the model in each iteration, and I want the value of the variable x to remain constant for the next iteration. However, I'm having trouble adding a row from Links to the model in each iteration! The number of rows in the tuple "Links" is equal to 7. I appreciate your help. Thank you.

{int}s={};
range M = 1..5;
tuple link {
    key int N;
    int     fromnode;
    int     tonode;
    int     EP;
    int     LD;
    float   I;
    int     dr;
};
{link} Links = ...;
main{
   var source = new IloOplModelSource("subset.mod");
   var cplex = new IloCplex();
   var def = new IloOplModelDefinition(source);
   
   var output = new Array(3);
   for (var i=1; i<=5; i++) {
     output[i] = new Array(3);
     for(var j=1; j<=7; j++) {
       output[i][j] = 0.0;
     }
   }
       for(var iter=1;iter<=7;iter++)
       {
      var opl = new IloOplModel(def,cplex);
      var data= new IloOplDataElements();
    data.M=thisOplModel.M;
    data.Links=thisOplModel.s;
    data.Links.add(iter);
    opl.addDataSource(data);
    opl.generate();
   if (iter!=1){
     for (var r in data.N){ 
            for (var k in data.M){
            if (output[k][r]==1){
       opl.x[k][r].LB=output[k][r];
       opl.x[k][r].UB=output[k][r];
     writeln("xds[",k,"]","[",r,"]"," = ",output[k][r]);
     }}}
   }
          
    if (cplex.solve()) {
        writeln('\n=================================');
        writeln("ITERATION ", iter, " / TIME = ", cplex.getCplexTime());
        writeln("N=",data.N);
        
        writeln('\n****OBJ************');
        writeln("OBJ = " + cplex.getObjValue());
      } else {
        writeln("No solution");
      }
      opl.postProcess(); 
   for (var k in data.M){
      for (var r in data.N){
       output[k][r]=opl.x[k][r].solutionValue;
     }
   }     
  data.end();    
  opl.end();   
    }
}```

Solution

  • See example https://github.com/AlexFleischerParis/howtowithoplchange/blob/master/changetupleset.mod

    main model

    tuple t
    {
    int e1;
    int e2;    
    }
    
    {t} s={<1,1>};
    
    main {
      var source = new IloOplModelSource("subtupleset.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
    
    
      for(var k=1;k<=5;k++)
      {
      var opl = new IloOplModel(def,cplex);
    
      var data2= new IloOplDataElements();
    
    data2.y=thisOplModel.s;
    Opl.item(thisOplModel.s,0).e2=Opl.item(thisOplModel.s,0).e2+1;
    data2.y.add(k,k+1);
    
      opl.addDataSource(data2);
      opl.generate();
    
      if (cplex.solve()) {
         writeln("OBJ = " + cplex.getObjValue());
      } else {
         writeln("No solution");
      }
    data2.end();
     opl.end();
    
    
    }  
    
    }
    

    subupleset.mod

    tuple t
    {
     int e1;
     int e2;    
    }
    
    {t} y=...;
    
    execute
    {
      writeln("y=",y);
    }
    
    dvar float x;
    
    maximize x;
    subject to {
      x<=sum(i in y)i.e2;
    }
    
    execute
    {
      writeln("x=",x);
    }