Proposal: Unified Time and Event Scheduling API #2921
Replies: 3 comments 1 reply
-
An alternative API could be using a decorator: class MyModel(Model):
...
@recurring
def step(self):
self.agents.shuffle_do("step")
@recurring(interval=7)
def weekly_update(self):
self.collect_stats()
def helper_method(self): # Not recurring, no decorator
passExisting models only have to add |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for this. I have been thinking about cleaning up the event scheduling and integrating it better into MESA as well, but you have already done most of the work. Broadly speaking, I endorse the ideas outlined. At long last, it adds proper control for running models to MESA (i.e., run for x steps, run until a given time, etc.). Just a few quick reactions.
At the moment, DEVSSimulator has the following methods
Next to the methods, there is also a check to ensure that valid time units are used. DEVS allows floats and integers; ABM only integers. Lastly, the def __init__(
self,
*args: Any,
seed: float | None = None,
rng: RNGLike | SeedLike | None = None,
simulator:Simulator = ABMSimulator,
**kwargs: Any,
) -> None:This is compatible with the API sketched here, while giving users full control if they want to use full DEVS or just ABM style fixed time advancement. |
Beta Was this translation helpful? Give feedback.
-
|
I am not convinced by your points regarding methods versus keywords. Autocomplete at the method/function level shows up before you dive into keywords and thus, at least for me, works better to find what I need. Moreover, On the recurring point, you might be on to something. So yes, there is indeed a difference between what you call global processes and events.
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
This proposal redesigns Mesa's time advancement and event scheduling into a simple, unified API. The goal: make simple models simple, while enabling advanced discrete-event simulation for those who need it.
And everything in between.
Key changes:
Simulatorclasses (ABMSimulator,DEVSimulator)Modelrun()andschedule()methodsstep()optional sugar that's automatically scheduledMotivation
The problem with two objects
The current experimental DEVS implementation requires users to manage two separate objects:
This raises immediate questions: Why do I need a simulator? What's the difference between
ABMSimulatorandDEVSimulator? What doessetup()do? When would I callreset()?For most users, these questions shouldn't exist. They have a model, they want to run it.
The simplest API imaginable
Let's try to make it as simple as possible. You get something like:
The simulator is an implementation detail. Users shouldn't need to think about it.
Proposed API
Running models
Scheduling events
Model initialization
Usage progression
The API lets users start simple and incrementally adopt advanced features without rewriting their model. I think this is the coolest part of an unified API.
Level 1: Just define step()
This is how most Mesa models work today, and it continues to work unchanged:
No simulators, no setup, no complexity. Define
step(), callrun().Level 2: Add scheduled events
Now suppose you want a drought to occur at t=50. Just schedule it:
The model still steps normally at t=1, 2, 3..., and the drought fires at t=50. No changes to how you run the model.
Level 3: Agents scheduling events
Agents can schedule their own events. This is useful for agents that need to "wake up" after some time:
This is the pattern from Epstein's civil violence model. Jailed citizens don't need to be activated every tick just to check if they're still in jail.
Level 4: Agents with event-driven behavior
Some agents don't need regular activation at all. Grass in Wolf-Sheep only needs to act when eaten:
Instead of activating thousands of grass patches every tick to decrement a counter, each patch just schedules its own regrowth. This can dramatically improve performance.
Level 5: Multiple time scales
Some models have processes that operate at different speeds:
Weather updates 24 times per day, markets once per day, policy once per month. All coexist naturally.
Level 6: Pure event-driven
For true discrete-event simulation with no regular stepping:
Time jumps directly from event to event. No wasted computation on empty ticks.
Configuration reference
Migration
From current step-based models
No changes required:
From experimental Simulator API
Open questions
recurringclear? Alternatives:tick,auto_schedulemodel.stepsalongsidemodel.time? It's redundant for pure event-driven models, but familiar for step-based models.model.run()with no arguments run untilmodel.running = False? Or can it be defined additionally, together with something else?model.step()still work for manualforloops?Feedback welcome
I would love to hear your thoughts:
recurringmethods (step, etc.)Beta Was this translation helpful? Give feedback.
All reactions