Skip to content

Event scheduling allows execution of events in the past violating time monotonicity #3342

@souro26

Description

@souro26

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: 15

The 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelease notes label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions