2

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.

2
  • Hi , what errors do you get ? Commented Mar 15 at 16:10
  • Hi. I am getting two errors ,Scripting runtime error: cannot convert to a number, " [2 0 0]" for the line : if(j<=opl.sreq) and Scripting parser error: missing expression for the line var pa= new int [np][TimeRange]; Commented Mar 15 at 17:59

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Additionally, I am trying to retrieve the values of the pa matrix across all iterations. The pa[np][Tslot] matrix is defined in the subvalue.mod file. However, with the current code, I am getting the pa values separately for each iteration. For example, when v = 1 is executed, the corresponding pa values are generated, but they get overwritten when v = 2 is executed. I want to retain the pa values for all iterations in a single matrix. Please help me in getting this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.