Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to get second lagest value from a decision array

    Posted 02/01/19 02:38 PM

    Originally posted by: GrauJack


    I have this decision variable as

    dvar float DemMax[1..12];
    

    where each input is the maximum "Power Demand" for each month of the year (12 values).

     I need to use the maximum value and second largest value of that array on my objective function.

    The largest value is no problem with

    max(i in 1..12) DemMaxHP[i]
    

    but I have not been able to get the second largest value.  

    Any simple way for doing this?

    I attach my code if helpful.

    Thanks!

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to get second lagest value from a decision array

    Posted 02/02/19 11:36 AM

    Hi,

    you can use logical constraints.

    Let me give you a tiny example:

    dvar int x[1..5];

    dvar int y; // biggest value
    dvar int y2; // second biggest value
    dvar int nby; // nb times biggest value

    subject to
    {
    x[1]==2;
    x[2]==3;
    x[3]==2;
    x[4]==3;
    x[5]==1;

    y==max(i in 1..5) x[i];
    nby==sum(i in 1..5) (x[i]==y);
    sum(i in 1..5) (x[i]==y2)>=1;  // y2 is one of the x
    y2<=y-0.01;
    sum(i in 1..5) (y2>=x[i])==5-nby;
    }

    which gives

     

    y2=2

    regards

     

    PS : Many tricks at https://www.linkedin.com/pulse/how-opl-alex-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to get second lagest value from a decision array

    Posted 02/04/19 10:08 AM

    Originally posted by: GrauJack


    Thank you!, very simple and straightforward, I guess I need to study this language more Laughing


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to get second lagest value from a decision array

    Posted 02/04/19 03:40 PM

    Originally posted by: GrauJack


    How would this change if I want the 2 highest values? (not necessarily the second largest) For example, if my values are [2,3,2,3,1] I need 3 and 3.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: How to get second lagest value from a decision array

    Posted 02/05/19 02:56 AM

    Hi,

    why not trying

    dvar int x[1..5];

    dvar int y; // biggest value
    dvar int y2; // second biggest value
    dvar int nby; // nb times biggest value

    subject to
    {
    x[1]==2;
    x[2]==3;
    x[3]==2;
    x[4]==3;
    x[5]==1;

    y==max(i in 1..5) x[i];
    nby==sum(i in 1..5) (x[i]==y);
    sum(i in 1..5) (x[i]==y2)>=1;  // y2 is one of the x

    (nby>=2) => (y2==y);
    (nby==1) => y2<=y-0.01;
    (nby==1) => (sum(i in 1..5) (y2>=x[i])==5-nby);
    }

    ?

     

    regards

     

    https://www.linkedin.com/pulse/what-optimization-how-can-help-you-do-more-less-zoo-buses-fleischer/


    #DecisionOptimization
    #OPLusingCPLEXOptimizer