what is the problem with the following code?

62 Views Asked by At

I have an array of facilities. I want to have indexes of the facility which is selected and allocated. In the end, I want to have a CSV output which shows me each of the facilities. But instead of showing them like [24 15 30 ...] I want to separate them like: [24,25,30,...]. The following code gives me an error. Is it possible to let me know what is the problem? The error is 1. element "string" does not in an OPL model. The 2.element hub has never been used. (but as you can see I used it)

{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

There are 1 best solutions below

1
Alex Fleischer On BEST ANSWER
{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();
}

This will give:

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();
}

gives

Facilities =
[1,3]