AnyLogic
Expand
Font size

Select agent(s) from a population

To select a specific agent from a population

Function Description
Agent top(population, value) Returns agent having maximum value in given population.

Example:

Person person = top( people, p -> p.age );

Here we get the oldest person from the population people. If the population is empty, the function returns null.
List filter(population, condition) Returns a subset of a given population — a new list with agents from the original population which meet the given condition.

Examples:

List women = filter( people, p -> p.gender == FEMALE );
List idleTrucks = filter( trucks, t -> t.inState(Idle) );
List findAll(population, condition) This function is the same as filter(population, condition).
Agent findFirst(population, condition) Returns the first agent from the given population which meets the given condition (the element which has true value of the given condition). Returns null if there is no such element or the collection is empty.

Example:

Person person = findFirst( people, p -> p.age > 20 );

To select a random agent from a population

AnyLogic provides functions to select a random agent from a given population. These functions are defined in the class Utilities 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: randomWhere( people, p -> p.income > 10000 );.

Function Description
Agent randomWhere(population, condition) Returns one randomly selected agent within the given population satisfying the specified condition. If the population is empty, or no agents satisfy the specified condition(s), the function returns null.

Examples:

Person wealthyPerson = randomWhere( people, p -> p.income > 10000 );
Person wealthyMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE );

Here we check two conditions (we use logical AND operator && to tell AnyLogic that both the first AND the second condition should be true). The function returns a randomly selected agent that satisfies both conditions.
Person wealthyYoungMan = randomWhere( people, p -> p.income > 10000 && p.sex == MALE && p.age < 21 );
Agent randomWhere(population, condition, java.util.Random r) Similar to randomWhere(population, condition). Uses the specified custom random number generator to choose the element.
How can we improve this article?