Implement unified time and scheduling API #2928
Draft
+532
−369
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a pathfinding PR for #2921, under active development. The goal is to explore the structure, design and API, and gather feedback before finalizing. Breaking changes may occur as we iterate.
Summary
This PR introduces a unified API for time advancement and event scheduling, integrating these capabilities directly into the Model class. It represents the first implementation step toward the design outlined in #2921.
Motive
Currently, Mesa's experimental discrete event simulation requires users to manage two separate objects:
This raises unnecessary questions: Why do I need a simulator? What's the difference between ABMSimulator and DEVSimulator? What does setup() do? For most users building standard ABMs, these questions shouldn't exist.
The deeper motivation is enabling a smooth progression from simple step-based models to full discrete-event simulation without rewriting code. A user should be able to:
step()andrun(steps=100)model.schedule(drought, at=50))self.model.schedule(self.release, after=sentence))All using the same unified API, without switching between different simulator classes or paradigms.
Implementation
The implementation uses composition to keep concerns separated:
Scheduler(new class inscheduler.py): Owns the EventList and implements all scheduling/execution logicModel: Adds thin delegation methods (schedule(),run(),cancel()) that forward to the internal SchedulerSimulatorclasses: Deprecated but still functional, now delegate to Model's APIThis design keeps Model focused (~50 new lines) while the Scheduler contains the complexity (~150 lines). The Scheduler can be tested independently and potentially subclassed for custom behavior.
Usage Examples
API is non-final, we're currently exploring both keyword argument and separate method APIs. This implemenation uses keyword arguments.
Simple step-based model (unchanged):
Adding scheduled events:
Migration from old API:
Additional Notes
What this PR does NOT include (yet):
recurringparameter for automatic method scheduling (needs more design discussion)@scheduled)Open questions for reviewers:
_schedulerbe public (scheduler) for advanced users?Related:
mesa/experimental/devs/