0

I have an optimization model that I want to implement in IBM CPLEX Optimization Studio 12.10. I wrote the model code in OPL and the first implementation is working. What I would like to do now is to iterate the model multiple times to see how the resolution time changes depending on the dimension of the parameters.

In the .mod file I have defined three sets:

  • int numSet1=...;
  • int numSet2=...;
  • int numSet3=...;
  • range Set1 = 1..numSet1;
  • range Set2 = 1..numSet2;
  • range Set3 = 1..numSet3;

And four parameters:

  • float Par1[Set1]=...;
  • float Par2[Set1][Set2]=...;
  • float Par3[Set1]=...;
  • float Par4[Set1][Set2][Set3]=...;

In the .dat file, I have defined the initial values for these sets and parameters.

What I would like to do now is to define, in the flow control, a code that allows me to change the dimensions fo the sets, and thus, of the parameters, and save the resolution time for each resolution:

main {
    var mod = thisOplModel.modelDefinition; 
    var dat = thisOplModel.dataElements; 
    for (var sizenumSet1 = 2; sizenumSet1 <= 10; sizenumSet1 += 2) {
    for (var sizenumSet2 = 1; sizenumSet2 <= 5; sizenumSet2 +=1) {
      for (var sizenumSet3 = 1; sizenumSet3 <=5; sizenumSet3 +=1) {
        var MyCplex = new IloCplex(); 
        var opl = new IloOplModel(mod, MyCplex); 
        dat.changenumSet1=sizenumSet1; 
        dat.changenumSet2=sizenumSet2; 
        dat.changenumSet3=sizenumSet3; 
        opl.addDataSource(dat); 
        opl.generate(); 
        if (MyCplex.solve()) {
         writeln("Solution: ", MyCplex.getObjValue(),
        " / sizeSet1: ", sizenumSet1,
        " / sizeSet2: ", sizenumSet2,
        " / sizeSet3: ", sizenumSet3,
        " / time: ", MyCplex.getCplexTime());
        }    
        opl.end(); 
        MyCplex.end(); 
        }
    }
    }         
}

When I launch this code what I obtain is the following list of errors:

  • Execution of main failed. Processing OPL model failed
  • Index out of bound for array Par4(1)(1):3
  • Scripting runtime error: (in generate) Processing OPL model failed

How can I solve this? Thank you for your help.

sroverto
  • 57
  • 7

1 Answers1

0

In

dat.changenumSet1=sizenumSet1;
dat.changenumSet2=sizenumSet2;
dat.changenumSet3=sizenumSet3;

you are changing the wrong elements. You should be changing

dat.numSet1=sizenumSet1;
dat.numSet2=sizenumSet2;
dat.numSet3=sizenumSet3;

Moreover, it seems you are missing updates to the Par arrays. These arrays become larger in each iteration, so need to provide more data for them.

Daniel Junglas
  • 5,830
  • 1
  • 5
  • 22
  • Hi! Thank you for your answer. How can I update the arrays and the values in them? I tried with a for cycle such as for (x in numSet1){ dat.Par1[x] = 1 + Opl.rand(10); writeln(Par1); } But now the error is "Scripting runtime error: Index out of bound for array "Par1", "toString". If I write "Par1= 1 + Opl.rand(10);" instead of "dat.Par1[x] = 1 + Opl.rand(10);" the error is "Scripting runtime error: unknow variable "Par1"". – sroverto Feb 07 '20 at 15:16