Hi, I'm trying to read a (potentially large) .txt data file into my model using an OPL execute block. My data is a list that has a varying number of integers stored in each element.
As a toy example, I'm trying to read a data file (called "test_3x3_burnID.txt") and assign it to a variable called burnID. The data file looks like this:
1
2
2, 3
4
5
5, 6
7
7, 8
7, 9
Line breaks indicate new elements of the list. There will be N rows in the file.
I'd like to read it into my .mod file as an array of integer-valued sets, i.e. like this:
burnID= [
{1}
{2}
{2 3}
{4}
{5}
{5 6}
{7}
{7 8}
{7 9}
];
I'm struggling with appending data of different lengths and assigning the correct data type within the OPL execute block. Here is what I have so far (full model and data files are attached):
//Parameters
int N= ...; //number of ignition pixels
range ignition_pixel= 1.. N; //ith pixel
{int} burnID2[ignition_pixel]=...; //list of pixels that ignite cell burnID
//Read in data from external text file (using execute block)
{int} t; //initialize a variable to store a set of integers (i.e. one line of the data file)
execute
{
var f = new IloOplInputFile("test_3x3_burnID.txt");
if (f.exists) {
writeln("the file output.txt exists");
var s;
var counter=1;
while (!f.eof) {
s= f.readline().split(",");
writeln("s=",s.length)
writeln("s=",s[0],s[1]);
//add s to variable t
for (var i=0; i<s.length; i++){
t.add(Opl.intValue(s[i]));
}
writeln("t=",t); //looks OK
burnID2[counter]=t; //add t to burnID2 array
writeln("BID2[i]",burnID2[counter]); //going in OK
//clear/reset set t
for (var i=0; i<s.length; i++){
t.remove(Opl.intValue(s[i]));
}
writeln("t=",t); //clears set t OK-- also wiping burnID2?
counter= counter +1;
}
f.close();
}
writeln("BID=",burnID2); //everything is cleared :(
}
I think my problem is that the set 't' keeps getting cleared when I try to reassign it. But I can't figure out how to clear it within the execute statement while also keeping what I've stored in my burnID2 variable.
Can anyone help me with how to correctly append a dynamic list in OPL?
------------------------------
Sam Nicol
------------------------------
#DecisionOptimization