Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Flow Control with changing dat.file

    Posted 07/11/16 07:31 AM

    Originally posted by: Ashalor


    Hi and again thanks for any help I get.

     

    Question: How can i rerun a model several times, using new parameters and at the same time take into account the value of a decision variable from the last iteration?

    The backround: I want to simulate a rolling horizon approach. The model is solved for 5 weeks, however after 2 weeks i want to run the model again, because e.g sales prices changed. Therfore i want resolve the problem with the new sales price and the inventory i had in week 2.

    Since my original model takes to long to solve I use a very easy model here, which is solved 3 times.

     

    mod:

    main{
      thisOplModel.generate();
      var counter = 1;
      var curr = Infinity;
      
      while(counter<=3){
        if (cplex.solve()) {
            curr= cplex.getObjValue();
            writeln("Objective=", curr);
            }      
        else{
          writeln("Error");
          break;
          }
          counter++;
        }
    }  

      int maxT= ...;
     range T = 1..maxT;
     
     /*Parameters*/
     int D[T]=...;
     int Cap = ...;
     float ci = ...;
     float cp = ...;
     float pd[T] = ...;
     float StartInv=...;

     
     /*dec variables*/
     dvar int+ x[T];
     dvar int+ i[0..maxT];
     dvar int+ d[T];
     
     /*constraints*/
     constraint ctDemand[T];
     constraint ctCapacity[T];

      
     dexpr float Profit = sum(t in T)d[t]*pd[t] - sum(t in T) i[t]*ci - sum(t in T) x[t]+cp;
     
     maximize Profit;
     
     subject to{
       
      forall (t in T)
        ctDemand[t]:
        d[t]<=D[t];
        
      forall (t in T)
        ctCapacity[t]:
        x[t]<=Cap;
        
      forall (t in T)
        ctInvBalance:
        i[t]+d[t] == i[t-1]+x[t];
      
        ctInventory1:
        i[0] == StartInv;   
     }  

     

    dat:

    maxT = 5; 
    Cap = 100;
    ci = 0.1;
    cp = 1.2;
    SheetConnection Data ("testExcel.xlsx");

    pd from SheetRead (Data, "testpd"); /*contains pd=2 for all t*/

    D = [50, 90, 120, 110, 130];
    StartInv = 0;


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Flow Control with changing dat.file



  • 3.  Re: Flow Control with changing dat.file

    Posted 07/12/16 07:38 AM

    Originally posted by: Ashalor


    That already helped a lot. I'm just wondering, why I didn't found that topic myself. However I still have a question.

     

    I finally managed to run the model several times, using new data I provide(thanks for that). What I still don't know is the command to return the value of my Inventory in period. You can see the problem highlighted in my new main code.

    There are 2 .dat files now of course . 1 Containing StartInv = 0;  and 1 containing all other parameters, which stay the same over all iterations.

     

    main{
      var source = new IloOplModelSource("test2.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      
      var globalValue = 0;

      var curr = Infinity;
      
      for(var counter=1; counter<=3; counter++){
        var opl = new IloOplModel(def, cplex);
        var data = new IloOplDataSource("test2.dat");
        opl.addDataSource(data);
        
        var data2= new IloOplDataElements();    
        data2.StartInv=globalValue;
        
        opl.addDataSource(data2);
        
        opl.generate();
        
        if (cplex.solve()) {
            curr= cplex.getObjValue();
            writeln("Objective=", curr);
            globalValue= ............... ;   // cplex.getValue().i[2] obviously doesn't work :)
            }      
        else{
          writeln("Error");
          break;
          }

        }
        opl.end();
        data.end(); 
          def.end(); 
          cplex.end(); 
          source.end();
    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Flow Control with changing dat.file

    Posted 07/13/16 09:07 AM

    Hi,

    to go on with the example I provided:

    sub.mod

    float maxOfx = ...;
    dvar float x[1..10];

    maximize sum (i in 1..10) x[i];
    subject to {
      forall(i in 1..10) x[i]<=maxOfx;
    }

    and then the main model

    main {
      var source = new IloOplModelSource("sub.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      var opl = new IloOplModel(def,cplex);
     
     var globalValue=0;
     
      for(var k=11;k<=15;k++)
      {
      var opl = new IloOplModel(def,cplex);
        
      var data2= new IloOplDataElements();
      data2.maxOfx=globalValue;

      opl.addDataSource(data2);
      opl.generate();

      if (cplex.solve()) {
         writeln("OBJ = " + cplex.getObjValue());
         globalValue=10+opl.x[2].solutionValue;
         writeln("globalValue = " ,globalValue);  
      } else {
         writeln("No solution");
      }
    data2.end();
     opl.end();
     
     
    }  
     
    }

    gives

    OBJ = 0
    globalValue = 10
    OBJ = 100
    globalValue = 20
    OBJ = 200
    globalValue = 30
    OBJ = 300
    globalValue = 40
    OBJ = 400
    globalValue = 50

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Flow Control with changing dat.file

    Posted 07/14/16 06:52 AM

    Originally posted by: Ashalor


    Hi Alexander

     

    thank you very much for your help. Thanks to you (and also a bit to some websites giving basics about javascript) I managed to solve the problem.

    The model is now solved multiple times. 

    A decision(i[2]) variable aquired in iteration x-1 is now used in iteration x

    A parameter array (sp([t]) is substituted each iteration with a new array.

     

    Just in case anyone encounters the same problem and has as much experience as me, i add my code. Even if I assume, that there are much more elegant solutions.

     

    main{
      var source = new IloOplModelSource("test.mod");
      var cplex = new IloCplex();
      var def = new IloOplModelDefinition(source);
      
      var globalValue = 0;

      var curr = Infinity;
      
      var subSupplyCost1 = new Array (0, 1.2, 1.3, 1.4, 1.5, 1.6);
      var subSupplyCost2 = new Array (0, 1, 1, 1, 1, 1);
      var subSupplyCost3 = new Array (0, 1.3, 1.3, 1.3, 1.3, 1.3);
      var SupplyCost = new Array (0, subSupplyCost1, subSupplyCost2, subSupplyCost3);
      
      for(var counter=1; counter<=3; counter++){
        var opl = new IloOplModel(def, cplex);
        var data = new IloOplDataSource("test.dat");
        opl.addDataSource(data);
        
        var data2= new IloOplDataElements();    
        data2.StartInv=globalValue;
        
        data2.sp=thisOplModel.tArray;
      
          for(var time=1; time<=5; time++){  
        data2.sp[time]=SupplyCost[counter][time];
       }        
       
        opl.addDataSource(data2);
        
        opl.generate();
        
        if (cplex.solve()) {
              writeln ("StartInv=", globalValue);
            curr= cplex.getObjValue();
            writeln("Objective=", curr);
            
            globalValue=opl.m;
            writeln ("new StarInv=",globalValue);
            writeln("i[2]=", opl.c);
            writeln("i[3]=", opl.d2);
            writeln("sp=", opl.sp);
            }      
        else{
          writeln("Error");
          break;
          }

        }
        opl.end();
        data.end(); 
          def.end(); 
          cplex.end(); 
          source.end();
    }


     int maxT= ...;
     range T = 1..maxT;
     
     /*Parameters*/
     int D[T]=...;
     int Cap = ...;
     float ci = ...;
     float sp[T] = ...;
     float pd[T] = ...;
     float StartInv=...;
     float tArray[T];
     
     /*dec variables*/
     dvar int+ x[T];
     dvar int+ i[0..maxT];
     dvar int+ d[T];
     
     /*constraints*/
     constraint ctDemand[T];
     constraint ctCapacity[T];

      
     dexpr float Profit = sum(t in T)d[t]*pd[t] - sum(t in T) i[t]*ci - sum(t in T) x[t]*sp[t];
     
     maximize Profit;
     
     subject to{
       
      forall (t in T)
        ctDemand[t]:
        d[t]<=D[t];
        
      forall (t in T)
        ctCapacity[t]:
        x[t]<=Cap;
        
      forall (t in T)
        ctInvBalance:
        i[t]+d[t] == i[t-1]+x[t];
      
        ctInventory1:
        i[0] == StartInv; 
    }

     

    float m = i[1];
    float a = i[0];
    float b = i[1];
    float c = i[2];
     

    test.dat:

    maxT = 5; 
    Cap = 100;
    ci = 0.1;

    SheetConnection Data ("testExcel.xlsx");

    pd from SheetRead (Data, "testpd");                         //contains pd=2 for all t

    D = [50, 90, 120, 110, 120];

     

    test2.dat:

    StartInv = 0;
    sp = [1.2, 1.2, 1.2, 1.2, 1.2];


    #DecisionOptimization
    #OPLusingCPLEXOptimizer