Agents can be instantiated with or without data. Instantiating
with data is equivalent to creating an Agent and immediately
calling Agent.set to add data.
// instantiates an Agent without data
const a = new Agent();
// instantiates an Agent with data
const b = new Agent({
x: 50,
y: 100
});
An Agent can only belong to a single Environment. When
environment.addAgent(agent); is called, this is value is updated
to point to that Environment.
const environment = new Environment();
const agent = new Agent();
agent.environment; // returns `null`
environment.addAgent(agent);
agent.environment === environment;
Agents are automatically assigned a unique ID when they are created.
This can be useful when you need to refer to a specific Agent, and
they can be retrieved using their ID from their Environment by calling
environment.getAgentById(id);
const agent = new Agent();
const id = agent.id; // returns "59B4F928-46C8-..." (for example)
Decrement a numeric piece of data associated with this Agent
(decreasing its value by 1). This method is synchronous —
it immediately decreases the value (to asynchronously decrease it,
the rule function should instead return a new value.
agent.set('x', 50);
agent.decrement('x');
agent.get('x'); // returns 49
If the second parameter n is included, decrements by that amount.
agent.set('x', 50);
agent.decrement('x', 10);
agent.get('x'); // returns 40
If the value has not yet been set, calling this method sets it to -1
(or to -n).
Emit an event to the environment's event bus.
Event type
Event payload
Retrieve an arbitrary piece of data associated by name.
If the data has not been set, returns null.
increment a numeric piece of data associated with this Agent
(increasing its value by 1). This method is synchronous —
it immediately increases the value (to asynchronously increase it,
the rule function should instead return a new value.
agent.set('x', 50);
agent.increment('x');
agent.get('x'); // returns 51
If the second parameter n is included, decrements by that amount.
agent.set('x', 50);
agent.increment('x', 10);
agent.get('x'); // returns 60
If the value has not yet been set, calling this method sets it to 1
(or to n).
Subscribe to an event type. The handler receives the agent as the first argument and the event object as the second.
Event type to listen for
Function to call when event is emitted
Unsubscribe function
Schedule this agent to tick at a specific environment time. Only has an effect if the environment uses a scheduler that supports explicit scheduling (e.g., PriorityScheduler).
The environment time at which to tick
Schedule this agent to tick after a delay (relative to current time).
Shorthand for agent.scheduleAt(environment.time + delay).
Number of ticks to wait before next activation
Set a piece of data associated with this agent. Name should be a string while value can be any valid type. Alternatively, the first parameter can be an object, which merges the current data with the new data (adding new values and overwriting existing). Ex. agent.set('x', 5); agent.set('color', 'red');
Optionalvalue: any
This class puts the
Agentin 'agent-based modeling.' More specifically, anAgentrepresents an individual unit of data and its associated behaviors.Since
0.0.5