Skip to content

DEVSimulator allows time travel (causality violation) with negative time deltas #2998

@Nithin9585

Description

@Nithin9585

Describe the bug
The DEVSimulator.schedule_event_relative() accepts negative time deltas, causing the simulation clock to rewind backwards. This violates the fundamental principle of time monotonicity in discrete event simulations.

Real-World Scenario:
I was building a "Late Student" agent simulation where:

  • A Teacher schedules class to start at t=10.0
  • A Student wakes up late at t=12.0
  • The Student tries to schedule arrival at the class start time

The Student's logic calculates: delay = class_start (10.0) - current_time (12.0) = -2.0

Instead of raising an error, the simulator accepts this and rewinds time from 12.0 to 10.0.


To Reproduce

from mesa.model import Model
from mesa.experimental.devs.simulator import DEVSimulator

class SchoolModel(Model):
    def __init__(self):
        self.time = 0.0

model = SchoolModel()
sim = DEVSimulator()
sim.setup(model)

def start_class():
    print(f"[{model.time}]  CLASS STARTED!")

def student_arrives():
    print(f"[{model.time}]  STUDENT ARRIVES!")

# 1. Schedule class at t=10.0
sim.schedule_event_absolute(start_class, 10.0)

# 2. Run until t=12.0 (Student is late)
sim.run_until(12.0)
print(f"[{model.time}] Student wakes up late")

# 3. Student calculates delay (BUG: negative value!)
class_start_time = 10.0
delay = class_start_time - model.time  # 10.0 - 12.0 = -2.0
print(f"[{model.time}] Student scheduling with delay: {delay}")

# 4. Schedule arrival (should fail, but succeeds)
sim.schedule_event_relative(student_arrives, delay)

# 5. Process event
sim.run_next_event()

print(f"[{model.time}] Final Time")

Actual Output (BUG):

[10.0] CLASS STARTED!
[12.0] Student wakes up late
[12.0] Student scheduling with delay: -2.0
[10.0] STUDENT ARRIVES!
[10.0] Final Time

Time went BACKWARDS from 12.0 → 10.0!
The student arrived at 10.0, but they didn't even wake up until 12.0!


Expected Output:

[10.0] CLASS STARTED!
[12.0] Student wakes up late
[12.0] Student scheduling with delay: -2.0
ValueError: Cannot schedule event in the past (current: 12.0, requested: 10.0)

Expected behavior
The simulator should raise a ValueError when time_delta would result in an event time less than model.time.

Additional context

  • Location: mesa/experimental/devs/simulator.py in schedule_event_relative and _schedule_event

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions