Decision Optimization

Decision Optimization

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


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

Describing a model using OPL Coding

ALEX FLEISCHER

ALEX FLEISCHER10/28/16 01:43 AM

ALEX FLEISCHER

ALEX FLEISCHER10/28/16 05:05 AM

  • 1.  Describing a model using OPL Coding

    Posted 10/26/16 10:43 PM

    Originally posted by: DannyYoung


    Please assist, I am a new user of Cplex.

    I am trying to code a problem (see attached file) which has 24 nodes and 38 lines (arcs).

    Below is what I could come up with but my solution is giving zeros for all decision variables.

    Please help check if my coding has issues. Thanks

     

    
    
    //parameters
    
    int B=...; //number of nodes
    int L=...; //number of lines
    
    range nodes =1..B;
    range lines =1..L;
    
    float cost_generation [nodes]= ...;
    float cost_shedding [nodes]= ...;
    float max_generation[nodes]= ...;
    float load_demand[nodes] = ...;
    
    float cost_transmission [lines]= ...;
    float max_power_flow[lines] = ...;
    float Mmatrix[1..38][1..24] = ...;
    
    //Variables
    
    dvar float+ G[nodes];
    dvar float+ D[nodes];
    dvar float+ F[lines];
    
    minimize sum (i in nodes, k in lines)
    (cost_generation[i]*G[i] + cost_transmission[k]*F[k] -     cost_shedding[i]*D[i]);
    
    subject to {
    forall (i in nodes)
    0<=G[i]<=max_generation[i];
    
    forall (i in nodes) 
    -load_demand[i]<=-D[i]<=0;
    
    forall (k in lines)
    -max_power_flow[k]<=F[k]<=max_power_flow[k];
    
    forall (k in lines, i in nodes)
     F[k]==Mmatrix[k][i]*(G[i]-D[i]);
      }
    

    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: Describing a model using OPL Coding

    Posted 10/27/16 02:21 AM

    Hi,

    can you also attach your .dat ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: Describing a model using OPL Coding

    Posted 10/27/16 09:07 AM

    Originally posted by: DannyYoung


    Thanks alot.
    Below is the .dat file, I also attached the data and a snapshot of the solution in Cplex.

     

    B=24;
    L=38;

    SheetConnection my_sheet("Area 1.xls");

    cost_generation from SheetRead(my_sheet,"cost_generation");
    cost_shedding from SheetRead(my_sheet,"cost_shedding");
    cost_transmission from SheetRead(my_sheet,"cost_transmission");
    max_generation from SheetRead(my_sheet,"Max_generation");
    load_demand from SheetRead(my_sheet,"Load_demand");
    max_power_flow from SheetRead(my_sheet,"maxi_power_flow");
    Mmatrix from SheetRead(my_sheet,"M_matrix");

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: Describing a model using OPL Coding

    Posted 10/27/16 03:15 PM

    Hi,

    instead of

    forall (k in lines, i in nodes)
     F[k]==Mmatrix[k][i]*(G[i]-D[i]);
      }

    could you rather try

    forall (k in lines)
     F[k]==sum(i in nodes) Mmatrix[k][i]*(G[i]-D[i]);
      }

    ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: Describing a model using OPL Coding

    Posted 10/27/16 05:28 PM

    Originally posted by: DannyYoung


    Thanks a million, it worked but I have some issues with the solution.
    Why is the value for each generator (G) zero and the loads (D) have values.

    Each generator represents a source while the loads are the sinks.

    I expect each generator (G) that has max value to have values, since it is the source that supplies the sink.

    Please assist, Many thanks.

     

     

     

     

     

     

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: Describing a model using OPL Coding

    Posted 10/28/16 01:43 AM

    Hi,

    first instead of

    dvar float+ F[lines];

    do not you want to try

    dvar float F[lines];

    ?

    Second you may try to add

    sum(i in nodes) D[i]==sum(i in nodes) G[i];

    if you want to have sums of D and G equal.

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: Describing a model using OPL Coding

    Posted 10/28/16 04:16 AM

    Originally posted by: DannyYoung


    Many thanks for your help. The value of each generator (G) is still zero.

    I also tried to introduce a parameter to represent (G[i]-D[i]) as shown below but generators (G) are all zero

     

    forall (i in nodes)
     P[i]==(G[i]-D[i]);

    forall (k in lines)
     F[k]==sum(i in nodes) Mmatrix[k][i]*P[i];
      }
     

    Please assist. Thanks.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: Describing a model using OPL Coding

    Posted 10/28/16 04:44 AM

    Hi,

    have you added

    sum(i in nodes) D[i]==sum(i in nodes) G[i];

    too ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: Describing a model using OPL Coding

    Posted 10/28/16 05:00 AM

    Originally posted by: DannyYoung


    Thanks. I added
    sum(i in nodes) D[i]==sum(i in nodes) G[i];

    but all decision variables are zero,

    I have attached a screenshot of the solution.

    Please assist.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: Describing a model using OPL Coding

    Posted 10/28/16 05:05 AM

    Hi,

    if I add

    execute
      {
      writeln("D=",D);
      writeln("G=",G);  
      }

    in the postprocess I get

    // solution (optimal) with objective -558.520625305391
    D= [0 0 36.264 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 27.405 0 0 0 0 0]
    G= [0 0 0 0 0 0 0 0 0 0 0 0 63.669 0 0 0 0 0 0 0 0 0 0 0]

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: Describing a model using OPL Coding

    Posted 10/28/16 09:43 AM

    Originally posted by: DannyYoung


    Thanks, but I see that it is only one generator that has a value.

     

    Is it not possible for all the generators that have maximum value to have value for G?

     

    Thanks,


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: Describing a model using OPL Coding

    Posted 10/28/16 10:11 AM

    Hi

    forall (i in nodes)
    G[i]==max_generation[i];

    would do that

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 13.  Re: Describing a model using OPL Coding

    Posted 10/28/16 12:20 PM

    Originally posted by: DannyYoung


    Thanks alot Alex, you've been so helpful.

    However, the research restricts the constraint to G[i]<=max_generation[i];

    Just like any other network problem, the source should be within the range of a lower bound and an upper bound

    Please is there any other way around it?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: Describing a model using OPL Coding

    Posted 10/29/16 03:29 AM

    Originally posted by: DannyYoung


    Please, is there anything I can do to ensure that each generator G has value its value and still maintain the constraint

    G[i]<=max_generation[i];


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 15.  Re: Describing a model using OPL Coding

    Posted 10/29/16 02:04 PM

    Hi,

    So simply keep

    forall (i in nodes)
    0<=G[i]<=max_generation[i];

     

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 16.  Re: Describing a model using OPL Coding

    Posted 10/29/16 04:34 PM

    Originally posted by: DannyYoung


    Thanks,

    I'm still getting zero for all the G[i] and the objective function is still negative.

    I want a solution where the G[i] with defined upper bound will have values and the objective will be positive.

    Please, can I specify that in my model?

    Many thanks for your assistance so far.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 17.  Re: Describing a model using OPL Coding

    Posted 10/29/16 05:22 PM

    Originally posted by: DannyYoung


     

    Thanks Alex,

     

    I used the model to solve a network with 5 nodes and 5 lines.

    The same problem was observed, G[i] is zero for each and the obj function is negative.

    Please what can I do to get my desired solution.

    Thanks


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 18.  Re: Describing a model using OPL Coding

    Posted 10/31/16 01:29 PM

    Originally posted by: DannyYoung


    Please what can be done to ensure that generators have values and get a positive objective function?

    Kindly assist, thanks.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 19.  Re: Describing a model using OPL Coding

    Posted 11/01/16 04:31 AM

    Hi,

    if you want the G not to be too low with regards the max of generation you may try

    forall (i in nodes)
    G[i]>=0.5*max_generation[i];

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 20.  Re: Describing a model using OPL Coding

    Posted 11/04/16 10:00 PM

    Originally posted by: DannyYoung


    Thanks alot. I really appreciate.

    I observe that my results were not copied to the I excel sheet when I used the code below

     

    G to SheetWrite(my_sheet,"resultg");
    D to SheetWrite(my_sheet,"resultd");
    P to SheetWrite(my_sheet,"resultp");
    F to SheetWrite(my_sheet,"resultf");

    Are the codes wrong.

    Thanks

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 21.  Re: Describing a model using OPL Coding

    Posted 11/05/16 09:45 AM

    This looks good.

    Can you post .mod, .dat and .xls ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 22.  Re: Describing a model using OPL Coding

    Posted 11/05/16 01:09 PM

    Originally posted by: DannyYoung


    Thanks, please find below and attached as requested.

    I also observed another problem, when I tried to change the max power flow values to zero in the excel, the model still run using the previous data.

     

    Please help.

     

    //parameters

    int B=...; //number of nodes
    int L=...; //number of lines

    range nodes =1..B;
    range lines =1..L;

    float cost_generation [nodes]= ...;
    float cost_shedding [nodes]= ...;
    float max_generation[nodes]= ...;
    float load_demand[nodes] = ...;

    float cost_transmission [lines]= ...;
    float max_power_flow[lines] = ...;
    float Mmatrix[1..38][1..24] = ...;

    //Variables

    dvar float+ G[nodes];
    dvar float+ D[nodes];
    dvar float P[nodes];
    dvar float F[lines];

    minimize sum (i in nodes, k in lines)
    (cost_generation[i]*G[i] + cost_transmission[k]*F[k] - cost_shedding[i]*D[i]);

    subject to {
    forall (i in nodes)
    0.5*max_generation[i]<=G[i]<=max_generation[i];

    forall (i in nodes)
    -load_demand[i]<=-D[i]<=0;

    forall (k in lines)
    -max_power_flow[k]<=F[k]<=max_power_flow[k];

    forall (i in nodes)
     P[i]==G[i]-D[i];
     
    forall (k in lines)
     F[k]==sum (i in nodes)Mmatrix[k][i]*P[i];

     }

     

     

    B=24;
    L=38;

    SheetConnection my_sheet("Area.xlsx");

    cost_generation from SheetRead(my_sheet,"cost_generation");
    cost_shedding from SheetRead(my_sheet,"cost_shedding");
    cost_transmission from SheetRead(my_sheet,"cost_transmission");
    max_generation from SheetRead(my_sheet,"Max_generation");
    load_demand from SheetRead(my_sheet,"Load_demand");
    max_power_flow from SheetRead(my_sheet,"max_powerflow");
    Mmatrix from SheetRead(my_sheet,"Mmatrixx");


    G to SheetWrite(my_sheet,"resultg");
    D to SheetWrite(my_sheet,"resultd");
    P to SheetWrite(my_sheet,"resultp");
    F to SheetWrite(my_sheet,"resultf");


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 23.  Re: Describing a model using OPL Coding

    Posted 11/07/16 08:40 AM

    Hi,

    I see the results in the spreadsheet.

    In order to see change in the result when you change the input do not forget to save the xls file

    regards
     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 24.  Re: Describing a model using OPL Coding

    Posted 11/07/16 01:37 PM

    Originally posted by: DannyYoung


    Thanks a million, Alex.

    I saved it and I can now see the results. Thanks alot.

     

    Please how do I include the below in my constraint,

    sum of all lines F[k] connected to a node equals the P[i] of that same node.

    For example F[1], F[2] & F[3] are connected to node 1.

    So F[1]+F[2]+F[3] = P[1]

     

    Pleas assist. Thanks


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 25.  Re: Describing a model using OPL Coding

    Posted 11/07/16 01:38 PM

    Originally posted by: DannyYoung


    Please how do I include the below in my constraint,

    sum of all lines F[k] connected to a node equals the P[i] of that same node.

    For example F[1], F[2] & F[3] are connected to node 1.

    So F[1]+F[2]+F[3] = P[1]

     

    Pleas assist. Thanks


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 26.  Re: Describing a model using OPL Coding

    Posted 11/07/16 02:44 PM

    You could try something like

    forall(i in nodes) ct:sum(k in lines:abs(Mmatrix[k][i])>=0.01)F[k]==P[i];

    if Mmatrix describes whether a line is connected to a node

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 27.  Re: Describing a model using OPL Coding

    Posted 11/08/16 12:16 PM

    Originally posted by: DannyYoung


    Thanks.

     

    However, Mmatrix does not describe whether a line  is connected to a node.

    I input your codes in my model but didn't get the needed result.

    is there any other way it can be represented.

    sum of all arcs connected to a node == the output of the node


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 28.  Re: Describing a model using OPL Coding

    Posted 11/09/16 04:47 AM

    Hi

    if

    int connect[1..38][1..24]=...;

    describes connection then you could try something such as

     forall(i in nodes) sum(k in lines:connect[i][k]==1) F[k]==G[i];

    regards

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer