mesa package#

Submodules#

mesa.agent module#

Agent related classes.

Core Objects: Agent.

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

Bases: Generic

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

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.

If the agent is currently performing an action, the action’s scheduled completion event is cancelled silently. The action’s on_interrupt() callback is NOT fired, because the agent is being destroyed — not making a behavioral decision. The action moves to no defined end state; it is simply abandoned.

If your action holds external resources (e.g., a Resource slot, a reservation, a lock), override Agent.remove() and call self.cancel_action() before super().remove() to ensure on_interrupt() fires and cleanup logic runs:

def remove(self):

self.cancel_action() # Fires on_interrupt for cleanup super().remove()

Notes

This is a deliberate design choice. The default silent cleanup is safe and avoids callbacks touching agent state during teardown. Models that need cleanup should opt in explicitly.

step() None[source]#

A single step of the agent.

advance() None[source]#
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.

start_action(action: Action) Action[source]#

Start performing an action.

The action must be in PENDING or INTERRUPTED state and the agent must not be currently performing another action.

Parameters:

action – The Action to perform. Must have been created with this agent as its agent.

Returns:

The started Action.

Raises:

ValueError – If the agent is already performing an action, or if the action doesn’t belong to this agent.

interrupt_for(new_action: Action) bool[source]#

Interrupt the current action and start a new one.

If there is no current action, simply starts the new one. If the current action is non-interruptible, returns False and does nothing.

Parameters:

new_action – The Action to perform instead.

Returns:

True if the new action was started (either no current action, or the current one was successfully interrupted). False if the current action is non-interruptible.

cancel_action() bool[source]#

Cancel the current action, ignoring interruptible flag.

Calls on_interrupt with partial progress. Returns False only if there is no current action.

Returns:

True if an action was cancelled, False if idle.

property is_busy: bool#

Whether the agent is currently performing an action.

mesa.datacollection module#

Mesa Data Collection Module.

DataCollector is meant to provide a simple, standard way to collect data generated by a Mesa model. It collects four types of data: model-level data, agent-level data, agent-type-level data, and tables.

A DataCollector is instantiated with three dictionaries of reporter names and associated variable names or functions for each, one for model-level data, one for agent-level data, and one for agent-type-level data; a fourth dictionary provides table names and columns. Variable names are converted into functions which retrieve attributes of that name.

When the collect() method is called, each model-level function is called, with the model as the argument, and the results associated with the relevant variable. Then the agent-level functions are called on each agent, and the agent-type-level functions are called on each agent of the specified type.

Additionally, other objects can write directly to tables by passing in an appropriate dictionary object for a table row.

The DataCollector then stores the data it collects in dictionaries:
  • model_vars maps each reporter to a list of its values

  • tables maps each table to a dictionary, with each column as a key with a list as its value.

  • _agent_records maps each model step to a list of each agent’s id and its values.

  • _agenttype_records maps each model step to a dictionary of agent types, each containing a list of each agent’s id and its values.

Finally, DataCollector can create a pandas DataFrame from each collection.

class DataCollector(model_reporters=None, agent_reporters=None, agenttype_reporters=None, tables=None)[source]#

Bases: object

Class for collecting data generated by a Mesa model.

A DataCollector is instantiated with dictionaries of names of model-, agent-, and agent-type-level variables to collect, associated with attribute names or functions which actually collect them. When the collect(…) method is called, it collects these attributes and executes these functions one by one and stores the results.

Instantiate a DataCollector with lists of model, agent, and agent-type reporters.

Both model_reporters, agent_reporters, and agenttype_reporters accept a dictionary mapping a variable name to either an attribute name, a function, a method of a class/instance, a partial function, or a function with parameters placed in a list.

Model reporters can take five types of arguments: 1. Lambda function:

