Re: How to export into a csv file ?

Hi,

in flow control you may access dexpr.

Let me adapt https://www.ibm.com/developerworks/community/forums/html/topic?id=ae375381-62c8-486f-b43f-c9d7c7f6a2a9&ps=25

from https://www.linkedin.com/pulse/how-opl-alex-fleischer/

to give you an example:

sub.mod

 float maxOfx = …;
    dvar float x;

    maximize x;
    dexpr float xtimes2=2*x;
    subject to {
      x<=maxOfx;
    }

    execute
    {
    writeln(“x= “,x);
    }

and then main.mod

 main {
      var source = new IloOplModelSource(“sub.mod”);
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
     
     var f=new IloOplOutputFile(“export.csv”);
     
      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();
         f.writeln(k,”;”,opl.xtimes2,”;”);
         writeln(“OBJ = ” + cplex.getObjValue());
      } else {
         writeln(“No solution”);
      }
     opl.end();
    }  
    
    f.close();
     
    }

 

will give

 

export.csv

 

regards

Source: Re: How to export into a csv file ?