Agent#

Agent related classes.

Core Objects: Agent.

class Agent(model: M, *args, **kwargs)[source]#

Base class for a model agent in Mesa.

model#

A reference to the model instance.

Type:

Model

unique_id#

A unique identifier for this agent.

Type:

int

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.

step() None[source]#

A single step of the agent.

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.

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 random: Random#

Return a seeded stdlib rng.

property rng: Generator#

Return a seeded np.random rng.

property scenario#

Return the scenario associated with the model.