{“agent_count”: lambda m: len(m.agents)}

  1. Method of a class/instance: {“agent_count”: self.get_agent_count} # self here is a class instance {“agent_count”: Model.get_agent_count} # Model here is a class

  2. Class attributes of a model: {“model_attribute”: “model_attribute”}

  3. Partial function: {“agent_count”: functools.partial(count_agents, multiplier=2)}

  4. Functions with parameters that have been placed in a list: {“Model_Function”: [function, [param_1, param_2]]}

Agent reporters can similarly take: 1. Attribute name (string) referring to agent’s attribute:

{“energy”: “energy”}

  1. Lambda function: {“energy”: lambda a: a.energy}

  2. Method of an agent class/instance: {“agent_action”: self.do_action} # self here is an agent class instance {“agent_action”: Agent.do_action} # Agent here is a class

  3. Partial function: {“energy”: functools.partial(get_energy, scale=2)}

  4. Functions with parameters placed in a list: {“Agent_Function”: [function, [param_1, param_2]]}

Agenttype reporters take a dictionary mapping agent types to dictionaries of reporter names and attributes/funcs/methods, similar to agent_reporters:

{Wolf: {“energy”: lambda a: a.energy}}

The tables arg accepts a dictionary mapping names of tables to lists of columns. For example, if we want to allow agents to write their age when they are destroyed (to keep track of lifespans), it might look like:

{“Lifespan”: [“unique_id”, “age”]}

Parameters:
  • model_reporters – Dictionary of reporter names and attributes/funcs/methods.

  • agent_reporters – Dictionary of reporter names and attributes/funcs/methods.

  • agenttype_reporters – Dictionary of agent types to dictionaries of reporter names and attributes/funcs/methods.

  • tables – Dictionary of table names to lists of column names.

Notes

  • If you want to pickle your model you must not use lambda functions.

  • If your model includes a large number of agents, it is recommended to use attribute names for the agent reporter, as it will be faster.

collect(model)[source]#

Collect all the data for the given model object.

add_table_row(table_name, row, ignore_missing=False)[source]#

Add a row dictionary to a specific table.

Parameters:
  • table_name – Name of the table to append a row to.

  • row – A dictionary of the form {column_name: value…}

  • ignore_missing – If True, fill any missing columns with Nones; if False, throw an error if any columns are missing

get_model_vars_dataframe()[source]#

Create a pandas DataFrame from the model variables.

The DataFrame has one column for each model variable, and the index is (implicitly) the model tick.

get_agent_vars_dataframe()[source]#

Create a pandas DataFrame from the agent variables.

The DataFrame has one column for each variable, with two additional columns for tick and agent_id.

get_agenttype_vars_dataframe(agent_type)[source]#

Create a pandas DataFrame from the agent-type variables for a specific agent type.

The DataFrame has one column for each variable, with two additional columns for tick and agent_id.

Parameters:

agent_type – The type of agent to get the data for.

get_table_dataframe(table_name)[source]#

Create a pandas DataFrame from a particular table.

Parameters:

table_name – The name of the table to convert.

mesa.main module#

mesa.model module#

The model class for Mesa framework.

Core Objects: Model

class Model(*args: Any, rng: RNGLike | SeedLike | None = None, scenario: S | type[S] = <class 'mesa.experimental.scenarios.scenario.Scenario'>, **kwargs: Any)[source]#

Bases: HasEmitters, Generic

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.

Type Parameters:

A: The agent type used in this model S: The scenario type used in this model

running#

A boolean indicating if the model should continue running.

steps#

the number of times model.step() has been called.

time#

the current simulation time.

random#

a seeded python.random number generator.

rng#

a seeded numpy.random.Generator

scenario#

the scenario instance containing model parameters

Notes

Model.agents returns the AgentSet containing all agents registered with the model. Changing the content of the AgentSet directly can result in strange behavior. If you want change the composition of this AgentSet, ensure you operate on a copy.

Create a new model.

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

Parameters:
  • args – arguments to pass onto super

  • rng – Seed for the random number generator. Accepts any value accepted by numpy.random.default_rng(). Ignored if a Scenario instance is passed; used to instantiate the scenario when a Scenario class is passed.

  • scenario – A Scenario instance or subclass to use for this model. If a class is passed it is instantiated with rng. If an instance is passed, rng must not be set.

  • kwargs – keyword arguments to pass onto super

