AnyLogic
Expand
Font size

Functions to collect statistics on agent population

AnyLogic provides functions to collect statistics on agents living within an agent population. 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 population name: count( people, p -> p.income > 10000 );

Demo model: Statistics on Agent Population Open the model page in AnyLogic Cloud. There you can run the model or download it (by clicking Model source files).

Counting agents satisfying the specific condition

Function Description
int count(population, condition) Counts agents within the given population satisfying the specified condition.

Examples:

count(people, p -> p.income > 10000);

In this example we count the agents living in the population people, whose income is greater than 10000. As the first function argument, we specify the name of the population to iterate through. As the second argument, we specify the condition to be checked for each agent. 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 agent 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 agents living in the population people, and checks every agent's income value. If the agent’s income is greater than 10000 (the defined condition is true), we count this particular agent.

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 agents who satisfy both conditions.
To get the total number of agents currently living within a population, call the population's function size(). This function belongs to a population, so call it in this way: people.size();

Obtaining an average value (agent's attribute, etc.) within the population

Function Description
double average(population, value) Returns average value in the given agent population.

Example:

average(people, p -> p.income);

Here we calculate the average income of agents living in the population people.
double averageWhere(population, value, condition) Returns average of values in given population among agents which meet the given condition.

Example:

averageWhere(people, p -> p.income, p -> p.age > 18);

Here we calculate the average income of those agents, whose age is over 18. The first argument of the function specifies the population, the second defines the value (we will calculate average value for all agents in the population), and the third argument is the condition - we will calculate average value only for those agents who satisfy this condition. Note that we define the local variable p again before specifying the condition.

Obtaining minimum (maximum) values within the population

Function Description
double max(population, value) Returns maximum value in the given population.
double maxWhere(population, value, condition) Returns maximum value in given population among agents who meet the given condition.
double min(population, value) Returns minimum value in the given population.
double minWhere(population, value, condition) Returns minimum value in given population among agents who meet the given condition.

Examples:

maxWhere(people, p -> p.income);

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

Here we obtain the maximum income of all female agents living in the population people.

Calculating the sum of agent values

Function Description
double sum(population, value) Returns sum of values in given population.

Example:

sum(people, p -> p.income);

Here we calculate the total income of all agents living in the population people.
double sumWhere(population, value, condition) Returns sum of values in given population among agents who meet the given condition.

Example:

sumWhere(people, p -> p.income, p -> p.age > 18);

Here we calculate the total income of adults (agents, whose age is over 18).
How can we improve this article?