Reputation: 21
I have a CSV file which contains distances from node i
to node j
and also the weight of the nodes. This file wants to test a CPLEX code, which tries to solve a p-median problem. I'm a new CPLEX user and I don't have any idea how should I get my distances and weights from CSV file. I tried to test this code with data from an Excel file and I got an answer. However, because I will want to work with a large dataset, it is required to import CSV files and not Excel sheets. Could you please help me to know how should I read my d
column and w
column? In the following I brought the code and the image of my CSV file
int w[demands]=...;
float d[demands][facilities]=...;
}[CSV file][1]
Upvotes: 0
Views: 1144
Reputation: 5940
In order to read data from any text-based file format you can use scripting and an IloOplInputFile. That allows you to read the file line by line. You can then split the line on the CSV delimiter and extract the required fields.
You can find an example on the CPLEX forum here:
Assuming you have this file demand.csv
(where the first field gives the first index, the second field the second index and the third field the value)
1;1;5
1;2;10
1;3;15
2;1;20
2;2;25
2;3;30
The this code initializes the d
array:
range demands = 1..2;
range facilities = 1..3;
float d[demands][facilities];
execute {
var f = new IloOplInputFile("demand.csv");
while (!f.eof) {
var data = f.readline().split(";");
if (data.length == 3) // Beware of empty lines
d[Opl.intValue(data[0])][Opl.intValue(data[1])] = Opl.floatValue(data[2]);
}
writeln(d);
}
Similarly for the d
array.
Upvotes: 2