Notes

Pass either rng or a Scenario instance, not both. Passing rng alongside a Scenario class is valid — rng is forwarded to the class constructor.

time: float#

Observable descriptor.

An observable is an attribute that emits ObservableSignals.CHANGED whenever it is changed to a different value.

property scenario: S#

Return scenario instance.

property agents: _HardKeyAgentSet[A]#

Provides a _HardKeyAgentSet of all agents in the model, combining agents from all types.

Returns:

The agent set containing all agents with strong references.

Return type:

_HardKeyAgentSet

Warning

This returns the actual internal _HardKeyAgentSet used by Mesa for agent registration and tracking. It uses strong references to prevent premature garbage collection and reduce performance overhead caused by weak reference management.

Do not modify this AgentSet directly (e.g., by adding or removing agents manually). Direct modifications can break the model’s agent tracking system and cause unexpected behavior. Instead:

  • Use Agent() to create new agents (automatically registers them)

  • Use agent.remove() to remove agents (automatically deregisters them)

  • For read-only operations or transformations, work on a copy: model.agents.copy()

Notes

This is Mesa’s core agent registration system. All agents created via Agent.__init__ are automatically registered here.

property agent_types: list[type]#

Return a list of all unique agent types registered with the model.

property agents_by_type: dict[type[A], _HardKeyAgentSet[A]]#

A dictionary where keys are agent types and values are the corresponding _HardKeyAgentSets.

Returns:

Dictionary mapping agent types to their AgentSets.

Return type:

dict[type[A], _HardKeyAgentSet[A]]

Warning

Each AgentSet in this dictionary is a _HardKeyAgentSet with strong references, forming part of Mesa’s core agent registration system.

Do not modify these AgentSets directly. Direct modifications can break agent tracking and cause unexpected behavior. Instead:

  • Use Agent() to create new agents (automatically registers them)

  • Use agent.remove() to remove agents (automatically deregisters them)

  • For read-only operations, work on copies: model.agents_by_type[AgentType].copy()

Notes

This is part of Mesa’s core agent registration system. All agents are automatically registered in the appropriate type-specific AgentSet when created via Agent.__init__.

register_agent(agent: A)[source]#

Register the agent with the model.

Parameters:

agent – The agent to register.

Notes

This method is called automatically by Agent.__init__, so there is no need to use this if you are subclassing Agent and calling its super in the __init__ method.

deregister_agent(agent: A)[source]#

Deregister the agent with the model.

Parameters:

agent – The agent to deregister.

Notes

This method is called automatically by Agent.remove

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.

observables: dict[str, type[SignalType] | frozenset[SignalType]] = {'agents': frozenset({ModelSignals.AGENT_ADDED, ModelSignals.AGENT_REMOVED}), 'model': frozenset({ModelSignals.RUN_ENDED}), 'time': <enum 'ObservableSignals'>}#
remove_all_agents()[source]#

Remove all agents from the model.

Notes

This method calls agent.remove for all agents in the model. If you need to remove agents from e.g., a SingleGrid, you can either explicitly implement your own agent.remove method or clean this up near where you are calling this method.

schedule_event(function: Callable, *, at: float | None = None, after: float | None = None, priority: Priority = Priority.DEFAULT) Event[source]#

Schedule a one-off event.

Parameters:
  • function – The callable to execute

  • at – Absolute time to execute (mutually exclusive with after)

  • after – Relative time from now to execute (mutually exclusive with at)

  • priority – Priority level for the event

Returns:

The scheduled Event (can be used to cancel)

Raises:
  • ValueError – If both or neither of at/after are specified

  • ValueError – If both or neither of at/after are specified, or if the scheduled time is in the past.

schedule_recurring(function: Callable, schedule: Schedule, priority: Priority = Priority.DEFAULT) EventGenerator[source]#

Schedule a recurring event based on a Schedule.

Parameters:
  • function – The callable to execute repeatedly

  • schedule – The Schedule defining when events occur

  • priority – Priority level for generated events

Returns:

The EventGenerator (can be used to stop)

Raises:

ValueError – If the schedule start time is in the past.

run_for(duration: float | int) None[source]#

Run the model for the specified duration.

Parameters:

duration – Time units to advance

run_until(end_time: float | int) None[source]#

Run the model until the specified time.

Parameters:

end_time – Absolute time to run until

If model.time is larger than end_time, the method returns directly.

Module contents#

Mesa Agent-Based Modeling Framework.

Core Objects: Model, and Agent.

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

Bases: Generic

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

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.

If the agent is currently performing an action, the action’s scheduled completion event is cancelled silently. The action’s on_interrupt() callback is NOT fired, because the agent is being destroyed — not making a behavioral decision. The action moves to no defined end state; it is simply abandoned.

If your action holds external resources (e.g., a Resource slot, a reservation, a lock), override Agent.remove() and call self.cancel_action() before super().remove() to ensure on_interrupt() fires and cleanup logic runs:

def remove(self):

self.cancel_action() # Fires on_interrupt for cleanup super().remove()

Notes

This is a deliberate design choice. The default silent cleanup is safe and avoids callbacks touching agent state during teardown. Models that need cleanup should opt in explicitly.

step() None[source]#

A single step of the agent.

advance() None[source]#
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.

start_action(action: Action) Action[source]#

Start performing an action.

The action must be in PENDING or INTERRUPTED state and the agent must not be currently performing another action.

Parameters:

action – The Action to perform. Must have been created with this agent as its agent.

Returns:

The started Action.

Raises:

ValueError – If the agent is already performing an action, or if the action doesn’t belong to this agent.

interrupt_for(new_action: Action) bool[source]#

Interrupt the current action and start a new one.

If there is no current action, simply starts the new one. If the current action is non-interruptible, returns False and does nothing.

Parameters:

new_action – The Action to perform instead.

Returns:

True if the new action was started (either no current action, or the current one was successfully interrupted). False if the current action is non-interruptible.

cancel_action() bool[source]#

Cancel the current action, ignoring interruptible flag.

Calls on_interrupt with partial progress. Returns False only if there is no current action.

Returns:

True if an action was cancelled, False if idle.

property is_busy: bool#

Whether the agent is currently performing an action.

class DataCollector(model_reporters=None, agent_reporters=None, agenttype_reporters=None, tables=None)[source]#

Bases: object

Class for collecting data generated by a Mesa model.

A DataCollector is instantiated with dictionaries of names of model-, agent-, and agent-type-level variables to collect, associated with attribute names or functions which actually collect them. When the collect(…) method is called, it collects these attributes and executes these functions one by one and stores the results.

Instantiate a DataCollector with lists of model, agent, and agent-type reporters.

Both model_reporters, agent_reporters, and agenttype_reporters accept a dictionary mapping a variable name to either an attribute name, a function, a method of a class/instance, a partial function, or a function with parameters placed in a list.

Model reporters can take five types of arguments: 1. Lambda function:

{“agent_count”: lambda m: len(m.agents)}

  1. Method of a class/instance: {“agent_count”: self.get_agent_count} # self here is a class instance {“agent_count”: Model.get_agent_count} # Model here is a class

  2. Class attributes of a model: {“model_attribute”: “model_attribute”}

  3. Partial function: {“agent_count”: functools.partial(count_agents, multiplier=2)}

  4. Functions with parameters that have been placed in a list: {“Model_Function”: [function, [param_1, param_2]]}

Agent reporters can similarly take: 1. Attribute name (string) referring to agent’s attribute:

{“energy”: “energy”}

  1. Lambda function: {“energy”: lambda a: a.energy}

  2. Method of an agent class/instance: {“agent_action”: self.do_action} # self here is an agent class instance {“agent_action”: Agent.do_action} # Agent here is a class

  3. Partial function: {“energy”: functools.partial(get_energy, scale=2)}

  4. Functions with parameters placed in a list: {“Agent_Function”: [function, [param_1, param_2]]}

