Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to create different sets given tuple sets

    Posted 11/10/16 12:08 AM

    Originally posted by: 黃紹軒


    HI, all

    I want to know how to create sets with a start number and ending number and the elements in sets are the numbers between the start number and the ending number.

    eg. here is the results I want

    itv[<1,8>]={2,3,4,5,6,7}

    itv[<4,9>]={5,6,7,8}

     

    Here is the code I wrote:

    int n=50;

    int f[i in 1..n-2]=i+2;

    tuple t

    { int i;int j;}
    {t}s={<i,j>|i in 1..n-2,j in (f[i]..n)};

    {int}itv[s]={e|e in s.i..s.j};

     

    Thanks for your help

    Begards,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to create different sets given tuple sets

    Posted 11/10/16 05:59 AM

    Hi,

    you could write

    int n=50;

    int f[i in 1..n-2]=i+2;

    tuple t

    { int i;int j;}
    {t}s={<i,j>|i in 1..n-2,j in (f[i]..n)};

    {int}itv[is in s]={e|e in is.i+1..is.j-1};

    {int} itv2[is in s]=asSet(is.i+1..is.j-1);

    {int} res=itv[<1,8>];
    {int} res2=itv2[<1,8>];


    execute
    {
    writeln(res);
    writeln(res2);
    }

    which gives

     

     {2 3 4 5 6 7}
     {2 3 4 5 6 7}

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to create different sets given tuple sets

    Posted 11/14/16 11:47 AM

    Originally posted by: 黃紹軒


    Hi Alex,

    Thank you very very much. You solved my problem.

    And if I want to use these sets on the constraint, how do I use them?

    The constraint's idea is to make sure binary decision variable Yij=1 if the sum of set of binary decision variable Xi equals 0. the set's elements are numbers  between i and j.

    e.g..

    if Y28=1, then X3+X4+x5+X6+X7=0. 

    if Y28=0, then X3+X4+x5+X6+X7 can be 0 or non zero.

     

    Here is the code I wrote:

    int n=50;
    int f[i in 1..n-2]=i+2;
    range time=1..n;<
    
    tuple t
    { int i;int j;}
    {t}select_pair={<i,j>|i in 1..n-2,j in (f[i]..n)};
    {int}itv[is in select_pair]={e|e in is.i+1..is.j-1};
    dvar boolean X[time];
    dvar boolean Y[select_pair];
    
    forall(i,j in select_pair)
        cons01:
            sum(mid in itv[<i,j>])X[mid]*Y[<i,j>]==0;
    

    Thanks for your help.

    Best regards,

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to create different sets given tuple sets

    Posted 11/14/16 11:54 AM

    Hi,

    you could write:

    using CP;

    int n=50;
    int f[i in 1..n-2]=i+2;
    range time=1..n;

    tuple t
    { int i;int j;};
    {t}select_pair={<i,j>|i in 1..n-2,j in (f[i]..n)};
    {int}itv[is in select_pair]={e|e in is.i+1..is.j-1};
    dvar boolean X[time];
    dvar boolean Y[select_pair];

    subject to
    {

    forall(<i,j> in select_pair)
        cons01:
            sum(mid in itv[<i,j>])X[mid]*Y[<i,j>]==0;
            
          }       

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: How to create different sets given tuple sets

    Posted 11/17/16 12:23 AM

    Originally posted by: 黃紹軒


    Hi,Alex,
    Thank you very very much again!But it still occurs error when running the interval constraint which is coding from line 30 to line 32. Is there something I type wrong in my code?
    And besides,I have another doubt about objective function--cost. Is the expression of objective function correct?
    Because I am not sure the number j of T[j][k] and the number i of P[i][k] are correspond to i,j of y[<i,j>].

    eg.

    when y[<i,j>]=y[<2,5>], then T[j][k]=T[5][k] and P[i][k]=P[2][k] 
    Here is the full code I wrote:
    int n=50;// no. of time_point
    int m=45;// no. of bus_stop
    
    range time=1..n;//
    range stop=1..m;//
    float T[time][stop]=...;
    float H[time][stop]=...;
    float P[time][stop]=...;
    int capacity=...;
    int F=...;
    int f[i in 1..n-2]=i+2;
    
    tuple pick{int start;int end;}
    {pick} pick_point={<i,j>|i in 1..n-1,j in i+1..n};
    {pick}select_pair={<i,j>|i in 1..n-2,j in (f[i]..n)};
    {int}itv[is in select_pair]={e|e in is.start+1..is.end-1};
    
    //variable
    dvar boolean x[time];
    dvar boolean y[pick_point];
    dexpr float cost= sum (<i,j> in pick_point, k in stop)
    y[<i,j>]*(((T[j][k]-T[i][k])/(T[n][k]-T[1][k]))/((capacity-P[i][k])/capacity));
    
    minimize cost;
    subject to {
            cons01:
                x[1]==1;
            cons02:
                    sum(i in time) x[i]==F;
            forall(<i,j> in select_pair)
                    cons03:
                            sum(mid in itv[<i,j>])x[mid]*y[<i,j>]==0;   
            forall(i in time)
                    cons04:
                            sum(j in stop,i in 1..i) x[i]*P[i][j]<=sum(d in stop,i in i..i) x[i]*H[i][d];
    }
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: How to create different sets given tuple sets

    Posted 11/17/16 03:46 AM

    Hi,

    can you attach your .dat too ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: How to create different sets given tuple sets

    Posted 11/17/16 04:21 AM

    Originally posted by: 黃紹軒


    Hi Alex,

    No problem. I also attach the excel file as well. Thought it might come in handy for you finding the bug.

    Best regards,

    Shao Hsuan


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: How to create different sets given tuple sets

    Posted 11/17/16 06:00 AM

    hi,

    can you try with

    using CP;

    at the beginning of your model ?

    And then can you double check

    forall(i in time)
                    cons04:
                            sum(j in stop,i in 1..i) x[i]*P[i][j]<=sum(d in stop,i in i..i) x[i]*H[i][d];

     

    Too many i! I think you should use other index like k!

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: How to create different sets given tuple sets

    Posted 11/23/16 09:24 AM

    Originally posted by: 黃紹軒


    Hi Alex, 

    Thanks for your advice, the code can run now. But, it had already run for over 9 hours. Is it still normal for that long running? 

    Or is there any coding skills I can use to alleviate the time of compiling the code?

    I also want to ask another question about the code of desicion variables. The relation of Xi and Yij is that the decision variable Yij is determined by the decision variable Xi.

    eg.

    if the optimal solution is X1=X3=X7=1, else Xi equal to 0,

    then Yij have to be Y13=Y37=1, else Yij equal to 0.

    The follow is the content of .mod file: 

    using CP;
    int n=...;// no. of time_point
    int m=...;// no. of bus_stop
    
    range time=1..n;//time window of a day operating bus
    range stop=1..m;//no. of bus_stop in the route
    
    float T[time][stop]=...;//time of arriving each stop
    float H[time][stop]=...;//adjusted passenger no. of each stop
    float P[time][stop]=...;//real passenger no. of each stop
    
    int capacity=...;//capacity of the bus
    int F=...;//frequency of bus in one day
    
    int f[i in 1..n-2]=i+2;
    tuple pick{int start;int end;}
    {pick} pick_point={<i,j>|i in 1..n-1,j in i+1..n};
    
    {pick}select_pair={<i,j>|i in 1..n-2,j in (f[i]..n)};
    
    {int}itv[is in select_pair]={e|e in is.start+1..is.end-1};
    
    //variable
    dvar boolean x[time];// determine at which time to departure the bus
    dvar boolean y[pick_point];//determine the pair of two adjacent buses to calculate the objective function
    
    dexpr float cost= sum (j in stop,<i,k> in pick_point) 
    y[<i,k>]*(((T[k][j]-T[i][j])/(T[n][j]-T[1][j]))/((capacity-P[i][j])/capacity));//objective function form 
    
    minimize cost;
    
    subject to {
        cons01:
                x[1]==1;//first bus must be chosen to departure
            cons02:
                    sum(i in time) x[i]==F;//fixed frequency is given
                    
            cons03:
                    sum(<i,j> in pick_point)y[<i,j>]==F-1;//no. of pairs of two adjacent buses is also fixed due to fixed frequency
              
            forall(<i,j> in select_pair)
                    cons04:
                            sum(mid in itv[<i,j>])x[mid]*y[<i,j>]==0;//ensure the chosen pairs of buses is adjencent, no other chosen bus is among the pair of buses    
              
            forall(i in time)
                    cons05:
                            sum(j in stop) x[i]*P[i][j]<=sum(d in stop) x[i]*H[i][d];//ensure the new timetable will not overflow the original demand
    }
    

    Thank you very much, and sorry to bother you so many times,

    Best regards,
     

    Shao Hsuan


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: How to create different sets given tuple sets

    Posted 11/24/16 10:14 AM

    Hi,

    maybe you could try to set a 10 minutes time limit:

    execute
    {

    cp.param.TimeLimit=60;


    }

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: How to create different sets given tuple sets

    Posted 11/24/16 10:52 AM

    Originally posted by: 黃紹軒


    Hi Alex,

    Thank you, I will try these code. And should I insert these code into the beginning of the original code or just put them behind [using CP;].

     

    Best regards,

    Shao Hsuan


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: How to create different sets given tuple sets

    Posted 11/24/16 03:09 PM

    Hi,

    after the "using CP;" and before the maximize or minimize

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer