Search code examples
filedirectorycplexopl

different files in one folder as several data for opl model


I have an opl model in IBM ILOG CPLEX Optimization studio. Every time in this model two CSV files are imported. There are 272 files, so this model should be run 136 times. Each time one CSV output is exported as a result. I need a solution to automatically read each of these two files each time, get the result and import the next two files until all the files are read. I added a picture of some of the files so you can see how they're named. How can I do this automatically. Also, the second part of my question is: for example one of my filename is: pmed1_DP=10_P=5_n=100 , I need to get the DP, P and n as inputs for my code. Is there any way to use the filename and get our required inputs?filenames


Solution

  • For your first question, you can use a main to set a different output csv file. Let me give you a tiny example:

    test.mod

    tuple t
    {
      string firstname;
      int number;
    }
    
    {t} s={<"Nicolas",2>,<"Alexander",3>};
    
    string csvOutput=...;
    
    execute
    {
     var f=new IloOplOutputFile(csvOutput);
     for(var i in s)
     {
      f.writeln(i.firstname,";",i.number,";");
     }
    f.close();
    }
    

    and then main.mod

    {string} csvOutputs={"csv1.csv","csv2.csv"};
    
        main {
          var source = new IloOplModelSource("test.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
    
    
    
          for(var o in thisOplModel.csvOutputs)
          {
          var opl = new IloOplModel(def,cplex);
    
          var data2= new IloOplDataElements();
          data2.csvOutput=o;
          opl.addDataSource(data2);
          opl.generate();
    
          if (cplex.solve()) {  
             opl.postProcess();
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
         opl.end();
        }  
    
        }
    

    This will generate 2 csv files, csv1 and csv2.

    For your second question you could either send parameters through a main like that or call oplrun many time and change the parameters with the -D parameter:

    oplrun -DcsvOutput="csv3.csv" test.mod
    

    This will generate csv3.csv.