1

On the ibm website http://www-01.ibm.com/support/docview.wss?uid=swg21573358 they defined and initialized a 2- dimensional array in the Main Opl script as below

main {

    var var_2d = new Array(3);
       for (var i=0; i<3; i++) {
         var_2d[i] = new Array(3);
         for(var j=0; j<3; j++) {
           var_2d[i][j] = 1;
         }
       }

}

I am trying to define and initialize a 3 dimensional array but the array I defined and initialized gives me " undefined as the result" my code is below:

a =2
 m=3;
 n= 2;

 Range A= 1..2;
 Range M = 1..3;
 Range N = 1..2;

// declare 3 dimensional array in the Main script

main{
var var_var_temp2 = new Array (thisOplModel.a+thisOplModel.m+thisOplModel.n) ; 

// initialize the  3 dimensional array
     for( var r in thisOplModel.A){
         var_var_temp2[r] = new Array(thisOplModel.m);
            for( var i in thisOplModel.M) {
                 var_var_temp2[r][i] = new Array(thisOplModel.n);
                    for ( var j in thisOplModel.N) {
                     var_var_temp2[r][i][j]= 1.0;    
                        }
              }  
          }  
// code to print the value stored in temp2[r][i][j]

for (r in thisOplModel.A) {
                 for (i in thisOplModel.M){ 
                      for (i in thisOplModel.N) {  
                                writeln( var_var_temp2[r][i][j]);                                           
              }                 
                 }       
                      } 

}

The value I got was a

undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
Cœur
  • 37,241
  • 25
  • 195
  • 267
johnO
  • 11
  • 3

1 Answers1

0

As is, your example has syntax errors in CPLEX Optimization Studio 12.8.

If I change the top part to the following:

int a = 2;
int m = 3;
int n = 2;

range A = 1..2;
range M = 1..3;
range N = 1..2;

That is, include types and replace "Range" with "range", then it seems to run fine. The output is as we would expect:

1
1
1
1
1
1
1
1
1
1
1
1
rkersh
  • 4,447
  • 2
  • 22
  • 31
  • thanks rkersh i made a typo error in the question. my "range" was written as range in the program and still the result was underfined underfined etc – johnO Aug 08 '18 at 11:08
  • I made a typo error in my question. My code was actually written with range and not Range. The results still gives me underfined – johnO Aug 08 '18 at 11:18
  • What version of CPLEX are you using? How are you running the example? With 12.8, using the IDE, your example does not even run due to the syntax errors. – rkersh Aug 08 '18 at 14:25
  • i use cplex 12.8. could you help me with correct syntax for declaring and initializing a 3 dimensional array on the IDE? – johnO Aug 08 '18 at 18:37
  • Did you also fix the declaration of `a`, `m`, and `n` as I suggested? After the changes above, the example is working for me, so I'm not sure what is going on with your setup. Can you successfully run other OPL examples? What operating system are you using? – rkersh Aug 08 '18 at 18:41