如何在 OPL ILOG studio 上正确加载 2 x 2 矩阵而不出现错误?

问题描述 投票:0回答:1

我从 OPL studio 中的数据文件加载 2 × 2 矩阵,并使用以下脚本

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;

我收到“数据解析错误:语法错误,意外准备,期待 $end” 我想从具有以下格式的文件数据加载并将其从文件读取到 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

我使用 OPL 文档开发了上面的脚本

java cplex opl
1个回答
0
投票

您可以在模型中进行解析。假设您的文本位于 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();
}

这将解析您的所有数字,然后您可以做任何您需要的事情。

© www.soinside.com 2019 - 2024. All rights reserved.