Skip to content

Using DEVSimulator: How to prevent simulation from stopping when no events are present? #2729

@D-viddddd

Description

@D-viddddd

Problem Description:

I am using DEVSimulator in a Mesa-based simulation, and I noticed that when there are no events in the queue, the simulation stops running. This behavior is problematic when I want to keep the simulation running indefinitely and allow new events to be scheduled dynamically.

Expected Behavior:

  • When there are no events, the simulation should not stop completely but rather wait for new events to be scheduled (or continue advancing time at a fixed interval).
  • Is there a built-in way to handle this behavior in DEVSimulator?

Current Workaround:

To prevent the simulation from stopping, I am considering:

  1. scheduling a periodic "idle" event that does nothing but keeps the queue active.
    such as:
def idle_event(model):
    # idle event can do nothing or just update the time
    pass

# Schedule a periodic idle event after the simulator is initialized (e.g. before run_for())
def schedule_idle_events(simulator, interval=1):
    # schedule an idle event every interval seconds
    simulator.schedule_event_relative(idle_event, interval, function_kwargs={'model': simulator.model})
    # call itself to ensure loop
    simulator.schedule_event_relative(schedule_idle_events, interval, function_kwargs={'simulator': simulator, 'interval': interval})

# schedule the idle event immediately once initializing the simulator
schedule_idle_events(simulator, interval=1)
  1. Modifying the event processing loop(function run_until) to wait instead of stopping when the queue is empty.
    such as:
def run_until(self, end_time):
    while self.time < end_time:
        try:
            event = self.event_list.pop_event()
        except IndexError:
            # the queue is empty, wait for a while, and then continue the cycle
            time.sleep(0.1)
            continue
        self.time = event.time
        event.execute()

Is there a recommended way to handle this within DEVSimulator, or is this expected behavior?

Steps to Reproduce:

  1. Create a DEVSimulator-based simulation.
  2. Run it until all scheduled events are completed.
  3. Observe that the simulation stops running.

System Info:

  • Mesa version: 3.1.4
  • Python version: 3.11.6
  • OS: macOS Sequoia 15.2

Any guidance on the best approach would be greatly appreciated!

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