Add replication and experimentation support to DEVS Simulator
What's the problem this feature will solve?
The new mesa.experimental.devs.Simulator currently supports single-run execution but lacks native support for batch processing or replications. For stochastic Agent-Based Models (ABMs), running a model once is insufficient, researchers need to run N replications with different random seeds to establish statistical significance.
The source code explicitly identifies this gap in mesa/experimental/devs/simulator.py (Lines 51-52):
# TODO: add replication support
# TODO: add experimentation support
Currently there's no way to:
- Run the same model N times with different seeds
- Aggregate
DataCollectorresults (mean, std) across runs - Conduct parameter sweep experiments native to the simulator
Describe the solution you'd like
I propose implementing a run_replications method directly within the base Simulator class. This allows both ABMSimulator and DEVSimulator to inherit the functionality.
The method will orchestrate the model lifecycle: instantiation, execution, data collection, and teardown/reset.
from mesa.experimental.devs import Simulator
sim = Simulator(time_unit=float, start_time=0.0)
# The simulator handles re-instantiating the model class N times.
results = sim.run_replications(
model_class=MyModel,
model_params={"width": 10, "height": 10, "n_agents": 50},
replications=100, # Number of independent runs
base_seed=42, # Starting seed for reproducibility
max_time=1000 # Stop condition (supports int or float)
)
# Returns a standardized Pandas DataFrame with a 'RunId' column
# Example: Calculate average infection rate across all 100 runs
mean_results = results.groupby("RunId")["infection_rate"].mean()
Implementation approach:
- Seed Management: Utilize
numpy.random.SeedSequenceto generate a non-overlapping stream of seeds from thebase_seed. Lifecycle: The method will handle themodel = model_class(...)->sim.setup(model)->sim.run_until()->collect data->sim.reset()loop. - Compatibility: Since the base
Simulatoruses generic time units, this will supportmax_timeas either integers (for ABM) or floats (for DEVS).
Additional context
-
Target File:
mesa/experimental/devs/simulator.py -
Prior Art: Logic will be adapted from
mesa/batchrunner.pyto ensure feature parity (e.g., consistent data collection columns) but optimized for the event-driven loop. -
Testing: I plan to include a basic test suite to verify that running replications with a fixed
base_seedproduces deterministic, reproducible results.
Question: I am leaning towards integrating this into Simulator rather than a separate DEVSBatchRunner class to keep the API surface clean. Does this align with the long-term vision for the experimental module?
Happy to implement with tests and docs if this approach looks good.
There are to many moving parts at the moment related to this. We have the new Scenario class that contains rng. The simulator is being integrated into the core of mesa. A new run control API is emerging. We are also fully reworking data collection, and there are plans for a full replacement of how to run ensembles of experiments. And there is still an open PR on using numpy's spawn method to create independent random number generators from a given initial seed value.
The proposed direction does not align well with any of these developments. Rather, replications must be integrated either into the scenario class, into the emerging run control API, or the generation and execution of experiments.
Thanks @quaquel for the detailed explanation.
After reading the Mesa 4 goals discussion (#2972), I understand now that these areas are part of the larger v3→v4 architectural redesign involving the Scenario class, new RunControl API, and data collection overhaul.
I'll focus my contributions on more stable areas aligned with the project's current direction. I've been working on updating examples to use new patterns (e.g., projectmesa/mesa-examples#316 + PR #317 for an rng fix in BankReserves).
Should I close this issue, or would you prefer to keep it open as a placeholder for when the new pieces are more concrete?
Thanks again for the guidance