Agent#
Agent related classes.
Core Objects: Agent.
- class Agent(model: M, *args, **kwargs)[source]#
Base class for a model agent in Mesa.
- pos#
A reference to the position where this agent is located.
- Type:
Position
Notes
Agents must be hashable to be used in an AgentSet. In Python 3, defining __eq__ without __hash__ makes an object unhashable, which will break AgentSet usage. unique_id is unique relative to a model instance and starts from 1
Create a new agent.
- Parameters:
model (Model) – The model instance in which the agent exists.
args – Passed on to super.
kwargs – Passed on to super.
Notes
to make proper use of python’s super, in each class remove the arguments and keyword arguments you need and pass on the rest to super
- remove() None[source]#
Remove and delete the agent from the model.
Notes
If you need to do additional cleanup when removing an agent by for example removing it from a space, consider extending this method in your own agent class.
- classmethod create_agents(model: Model, n: int, *args, **kwargs) AgentSet[T][source]#
Create N agents.
- Parameters:
model – the model to which the agents belong
args – arguments to pass onto agent instances each arg is either a single object or a sequence of length n
n – the number of agents to create
kwargs – keyword arguments to pass onto agent instances each keyword arg is either a single object or a sequence of length n
- Returns:
AgentSet containing the agents created.
Warning
A list, tuple, ndarray, or pandas Series argument whose length equals n is always treated as one value per agent, even if you intended it as a single shared value. This is especially easy to hit with coordinate tuples: create_agents(model, 2, pos=(10, 20)) does NOT give both agents pos=(10, 20); it gives agent 0 pos=10 and agent 1 pos=20, since the tuple’s length (2) matches n (2).
To share a value across all agents regardless of its length, wrap it so its own length no longer matches n, e.g.: create_agents(model, 2, pos=[(10, 20)] * 2)
- classmethod from_dataframe(model: Model, df: pd.DataFrame, **kwargs) AgentSet[T][source]#
Create agents from a pandas DataFrame.
Each row of the DataFrame represents one agent. The DataFrame columns are mapped to the agent’s constructor as keyword arguments. Additional keyword arguments (**kwargs) can be used to set constant attributes for all agents.
- Parameters:
model – The model instance.
df – The pandas DataFrame. Each row represents an agent.
**kwargs – Constant values to pass to every agent’s constructor. Only non-sequence data is allowed in kwargs to avoid ambiguity with DataFrame columns.
- Returns:
AgentSet containing the agents created.
Note
If you need to pass variable data or sequences, add them as columns to the DataFrame before calling this method.
- property rng: Generator#
Return a seeded np.random rng.
- property scenario#
Return the scenario associated with the model.