Reputation: 21
I know I ask a lot of questions about reading CSV file and coding it in CPLEX. But now my question is about a strategy. As I explained before I have a CSV file and I want to read it in a CPLEX. My question this time is that: I want to use a for loop to create an array in CPLEX from CSV file and I don't know how? This time I need to use for loop, not any other methods and I want to know is it possible or not: My code so far is:
range demand = 1..10;
int index[demand];
int weight[demand];
execute {
var f = new IloOplInputFile("weight.csv");
var data = f.readline();
while (!f.eof) {
var data = f.readline().split(",");
if (data.length == 2)
for (var i=1; i<=demand.length; i++){
index[i].add(Opl.intValue(data[0]));
weight[i].add(Opl.intValue(data[1]));
}
writeln(index);
writeln(weight);
}
}
The problem is that I get [00..0] values for both index and weight. The weight file is attached as a picture: weight file
I need I ask so many questions about this problem but I need this time that I use for loop and not define any tuple. I want to define each array separately.
Upvotes: 0
Views: 455
Reputation: 5940
There is a bug in your code. If the code does not do what you want, it may help to add writeln(...)
statements to trace what the code is actually doing. Then you can probably figure out these things yourself.
In your case there are a number of issues:
demand.length
but that property is undefined. You can see this by adding writeln(demand.length)
to your code. So the condition i<=demand.length
is never true and that loop is never executed.i
for each line in the CSV file although each line in the CSV gives you only one entry in the array.The correct code to read your arrays from CSV would be this:
range demand = 1..10;
int index[demand];
int weight[demand];
execute {
var f = new IloOplInputFile("weight.csv");
var data = f.readline();
var i = 1;
while (!f.eof) {
var data = f.readline().split(",");
if (data.length == 2) {
index[i] = Opl.intValue(data[0]);
weight[i] = Opl.intValue(data[1]);
i = i + 1;
}
}
writeln(index);
writeln(weight);
}
Upvotes: 2