'Saving results of CPLEX with flow control (change data solve with regenerate)

After looking through stackoverflow + IBM forum + Alex Fleischer's How to Linkedin page, I manage to run my problem with script, changing a parameter at every iteration(flow control). However, I want to save the output of each iteration into a unique excel file. I read through the references, and manual/user guide but i am still not sure how to achieve it.

Before using the script, I used SheetWrite in the .dat file for each variable i want to save.

How can I do that while running the script so that it saves to a new excel file for every iteration? (I have a finite number of iterations about 7 runs so I just need 7 files each run)

Thanks in advance..

main{
 var status = 0;
 thisOplModel.generate();
 var produce = thisOplModel;
 var best;
 var curr = Infinity;
 var maxdisp = produce.allowedwindisp;

 var ofile = new IloOplOutputFile("testresult.txt");

 while ( maxdisp>=1 ) {
    best = curr;
    writeln();
    writeln("Solve with maxdisp = ",maxdisp);
    if ( cplex.solve() ) {
    curr = cplex.getObjValue();
    writeln("OBJECTIVE: ",curr);
    ofile.writeln("Objective with maxdisp = ", maxdisp, " is ", curr);        
    } 
    else {
      writeln("No solution!");
      break;
    }

    // prepare next iteration
    var def = produce.modelDefinition;
    var data = produce.dataElements;

    if ( produce!=thisOplModel ) {
    produce.end();
    }

    produce = new IloOplModel(def,cplex);
    maxdisp--;
    data.allowedwindisp = maxdisp;
    produce.addDataSource(data);
    produce.generate();
    }    
    ofile.close();
    status;}

this is what i did to change the variable at each iteration, but i am not sure how to link to write a new excel file from each iteration.

EDIT this is my solution to achieve a new excel file for each run with data changed (modified the file names to be more inline with Alex's examples): this is the main.mod, i will change the variable called delta in the code

    main{
 var status
 var source = new IloOplModelSource("sub.mod");
 var cplex = new IloCplex();
 var def = new IloOplModelDefinition(source);
 var best;
 var current = Infinity; 


   for(var delta = 0;delta<=3;delta++)
  {
    var opl = new IloOplModel(def,cplex);
    var data2= new IloOplDataElements();
    data2.fileName="reference_filename"+delta+".xlsx";
    opl.addDataSource(data2);
    var data1 = new IloOplDataSource("datafile4Input_param.dat");
    opl.addDataSource(data1);
    var data3 = new IloOplDataElements();
    data3.delta_from_reference_input = delta;
    opl.addDataSource(data3)

    opl.generate();
    best = current;
    writeln();
    writeln("Solve with maxdisp = ",maxdisp);
    cplex.tilim = 3600;

    if (cplex.solve()) {
       current = cplex.getObjValue();
       writeln("OBJECTIVE: ",current);
       opl.postProcess();

    } 
    else {
       writeln("No solution");
    }
    opl.end();
}  
    status;
}

hope it helps.



Solution 1:[1]

You should use SheetWrite in submodels dat part and do not forget to call opl.postProcess in the main script

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman