-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Event scheduling allows execution of events in the past violating time monotonicity #3342
Copy link
Copy link
Closed
Labels
bugRelease notes labelRelease notes label
Description
Summary
It is currently possible to schedule and execute events in the past relative to the model’s current time. This allows the model clock to move backwards during execution, which appears to violate time monotonicity guarantees.
Reproduction
from mesa import Model
log = []
def event1():
log.append(("event1", model.time))
def event2():
log.append(("event2", model.time))
model = Model()
# Run forward
model.schedule_event(event1, at=5)
model.run_until(10)
# Now schedule an event in the past
model.schedule_event(event2, at=3)
model.run_until(15)
print("Log:", log)
print("Final time:", model.time)Observed output
Log: [('event1', 5), ('event2', 3)]
Final time: 15The model time moves from 10 to 3 to 15 during execution. The current _advance_time implementation sets self.time = event.time without enforcing that event.time >= current_time.
This means:
- Events scheduled in the past are executed.
- The model clock moves backwards.
- No rollback mechanism exists for model state.
If earlier events mutated state, executing a past event afterward may lead to inconsistent or causally invalid model behavior.
Proposed Change
Enforce a monotonic time invariant. For example, in schedule_event
if time < self.time:
raise ValueError(
f"Cannot schedule event at time {time}, current time is {self.time}"
)Alternatively, enforcing the invariant in _advance_time before executing an event also works
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugRelease notes labelRelease notes label