Decision Optimization

 View Only
  • 1.  CPLEX optimization Studio and Input Data from buch of files

    Posted Fri May 29, 2020 10:30 AM
    Hi there,
     I am using CPLEX Studio IDE 12.9.0 .  I am working on a problem where I have too many instances I need to solve. All data files I have are .txt files (a sample attached).
    First of All, I need to know how to import the data from the txt files to my IDE.
    Second, I need an efficient way to go through all data files instead of working on files one by one since I have more than 1000 instances I want to solve.
    Third, How to export the result to an excel sheet?
    Thanks in Advance!



    ------------------------------
    Ahmed Azab
    ------------------------------

    #DecisionOptimization


  • 2.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Fri May 29, 2020 03:30 PM
    Hi

    1)

    If you write in your .mod

    int a=...;
    int b=...;
    int C=...;
    int H=...;
    int N=...;
    int T=...;
    int G=...;
    
    int p[1..N]=...; 
    
    int I[1..N][1..H][1..H]=...;​


    and you call .dat your text file , it will be ok

    2)

    You may write a loop in a flow control loop (main) in order to use many different dat files

    3)

    You could have a look at SheetWrite

    regards







    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 3.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Sun May 31, 2020 11:32 AM
    Thank you for your reply,
    could you please give more details about the "loop in a flow control loop (main)​"  to call many different data files!
    Thanks in advance

    ------------------------------
    Ahmed Azab
    ------------------------------



  • 4.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Sun May 31, 2020 01:42 PM
    Hi,

    I answered that many times in this forum.

    Let me give you a very simple example out of Making Decision optimization simple

    zoodat.mod

    int nbKids=...;
    float costBus40=...;
    float costBus30=...;
     
    dvar int+ nbBus40;
    dvar int+ nbBus30;
     
    minimize
     costBus40*nbBus40  +nbBus30*costBus30;
     
    subject to
    {
     40*nbBus40+nbBus30*30>=nbKids;
    } 
    
    execute DISPLAY_After_SOLVE
        {
        writeln("The minimum cost is ",cplex.getObjValue());
        writeln("We will use ",nbBus40," 40 seats buses and ",nbBus30," 30 seats buses ");
        }
    ​

    zoodat.dat

    nbKids=300;
    costBus40=50;
    costBus30=40; ​

    zoodat2.dat

    nbKids=400;
    costBus40=50;
    costBus30=40; ​

    and then

       {string} datFiles={"zoodat.dat","zoodat2.dat"};
        
        main
        {
          var source = new IloOplModelSource("zoodat.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
          for(datFile in thisOplModel.datFiles)
          {
             writeln("with ",datFile);
             var opl1 = new IloOplModel(def,cplex);
             var data1=new IloOplDataSource(datFile);
              opl1.addDataSource(data1);
            
              opl1.generate();
              cplex.solve();
              opl1.postProcess();
              writeln();
       
    
            }
    
     }​

    gives

    with zoodat.dat
    The minimum cost is 380
    We will use 6 40 seats buses and 2 30 seats buses 
    
    with zoodat2.dat
    The minimum cost is 500
    We will use 10 40 seats buses and 0 30 seats buses ​


    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 5.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Tue June 02, 2020 04:46 AM
    Thanks ALEX FLEISCHER
    this is really helpful. 
    let me share what exactly I did 
     in the .mod  
    I used postprocessing :
    execute DISPLAY_After_SOLVE
        {
        writeln("The minimum relocations are ",cplex.getObjValue()," in Time of ",cplex.getSolvedTime()," Sec");
        }​

    and then, I called both .mod and .dat file using 

    main {
    var source = new IloOplModelSource("BRPAS.mod");
    var def = new IloOplModelDefinition(source);
    var opl = new IloOplModel(def,cplex);
    var data = new
    IloOplDataSource("C:/Users/AZAB/Desktop/Try/brtp_4_4_3_70_1.dat");
    opl.addDataSource(data);
    opl.generate();
    cplex.solve();
    opl.postProcess();
    writeln();
    opl.end();
    data.end();
    def.end();
    source.end();
    }
    and I got Scripting log 
    The minimum relocations are 0 in Time of 0.422 Sec

    now I would like to loop through all data files in the folder 
    "C:/Users/AZAB/Desktop/Try"​


    I tried your zoo example and it works well,  by using 

    {string} datFiles={"brtp_4_4_3_70_1.dat","brtp_4_4_3_70_2.dat","brtp_4_4_3_70_3.dat","brtp_4_4_3_70_4.dat"};
        
        main
        {
          var source = new IloOplModelSource("BRPAS.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
          for(datFile in thisOplModel.datFiles)
          {
             writeln("with ",datFile);
             var opl1 = new IloOplModel(def,cplex);
             var data1=new IloOplDataSource(datFile);
              opl1.addDataSource(data1);
            
              opl1.generate();
              cplex.solve();
              opl1.postProcess();
              writeln();
       
    
            }
    
     }

    However, in my case with over 1000 data files, defining data files one by one using {string} datFiles={ .......}  will be impossible. 

    I would like to loop over all data files in "C:/Users/AZAB/Desktop/Try", How I can do this?
    Thanks in advance,



    ------------------------------
    Ahmed Azab
    ------------------------------



  • 6.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Tue June 02, 2020 04:52 AM
    You can create the name of the data file dynamically:
    for (var i = 1; i < 1000; ++i) {
      ...
      var data1=new IloOplDataSource("brtp_4_4_3_70_" + i + ".dat");
      ...
    }

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



  • 7.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Tue June 02, 2020 10:27 AM
    Got  it, Thank you 
    so that, shall I load all data files to my OPL project to loop over them, or I can load from a directory "C:/Users/AZAB/Desktop/Try" in the for loop without loading them to the OPL project!

    ------------------------------
    Ahmed Azab
    ------------------------------



  • 8.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Tue June 02, 2020 10:33 AM
    Both ways are possible

    ------------------------------
    ALEX FLEISCHER
    ------------------------------



  • 9.  RE: CPLEX optimization Studio and Input Data from buch of files

    Posted Sun May 31, 2020 11:36 AM
    Edited by System Fri January 20, 2023 04:44 PM