Base Classes#

class Agent(unique_id: int, model: Model)[source]#

Base class for a model agent in Mesa.


unique_id (int): A unique identifier for this agent. model (Model): A reference to the model instance. self.pos: Position | None = None

Create a new agent.


unique_id (int): A unique identifier for this agent. model (Model): The model instance in which the agent exists.

remove() None[source]#

Remove and delete the agent from the model.

step() None[source]#

A single step of the agent.

class Model(*args: Any, **kwargs: Any)[source]#

Base class for models in the Mesa ABM library.

This class serves as a foundational structure for creating agent-based models. It includes the basic attributes and methods necessary for initializing and running a simulation model.


running: A boolean indicating if the model should continue running. schedule: An object to manage the order and execution of agent steps. current_id: A counter for assigning unique IDs to agents. agents_: A defaultdict mapping each agent type to a dict of its instances.

This private attribute is used internally to manage agents.


agents: An AgentSet containing all agents in the model, generated from the _agents attribute. agent_types: A list of different agent types present in the model.


get_agents_of_type: Returns an AgentSet of agents of the specified type. run_model: Runs the model’s simulation until a defined end condition is reached. step: Executes a single step of the model’s simulation process. next_id: Generates and returns the next unique identifier for an agent. reset_randomizer: Resets the model’s random number generator with a new or existing seed. initialize_data_collector: Sets up the data collector for the model, requiring an initialized scheduler and agents.

Create a new model. Overload this method with the actual code to start the model. Always start with super().__init__() to initialize the model object properly.

property agents: AgentSet#

Provides an AgentSet of all agents in the model, combining agents from all types.

property agent_types: list[type]#

Return a list of different agent types.

get_agents_of_type(agenttype: type[Agent]) AgentSet[source]#

Retrieves an AgentSet containing all agents of the specified type.

run_model() None[source]#

Run the model until the end condition is reached. Overload as needed.

step() None[source]#

A single step. Fill in here.

next_id() int[source]#

Return the next unique ID for agents, increment current_id

reset_randomizer(seed: int | None = None) None[source]#

Reset the model random number generator.


seed: A new seed for the RNG; if None, reset using the current seed