Decision Optimization

Expand all | Collapse all

Creating scenarios for stochastic problem

ALEX FLEISCHERThu April 09, 2020 06:30 AM

Archive UserMon April 13, 2020 05:49 PM

  • 1.  Creating scenarios for stochastic problem

    Posted Thu April 09, 2020 05:28 AM

    Originally posted by: ClaraD


    Hi,

    I am having trouble creating scenarios in a stochastic problem.
    Let's say I have two objects and each of them can have two values. What I want is to create four scenarios where I have these values combined. 

    I had thought about having a variable  like this:
    int w[Objects][Weights] = [[7,8], [6,7]]; //Objects' weights

    In the first scenario I take the values 7 and 6.
    In the second scenario I take the values 7 and 7.
    In the third scenario I take the values 8 and 6.
    In the fourth scenario I take the values 8 and 7.

    but I don't know how to select these scenarios. In the constraints I had to manually create them, but this is an easy example and I would like to increase the number of objects and its values.

    Here is what I am trying:

    ...
    range scenarios = 1..(NbObjects*NbWeights); //in this case it's 2*2
    dvar boolean x[Objects];//if positive then it goes into the knapsack
    dvar float+ z[scenarios];
    dvar float+ y[scenarios];
    int b = 14; //knapsack capacity
    ...
    subject to {
    ct1: //7-6 13
         z[1] >= (w[1][1]*x[1]+w[2][1]*x[2])-b; //13-14 -1
         z[1] >= -(w[1][1]*x[1]+w[2][1]*x[2])+b; //-13+14 1
         y[1] == (((w[1][1]*x[1]+w[2][1]*x[2])-b) + z[1])/2; //I only want the positive ones

         ct2: //7-7 14
         z[2] >= (w[1][1]*x[1]+w[2][2]*x[2])-b;
         z[2] >= -(w[1][1]*x[1]+w[2][2]*x[2])+b;
         y[2] == (((w[1][1]*x[1]+w[2][2]*x[2])-b) + z[2])/2;
         ct3: ...
         ct4: ....


         My question is:
         Is this the right way to store the different objects and values? Is there a way to put the constraints in a loop?

     Thank you so much for your help.

         PS: I have been reading in this forum for a while to find for possible solutions and I see that the site should be theoretically down, but I cannot find the equivalent of this forum in the new IBM site.

     



  • 2.  Re: Creating scenarios for stochastic problem



  • 3.  Re: Creating scenarios for stochastic problem

    Posted Mon April 13, 2020 05:49 PM

    Originally posted by: ClaraD


    Thank you Alex,

     

    I had already looked at this example that you provided. I've combined this example with another one you wrote about tuples:https://www.ibm.com/developerworks/community/forums/html/topic?id=9748cb9c-fa58-471a-8f9b-fba546df609b&ps=25

    I am now trying to adapt my code to see if by saving the data in tuples I can find a solution. I am confident that this is the good way to go, thanks for pointing me in this direction.

     

    Regards,

    Clara

     

    PS: Just in case it's useful to someone I'm gonna leave the code about the stochastic buses with tuples here:

     

    int nbKids=300;
    {int} nbKidsScenarii={nbKids+i*10 | i in -10..2};
    float proba[nbKidsScenarii]=[ 1, 1, 2, 2, 2 ,3 ,3 ,4,  5 ,10 ,50 ,10, 7];
    assert sum(s in nbKidsScenarii) proba[s]==100; // total probability is 100
    tuple bus{
    key int nbSeats;
    float cost;}

    // This is a tuple set
    {bus} buses={<40,500>,<30,400>};
    // asserts help make sure data is fine
    assert forall(b in buses) b.nbSeats>0;
    assert forall(b in buses) b.cost>0;

    float costIncreaseIfLastMinute=1.1;

    // number of buses booked in advance
    // decision variable array
    dvar int+ nbBus[buses];
    // number of buses booked at the last minute which is far more expensive
    // we call those recourse decision
    dvar int+ nbBusonTop[buses][nbKidsScenarii] ;

    minimize
       sum(b in buses) b.cost*nbBus[b]
     +
    1/100*costIncreaseIfLastMinute*
    sum(a in nbKidsScenarii,b in buses) proba[a]*b.cost*(nbBusonTop[b][a]);
     
    subject to
    {
     forall(a in nbKidsScenarii)
       (sum(b in buses) b.nbSeats*(nbBus[b]+nbBusonTop[b][a]))>=a;
    }

     

    Results:

     

    nbBus = [6
             0];
    nbBusonTop = [[0 0 0 0 0 0 0 0 1 0 0 1 2]
                 [0 0 0 0 0 1 1 1 0 2 2 1 0]];