optimizationiterationcplexopl

How to update a matrix in iterative optimization using cplex opl?


I am solving an iterative optimization problem in cplex opl using the main function. However, I am getting errors while printing a matrix value. Please help me in solving this issue.

int Nod=41;  
range NodRange = 1..Nod;

int nr=140;  
range RRange = 1..nr;

int TSlot = 96;  
range TRange = 1..TSlot;

int ncs=6;
range csrange=1..ncs;

int piles=12;
range np=1..piles; 

int nv=3;
range nvrange=1..nv;

 main {
    var source = new IloOplModelSource("subvalue.mod");
    var cplex = new IloCplex;
    var def = new IloOplModelDefinition(source);
    
    var pa= new int [np][TRange];
    
    for(var v=1;v<=3;v++)
    {
      var opl = new IloOplModel(def, cplex);
      var data = new IloOplDataSource("MultipleEtoCS.dat");
      var data2=new IloOplDataElements();
      
      data2.cev=v;
      opl.addDataSource(data2);
      opl.addDataSource(data);
      opl.generate();
      
      if (!cplex.solve()) {
      writeln("No solution exists. Stopping.");
    }else{
        opl.postProcess();
        writeln(" solution is =" + cplex.getObjValue());
        for (var j in opl.csrange){
          if(j<=opl.sreq)
            opl.pa[opl.vpile[v]][opl.cslot+1+j]=+1;
         }              
               
  }
    
}
    
     opl.end();
}

How to print the pa matrix. I am getting the error as a scripting parser error. In the code sreq[nvrange],vpile[nvrange] and cslot[nvrange] are integer decision variables, defined in the subvalue.mod file. How can I implement this logic correctly to print the pa matrix for all values of v.


Solution

  • I get your error with

    int sreq[1..2]=[1,2];
    int j=1;
    execute
    {
      opl=thisOplModel;
      if (j<=opl.sreq) writeln("KO");
      
    }
    

    And that's normal since sreq is an array and j a value

    If I write

    int sreq[1..2]=[1,2];
    int j=1;
    execute
    {
      opl=thisOplModel;
      if (j<=opl.sreq[2]) writeln("OK");
      
    }
    

    Then it's ok and I see "OK"

    And to create new arrays in scripting let me share a small example I wrote

    {int} s={1,2};
    int a[1..2]=[3,4];
    range r=1..4;
    
    int ar[j in 1..4]=j;
    
    execute
    {
     
      // Scripting variables are local to the scripting block
      var ar=new Array(4);
      
      ar[1]=s;
      ar[2]=a;
      ar[3]=new Date();
      ar[4]=new Array(2);
      ar[4][1]="hello";
      ar[4][2]=2;
      writeln("ar = (in the first execute block)",ar);
      for(var i in r) writeln("ar[",i,"] = ",ar[i]);
      writeln("ar[4][1] = ",ar[4][1]);
      writeln("ar[4][2] = ",ar[4][2]);
    }
    
    execute
    {
      writeln("ar = (in the second execute block)",ar);
    }