Agenttype reporters take a dictionary mapping agent types to dictionaries of reporter names and attributes/funcs/methods, similar to agent_reporters:

{Wolf: {“energy”: lambda a: a.energy}}

The tables arg accepts a dictionary mapping names of tables to lists of columns. For example, if we want to allow agents to write their age when they are destroyed (to keep track of lifespans), it might look like:

{“Lifespan”: [“unique_id”, “age”]}

Parameters:
  • model_reporters – Dictionary of reporter names and attributes/funcs/methods.

  • agent_reporters – Dictionary of reporter names and attributes/funcs/methods.

  • agenttype_reporters – Dictionary of agent types to dictionaries of reporter names and attributes/funcs/methods.

  • tables – Dictionary of table names to lists of column names.

Notes

  • If you want to pickle your model you must not use lambda functions.

  • If your model includes a large number of agents, it is recommended to use attribute names for the agent reporter, as it will be faster.

collect(model)[source]#

Collect all the data for the given model object.

add_table_row(table_name, row, ignore_missing=False)[source]#

Add a row dictionary to a specific table.

Parameters:
  • table_name – Name of the table to append a row to.

  • row – A dictionary of the form {column_name: value…}

  • ignore_missing – If True, fill any missing columns with Nones; if False, throw an error if any columns are missing

get_model_vars_dataframe()[source]#

Create a pandas DataFrame from the model variables.

The DataFrame has one column for each model variable, and the index is (implicitly) the model tick.

get_agent_vars_dataframe()[source]#

Create a pandas DataFrame from the agent variables.

The DataFrame has one column for each variable, with two additional columns for tick and agent_id.

get_agenttype_vars_dataframe(agent_type)[source]#

Create a pandas DataFrame from the agent-type variables for a specific agent type.

The DataFrame has one column for each variable, with two additional columns for tick and agent_id.

Parameters:

agent_type – The type of agent to get the data for.

get_table_dataframe(table_name)[source]#

Create a pandas DataFrame from a particular table.

Parameters:

table_name – The name of the table to convert.

class Model(*args: Any, rng: RNGLike | SeedLike | None = None, scenario: S | type[S] = <class 'mesa.experimental.scenarios.scenario.Scenario'>, **kwargs: Any)[source]#

Bases: HasEmitters, Generic

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.

Type Parameters:

A: The agent type used in this model S: The scenario type used in this model

running#

A boolean indicating if the model should continue running.

Type:

bool

steps#

the number of times model.step() has been called.

time#

the current simulation time.

Type:

float

random#

a seeded python.random number generator.

rng#

a seeded numpy.random.Generator

Type:

numpy.random._generator.Generator

scenario#

the scenario instance containing model parameters

Notes

Model.agents returns the AgentSet containing all agents registered with the model. Changing the content of the AgentSet directly can result in strange behavior. If you want change the composition of this AgentSet, ensure you operate on a copy.

Create a new model.

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

Parameters:
  • args – arguments to pass onto super

  • rng – Seed for the random number generator. Accepts any value accepted by numpy.random.default_rng(). Ignored if a Scenario instance is passed; used to instantiate the scenario when a Scenario class is passed.

  • scenario – A Scenario instance or subclass to use for this model. If a class is passed it is instantiated with rng. If an instance is passed, rng must not be set.

  • kwargs – keyword arguments to pass onto super

Notes

Pass either rng or a Scenario instance, not both. Passing rng alongside a Scenario class is valid — rng is forwarded to the class constructor.

running: bool#
time: float#

Observable descriptor.

An observable is an attribute that emits ObservableSignals.CHANGED whenever it is changed to a different value.

agent_id_counter: int#
property scenario: S#

Return scenario instance.

rng: Generator#
property agents: _HardKeyAgentSet[A]#

