Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Reading variable length list from text file with OPL

    Posted 06/09/20 09:01 PM
    Edited by System Admin 01/20/23 04:44 PM
    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


  • 2.  RE: Reading variable length list from text file with OPL

    Posted 06/10/20 12:43 AM
    I've solved my own problem-- didn't need to have the intermediate variable t. For anyone else trying to do this, here's my working script:

    //Parameters
    int N= ...;  //number of ignition pixels 
    range ignition_pixel= 1.. N;  //ith pixel
    {int} burnID[ignition_pixel];
    
    execute 
       {  
       
       var f = new IloOplInputFile("test_3x3_burnID.txt");
       if (f.exists) {
        writeln("the input file exists");
         var s;
         var counter=1;
        	 while (!f.eof) {
         		s= f.readline().split(",");     		
         		
    			//add s to variable t
        		for (var i=0; i<s.length; i++){       		
         			burnID[counter].add(Opl.intValue(s[i]));
           		}     				 
    
         		
         		//for debugging only
    		//writeln("BID2[i]",burnID[counter]);
    		//writeln("BIDall=", burnID);
         		
          		counter= counter +1;    			
      		}   
      		f.close();	
       	}
       	writeln("BID=",burnID); 
       }        
    
     ​
    This returns:

    BID= [{1} {2} {2 3} {4} {5} {5 6} {7} {7 8} {7 9}]​


    ------------------------------
    Sam Nicol
    ------------------------------



  • 3.  RE: Reading variable length list from text file with OPL

    Posted 06/10/20 01:04 AM
    Your original code did not work because there is only one instance of set t. You just add this reference multiple times to burnID and clear it multiple times. To avoid this you could explicitly create a new set by creating the union of t and an empty set. However, your second approach looks clearer anyway.

    ------------------------------
    Daniel Junglas
    ------------------------------