Model#
The model class for Mesa framework.
Core Objects: Model
- class Model(*args: Any, seed: float | None = None, rng: Generator | BitGenerator | int | integer | Sequence[int] | SeedSequence | None = None, **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.
- steps#
the number of times model.step() has been called.
- random#
a seeded python.random number generator.
- rng#
a seeded numpy.random.Generator
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
seed – the seed for the random number generator
rng – Pseudorandom number generator state. When rng is None, a new numpy.random.Generator is created using entropy from the operating system. Types other than numpy.random.Generator are passed to numpy.random.default_rng to instantiate a Generator.
kwargs – keyword arguments to pass onto super
Notes
you have to pass either seed or rng, but not both.
- 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 all unique agent types registered with the model.
- property agents_by_type: dict[type[Agent], AgentSet]#
A dictionary where the keys are agent types and the values are the corresponding AgentSets.
- register_agent(agent)[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)[source]#
Deregister the agent with the model.
- Parameters:
agent – The agent to deregister.
Notes
This method is called automatically by
Agent.remove
- reset_randomizer(seed: int | None = None) None [source]#
Reset the model random number generator.
- Parameters:
seed – A new seed for the RNG; if None, reset using the current seed
- reset_rng(rng: Generator | BitGenerator | int | integer | Sequence[int] | SeedSequence | None = None) None [source]#
Reset the model random number generator.
- Parameters:
rng – A new seed for the RNG; if None, reset using the current seed
- 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.