Decision Optimization

 View Only
  • 1.  Best way to run severall optimizations with a loop for

    Posted Mon February 13, 2023 06:07 PM

    Hi everyone,

    I have a Cplex model that calls data from a database and publish in it when the optimization is over. There is also a .dat file that contains parameters and one of these parameters is the year. All the optimization runs for the data corresponding to that year. My goal is now to create a ilog script (or Python script if it's possible) so that the optimization will run year after year for a time window I choose (10 years for exemple) without the need to change the .dat file manually and to launch a new optimization.

    What would be the best way to do that ?

    Thank you,

    Best regards



    ------------------------------
    Grégoire Turzo
    ------------------------------


  • 2.  RE: Best way to run severall optimizations with a loop for

    Posted Tue February 14, 2023 04:28 AM

    Dear Gregoire,

    So you want to:

    • load an OPL .dat file once
    • inside an OPL script loop:
      • set the YEAR parameter
      • generate constraints by pulling , depending on YEAR, selected data from the ones loaded from the .dat file
      • solve the constraint model

    If it is the case, you might be interested in the OPL example in

    C:\Program Files\IBM\ILOG\CPLEX_Studio<version>\opl\examples\opl\cutstock

    where the OPL script manipulates master and subordinate OPL models, populating the subordinate model with information from the master model. I believe you could take inspiration from it.

    I hope this helps,

    Cheers,



    ------------------------------
    Renaud Dumeur
    ------------------------------



  • 3.  RE: Best way to run severall optimizations with a loop for
    Best Answer

    Posted Tue February 14, 2023 07:10 AM

    Hi,

    in https://github.com/AlexFleischerParis/howtowithoplchange/blob/master/changevalue.mod

    you have a basic example that could help you.

    main {
          var source = new IloOplModelSource("subvalue.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         
         
          for(var k=1;k<=10;k++)
          {
          var opl = new IloOplModel(def,cplex);
            
          var data2= new IloOplDataElements();
          data2.maxOfx=k;
          opl.addDataSource(data2);
          opl.generate();
    
          if (cplex.solve()) {  
             opl.postProcess();
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
         opl.end();
        }  
         
        }

    with subvalue.mod

    float maxOfx = ...;
        dvar float x;
    
        maximize x;
        subject to {
          x<=maxOfx;
        }
    
        execute
        {
        writeln("x= ",x);
        }


    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------



  • 4.  RE: Best way to run severall optimizations with a loop for

    Posted Wed February 15, 2023 05:51 PM

    Hi Alex,

    Thank you for your answer, it worked !

    However, I would like to improve the time resolution by running severall years at the same time. I read your thread (https://community.ibm.com/community/user/ai-datascience/discussion/how-to-call-many-models-in-parallel-thanks-to-ilooplexec) but I didn't manage to adapt it to my code because you don't use a main, just an "Execute" at the end of your model. Besides, I would like to choose the number of simulations I launch at the same time (for example, 5 simulations at the same time for a span of 10 years and when one is over a new one starts) because my laptop is not so powerful and launching 10 years at the same time would be inefficient.

    Here is my code:

     main {
          var source = new IloOplModelSource("MyModel.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
          for(var k=2023;k<=2033;k++)
          {
          var opl = new IloOplModel(def,cplex);
          var dataParam= new IloOplDataElements();
          dataParam.prmAnneeResolution=k;
          opl.addDataSource(dataParam);
          writeln("Année de résolution : " + dataParam.prmAnneeResolution);
          var data = new IloOplDataSource("Source1.dat");
          var data2 = new IloOplDataSource("Source2.dat");
          var data3 = new IloOplDataSource("Source3.dat");
          opl.addDataSource(data);
          opl.addDataSource(data2);
          opl.addDataSource(data3);
          opl.generate();

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

    I tried to add the command "IloOplExec()" in different ways but I didn't manage to get results.

    Thanks in advance for your help
    Best regards



    ------------------------------
    Grégoire Turzo
    ------------------------------



  • 5.  RE: Best way to run severall optimizations with a loop for

    Posted Mon February 20, 2023 02:35 AM

    See https://community.ibm.com/community/user/ai-datascience/discussion/cplex-parallel-optimization-using-ilooplexec-in-a-main?ReturnUrl=%2fcommunity%2fuser%2fai-datascience%2fcommunities%2fcommunity-home%2fdigestviewer%3fcommunitykey%3dab7de0fd-6f43-47a9-8261-33578a231bb7



    ------------------------------
    [Alex] [Fleischer]
    [Data and AI Technical Sales]
    [IBM]
    ------------------------------