AnyLogic
Expand
Font size

Functions to collect statistics on a collection

AnyLogic provides functions to collect statistics on elements belonging to a collection. These functions are defined in the UtilitiesCollection class and are global (accessible from each place of model code). You call any function from the list below by typing just its name, without a prefix with the collection name: count( people, p -> p.income > 10000 );

Counting elements satisfying the specific condition

  • int count(collection, condition) — Counts elements of the given collection satisfying the specified condition.
    Examples:
    count( people, p -> p.income > 10000 );
    In this example we count the elements in the people collection, whose income is greater than 10000. As the first function argument, we specify the name of the collection to iterate through. As the second argument, we specify the condition to be checked for each element. In the condition, we first define the local variable (it is named p here, but it can have any other name). We can use this local variable in our condition to refer to the element that is currently being checked. Then we type the arrow operator -> (it consists of two symbols) and define the condition after them (ft. the local variable p): p.income > 10000. The function count() iterates through all elements in the people collection and checks every element’s income value. If the element’s income is greater than 10000 (the defined condition is true), we count this particular element.

    count( people, p -> p.sex == MALE && p.age >= 18);
    Here we check two conditions (we use logical AND operator && to tell AnyLogic that both the first AND the second condition should be true), and count only the elements that satisfy both conditions.
    To get the total number of elements in a collection, call the collection's function size(). This function belongs to a collection, so call it this way: people.size();

Obtaining an average value (element’s attribute, etc.) within the collection

  • double average(collection, value) — Returns average value in the given collection.
    Example:
    average( people, p -> p.income );
    Here we calculate the average income of elements living in the people collection.
  • double averageWhere(collection, value, condition ) — Returns the average of values in a given collection among the elements satisfying the specified condition.
    Example:
    average( people, p -> p.income, p -> p.age > 18 );
    Here we calculate the average income of those elements, whose age is over 18. The first argument of the function specifies the collection, the second defines the value (we will calculate average value for all elements in the collection), and the third argument is the condition - we will calculate average value only for those elements which satisfy this condition. Note that we define the local variable p again before specifying the condition.

Obtaining minimum (maximum) values within the collection

  • double max(collection, value) — Returns maximum value in the given collection.
  • double maxWhere(collection, value, condition) — Returns maximum value in the given collection among the elements satisfying the specified condition.
  • double min(collection, value) — Returns minimum value in the given collection.
  • double minWhere(collection, value, condition) — Returns minimum value in the given collection among the elements satisfying the specified condition.

Examples:

maxWhere( people, p -> p.income );
Here we obtain the maximum income of all elements.

maxWhere( people, p -> p.income, p -> p.sex == FEMALE );
Here we obtain the maximum income of all female elements of the people collection.

Calculating the sum of element values

  • double sum(collection, value) — Returns the sum of values in the given collection.
    Example:
    sum( people, p -> p.income );
    Here we calculate the total income of all elements in the people collection.
  • double sumWhere(collection, value, condition) — Returns the sum of values in the given collection among the elements satisfying the specified condition.
    Example:
    sumWhere( people, p -> p.income, p -> p.age > 18 ); Here we calculate the total income of adults (elements, whose age is over 18).
How can we improve this article?