mesa icon indicating copy to clipboard operation
mesa copied to clipboard

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

Open Nithin9585 opened this issue 11 months ago โ€ข 1 comments

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

Nithin9585 avatar Dec 23 '25 16:12 Nithin9585

๐Ÿ“ CodeRabbit Plan Mode

Generate an implementation plan and prompts that you can use with your favorite coding agent.

  • [ ] Create Plan
Examples

๐Ÿงช Issue enrichment is currently in open beta.

You can configure auto-planning by selecting labels in the issue_enrichment configuration.

To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

issue_enrichment:
  auto_enrich:
    enabled: false

๐Ÿ’ฌ Have feedback or questions? Drop into our discord or schedule a call!

coderabbitai[bot] avatar Dec 23 '25 16:12 coderabbitai[bot]