Skip to content

Migrate tutorials and examples to model.run_for()#3270

Merged
EwoutH merged 10 commits intomesa:mainfrom
falloficarus22:migrate-run-for
Feb 11, 2026
Merged

Migrate tutorials and examples to model.run_for()#3270
EwoutH merged 10 commits intomesa:mainfrom
falloficarus22:migrate-run-for

Conversation

@falloficarus22
Copy link
Copy Markdown
Contributor

@falloficarus22 falloficarus22 commented Feb 10, 2026

Summary

This PR migrates Mesa's tutorials, examples, and benchmarks to use the new Model.run_for() method. It replaces manual for _ in range(N): model.step() loops with a single, unified call to model.run_for(N).

Motive

With the introduction of event-based scheduling, running a model for a specific duration should be handled by the model's internal time-advancement mechanism rather than external loops. Manual loops calling .step() can lead to inconsistencies when models use discrete-event simulators. model.run_for() provides a declarative, robust, and unified way to advance a simulation regardless of the underlying scheduling mechanism.

Implementation

  1. Tutorials: Updated all Jupyter notebooks in docs/tutorials/ to use run_for() which was introduced in Add public event scheduling and time advancement methods #3266 . This involved updating both the code cells and the surrounding markdown explanation to reflect the recommended usage.
  2. Benchmarks: Refactored benchmarks/global_benchmark.py to use model.run_for(). This significantly simplified the code by removing the need to check if a model was using a Simulator or a standard loop.
  3. Tests:
  • Refactored tests/examples/test_examples.py to replace repetitive test loops with concise run_for() calls.
  • Added a dedicated unit test in tests/test_model.py to verify that run_for() correctly increments both model steps and simulation time.

Usage Examples

Legacy Pattern:

model = MoneyModel(10)
for _ in range(30):
    model.step()

New Pattern:

model = MoneyModel(10)
model.run_for(30)

Additional Notes

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +2.2% [+1.4%, +3.1%] 🔵 -0.4% [-0.6%, -0.2%]
BoltzmannWealth large 🔵 +2.5% [+1.7%, +3.4%] 🔵 +4.2% [+2.1%, +6.1%]
Schelling small 🔵 +0.4% [-0.1%, +0.9%] 🔵 -0.1% [-0.3%, +0.2%]
Schelling large 🔵 -1.8% [-2.6%, -1.2%] 🟢 -10.4% [-12.7%, -7.9%]
WolfSheep small 🔵 -0.4% [-0.9%, +0.0%] 🔵 -1.4% [-1.5%, -1.2%]
WolfSheep large 🔵 -1.7% [-3.7%, -0.2%] 🔵 -3.4% [-5.5%, -1.9%]
BoidFlockers small 🔵 +0.7% [+0.4%, +1.1%] 🔵 +2.6% [+2.5%, +2.7%]
BoidFlockers large 🔵 +2.3% [+1.7%, +2.9%] 🔵 +2.6% [+2.3%, +2.9%]

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Feb 10, 2026

Thanks for working on this. I will look into it after #3266 is merged.

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Feb 11, 2026

#3266 is merged. Go ahead and update/rebase this PR!

@EwoutH EwoutH added docs Release notes label example Changes the examples or adds to them. labels Feb 11, 2026
Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now this is great, thanks.

Of course at some point the tutorial on Agent activation, event scheduling and time progression has to be actually rewritten.

@EwoutH EwoutH removed the example Changes the examples or adds to them. label Feb 11, 2026
@EwoutH EwoutH merged commit 6173154 into mesa:main Feb 11, 2026
14 of 15 checks passed
@falloficarus22 falloficarus22 deleted the migrate-run-for branch February 11, 2026 13:46
@EwoutH EwoutH mentioned this pull request Feb 11, 2026
42 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants