UNPKG

agentscape

Version:

Agentscape is a library for creating agent-based simulations. It provides a simple API for defining agents and their behavior, and for defining the environment in which the agents interact. Agentscape is designed to be flexible and extensible, allowing

119 lines (118 loc) 4 kB
import Cell from '../entities/Cell'; import Agent from '../entities/Agent'; import RandomGenerator from '../numbers/RandomGenerator'; import CellGrid from './CellGrid'; export default class AgentSet<T extends Agent> { private agents; /** * Generates an AgentSet from an array of agents. */ static fromArray<T extends Agent>(agents: T[]): AgentSet<T>; /** * Generates an AgentSet from a Set of agents. */ static fromSet<T extends Agent>(agents: Set<T>): AgentSet<Agent>; /** * Generates an AgentSet from a factory function. * The factory function is called `count` times with an sequential index * and a non-sequential random seed. The function should return an Agent. */ static fromFactory<T extends Agent>(count: number, agentFactory: (index: number, randomSeed: number) => T, options?: { randomSeed?: number; }): AgentSet<T>; constructor(); [Symbol.iterator](): Generator<T, void, unknown>; toArray(): T[]; /** * Runs the 'act' method on all agents in the set. */ step(world: CellGrid<Cell>, agentSets: { [key: string]: AgentSet<T>; }, tick: number): void; /** * Inserts an agent into the set. * Optionally inserts the agent into the grid. */ add(agent: T, world?: CellGrid<Cell>): void; /** * Removes an agent from the set. */ remove(agent: T): void; /** * Iterates over all agents in the set. */ forEach(callback: (agent: T, i: number) => void): void; /** * Maps a callback function over all agents in the set. */ map<U>(callback: (agent: T, i: number) => U): U[]; /** * Gets the ith agent in the set. */ get(index: number): T | undefined; reduce<U>(callback: (accumulator: U, agent: T) => U, initialValue: U): U; /** * Finds the Agent with the minimum value in the set of agents. * * Returns undefined if the set is empty. */ minimizeBy(fn: (agent: T) => number): T; /** * Finds the Agent with the maximum value in the set of agents. * * Returns undefined if the set is empty. * * @since 1.0.0 */ maximizeBy(fn: (agent: T) => number): T; /** * Returns the number of agents in the set. */ get size(): number; /** * Removes all dead agents from the set. * Note: This is handled automatically by the model. */ cullDeadAgents(): void; /** * Gets the agent at a location on a grid. */ getAgentAt(location: [number, number]): T | undefined; /** * Gets the nearest agent to a location. Returns undefined if the set is empty. */ getNearestAgentAt(world: CellGrid<Cell>, location: [number, number], options?: { metric?: 'manhattan' | 'euclidean'; }): T | undefined; filter(predicate: (agent: T) => boolean): AgentSet<T>; findFirst(predicate: (agent: T) => boolean): T | undefined; sortBy(compareFn: (a: T, b: T) => number): AgentSet<T>; slice(start: number, end: number): AgentSet<T>; /** * Gets all agents within a certain range of a location. */ getWithinRange(world: CellGrid<Cell>, location: [number, number], range: number, options?: { metric?: 'manhattan' | 'euclidean'; }): AgentSet<T>; /** * Gets the mean position of all agents in the set. */ getCentroid(): [number, number]; /** * Picks a single agent at random from the set. * Optionally provide a list of agents to exclude from the selection. * * Returns undefined if the set is empty. */ random(rng: RandomGenerator, options?: { exclude?: T[]; }): T | undefined; /** * Picks a random sample (without replacement) of agents from the set. * Optionally provide a list of agents to exclude from the sample. */ randomSample(rng: RandomGenerator, count: number, options?: { exclude?: T[]; }): AgentSet<T>; private distance; }