-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
EventList.peak_ahead() returns events in wrong order #3009
Copy link
Copy link
Closed
Description
Describe the bug
The peak_ahead(n) method in EventList iterates directly over the internal heap list (self._events), which is not sorted. A binary heap only guarantees the minimum element at index 0; the rest is in heap-structure order, not chronological order.
Location: mesa/experimental/devs/eventlist.py, method peak_ahead()
Expected behavior
peak_ahead(n) should return the next n events in chronological order (sorted by time, priority, then unique_id).
Example: Events at times [5, 8, 10, 15, 20] - peak_ahead(5) should return them in that order.
Actual behavior: Returns [5, 15, 8, 25, 20] (heap storage order, not sorted).
Proposed Fix
Use heapq.nsmallest() instead of iterating the heap list directly.
Additional context
- Impact: Debugging tools, UI dashboards, and AI look-ahead get wrong event order.
- Note:
pop_event()works correctly bug only affects the inspection API. - Typo: Method should be
peek_ahead(peek = look), notpeak_ahead.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels