如何在CPLEX,OPL中读取具有整数,字符串和其他集合的元组

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

我正在尝试从excel阅读元组。元组具有整数,字符串和集合。我尝试了以下操作,但由于出现错误,因此:工作表不支持{Path}类型的数据元素“ Pbd”。并且处理失败。

这是我的.mod文件的一部分

tuple Path {
int id;
string source;
string dest;
{string} pitblockSet;
{string} roadPoints; // not used
{string} dumpblockSet;
{string} others;
float dist;
};


{Path} Pbd=...; 

The corresponding part in the dat file is :
Pbd from SheetRead(sheet,"all_paths!A2:C30910");

在工作表上的Excel文件all_paths中,我具有以下内容。在此模型中,还从同一Excel中读取了其他几个变量。

正在读取到该元组的部分excel数据如下:

PathId  Source  Dest    pitblockSet RoadPoints  dumpblockSet    others  dist
1       P1      D1      P1          R8         D45 D42 D39 D14 D1       581.3956
2       P1      D1      P1          R8         D40 D14 D1               587.1185
3       P1      D1      P1          R8         D43 D16 D2 D1            588.7774
4       P2      D1      P2          R8         D45 D42 D39 D14 D1       539.7307
5       P2      D1      P2          R8         D40 D14 D1               545.4535
6       P2      D1      P2          R8         D43 D16 D2 D1            547.1124
7       P3      D1      P3          R8         D45 D42 D39 D14 D1       500.0794

我也尝试过将数据更改为逗号分隔的集合,如下所示

PathId  Source  Dest    pitblockSet RoadPoints  dumpblockSet    Others  Distance
1       P1      D1      P1,          R8,        D45,D42,D39,D14,D1,     581.3956
2       P1      D1      P1,          R8,        D40,D14,D1,             587.1185
3       P1      D1      P1,          R8,        D43,D16,D2,D1,          588.7774
4       P2      D1      P2,          R8,        D45,D42,D39,D14,D1,     539.7307
5       P2      D1      P2,          R8,        D40,D14,D1,             545.4535
6       P2      D1      P2,          R8,        D43,D16,D2,D1,          547.1124
7       P3      D1      P3,          R8,        D45,D42,D39,D14,D1,     500.0794
8       P3      D1      P3,          R8,        D40,D14,D1,             505.8023

但是我仍然收到相同的错误。

我想要这些的目的是,我正在.mod文件中使用它们,如下所示:

float hc[Pathid][TimePeriods];    //PathId is another int variable read seperately

//determine haulage cost for each path
execute {
//distances to plant
for (var i in Pbm.id) {
for (var t in TimePeriods){
  hc[i][t] = Pbm.dist*HaulageCost[t];
}
}
}

最后要在约束中将其用作

forall( i in Pbd.pitblockSet ,  t in TimePeriods) { 
       // blockabove exposed Pbd:
        sum(j in BlockBelow[i]) schedulePit[j.id][t] * totalVolume[j.id] <= 
        (sum(j in BlockBelow[i],r in TimePeriods : r <= t,d in DumpBlocks)(Xbdt[j.id][d][r])  
        + sum(j in BlockBelow[i],r in TimePeriods : r <= t, s in Stockpiles)(Xbst[j.id][s][r]/density[j.id])
        +sum(j in BlockBelow[i],r in TimePeriods : r <= t, m in Plants)(Xbmt[j.id][m][r]/density[j.id]))  ;      
        }  

读取名为Path的元组的最佳方法是什么,不确定为什么我会收到错误。

websphere cplex opl
1个回答
0
投票
tuple Raw { int id; string source; string dest; string pitblockSet; string roadPoints; string dumpblockSet; string others; float dist; } {Raw} raw = ...; // This is read from the spreadsheet tuple Path { int id; string source; string dest; {string} pitblockSet; {string} roadPoints; {string} dumpblockSet; {string} others; float dist; } {Path} path = {}; // This is filled in the scripting block below. {string} emptyset = {}; // Helper to create new empty sets in scripting. // Populate the path set. execute { // Split a string by a separator and add all the tokens to a set. function splitAndAdd(set, data, sep) { var fields = data.split(sep); for (var i = 0; i < fields.length; ++i) { set.add(fields[i]); } } for (var r in raw) { var s = Opl.operatorUNION(emptyset, emptyset); // Add a new tuple. The fields of type set are empty sets for now. // We cannot just pass 'emptyset' because then all fields would // reference the same set. So we create a new emptyset as the union // of two empty sets. var t = path.add(r.id, r.source, r.dest, Opl.operatorUNION(emptyset, emptyset), Opl.operatorUNION(emptyset, emptyset), Opl.operatorUNION(emptyset, emptyset), Opl.operatorUNION(emptyset, emptyset), r.dist); // Now populate the fields of type set in the newly created tuple. splitAndAdd(t.pitblockSet, r.pitblockSet, " "); splitAndAdd(t.roadPoints, r.roadPoints, " "); splitAndAdd(t.dumpblockSet, r.dumpblockSet, " "); } writeln(path); }
© www.soinside.com 2019 - 2024. All rights reserved.