Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Reading data line by line in a flow control

    Posted 10/13/17 10:55 AM

    Originally posted by: ZeBo


    Hi everybody,

    i have a model which gives me the optimal allocation of suppliers in a transportation network. For now, my .dat file contains data for a tuple (coordinates for the suppliers).

    I want to solve this model with 1000 of  networks and so i created a .txt file which contains all the coordinates for each network. how can i update the model with the new tuple data? Is it possible with one .txt file?

    The logical way would be to solve the model, then to delete the first row and solve again with the "new" first row. But how can i do this?

    Best regards

    ZeBo


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Reading data line by line in a flow control

    Posted 10/14/17 05:21 AM

    Hi,

    first to read and write .txt you could use IloOplInputFile and IloOplOutputFile :

    https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.ide.help/refjsopl/html/IloOplInputFile.html

    https://www.ibm.com/support/knowledgecenter/SSSA5P_12.7.1/ilog.odms.ide.help/refjsopl/html/IloOploutputFile.html

    about modifying the model and solving again I would suggest some links in https://www.linkedin.com/pulse/how-opl-alex-fleischer/

    • Change some data and solve again without regenerate
    • Change some data and solve again with regenerate
    • Change a 2D array and solve again
    • Change a set and solve again

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Reading data line by line in a flow control

    Posted 10/15/17 06:12 AM

    Originally posted by: ZeBo


    @AlexFleischer a1d4492b-6cf8-402d-ae3c-dd656c427dc7​ Thank you very much for this detailled answer, it helped a lot!

     

    Every line in my .txt file is looking like this and i want to have one line for one optimization iteration so that the output below is always the newest solution added to the "solution.txt":

     

    {<1,10.636, 8.087>,<2, 4.799, 8.960>,<3, 7.466, 8.589>,<4, 5.135, 0.150>,<5, 3.645, 1.659 >,<6,4.457 ,0.047 >,<7,3.779, 5.712>,<8, 6.072 ,1.933 >,<9,5.850, 3.503 >,<10,8.228, 1.741 >,<11,7.105, 3.040 >,<12,0.914 ,1.473>,<13, 9.885 ,1.191 >,<14,0.089 ,5.317 >,<15,6.018, 1.662>};

     

    When i run the optimization it says: "Cannot set Arcs property".

    What is my fault?

     

    tuple Arc{
     int id;
     float xKoord;
     float yKoord;
    }

    {Arc} Arcs = {};

     tuple ArcNew{
     int id;
     float xKoord;
     float yKoord;
    }

    {ArcNew} ArcsNew = {};

     

    //HERE IS MY MAXIMIZE FUNCTION AND RESTRICTIONS

     

    main
        {
            var f = new IloOplInputFile("netzwerke.txt");
             var s;
         
             for(var k=1;k<=6;k++)
              {
              s=f.readline();  
        
                  var source = new IloOplModelSource("KP.mod");
                  var cplex = new IloCplex();
                  var def = new IloOplModelDefinition(source);
             
                  var opl = new IloOplModel(def,cplex);
              
                  var data2= new IloOplDataElements();
             
                  data2.Arcs=thisOplModel.ArcsNew;
                  data2.Arcs=s;
                  opl.addDataSource(data2);
                 
                  opl.generate();
                  
                  f.close();
                  data2.end();
                 opl.end();
            }  
        }
            execute{
            
            var ofile = new IloOplOutputFile("modelRun.txt", true);
            
                for(var j = 1; j <= 5; j++ ){
                    for(var i = 1; i <= 15; i++ ){    
                        ofile.write(x[i][j]);
                    }    
                writeln();
                }
                ofile.close();
            }


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Reading data line by line in a flow control

    Posted 10/16/17 11:55 AM

    Originally posted by: ZeBo


    It says that my Tuple has already been set. How can i give new Data to a Tuple set from a data file?


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Reading data line by line in a flow control

    Posted 10/16/17 12:04 PM

    Hi,

    yes it is your code.

    You could get ideas at https://www.ibm.com/developerworks/community/forums/html/threadTopic?id=5b68f185-2132-4296-9ea4-a4d128937d65&ps=25

    in order to set Arcs as you do I would write

    Arc} Arcs = ...;

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Reading data line by line in a flow control

    Posted 10/16/17 12:25 PM

    Originally posted by: ZeBo


    Yes you are right, this way is better when im reading from a data file.

    {Arc} Arcs = ...;

    #.dat file#

    Arcs = { < 1, 12,56 > < ... > };

    #.dat file#

    But i dont get it how to solve it when i want to have many of those rows in my .dat file & every row is a new iteration of my model.

    #.dat file#

    Arcs = { < 1, 12,56 > < ... > };
    Arcs = { < 1, 27,86 > < ... > };
    Arcs = { < 1, 20,76 > < ... > };

    #.dat file#


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Reading data line by line in a flow control

    Posted 10/16/17 01:51 PM

    Originally posted by: ZeBo


    Before i continue thinking i need to know: Is it possible to update a whole tuple???

    I mean its a difference between a component of a tuple or the whole tuple right? so my next step is to generate an Array with n=lines in textfile to save the string of one line in the array. but now it says the array doesnt exists in the model...

     

     main {
            
       
          var source = new IloOplModelSource("KP.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
            
            new Array(6);
    var f = new IloOplInputFile("netzwerke.txt");
             var s;
         
             for(var k=1;k<=6;k++)
              {
              
                  s = f.readline();
                  Array[k] = s;                  
             }
         
          for(var k=1;k<=6;k++)
          {
          var opl = new IloOplModel(def,cplex);
            
          var data2= new IloOplDataElements();
         
        data2.Arcs=thisOplModel.Array;
        data2.Arcs = Array[k];
          opl.addDataSource(data2);
          opl.generate();

          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
        data2.end();
         opl.end();
         
         
        }  
         
        }


    #CPLEXOptimizers
    #DecisionOptimization