Provides a _HardKeyAgentSet of all agents in the model, combining agents from all types.

Returns:

The agent set containing all agents with strong references.

Return type:

_HardKeyAgentSet

Warning

This returns the actual internal _HardKeyAgentSet used by Mesa for agent registration and tracking. It uses strong references to prevent premature garbage collection and reduce performance overhead caused by weak reference management.

Do not modify this AgentSet directly (e.g., by adding or removing agents manually). Direct modifications can break the model’s agent tracking system and cause unexpected behavior. Instead:

  • Use Agent() to create new agents (automatically registers them)

  • Use agent.remove() to remove agents (automatically deregisters them)

  • For read-only operations or transformations, work on a copy: model.agents.copy()

Notes

This is Mesa’s core agent registration system. All agents created via Agent.__init__ are automatically registered here.

property agent_types: list[type]#

Return a list of all unique agent types registered with the model.

property agents_by_type: dict[type[A], _HardKeyAgentSet[A]]#

A dictionary where keys are agent types and values are the corresponding _HardKeyAgentSets.

Returns:

Dictionary mapping agent types to their AgentSets.

Return type:

dict[type[A], _HardKeyAgentSet[A]]

Warning

Each AgentSet in this dictionary is a _HardKeyAgentSet with strong references, forming part of Mesa’s core agent registration system.

Do not modify these AgentSets directly. Direct modifications can break agent tracking and cause unexpected behavior. Instead:

  • Use Agent() to create new agents (automatically registers them)

  • Use agent.remove() to remove agents (automatically deregisters them)

  • For read-only operations, work on copies: model.agents_by_type[AgentType].copy()

Notes

This is part of Mesa’s core agent registration system. All agents are automatically registered in the appropriate type-specific AgentSet when created via Agent.__init__.

register_agent(agent: A)[source]#

Register the agent with the model.

Parameters:

agent – The agent to register.

Notes

This method is called automatically by Agent.__init__, so there is no need to use this if you are subclassing Agent and calling its super in the __init__ method.

deregister_agent(agent: A)[source]#

Deregister the agent with the model.

Parameters:

agent – The agent to deregister.

Notes

This method is called automatically by Agent.remove

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.

observables: dict[str, type[SignalType] | frozenset[SignalType]] = {'agents': frozenset({ModelSignals.AGENT_ADDED, ModelSignals.AGENT_REMOVED}), 'model': frozenset({ModelSignals.RUN_ENDED}), 'time': <enum 'ObservableSignals'>}#
remove_all_agents()[source]#

Remove all agents from the model.

Notes

This method calls agent.remove for all agents in the model. If you need to remove agents from e.g., a SingleGrid, you can either explicitly implement your own agent.remove method or clean this up near where you are calling this method.

schedule_event(function: Callable, *, at: float | None = None, after: float | None = None, priority: Priority = Priority.DEFAULT) Event[source]#

Schedule a one-off event.

Parameters:
  • function – The callable to execute

  • at – Absolute time to execute (mutually exclusive with after)

  • after – Relative time from now to execute (mutually exclusive with at)

  • priority – Priority level for the event

Returns:

The scheduled Event (can be used to cancel)

Raises:
  • ValueError – If both or neither of at/after are specified

  • ValueError – If both or neither of at/after are specified, or if the scheduled time is in the past.

schedule_recurring(function: Callable, schedule: Schedule, priority: Priority = Priority.DEFAULT) EventGenerator[source]#

Schedule a recurring event based on a Schedule.

Parameters:
  • function – The callable to execute repeatedly

  • schedule – The Schedule defining when events occur

  • priority – Priority level for generated events

Returns:

The EventGenerator (can be used to stop)

Raises:

ValueError – If the schedule start time is in the past.

run_for(duration: float | int) None[source]#

Run the model for the specified duration.

Parameters:

duration – Time units to advance

run_until(end_time: float | int) None[source]#

Run the model until the specified time.

Parameters:

end_time – Absolute time to run until

If model.time is larger than end_time, the method returns directly.