Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Run same model with different data

    Posted 01/28/18 03:16 PM

    Originally posted by: joahumadaz


    Hi!

    I want to solve the same .mod problem, but using different data files

    Can anyone please help me with this?

    I don't know how to do this in Cplex, and it will be very useful to do this

    My .dat files names are: U1S1-1 / U1S1-2..... / U1S1-5 

    So in total I've one .mod but want to run it on a same time the five .dat files commented bellow

     

    thanks!

     

    I have this but I don'w know how to change the sheet:

     main {

     

     for(var i = i; i<=5; i++){

      

      thisOplModel.generate();

      cplex.solve();

     }

     

    }

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Run same model with different data

    Posted 01/28/18 04:47 PM

    Hi,

    could you have a look at the example in

    CPLEX_Studio128\opl\examples\opl\examples_suite

    ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Run same model with different data

    Posted 01/28/18 05:27 PM

    Originally posted by: joahumadaz


    I get it, but where is the .dat file where they read the information?

    I only want to run the same .mod but with different excel files

    anyway to help me with the .dat file?

     

    I already did this:

     

    execute {

      function ProjectInfo(dirName, prjName, runConfigName) {

        this.dir = dirName;

        this.name = prjName;

        this.runConfig = runConfigName;

      }    

       function AddProject(projects, dir, name, runConfig) {

        projects[projects.length] = new ProjectInfo(dir, name, runConfig);

      }

      }

     

     

     

     main {

      thisOplModel.settings.mainEndEnabled = true;

      var path = "";

      var OK = true;

     

      var projects = new Array();

      var projectCurr = 0;

      

      AddProject(projects, "", "/Users/joseahumada/Desktop/Bases/U1S1/1", "");

      AddProject(projects, "", "/Users/joseahumada/Desktop/Bases/U1S1/2", "");

      while (projectCurr < projects.length ) {

        var withData = true;

        var projectDir = projects[projectCurr].dir;

        var projectName = projects[projectCurr].name;

        var runConfigName = projects[projectCurr].runConfig;

        projectName = path + projectDir;

     

      var allName=projectName;

        writeln("---------------");

        writeln("solving: ", projectName);

        var project = new IloOplProject(projectName);

        var rc;

        write("run configuration: ");

        if (runConfigName != "") {

           rc=project.makeRunConfiguration(runConfigName);

           writeln(runConfigName);

        }

        else { //default run configuration

           rc=project.makeRunConfiguration();  

           writeln("default");

        }

        rc.end();

        project.end();

        projectCurr = projectCurr + 1;   

     

    }

    }

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Run same model with different data

    Posted 01/29/18 09:41 AM

    Hi,

    so let me change the example I mentioned with the files in .dat

    .mod

     


    int checkResults=1; // In order to check objectives

    tuple example
    {
    string folder;
    string modFile;
    string datFile;
    float obj;
    }

    {example} examples=...;

    execute {
      function ParseDat(dataFiles) {
        if (dataFiles == "")
          return null;
        else {
          var pos = 0;
          var datFileNames = new Array();
          while (dataFiles.indexOf(",", pos) != -1) {
            var pos2 = dataFiles.indexOf(",", pos);
            datFileNames[datFileNames.length] = dataFiles.substring(pos, pos2);
            pos = pos2+1;
          }
          datFileNames[datFileNames.length] = dataFiles.substring(pos);
          return datFileNames;
        }
      }
      function ModelInfo(dirName, modName, datParam, expRes) {
        this.dir = dirName;
        this.name = modName;
        this.data = ParseDat(datParam);
        this.result = expRes;
      }
      function AddModel(models, dir, name, dat, res) {
        models[models.length] = new ModelInfo(dir, name, dat, res);
      }
      function isNearEqual(result, expectedResult) {
        var tolerance = 0.000001;
       return Math.abs(expectedResult-result)<tolerance;
      }
    }

    main {
      // activate end() method in the IDE
      thisOplModel.settings.mainEndEnabled = true;
     
      writeln("Executing some of the distributed examples...");
      writeln();
      writeln();
     
      var path = "";
      var OK = true;

      var models = new Array();
      var modelCurr = 0;
     
      for(var i in thisOplModel.examples)
          AddModel(models,i.folder,i.modFile,i.datFile,i.obj);


      //AddModel(models, "../blending", "blending.mod", "blending.dat", 653.61);
      //AddModel(models, "../cutstock", "cutstock_main.mod", "cutstock.dat", 0);
      //AddModel(models, "../timetabling", "timetabling.mod", "timetabling-base.dat,timetabling-small.dat", 20);
      //AddModel(models, "../sched_bridge", "sched_bridge.mod", "sched_bridge.dat",null);
       

      while (modelCurr < models.length ) {
        var withData = true;
        
        var dirName = models[modelCurr].dir;
        var modelName = models[modelCurr].name;
        var dataName = models[modelCurr].data;
        var expectedResult = models[modelCurr].result;
        
        writeln(modelName);

        if (dataName.length <= 0)
          withData = false;
     
        modelName = path + dirName + "/" + modelName;
        
        var allName;
        if (withData) {
          allName = modelName + " - " ;
          for (var i=0; i<dataName.length; i++) {
            dataName[i] = path + dirName + "/" + dataName[i];
            allName = allName + dataName[i] + " ";
          }
        } else {
          allName = modelName + " - without data file";
        }
        writeln("---------------");
        writeln("solving: ", allName);
        var source = new IloOplModelSource(modelName);
        
        var algo;      
        var def = new IloOplModelDefinition(source);
        
        if (def.isUsingCplex())
        {
          algo = new IloCplex();
        }  
        if (def.isUsingCP())
        {
          algo = new IloCP();       
        }
        var theModel;
        
        theModel= new IloOplModel(def,algo);
         
        
        if (withData) {
          var data = new Array();
          for (i=0; i<dataName.length; i++) {
            data[i] = new IloOplDataSource(dataName[i]);
            theModel.addDataSource(data[i]);
          }
        }
        
        var result;
        if (def.hasMain()) {
          result = theModel.main();
          if ((expectedResult != null && result != expectedResult) || (expectedResult==null && result!=0)) {
            OK = false;
            writeln(allName, " is NOT OK");
            break;
          } else {
            writeln(allName, " is OK");
          }
        } else {
          theModel.generate();
          if ( algo.solve() ) {
            result = algo.getObjValue();
            if (expectedResult != null) {
               if (thisOplModel.checkResults==1)
               if (isNearEqual(result,expectedResult)) {
                 writeln(allName, " is OK");
                 theModel.postProcess();
               } else {
                 writeln("Fail to solve: ", allName);
                 writeln("\tExpected result: ", expectedResult, " found result: ", result);
                 OK = false;
                 break;
               }
            }
          } else {
            writeln("Fail to solve: ", allName);
            writeln("\tNo solution found!");
            OK = false;
            break;
          }
        }  
        theModel.end();
        algo.end();
        def.end();
        if (withData) {
          for (i=0; i<data.length; i++)
            data[i].end();
        }
        source.end();
        modelCurr = modelCurr + 1;
      }
      writeln("---------------");
      writeln();
      if (OK) {
        writeln("All models have been solved.");
      } else {
        writeln("Not all models have been solved as expected.");
      }
      writeln();

      (OK)?0:1;
    }

     

    .dat

    examples={
    <"../blending", "blending.mod", "blending.dat",653.61>,
    <"../cutstock", "cutstock_main.mod", "cutstock.dat", 0>,

    <"../sched_bridge", "sched_bridge.mod", "sched_bridge.dat",0>
    };

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Run same model with different data

    Posted 03/30/18 12:30 PM

    Originally posted by: Srihitha Reddy B


    Hi,

    Even am having the same issue. Could someone please help me out with it?

     

    Thanks and regards,

    Srihitha.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer