Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Problem with flow control

    Posted 08/25/15 08:18 AM

    Originally posted by: Cplex


    Hey!

     

    I have to run my model several times in different scenarios. I tried to so it with a flow control inside a main block as follows:

     

    main {
     
      var K = 92;
     
       var sum = 0.0;
       
       
         // Create the model and solve it
         var cplex = new IloCplex();
         var source = new IloOplModelSource("KH 3.mod");
         var def = new IloOplModelDefinition(source);
         var opl = new IloOplModel(def, cplex);
         var data = new IloOplDataSource("KH 3.dat");
         opl.addDataSource(data);
         opl.generate();
         for(var k in 1..K){
           for(var l in 1..K| l > k){
                 for(var n in 1..K| n >l){
                   z[k] == 0;
                   z[l] == 0;
                   z[n] == 0;
                   cplex.solve();
                   if (cplex.solve()) {
                     writeln("OBJ = " + cplex.getObjValue());
                   } else {
                     writeln("No solution");
                 }}}}
       
         
         // Cleanup
         opl.end();
         data.end();
         def.end();
         source.end();
         cplex.end();
    }

     

    The model runs but then says that there's no solution and I don't even know if the idea of flow control is the right approach.

    I attached both my model and my data file as well as the model file containing the main block.

     

    I really need some help in this case.

    Thanks in Advance!


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Problem with flow control

    Posted 08/25/15 10:43 AM

    Hi,

    1) You have not attached any excel files that are needed

    2)

     

    z[k] == 0;

     

    z[l] == 0;

     

    z[n] == 0;

    not allowed in the scripting part.

    3) You may have a look at an example such as https://www.ibm.com/developerworks/community/forums/html/topic?id=bf6ac1b9-1fd3-4b33-92e1-4a97d1a314ef&ps=25

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Problem with flow control

    Posted 08/26/15 03:11 AM

    Originally posted by: Cplex


    Hey.

     

    I attached the missing excel files.

     

    But isn't it possible to run several scenarios with different variables without defining a new data set and a new model for each scenario in the main block?

    I my case the model should generate the scenarios on its own because I have almost 1000 scenarios and it's just one variable (z) that changes in each scenario. I mean the generation of the new data set and the model should be inside a forall or while loop?

     

    Thanks in advance!


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Problem with flow control

    Posted 08/26/15 06:20 AM

    Hi,

    yes you can do that.

    Let me give you a small example:

    dvar float x in 0..10 ;
    dvar float y;

    maximize x;
    subject to
    {
    y==x+1;
    }

    execute
    {
    writeln("y=",y);
    }

    main
    {
    thisOplModel.generate();

    for(var i=1;i<=10;i++)
    {
     write("i=",i," ==> ");
     thisOplModel.x.LB=i;
     thisOplModel.x.UB=i;
     cplex.solve();
     thisOplModel.postProcess();
    }

    }

    that gives

    i=1 ==> y=2
    i=2 ==> y=3
    i=3 ==> y=4
    i=4 ==> y=5
    i=5 ==> y=6
    i=6 ==> y=7
    i=7 ==> y=8
    i=8 ==> y=9
    i=9 ==> y=10
    i=10 ==> y=11

    regards

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Problem with flow control

    Posted 08/26/15 08:36 AM

    Originally posted by: Cplex


    I can understand this example.

     

    But in my case I have to change the index of decision variable z in each of the scenarios. So three of the z[k] have to be equal to zero while the other ones should be equal to one. I tried to apply your example to this case but I don't know how to do that.

     

    I tried it as follows, but there's an error:

     


    //Indizes

    range I =1..115;            //RD: 115
    range J =1..3766;            //Häuser: 1635
    range K =1..92;                //KH: 92
    range S =1..23;                //RTW: 23


    //Datenvariablen

    float t[J][I] =...;        //Distanz RD-Haus
    float c[J][K] =...;        //Distanz KH-Haus
    int KH =...;            //Anzahl KH
    int RD =...;            //Anzahl RD
    int RTW =...;            //Anzahl RTW
    float a =...;            //Wahrscheinlichkeit, dass RTW nicht verfügbar ist
    int d =...;                //Einwohner/Haus
    int p[J][I] =...;        //Zuordnung RD möglich?
    int q[J][K] =...;        //Zuordnung KH möglich?


    //Entscheidungsvariablen

    dvar boolean Cov[J][S];    //=1,falls Haus j von mind. s Fahrzeugen und einem KH abgedeckt ist
    dvar boolean y[I];        //=1,falls RD i eröffnet wird
    dvar boolean z[K];        //=1,falls KH k eröffnet wird
    dvar int+ m[I];            //Anzahl stationierter RTW an RD i
        
    dvar int e in 1..92;
    dvar int f in 1..92;
    dvar int g in 1..92;


    //Zielfunktion

    dexpr float Covering = sum(j in J, s in S) Cov[j][s] * d * (1-a) * a^(s-1);
    maximize Covering;

     
    //Nebenbedingungen

    subject to{
     
          z[e] == 0;
          z[f] == 0;
          z[g] == 0;
          
        Opening_RD:
        sum(i in I) y[i] == RD;
        
        Opening_KH:
        sum(k in K) z[k] == KH;
        
        Covering_KH:
        forall(j in J,s in S)
          Cov[j][s] <= sum(k in K) q[j][k] * z[k];
          
        Opening_KH_q_jk:
        forall(k in K)
          z[k] <= sum(j in J) q[j][k];
          
        Covering_RD:
        forall(j in J)
          sum(s in S) Cov[j][s] <= sum(i in I) p[j][i] * m[i];
        
        RD_RTW:    
        forall(i in I)
          y[i]*RTW >= m[i];
          
        RTW_stationieren:
        sum(i in I) m[i] == RTW;                                                            
    }

    main
    {
    thisOplModel.generate();
    for(var o=1;o<=92;o++){
    for(var l=1;l<=92;l++){
    for(var n=1;n<=92;n++){
     
     thisOplModel.e.LB=o;
     thisOplModel.e.UB=o;
     thisOplModel.f.LB=l;
     thisOplModel.f.UB=l;
     thisOplModel.g.LB=n;
     thisOplModel.g.UB=n;
     cplex.solve();
     thisOplModel.postProcess();
    }
    }}}

     

     

    But it's not possible to equalize the three decision variables (z) to zero inside the constraints.

     

    Do you understand what's my problem? Is it possible to change the Index for these constraints in each szenario??


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Problem with flow control

    Posted 08/26/15 09:20 AM

    Hi,

    z[e]==0

    in your model is not good since e is a dvar.

    But let me give you an example:

    dvar float x[1..10] in 0..10 ;
    dvar float y;

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

    execute
    {

    write("x=",x," ");
    writeln("y=",y);
    }

    main
    {
    thisOplModel.generate();

    for(var i=1;i<=10;i++)
    for(var j=i+1;j<=10;j++)
    {
     write("i=",i," j=",j," ==> ");
     for(var a=1;a<=10;a++)
     {
      thisOplModel.x[a].LB=0;
     thisOplModel.x[a].UB=10;
     }
     thisOplModel.x[i].LB=0;
     thisOplModel.x[i].UB=0;
     thisOplModel.x[j].LB=0;
     thisOplModel.x[j].UB=0;
     cplex.solve();
     thisOplModel.postProcess();
    }

    }

    which gives

    i=1 j=2 ==> x= [0 0 10 10 10 10 10 10 10 10] y=80
    i=1 j=3 ==> x= [0 10 0 10 10 10 10 10 10 10] y=80
    i=1 j=4 ==> x= [0 10 10 0 10 10 10 10 10 10] y=80
    i=1 j=5 ==> x= [0 10 10 10 0 10 10 10 10 10] y=80
    i=1 j=6 ==> x= [0 10 10 10 10 0 10 10 10 10] y=80
    i=1 j=7 ==> x= [0 10 10 10 10 10 0 10 10 10] y=80
    i=1 j=8 ==> x= [0 10 10 10 10 10 10 0 10 10] y=80
    i=1 j=9 ==> x= [0 10 10 10 10 10 10 10 0 10] y=80
    i=1 j=10 ==> x= [0 10 10 10 10 10 10 10 10 0] y=80
    i=2 j=3 ==> x= [10 0 0 10 10 10 10 10 10 10] y=80
    i=2 j=4 ==> x= [10 0 10 0 10 10 10 10 10 10] y=80
    i=2 j=5 ==> x= [10 0 10 10 0 10 10 10 10 10] y=80
    i=2 j=6 ==> x= [10 0 10 10 10 0 10 10 10 10] y=80
    i=2 j=7 ==> x= [10 0 10 10 10 10 0 10 10 10] y=80
    i=2 j=8 ==> x= [10 0 10 10 10 10 10 0 10 10] y=80
    i=2 j=9 ==> x= [10 0 10 10 10 10 10 10 0 10] y=80
    i=2 j=10 ==> x= [10 0 10 10 10 10 10 10 10 0] y=80
    i=3 j=4 ==> x= [10 10 0 0 10 10 10 10 10 10] y=80
    i=3 j=5 ==> x= [10 10 0 10 0 10 10 10 10 10] y=80
    i=3 j=6 ==> x= [10 10 0 10 10 0 10 10 10 10] y=80
    i=3 j=7 ==> x= [10 10 0 10 10 10 0 10 10 10] y=80
    i=3 j=8 ==> x= [10 10 0 10 10 10 10 0 10 10] y=80
    i=3 j=9 ==> x= [10 10 0 10 10 10 10 10 0 10] y=80
    i=3 j=10 ==> x= [10 10 0 10 10 10 10 10 10 0] y=80
    i=4 j=5 ==> x= [10 10 10 0 0 10 10 10 10 10] y=80
    i=4 j=6 ==> x= [10 10 10 0 10 0 10 10 10 10] y=80
    i=4 j=7 ==> x= [10 10 10 0 10 10 0 10 10 10] y=80
    i=4 j=8 ==> x= [10 10 10 0 10 10 10 0 10 10] y=80
    i=4 j=9 ==> x= [10 10 10 0 10 10 10 10 0 10] y=80
    i=4 j=10 ==> x= [10 10 10 0 10 10 10 10 10 0] y=80
    i=5 j=6 ==> x= [10 10 10 10 0 0 10 10 10 10] y=80
    i=5 j=7 ==> x= [10 10 10 10 0 10 0 10 10 10] y=80
    i=5 j=8 ==> x= [10 10 10 10 0 10 10 0 10 10] y=80
    i=5 j=9 ==> x= [10 10 10 10 0 10 10 10 0 10] y=80
    i=5 j=10 ==> x= [10 10 10 10 0 10 10 10 10 0] y=80
    i=6 j=7 ==> x= [10 10 10 10 10 0 0 10 10 10] y=80
    i=6 j=8 ==> x= [10 10 10 10 10 0 10 0 10 10] y=80
    i=6 j=9 ==> x= [10 10 10 10 10 0 10 10 0 10] y=80
    i=6 j=10 ==> x= [10 10 10 10 10 0 10 10 10 0] y=80
    i=7 j=8 ==> x= [10 10 10 10 10 10 0 0 10 10] y=80
    i=7 j=9 ==> x= [10 10 10 10 10 10 0 10 0 10] y=80
    i=7 j=10 ==> x= [10 10 10 10 10 10 0 10 10 0] y=80
    i=8 j=9 ==> x= [10 10 10 10 10 10 10 0 0 10] y=80
    i=8 j=10 ==> x= [10 10 10 10 10 10 10 0 10 0] y=80
    i=9 j=10 ==> x= [10 10 10 10 10 10 10 10 0 0] y=80

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Problem with flow control

    Posted 08/26/15 04:50 PM

    Originally posted by: Cplex


    Yes, now it's working fine. Thank you so much!!


    #DecisionOptimization
    #OPLusingCPLEXOptimizer