Decision Optimization

 View Only
Expand all | Collapse all

Cplex solving a model several times changing a single parameter for a .dat file

  • 1.  Cplex solving a model several times changing a single parameter for a .dat file

    Posted Fri October 13, 2023 05:40 PM

    Hi everyone,

    I'm trying to build the following model :

    • The model in modele.mod
    • Three data files in data1.dat, data2.dat, data3.dat that contains all the parameters and timeseries needed for the model
    • A main in main.mod that contains the iterative loop to change a parameter (a 1D array with a float) then solve the problem several times

    I tried to build this following the examples I found here : - AI and Data Science

    Ibm remove preview
    - AI and Data Science
    View this on Ibm >

    How to ... with OPL

    Linkedin remove preview
    How to ... with OPL
    OPL is a very easy to use modeling language within CPLEX. Let me start here a list of some examples and links I gave in the past to answer very simple and common questions: how to .
    View this on Linkedin >

    But I have troubles when I run my main.mod, Cplex doesn't find my data (a lot of "External data element XXXX was not defined") and I don't manage to change my parameter properly. Here is my main.mod :

    float paramreservoir[NumReservoir];
     
    main {
      
    var source = new IloOplModelSource("Modele.mod"); 
    var cplex = new IloCplex(); 
    var def = new IloOplModelDefinition(source);
     
    for(var k=100;k<=500;k+=50) {
      var opl = new IloOplModel(def,cplex)
      var data2= new IloOplDataElements();
      data2.VolumeInitial=thisOplModel.paramreservoir;
      data2.VolumeInitial[1]=k;
          opl.addDataSource(data2);
        writeln("Solve with Reservoir =", k);
        opl.generate();
    if ( cplex.solve() ) {
      curr = cplex.getObjValue();
      writeln("OBJECTIVE: ",curr);
      writeln();
    else {
      writeln("No solution!");
      break;
    }

    Does someone has an idea ?
    Thanks a lot for the help


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


  • 2.  RE: Cplex solving a model several times changing a single parameter for a .dat file

    Posted Mon October 16, 2023 02:50 AM

    Hi

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

    you can see

    int a[1..2][1..2];
    
        main {
          var source = new IloOplModelSource("sub2d.mod");
          var cplex = new IloCplex();
          var def = new IloOplModelDefinition(source);
         
         
         
          for(var k=11;k<=15;k++)
          {
          var opl = new IloOplModel(def,cplex);
         
         
            
          var data2= new IloOplDataElements();
         
          data2.y=thisOplModel.a;
          data2.y[1][1]=k;
          opl.addDataSource(data2);
         
          opl.generate();
    
          if (cplex.solve()) {
             writeln("OBJ = " + cplex.getObjValue());
          } else {
             writeln("No solution");
          }
          data2.end();
         opl.end();
         
         
        }  
         
        }

    which is a good starting point for updating an array in a main



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