Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Writing Solution into Excel

    Posted 09/15/18 12:23 PM

    Originally posted by: open_ball


    Hi,

     

    Suppose I have the following trivial model. 

    dvar int+ x[1..10];

    minimize sum(i in 1..10 )x[i];
    subject to{
     forall(i in 1..10 ) x[i] >=0;
    }

     

    Then I call main black to run the model as follows;

     

         main{
     thisOplModel.generate();
    if (cplex.solve()) {
    var obj=cplex.getObjValue();
    }    
            }

     

    Then, I want to write the results into a csv file. However, I could not figure out where I should place the following code block.

     

    tuple d{
    int i;
    int amount;
    }
    {d} Ydata={<i , x[i]> | i in 1..10};
       
    execute
        {
        var f14 = new IloOplOutputFile("C:\\Desktop\\Y.csv");
        f14.writeln("from,","to,");    
        for(var j in Ydata)
        f14.writeln(j.i,",",j.amount);
        f14.close();
    }

    My next question is; if I have an iterative algorithm and want to only write the final solution into an excel file, how should I handle that?

     

    Thanks,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Writing Solution into Excel

    Posted 09/17/18 02:42 AM

    Hi,

    in order to call what is after the subject to block from the main you should call postProcess.

    You should turn

    main{

     thisOplModel.generate();
    if (cplex.solve()) {
    var obj=cplex.getObjValue();
    }    
            }

    into

    main{

     thisOplModel.generate();
    if (cplex.solve()) {
    var obj=cplex.getObjValue();

    thisOplModel.postProcess();

    }    
            }

    and if you want to export only the last solution you simply call postProcess at the last iteration

     

    regards

     

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


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Writing Solution into Excel

    Posted 09/24/18 08:30 PM

    Originally posted by: open_ball


    Hi Alex,

    I have the following structure;

    minimize {

    ........

    }

    {Tup_Y} add_y = {< i , j , a, t,  Y[<i, j, a,  t>]> | <i,j, a, t > in Ytup};

    {Tup_Y} add_master_f = {< c1 , c2 , a,t,  master_f[<c1, c2, a,  t>]> | <c1, c2, a, t > in Ytup};

    With this two, I pass information to my sub problem at every iteration.

     

    In the main, I have the following code block.

     

    if(masterCplex.solve()){
    masterOpl.postProcess();
    }

     

    Now, I want to write one of the variables into an Excel file during the last iteration. If I update my code as follows;

     

    {Tup_Y} add_y = {< i , j , a, t,  Y[<i, j, a,  t>]> | <i,j, a, t > in Ytup};

    {Tup_Y} add_master_f = {< c1 , c2 , a,t,  master_f[<c1, c2, a,  t>]> | <c1, c2, a, t > in Ytup};

     

      tuple forX {
       string a;    
       string c;   
       int t;
       int amount;
    }
       {forX} Xdata={<a,c, t,  X[<a, c,t>]> | a in Assets, c in Cities,t in 1..time, xx in Xtup:
       xx.a == a && xx.c ==c&& xx.t == t};
       
     
        execute
        {
        var f13 = new IloOplOutputFile("C:\\Desktop\\X.csv");
            f13.writeln("asset,","city,","time,","Amount");
        for(var i in Xdata)
      f13.writeln(i.a,",",i.c,",",i.t,",",i.amount);
        f13.close();
        }

     

    Then post process function calls all of them together at every iteration. How should I distinguish them? I want to call post process and send the required information to sub problem at every iteration, but I want to use the part for Excel only during the last iteration.

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Writing Solution into Excel

    Posted 09/25/18 02:58 AM

    Hi,

    you can write a test on cplex.status to decide what to do.

    Let me give you an example:

    int Fixed        = 100;
    int NbWarehouses = 200;
    int NbStores     = 600;


    assert( NbStores > NbWarehouses );

    range Warehouses = 1..NbWarehouses;
    range Stores     = 1..NbStores;
    int Capacity[w in Warehouses] =
      NbStores div NbWarehouses +
      w % ( NbStores div NbWarehouses );
    int SupplyCost[s in Stores][w in Warehouses] =
      1 + ( ( s + 10 * w ) % 100 );
    dvar int Open[Warehouses] in 0..1;
    dvar float Supply[Stores][Warehouses] in 0..1;
    dexpr int TotalFixedCost = sum( w in Warehouses ) Fixed * Open[w];
    dexpr float TotalSupplyCost = sum( w in Warehouses, s in Stores )  SupplyCost[s][w] * Supply[s][w];
    minimize TotalFixedCost + TotalSupplyCost;

    subject to {

     

      forall( s in Stores )
        ctStoreHasOneWarehouse:
          sum( w in Warehouses )
            Supply[s][w] == 1;
      forall( w in Warehouses )
        ctOpen:
          sum( s in Stores )
            Supply[s][w] <= Open[w] * Capacity[w];
    }

    execute
    {
    writeln("Obj=",TotalFixedCost + TotalSupplyCost);
    writeln("cplex.status=",cplex.status);
    }

     

    with settings to postprocess feasible solutions

     

     

    gives

     

    // feasible solution with objective 16890
    Obj=16890
    cplex.status=0
    // feasible solution with objective 16800
    Obj=16800
    cplex.status=0
    // solution (integer optimal, tolerance) with objective 16800
    Obj=16800
    cplex.status=102

     

    You may check cplex status meanings  at https://www.ibm.com/support/knowledgecenter/SSSA5P_12.8.0/ilog.odms.cplex.help/refcallablelibrary/macros/Solution_status_codes.html

     

    regards


    #DecisionOptimization