Topic
• 3 replies
• Latest Post - ‏2013-09-26T01:20:21Z by Calvin Sun Hainan
Calvin Sun Hainan
23 Posts

# Pinned topic How to do a math.mode in OPL

‏2013-09-25T09:49:55Z | opl

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.

Updated on 2013-09-25T09:51:07Z at 2013-09-25T09:51:07Z by Calvin Sun Hainan
• AlexFleischer
3096 Posts

#### Re: How to do a math.mode in OPL

‏2013-09-25T12:13:54Z

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

• AlexFleischer
3096 Posts

#### Re: How to do a math.mode in OPL

‏2013-09-25T12:13:54Z

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

• Calvin Sun Hainan
23 Posts

#### Re: How to do a math.mode in OPL

‏2013-09-26T00:54:43Z

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

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];

}

}

• Calvin Sun Hainan
23 Posts

#### Re: How to do a math.mode in OPL

‏2013-09-26T01:20:21Z

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

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: