Decision Optimization

Decision Optimization

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


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

Create dynamic decision variables and expressions

  • 1.  Create dynamic decision variables and expressions

    Posted 05/22/18 09:35 AM

    Originally posted by: JMendes


    Hello, I'm new at cplex. I'm developing a fleet assignment problem, on xpress mosel. however, now I need to do it in opl.

    The decision variable x is defined on Declarations:

    x:dynamic array(N0,N0,Fleet) of mpvar
    

    In my xpress mosel code, I create decision variable, this way, it means that if x(1,2,1)=1, leg 2 its made after leg 1 flew by the fleet type 1 :

    forall(k in POSS)do
    create(x(leg_actual(k),leg_after(k),fleet_p(k)))
    
            forall(i in N0,j in N0, f in Fleet) do
                    if(RLinear=0)then
                            x(i,j,f) is_binary
                            else x(i,j,f)<=1
                    end-if  
            end-do
    end-do
    

    So, I did this, but is not like I did in xpress mosel, because my x indexes are not the values of the vectores.

    range N0=0..84; 
    range Fleet=1..3;
    range POSS=1..3016;
    
    int leg_actual[POSS]=...;
    int leg_after[POSS]=...;
    int fleet_p[POSS]=...;
    
    dvar boolean x[N0][N0][Fleet]; // Is this possible?! forall(k in POSS) dvar boolean x[leg_actual[k]][leg_after[k]][fleet_p[k]]
    

     

    Another question: 

    My model constraints, I guess that are okay. but my expressions don't. For example, the cost variable, isn't a constraint, but I need to calculate it... how should I model it? This is my xpess mosel code.

    forall(j in N, i in lig,  f in Fleet|org(j)=cod_org(i) and dest(j)=cod_dest(i))do
    cost(j,f):=cof(f)+cov_km(f)*distance_km(i)
    end-do
    

    Thanks!

    Regards 

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Create dynamic decision variables and expressions

    Posted 05/22/18 10:23 AM

    Hi

    all dimensions of an array should be the same.

    And you could use an array in preprocessing.

    range N0=0..84;
    range Fleet=1..3;
    range POSS=1..3016;

    //cost(j,f):=cof(f)+cov_km(f)*distance_km(i)

    float cost[j in N0][f in Fleet]=f*j;

    dvar boolean x[N0][N0][Fleet];

    subject to
    {

    }

    execute
    {
    writeln(cost);
    }

    works fine

    regards

    https://www.linkedin.com/pulse/o-que-%C3%A9-otimiza%C3%A7%C3%A3o-e-como-isso-pode-ajud%C3%A1-lo-fazer-mais-fleischer/

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Create dynamic decision variables and expressions

    Posted 05/22/18 12:19 PM

    In case Alex' answer does not work, I wonder whether what you are looking for are tuples:

    tuple T {
      int actual;
      int after;
      int fleet;
    }
    {T} tuples = { <leg_actual[k], leg_after[k], fleet_p[k]> | k in POSS };
    dvar boolean x[tuples];

    With this you can index the x variable with triples <a, b, c>.


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Create dynamic decision variables and expressions

    Posted 05/22/18 12:32 PM

    Originally posted by: JMendes


    Thank you! 

     

    One question about your solution, how can handle now the variable x? this way I cant do, for instance,

     forall(i,j in N0, k in Fleet)writeln(x[i][j][k])
    

    Regards 


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Create dynamic decision variables and expressions

    Posted 05/22/18 12:37 PM

    forall (i, j in N0, k in Fleet) writeln(x[<i,j,k>])

    should work.


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Create dynamic decision variables and expressions

    Posted 05/24/18 10:22 AM

    Originally posted by: JMendes


    Hi Daniel,

     

     

     

    can you see how can I model this expressions pls? In my mosel xpress code, I can define the spill, penalty and cost variables this way... Just receive proc_low, cap, penalty, cof, cov_km, dist_km in intput by vectors this way:

    proc_low=[ 
    ...];
    cof=[ 
    ...];
    cov_km=[ 
    ...];
    dist_km=[ 
    ...];
    penalty=[...];
    

    Should I put this expressions on constraints? or define them by dexpr? I dont know... 

     

    • spill by leg
    forall(l in 1..n2) pass_out(l):= spill(l)>=proc_low(l)-sum(f in Fleet)cap(f)*(sum(i in n3..n)x(i,l,f)+x(0,l,f))
    forall(l in n3..n) pass_in(l) := spill(l)>=proc_low(l)-sum(f in Fleet)cap(f)*sum(i in 1..n2)x(i,l,f)
    
    • penalty on the objective function per spill passangers
    forall(j in N,i in lig|org(j)=cod_org(i) and dest(j)=cod_dest(i))do
    pen(j):=penalty(i)*spill(j)
    end-do
    
    • Cost
    forall(j in N,i in ligs,  f in Fleet|org(j)=cod_org(i) and dest(j)=cod_dest(i))do
    cost(j,f):=cof(f)+cov_km(f)*dist_km(i)
    end-do
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Create dynamic decision variables and expressions

    Posted 05/25/18 07:40 AM

    It is hard to tell since you are only showing small snippets of your model. It is not clear what is a variable, what is data.

    It is also unclear what your expressions mean. For example, it looks like pass_in(l) is the truth value of 'spill(l) >= proc_low(l) - sum...'? Maybe you can write the mathematical formula for this?

    Did you take a close look at the various tutorials and examples that ship with CPLEX?


    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: Create dynamic decision variables and expressions

    Posted 05/25/18 08:31 AM

    Originally posted by: JMendes


    Hi, thank you for your attention. I'll try to explain better:

    My data is about flights. However, first of all I did a "leg generator", ie, getting the date and origin and destination of the flights, I create 3 vectors: the first vector means the actual leg (pair Origin-Destination of a flight). The second one, have all the subsequent flights that can happen by any plain. For example, the decision variable x(1,2,1) it means that after leg 1, its possible to fly leg 2 with the fleet type 1. The objective is to choose witch possibilities (x) are cheaper. 

    The decision variable is already code, like you suggested to me. My doubt is about to define the total cost, the number of spill passengers and the penalization cost per each spilled passenger, witch I'll use this variables in the objective function.

    So, the idea of these variables are:

    spill(l) = max(demand(l)-capacity;0)  #number of spill passengers for leg l.

    I cant do this in linear programming, so that's the reason that I spill the variable in 2 parts.

     

    The spill variables are dived in two parts, the inbound legs (legs that the destination its my hub) and the spill for the outbound legs (legs that the origin are the hub): so, the formula of spill are:

    spill(l in inbound legs) >=  demand of each leg (proc_low(l in legs)) - capacity of aircraft all aircrafts (sum(f in Fleet)cap(f)) * sum(i in outbound legs) (x(i,l,f)*x(0,l,f))

    Analogous for the spill(l in outbound legs).

     

    The penalty cost it's easier... I've the input of penalty cost for 1 spilled passenger for all the Origin-Destination pairs... So, It's only to do spill(l)*penalty(l). Just need to check some assumptions before. like I've in the code.

     

    For last, the cost is calculated by the fixed cost of assign the aircraft (cof(f)), plusthe variable cost per km of each aircraft (cov_km(f)), times the distance of the leg(dist(l)). 

     

    pass_in(l)  it's just the name of the expression... like all the name_here(...):=

     

    I've seen the forum here and the OPL examples... I've made the majority of the model, I just can't define these variables that depends of input data and decision variable x(leg_atual,leg_after,fleet)...

     

    Thank you again! regards

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: Create dynamic decision variables and expressions

    Posted 05/27/18 03:30 PM

    Hi,

    can you attach .mod and .dat in order to help other users to help you ?

    regards

    NB: same question at https://stackoverflow.com/questions/50514980/cplex-define-variables-with-expressions


    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: Create dynamic decision variables and expressions

    Posted 05/28/18 10:04 AM

    Originally posted by: JMendes


    Hi Alex. Thanks for your attention!

     

    I can attach the .mod file. However, the .dat file it's not possible. Should  I attach the .mod even so?

     

    Regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 11.  Re: Create dynamic decision variables and expressions

    Posted 05/28/18 10:09 AM

    Hi,

    the more we see the more we can help!

    regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 12.  Re: Create dynamic decision variables and expressions

    Posted 05/28/18 10:36 AM

    Originally posted by: JMendes


    Here is it. I hope that it's understandable.

     

    Thanks a lot!


    #CPLEXOptimizers
    #DecisionOptimization


  • 13.  Re: Create dynamic decision variables and expressions

    Posted 05/29/18 10:55 AM

    I am not sure I understand your model completely/correctly but I think 'spill' needs to be a variable and the conditions you have for that should be in the constraint section:

    dvar float+ spill[1..n];
    range range1=1..n2;
    range range2=n2..n;
    ...
    subject to {
       forall (l in range1) spill[l] >= demand[l]-sum(f in Fleet)capacity[f]*(sum(i in n3..n)x[<i,l,f>]+x[<0,l,f>]);
       forall (l in range2) spill[l] >= demand[l]-sum(f in Fleet)capacity[f]*sum(i in 1..n2)x[<i,l,f>];
    }

    Note that there is a potential problem since n2 is in range1 and range2. I am not sure this is intended.


    #CPLEXOptimizers
    #DecisionOptimization


  • 14.  Re: Create dynamic decision variables and expressions

    Posted 05/29/18 11:28 AM

    Originally posted by: JMendes


    Hi mr Daniel;

     

    Yes, it's suppose to be n3... I've made some changes to the model. execute the initialization of penalty and cost variables. And put the spill constraints into the subject to{}. 

    The model is attached. However my doubt about the decision variable persist: its possible to do for instance x[<1,2,3>]*cost[2,3] ?  Thanks.

     

    Regards


    #CPLEXOptimizers
    #DecisionOptimization


  • 15.  Re: Create dynamic decision variables and expressions

    Posted 05/29/18 11:33 AM

    I still don't see  definition for 'cost' in your .mod. You initialize it but you don't define it. If 'cost[2][3]' is either a constant or a variable then yes, you can multiply it by a variable.


    #CPLEXOptimizers
    #DecisionOptimization


  • 16.  Re: Create dynamic decision variables and expressions

    Posted 05/29/18 11:37 AM

    Originally posted by: JMendes


    And if I did this? 

    float cost[N][Fleet];
    

    Also the same for this variables:

    int spill[N];
    float pen[N];
    int AVD1[Fleet];
    

    #CPLEXOptimizers
    #DecisionOptimization


  • 17.  Re: Create dynamic decision variables and expressions

    Posted 05/28/18 01:25 PM

    Originally posted by: JMendes


    Hi mr Daniel. Sorry to bother you.

     

    I've already attached my .mod file, however I've a simple doubt, about this tuple x.

     

    In my objective function: 

    dexpr float objective =
    sum(i,j in N)(cost[j][1]*x[<i,j,1>] + cost[j][2]*x[<i,j,2>] + cost[j][3]*x[<i,j,3>]) + sum(l in N)(pen[l]);
    minimize objective;  

    Is this possible? make the cost[N][Fleet] * x[<i,j,1>].  It's because I've the error: "CP cannot extract expression". Should I get an alternative to this tuples?

     

    Thanks


    #CPLEXOptimizers
    #DecisionOptimization


  • 18.  Re: Create dynamic decision variables and expressions

    Posted 05/29/18 10:49 AM

    I don't see any definition for 'cost' in the .mod file you attached. So hard to tell what the problem is. I also don't see a "using CP" statement, so I am not clear why you get errors from CP. Any error should come from CPLEX, I think.

     


    #CPLEXOptimizers
    #DecisionOptimization