Model#
Quick intro#
The Model orchestrates the simulation lifecycle: creating and registering AgentSet``s, stepping the simulation, and integrating with ``DataCollector and spatial ``Grid``s. Typical usage:
Instantiate
Model, addAgentSetinstances tomodel.sets.Call
model.sets.do('step')inside your model loop to trigger set-level updates.Use
DataCollectorto sample model- and agent-level columns each step.
Minimal example#
from mesa_frames import Model, AgentSet, DataCollector
import polars as pl
class People(AgentSet):
def step(self):
self.add(pl.DataFrame({'wealth': [1, 2, 3]}))
class MyModel(Model):
def __init__(self):
super().__init__()
self.sets += People(self)
self.dc = DataCollector(model_reporters={'avg_wealth': lambda m: m.sets['People'].df['wealth'].mean()})
m = MyModel()
m.step()
API reference#
Lifecycle / Core
Create a new model. |
|
Run a single step. |
|
Run the model until the end condition is reached. |
|
Reset the model random number generator. |
Accessors / Properties
Get the current step count. |
|
Get the AgentSetRegistry object containing all agent sets in the model. |
|
Get the space object associated with the model. |
|
Return the current seed used by the model's random generator. |
- class Model(seed: int | Sequence[int] | None = None)[source]#
Base class for models in the mesa-frames 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.
Methods:
Create a new model.
Reset the model random number generator.
Run the model until the end condition is reached.
Run a single step.
Attributes:
Return the current seed used by the model's random generator.
Get the current step count.
Get the AgentSetRegistry object containing all agent sets in the model.
Get the space object associated with the model.
- __init__(seed: int | Sequence[int] | None = None) None[source]#
Create a new model.
Overload this method with the actual code to start the model. Always start with super().__init__(seed) to initialize the model object properly.
- reset_randomizer(seed: int | Sequence[int] | None) None[source]#
Reset the model random number generator.
- step() None[source]#
Run a single step.
The default method calls the step() method of all agents. Overload as needed.
- property steps: int#
Get the current step count.
- Returns:
The current step count of the model.
- Return type:
- property sets: AgentSetRegistry#
Get the AgentSetRegistry object containing all agent sets in the model.
- Returns:
The AgentSetRegistry object containing all agent sets in the model.
- Return type:
- Raises:
ValueError – If the model has not been initialized properly with super().__init__().
- property space: Space#
Get the space object associated with the model.
- Returns:
The space object associated with the model.
- Return type:
Space
- Raises:
ValueError – If the space has not been set for the model.