Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

Output computational times in scripting log

  • 1.  Output computational times in scripting log

    Posted 05/17/18 03:59 AM

    Originally posted by: Paroth


    Hello,

    I have 4 different run configurations and mod files with different parameters (for 1/2/3/4 runways), each of them pulling data from the same 8 .data input files (Airland_01-08). I already figured out how to output the optimal values for each .mod using a main block and instances, however, I am asking myself if there is a possibility to also output the computational time elapsed for solving each iteration. Basically, i want the output in the scripting log to be something like this (Example of mod file with r=2):

    Solution with 2 runways:
    Airland_01 - Total Penalty Cost: 90 - Solved in: XYZ
    Airland_02 - Total Penalty Cost: 210 - Solved in: XYZ
    Airland_03 - Total Penalty Cost: 60 - Solved in: XYZ
    Airland_04 - Total Penalty Cost: 640 - Solved in: XYZ
    Airland_05 - Total Penalty Cost: 650 - Solved in: XYZ
    Airland_06 - Total Penalty Cost: 554 - Solved in: XYZ
    Airland_07 - Total Penalty Cost: 0 - Solved in: XYZ
    Airland_08 - Total Penalty Cost: 135 - Solved in: XYZ

    Is there a way to do this? I found the getTime() function, but i get NaN errors in the output when trying to implement it. I copied the code of one mod file below (as the other mod files look very similar). If you need more information on the cplex files, just let me know.

    Thank you in advance.

     

     

    //SETS, PARAMETERS, ARRAYS
    int maxP = ...;
    int maxR = 2;
     
    range I = 1..maxP;
    range J = 1..maxP;
    range R = 1..maxR;
     
    int E[I] = ...;
    int L[I] = ...;
    int T[I] = ...;
     
    int S[I][J] = ...;
    int s[I][J] = ...;
     
    int g[I] = ...;
    int h[I] = ...;

    //SUBSET/TUPLES
    tuple SubsetW{
        int i;
        int j;
    }
    {SubsetW} W = {<i, j> | i, j in I: L[i] < E[j] && L[i] + S[i][j] <= E[j] && i != j};

    tuple SubsetV{
        int i;
        int j;
    }
    {SubsetV} V = {<i, j> | i, j in I: L[i] < E[j] && L[i] + S[i][j] > E[j] && i != j};

    tuple SubsetU{
        int i;
        int j;
    }
    {SubsetU} U = {<i, j> | i, j in I: E[j] <= E[i] <= L[j] || E[j] <= L[i] <= L[j] || E[i] <= E[j] <= L[i] || E[i] <= L[j] <= L[i] && j!=i};

    tuple SubsetUU{
        int i;
        int j;
    }
    {SubsetUU} UU = {<i, j> | <i, j> in U: E[j] + S[j][i] > L[i]};

    tuple SubsetUUU{
        int i;
        int j;
    }
    {SubsetUUU} UUU = {<i, j> | <i, j> in U: E[j] + s[j][i] > L[i]};

    //DECISION VARIABLES
    dvar int+ alpha[I];
    dvar int+ beta[I];
    dvar int+ x[I];
    dvar int+ z[I][J] in 0..1;
    dvar int+ y[I][R] in 0..1;
    dvar int+ delta[I][J] in 0..1;
     
    //DECLARATION CONSTRAINTS
    constraint ctWindow[I];             //1
    constraint ctDelta[I][J];             //2
    constraint ctSubsetW[W];             //6
    constraint ctSubsetV[V];            //6
    constraint ctAlpha1[I];                //14
    constraint ctAlpha2[I];                //15
    constraint ctBeta1[I];                //16
    constraint ctBeta2[I];                //17
    constraint ctAlphaBeta[I];            //18
    constraint ctRunway1[I];            //28
    constraint ctRunway2[I][J][R];        //29
    constraint ctSymmetry[I][J];        //30
    constraint ctPrecedence1[V];        //31
    constraint ctSeparation[I][J];        //33
    constraint ctDeltaSetting[U];        //40
    constraint ctDeltaSum;                //41
    constraint ctGapClosing[U];            //42
    constraint ctMinimumDeviation[U];    //43
    constraint ctPrecedence2[UU];        //47
    constraint ctPrecedence3[UUU];        //48

    //OBJECTIVE FUNCTION
    dexpr float TotalPenaltyCost = sum(i in I) (g[i] * alpha[i] + h[i] * beta[i]);
    minimize TotalPenaltyCost;
     
    //CONSTRAINTS
    subject to{
     
    //1
     forall(i in I) ctWindow[i]:
         E[i] <= x[i] <= L[i];

    //2
     forall(i in I, j in J : j > i) ctDelta[i][j]:
        delta[i][j] + delta[j][i] == 1;

    //6
     forall(s in W) ctSubsetW[s]:
         delta[s.i][s.j] == 1;
     forall(s in V) ctSubsetV[s]:
         delta[s.i][s.j] == 1;

    //14
     forall(i in I)    ctAlpha1[i]:
           alpha[i] >= T[i] - x[i];
           
    //15
     forall(i in I) ctAlpha2[i]:
          0 <= alpha[i] <= T[i] - E[i];
          
    //16   
     forall(i in I) ctBeta1[i]:
           beta[i] >= x[i] - T[i];
           
    //17   
     forall(i in I)    ctBeta2[i]:
           0 <= beta[i] <= L[i] - T[i];
           
    //18
     forall(i in I) ctAlphaBeta[i]:
           x[i] == T[i] - alpha[i] + beta[i];

    //28
     forall(i in I) ctRunway1[i]:
           sum(r in R) y[i][r] == 1;
           
    //29  
     forall(i in I, j in J : j > i) ctSymmetry[i][j]:
           z[i][j] == z[j][i];

    //30
     forall (i in I, j in J, r in R : j > i) ctRunway2[i][j][r]:
           z[i][j] >= y[i][r] + y[j][r] - 1;

    //31
     forall(v in V) ctPrecedence1[v]:
         x[v.j] >= x[v.i] + S[v.i][v.j] * z[v.i][v.j] + s[v.i][v.j] * (1-z[v.i][v.j]);

    //33       
     forall(i in I, j in J: j != i) ctSeparation[i][j]:
         x[j] >= x[i] + S[i][j] * z[i][j] + s[i][j] * (1 - z[i][j]) - (L[i] + maxl(S[i][j], s[i][j]) - E[j]) * delta[j][i];
     
    // RELAXATION
    //40
     forall(u in U) ctDeltaSetting[u]:
         delta[u.i][u.j] >= (x[u.j]-x[u.i]) / (L[u.j] - E[u.i]);
     
    //41
    sum(i in I, j in J: j != i) delta[i][j] == maxP*(maxP - 1)/2;

    //42
     forall(u in U: T[u.i] < T[u.j]) ctGapClosing[u]:
         delta[u.i][u.j] >= 1 - (beta[u.i] + alpha[u.j]) / (T[u.j] - T[u.i]);

    //43
     forall(u in U: T[u.i] < T[u.j] && (T[u.j] - T[u.i] < S[u.i][u.j])) ctMinimumDeviation[u]:
         (alpha[u.i] + beta[u.i]) + (alpha[u.j] + beta[u.j]) >=
         (S[u.i][u.j] - (T[u.j] - T[u.i])) * delta[u.i][u.j] +
         ((T[u.j] - T[u.i]) + S[u.j][u.i]) * delta[u.j][u.i] -
         maxl((S[u.i][u.j] - (T[u.j] - T[u.i])), ((T[u.j] - T[u.i]) + S[u.j][u.i])) * (1-z[u.i][u.j]);
     
    //45
     forall(uu in UU) ctPrecedence2[uu]:
          delta[uu.j][uu.i] + z[uu.i][uu.j] <= 1;
     
    //47
     forall(uuu in UUU) ctPrecedence3[uuu]:
          delta[uuu.j][uuu.i] + (1 - z[uuu.i][uuu.j]) <= 1;
     
    //48 MISSING
    }

    main{
    writeln("Solution with 2 runways:");
    var src = new IloOplModelSource("2 Runways.mod");
    var def = new IloOplModelDefinition(src);
    var iteration=1;
    while(iteration<=8){
            var opl = new IloOplModel(def,cplex);
            var filename="Airland_0"+iteration;
            var data = new IloOplDataSource(filename+".dat");
            opl.addDataSource(data);
            var details=opl.dataElements;
            opl.generate();
            if(cplex.solve()){
                writeln(filename+" - Total Penalty Cost: "+cplex.getObjValue());
            }
            else{
                writeln(filename+" - Total Penalty Cost: N/A");
            }
            iteration++
        }
    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Output computational times in scripting log

    Posted 05/17/18 05:13 AM

    Hi,

    why do not you use

    cplex.getSolvedTime()

    ?

    // --------------------------------------------------------------------------
    // Licensed Materials - Property of IBM
    //
    // 5725-A06 5725-A29 5724-Y48 5724-Y49 5724-Y54 5724-Y55
    // Copyright IBM Corporation 1998, 2013. All Rights Reserved.
    //
    // Note to U.S. Government Users Restricted Rights:
    // Use, duplication or disclosure restricted by GSA ADP Schedule
    // Contract with IBM Corp.
    // --------------------------------------------------------------------------

    // The scalable warehouse example has been artificially increased in size
    // so that the search is long enough for you to have time to interrupt it and look at feasible solutions.
    // The resulting size is greater than the size allowed in trial mode.
    // If you want to run this example, you need a commercial edition of CPLEX Studio to run this example.
    // If you are a student or teacher, you can also get a full version through the IBM Academic Initiative.

    //int n=...;
    //int nbthreads=...;
    int Fixed        = 100;
    int NbWarehouses = 200;
    int NbStores     = 400;


    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;
    dvar float obj;
    minimize obj;
    subject to {
    ctObj:obj==TotalFixedCost + TotalSupplyCost;


      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("Open=",Open);
    //}


    main
    {
    thisOplModel.generate();
    var o=new IloOplOutputFile("log.txt")
    cplex.intsollim=1;
    for(var i=1;i<=10;i++)
    {
        

        cplex.solve();
        thisOplModel.postProcess();
        o.writeln("sol ",i);
        o.writeln("getBestObjValue = ",cplex.getBestObjValue());
        o.writeln("getObjValue = ",cplex.getObjValue());
        
        o.writeln("solved time = ", cplex.getSolvedTime());
        
        //o.writeln("Open = ",thisOplModel.Open);
        o.writeln();
        
    }
    o.close();
    }

     

    gives log.txt

    sol 1
    getBestObjValue = 18200
    getObjValue = 18440
    solved time = 0.547

    sol 2
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0.14

    sol 3
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 4
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 5
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 6
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 7
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 8
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 9
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0

    sol 10
    getBestObjValue = 18200
    getObjValue = 18200
    solved time = 0.047

     

    regards

     

    https://www.linkedin.com/pulse/ist-mathematische-optimierung-und-wie-kann-sie-helfen-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer