Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  How to do a math.mode in OPL

    Posted 09/25/13 05:50 AM

    Originally posted by: Calvin Sun Hainan


    How to find the mode for the following list of values in OPL:

    {int} array = {13, 18, 13, 14, 13, 16, 14, 21, 13}

    The "mode" is the value that occurs most often.

    I need something like mode(array) to return me 13 in this case. Is there an easy way to do in OPL? It is very easy to do min or max, but it seems that mode is not so straightforward. It seems that I have to write OPL script to find mode.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: How to do a math.mode in OPL

    Posted 09/25/13 08:14 AM

    Hi,

    int array2[1..9]=[13, 18, 13, 14, 13, 16, 14, 21, 13];

    int arrayQ[i in 1..9]=count(array2,array2[i]);

    int maxOccur=max(i in 1..9) arrayQ[i];

    int maxValue=first({array2[i] | i in 1..9 :arrayQ[i]==maxOccur});

    execute
    {
     writeln(maxValue);
    }

     

    will do the job

     

    regards

     

    Alex Fleischer


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: How to do a math.mode in OPL

    Posted 09/25/13 08:55 PM

    Originally posted by: Calvin Sun Hainan


    Thanks. Realy smart solution. I never thought about using count in this way. The following is my solution, and it is not efficient.

    tuple twoInt {

      int a;

      int b;

    }

    twoInt array[1..9] = [<13,1>, <18,1>, <13,1>, <14,1>, <13,1>, <16,1>, <14,1>, <21,1>, <13,1>];

    {int} diffValuesInArray = {x.a | x in array};

    int countForEachValue[diffValuesInArray] = [d : sum (x in array : x.a == d) x.b  | d in diffValuesInArray];

    int mode;

    execute {

      var count = 0;

      for (var x in diffValuesInArray) {

        if ( count < countForEachValue[x]) {

          mode = x;

          count = countForEachValue[x];

      }

    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: How to do a math.mode in OPL

    Posted 09/25/13 09:20 PM

    Originally posted by: Calvin Sun Hainan


    I realize that "count" only works with int arrays. If my array is float, I think count will not work. I may still need to switch to using OPL script.

    count

    Purpose
    OPL function to count variables of a certain value.
    Context
    Model files (.mod) - Not allowed in a CPLEX constraint block.
    Type
    int
    
    Syntax
    count(int[ ],int)
    count(dvar int[ ],int) )
    
    
    Description
    This expression takes two arguments. It counts how many of the variables in the array given as the first argument are equal to the value given as the second argument. This function also works for integer arrays outside constraint blocks both in CP and CPLEX models.
    You can use this function within IBM® ILOG Script statements by specifying the OPL namespace:

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer