projectmesa / mesa

Mesa is an open-source Python library for agent-based modeling, ideal for simulating complex systems and exploring emergent behaviors.
https://mesa.readthedocs.io
Other
2.39k stars 863 forks source link

Enable DiscreteEventScheduler to handle system-level events and task interruptions #1893

Open EwoutH opened 10 months ago

EwoutH commented 10 months ago

The current implementation of the DiscreteEventScheduler (as of #1890) could be improved by allowing task interruptions and resumptions, and integrating system-level event handling.

Motivation

System-level events, such as policy changes or environmental shifts, can be crucial for simulating real-world scenarios. By enabling agents to interrupt and resume tasks and react to system-wide changes, we allows new systems to be modelled.

Description

The current implementation of the DiscreteEventScheduler in Mesa works great for simulations where agents operate based on discrete events. However, for more complex scenarios, especially those requiring dynamic interruption and resumption of tasks, as well as system-level event responses, the scheduler could benefit from additional functionalities.

Proposed Enhancements:

  1. Enhanced Event Scheduling:

    • Modify schedule_event to check an agent's current task status before scheduling new tasks.
    • Implement logic to handle scenarios where an agent is already engaged in a task (e.g., delay or reschedule the new task).
  2. Basic Interruption Handling:

    • Introduce a method in DiscreteEventScheduler to interrupt an agent's current task.
    • Update the agent's status to reflect the interruption and, optionally, reschedule the interrupted task for a later time.
  3. System-Level Event Integration:

    • Implement a feature to schedule system-wide events that can affect multiple or all agents.
    • These events could trigger task interruptions or other significant changes in agent behavior.
  4. Resumption Logic:

    • Develop a mechanism within agents to resume interrupted tasks.
    • This would involve checking their status at the beginning of their step method and deciding whether to continue a previous task or start a new one.

There is probably more that could fit in there. I think the most important is to think about the concepts of tasks in Mesa, a thing that an agent is currently working on, has some future benefit and can be interrupted by different things.

EwoutH commented 2 days ago

I wrote this issue before the whole new DEVS scheduler was in. Now that it is, I would like to revisit it.

I want to introduce especially the concept of a Task. A Task takes a non-fixed time, which can be determined stochastically. It might also be able to be interrupted by an other agent or a system level event, and if so, there are multiple options to handle that.

Expect some work on this the next few days/weeks.

CC @quaquel and @Corvince