javacplexopl

How can I load correctly a 2 by 2 matrix on OPL ILOG studio without an error?


Im loading a 2 by two matrix from a data file in OPL studio and am using the following script

P=5;
Customers={"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50"};
Warehouses={"A","B","C","D","E"};
Demand=[3,14,1,14,19,2,14,6,7,6,10,18,3,6,20,4,14,11,19,15,15,4,13,13,5,16,3,7,14,17,3,3,12,14,20,13,10,9,6,18,7,20,9,1,8,5,1,7,9,2];
prepare { 
   function read(element, name) {
      var customDataSource = 
IloOplCallJava("externaldatasource.SimpleTextReader","<init>", "(Ljava.lang.String;Ljava.lang.String;)V","C:/Users/Brian/opl/ccp/data.txt", ",");
      customDataSource.fillOplElement(element);
      return true;
   }   
}

Distance = invoke read;

Im getting a "Data parsing error: syntax error, unexpected prepare, expecting $end" I want to load from a file data with the following format and read it from the file into OPL studio

0    86    42    67    54    76    78   107   100    57
42    93    44    21    25    35    56    16    60   101
10    54    58    72    82    60    58    72    43    67
70    15    36    61   102    14    69    64    23    83
19    52    94    10    62    50    69    53    52     4


86     0    76    23    47    18    60    23    16    51
70     7    62    97    63    75    51    70    37    19
75    49    74    27    32    71    28    30    47    43
42    86    72    80    16    77    21    46    63     6
94    81    15    78    62    87    47    72    42    85

I developed the script above using the OPL documentation


Solution

  • You could do the parsing in your model. Suppose your text is in toto.txt

    int N=10;
    
    execute
    {
    var f=new IloOplInputFile("toto.txt");
    var str1=f.readline();
    var str2=f.readline();
    var rank=1;
    while (!f.eof)
    {
    var str=f.readline();
    while (str.charAt(0)==" ") str=str.substring(1);
    writeln(str);
    var ar=new Array(N+1);
    var arindex=0;
    while (str.indexOf(" ")!=-1)
    {
       ar[arindex]=str.substring(0,str.indexOf(" "));
       writeln(ar[arindex]);
       str=str.substring(str.indexOf(" ")+1);
       while (str.charAt(0)==" ") str=str.substring(1);
       arindex++;
    }
    
    ar[arindex]=str;
    writeln(ar[arindex]);
    rank++;
    }
    f.close();
    }
    

    This will parse all your numbers and then you could do whatever you need.