Topic
5 replies Latest Post - ‏2013-12-09T07:24:40Z by AlexFleischer
6A0Q_Hartioun_Arzoumanian
6 Posts
ACCEPTED ANSWER

Pinned topic Update values of data 2 dimensional arrays in opl main script

‏2013-12-07T12:00:02Z |

I do the following:

var src = new IloOplModelSource("project.mod");    
var def = new IloOplModelDefinition(src);     
var cplex = new IloCplex();      
var model = new IloOplModel(def, cplex);     
var data = new IloOplDataSource("project.dat");
var data2 = new IloOplDataElements();

data2.VMMapping[2][4] = 1; // here I get an error

it says VMMapping doesnot exist.

where VMMapping is a two dimensional array declared in project.mod file as follows:

int VMMapping[v in V][d in D] = ...; 

  • AlexFleischer
    AlexFleischer
    453 Posts
    ACCEPTED ANSWER

    Re: Update values of data 2 dimensional arrays in opl main script

    ‏2013-12-07T20:13:23Z  in response to 6A0Q_Hartioun_Arzoumanian

    Hi,

     

    let me give you an example:

    You have sub.mod

    float maxOfx = ...;
    int y[1..2][1..2]=...;

    execute
    {
    writeln("y=",y);
    }

    dvar float x;

    maximize x;
    subject to {
      x<=maxOfx+sum(i in 1..2, j  in 1..2) y[i][j];
    }

     

    and then you can write

    int a[1..2][1..2];

    main {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      var opl = new IloOplModel(def,cplex);
     
     
      for(var k=11;k<=15;k++)
      {
      var opl = new IloOplModel(def,cplex);
     
     
        
      var data2= new IloOplDataElements();
      data2.maxOfx=k;
    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 will give

     

    y= [[11 0]
             [0 0]]
    OBJ = 22
    y= [[12 0]
             [0 0]]
    OBJ = 24
    y= [[13 0]
             [0 0]]
    OBJ = 26
    y= [[14 0]
             [0 0]]
    OBJ = 28
    y= [[15 0]
             [0 0]]
    OBJ = 30
     

     

    Regards

     

     

     

    • 6A0Q_Hartioun_Arzoumanian
      6 Posts
      ACCEPTED ANSWER

      Re: Update values of data 2 dimensional arrays in opl main script

      ‏2013-12-07T21:08:00Z  in response to AlexFleischer

      Hello Alex,

      It gives error on the following line:

      data2.y=thisOplModel.a;

      The element a doesn't exist in the OPL model.

      • AlexFleischer
        AlexFleischer
        453 Posts
        ACCEPTED ANSWER

        Re: Update values of data 2 dimensional arrays in opl main script

        ‏2013-12-08T07:12:09Z  in response to 6A0Q_Hartioun_Arzoumanian

        Hi,

        are you sure you have not forgotten

        int a[1..2][1..2];

        in the main model ?

        Which version do you use ?

        Regards

        • 6A0Q_Hartioun_Arzoumanian
          6 Posts
          ACCEPTED ANSWER

          Re: Update values of data 2 dimensional arrays in opl main script

          ‏2013-12-08T20:54:30Z  in response to AlexFleischer

          This id my code:

           

          /*********************************************
           * OPL 12.5.1.0 Model
           * Author: Harut
           * Creation Date: 06/12/2013 at 16:18:01
           *********************************************/
          int a[1..11][1..5];
          main {          
          var src = new IloOplModelSource("project.mod");    
          var def = new IloOplModelDefinition(src);     
          var cplex = new IloCplex();      
          var model = new IloOplModel(def, cplex);     
          var data = new IloOplDataSource("project.dat");
          var data2 = new IloOplDataElements();
           
          data2.d1 = 1;
          data2.vMMapping = thisOplModel.a;
           
          model.addDataSource(data2); 
          model.addDataSource(data);
          model.generate(); 
          model.modelArray;   
            cplex.epgap=0.01;    
           
           
            var var_2d = new Array(model.nVirtMachines);
             for (var i=0; i<model.nVirtMachines; i++) { 
               var_2d[i] = new Array(model.nDataCenters);
               for(var j=0; j<model.nDataCenters; j++) { 
                 var_2d[i][j] = 0;
               }
             }
           for (var dataCenter = 1; dataCenter <= model.nDataCenters; dataCenter++) {
              
              cplex.clearModel();
              model = new IloOplModel(def, cplex);
              data2.d1 = dataCenter;
              
              model.addDataSource(data2); 
              model.addDataSource(data);
              model.generate();
                
            if (cplex.solve()) {       
            writeln("The cost is " + cplex.getObjValue()); 
           
              for (var i=0; i<model.nVirtMachines; i++) { 
                for(var j=0; j<model.nDataCenters; j++) { 
                  var_2d[i][j] += model.xvd[i+1][j+1];
                }
              }
            //writeln(model.xvd);  
            //writeln(x);   
                  //var_2d += model.xvd;
           
              } else {         
              writeln("Not solution found");    
              } 
              
            }       write("[");
            for (var i=0; i<model.nVirtMachines; i++) { 
            write("[");
                for(var j=0; j<model.nDataCenters; j++) { 
                  write(var_2d[i][j]);
                }
                write("]"); 
              }
              write("]");     
              model.end();     
              data.end();     
              def.end();     
              cplex.end();     
              src.end(); 
              //writeln(x);
              }; 

           

          and it doesnt work

          • AlexFleischer
            AlexFleischer
            453 Posts
            ACCEPTED ANSWER

            Re: Update values of data 2 dimensional arrays in opl main script

            ‏2013-12-09T07:24:40Z  in response to 6A0Q_Hartioun_Arzoumanian

            Hi,

             

            you should try to adapt the code I sent you.

            Or after enough effort, you may attach project.mod and project.dat so that other participants can have a look

             

            Regards