Agents
Agent-based models can contain multiple agents of different types.
This module provides a base class Agent
that is meant to be used as a template to create custom agent types.
Initial variables should be defined by overriding Agent.setup()
.
- class Agent(model, *args, **kwargs)[source]
Template for an individual agent.
- Parameters
model (Model) – The model instance.
**kwargs – Will be forwarded to
Agent.setup()
.
- Variables
- record(var_keys, value=None)
Records an object’s variables at the current time-step. Recorded variables can be accessed via the object’s log attribute and will be saved to the model’s output at the end of a simulation.
- Parameters
var_keys (str or list of str) – Names of the variables to be recorded.
value (optional) – Value to be recorded. The same value will be used for all var_keys. If none is given, the values of object attributes with the same name as each var_key will be used.
Notes
Recording mutable objects like lists can lead to wrong results if the object’s content will be changed during the simulation. Make a copy of the list or record each list entry seperately.
Examples
Record the existing attributes x and y of an object a:
a.record(['x', 'y'])
Record a variable z with the value 1 for an object a:
a.record('z', 1)
Record all variables of an object:
a.record(a.vars)
- setup(**kwargs)
This empty method is called automatically at the objects’ creation. Can be overwritten in custom sub-classes to define initial attributes and actions.
- Parameters
**kwargs – Keyword arguments that have been passed to
Agent
orModel.add_agents()
. If the original setup method is used, they will be set as attributes of the object.
Examples
The following setup initializes an object with three variables:
def setup(self, y): self.x = 0 # Value defined locally self.y = y # Value defined in kwargs self.z = self.p.z # Value defined in parameters