Decision Optimization

Decision Optimization

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

 View Only
  • 1.  How to export into a csv file ?

    Posted Wed September 21, 2016 06:51 AM

    Hi,

     

    I am often asked this so let me give an example:

    tuple t
    {
    string firstname;
    int number;
    }

    {t} s={<"Nicolas",2>,<"Alexander",3>};

    execute
    {
    var f=new IloOplOutputFile("export.csv");
    for(var i in s)
    {
    f.writeln(i.firstname,";",i.number,";");
    }
    f.close();
    }

    writes a file export.csv

    Nicolas;2;
    Alexander;3;

    regards

     

    Alex Fleischer

    PS:

    Many how to with OPL at https://www.linkedin.com/pulse/how-opl-alex-fleischer/

    Many examples from a very good book : https://www.linkedin.com/pulse/model-building-oplcplex-alex-fleischer/

    Making optimization simple : https://www.linkedin.com/pulse/making-decision-optimization-simple-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to export into a csv file ?

    Posted Fri April 27, 2018 11:30 AM

    Which can be a bit more generic:

    execute
    {
    // turn an OPL tupleset into a csv file
    function turnTuplesetIntoCSV(tupleSet,csvFileName)
    {
    var f=new IloOplOutputFile(csvFileName);

    var quote="\"";
    var nextline="\\\n";

    var nbFields=tupleSet.getNFields();
    for(var j=0;j<nbFields;j++) f.write(tupleSet.getFieldName(j),";");
    f.writeln();
    for(var i in tupleSet)
    {

     

    for(var j=0;j<nbFields;j++)
    {

    var value=i[tupleSet.getFieldName(j)];
    if (typeof(value)=="string") f.write(quote);
    f.write(value);
    if (typeof(value)=="string") f.write(quote);
    f.write(";");

    }
    f.writeln();
    }
    f.close();
    }
    }


    tuple t
    {
    string firstname;
    int number;
    }

    {t} s={<"Nicolas",2>,<"Alexander",3>};

    execute
    {
    turnTuplesetIntoCSV(s,"export.csv");
    }

     

    that gives

    firstname;number;
    "Nicolas";2;
    "Alexander";3;


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to export into a csv file ?

    Posted Sat December 15, 2018 08:53 AM

    Originally posted by: Kamran_Sr


    Hi Alex,

     

    I have written a code using main() which reads different data iteratively using a loop in my script. The model runs per itearation with different data and gives the solution.

    I need to have the objective value for each iteration. I have used the code below to get the solution per iteration:

     

    writeln

    ("run ",n);

    if(cplex.solve()) {

    writeln("\t cost :", cplex.getObjValue());

     

    This gives me the values in scripting log. However, I want to export the objective values per iteration into an excel file directly. the above code you provided is for exporting the variables which has already indices. But the iteration index I have here "n" is defined in my main for reading different data. I wanna know how to export each iteration objective value into csv file within the same loop as reading data?

     

    ~Reagrds,

    Kamran


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to export into a csv file ?

    Posted Sat December 15, 2018 12:34 PM

    Hi

    why not writing the obj in a csv file

    and then you could open that with excel 

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: How to export into a csv file ?

    Posted Thu August 08, 2019 07:28 AM

    Originally posted by: Kamran_Sr


    Hi Alex,

    I have a model in script which has a loop to run the model for different scenarios. I want to export the value of a dexpr float to a csv file at each iteration. I have used the above code, but what I get in csv file is undefined.

     

    Could you pleas advise me how I can export dexpr into csv file from opl script?

     

    Regards,

    Kamran


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: How to export into a csv file ?

    Posted Mon September 02, 2019 02:31 AM

    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


    #DecisionOptimization
    #OPLusingCPLEXOptimizer