以下代码有什么问题?

时间:2019-06-09 19:30:45

标签: csv output cplex ibm-ilog-opl

我有很多设施。我想要选择和分配的设施的索引。最后,我想要一个CSV输出,向我显示每个设施。但是,我不想像[24 15 30 ...]那样显示它们,而是要像[24,25,30,...]那样将它们分开。以下代码给我一个错误。是否可以让我知道是什么问题? 错误是1.元素“字符串”在OPL模型中不存在。 2.element中心从未使用过。 (但是您可以看到我用过它)

{int} hub = { s | s in facilities : y[s] == 1 };
//Output in a CSV file
execute{
string hubs="[";
for (var i=0; i<hub.length-1;i++){
  hubs += hub[i]+",";
}
hubs += hub[hub.length-1]+"]";
var f=new IloOplOutputFile("1.csv");
f.writeln("Facilities");
f.writeln(hubs);
f.close();
}

1 个答案:

答案 0 :(得分:0)

{int} facilities=asSet(1..3);

int y[facilities]=[1,0,1];

 {int} hub = { s | s in facilities : y[s] == 1 };
//Output in a CSV file
execute{
var f=new IloOplOutputFile("1.csv");
f.writeln("Facilities =");
var hubs="[";
for (var i in hub){
  hubs += i+",";
}
hubs+="]";


f.writeln(hubs);
f.close();
}

这将给出:

Facilities =
[1,3,]

PS:

{int} facilities=asSet(1..3);

int y[facilities]=[1,0,1];

 {int} hub = { s | s in facilities : y[s] == 1 };
//Output in a CSV file
execute{
var f=new IloOplOutputFile("1.csv");
f.writeln("Facilities =");
var hubs="[";
for (var i in hub){
  hubs += i;
  if (i!=Opl.last(hub)) hubs+=",";
}
hubs+="]";


f.writeln(hubs);
f.close();
}

给予

Facilities =
[1,3]