Population

class Population<A : AgentModel.Agent>(source: () -> Iterable<A>) : Iterable<A> (source)

A composable view over a group of agents.

Following the convention adopted by Mesa 3.0, a Population is not a scheduler — it does not decide when its members act. It is a typed collection with the operations a modeler typically needs when expressing agent activation: shuffle, filter by type, filter by predicate, group. Iteration order is the caller's choice:

// random-order activation
population.shuffled(stream).forEach { it.step() }

// staged activation
population.ofType<Predator>().forEach { it.hunt() }
population.ofType<Prey>().forEach { it.flee() }

// two-phase simultaneous activation
population.forEach { it.preStep() }
population.forEach { it.commit() }

A Population is a live view: changes to the underlying agent collection (for example, new agents created by an AgentModel.AgentGenerator) are reflected on the next iteration.

Constructors

Link copied to clipboard
constructor(source: () -> Iterable<A>)
constructor(agentModel: AgentModel, type: Class<A>)

Construct a population that always reflects the current contents of agentModel.agents, filtered to instances of A.

constructor(members: List<A>)

Construct a population backed by a snapshot of members. The population view reflects later mutations to members if it is a live collection.

Properties

Link copied to clipboard

True when no agents are in this population.

Link copied to clipboard
val size: Int

Number of agents currently in this population.

Functions

Link copied to clipboard
fun <K> groupBy(keySelector: (A) -> K): Map<K, List<A>>

Group the population by the value of keySelector.

Link copied to clipboard
open operator override fun iterator(): Iterator<A>
Link copied to clipboard

Convenience: return an AgentModel.AgentMailbox-bound list of (agent, mailbox) pairs for use in broadcast scenarios. The mailbox returned is the agent's default mailbox.

Link copied to clipboard
inline fun <T : A> ofType(): List<T>

Return a new list of agents whose type is exactly T or a subtype.

Link copied to clipboard
fun shuffled(stream: RNStreamIfc): List<A>

Return a new list containing the agents in a random order, drawn from stream. Using an explicit stream preserves reproducibility across replications.

Link copied to clipboard
fun where(predicate: (A) -> Boolean): List<A>

Return a new list of agents matching predicate.