Skip to content

Fix: peak_ahead returns events in correct chronological order#3010

Merged
quaquel merged 3 commits intomesa:mainfrom
Nithin9585:fix-peak-ahead-order
Dec 26, 2025
Merged

Fix: peak_ahead returns events in correct chronological order#3010
quaquel merged 3 commits intomesa:mainfrom
Nithin9585:fix-peak-ahead-order

Conversation

@Nithin9585
Copy link
Copy Markdown
Contributor

@Nithin9585 Nithin9585 commented Dec 24, 2025

Summary

Fixed EventList.peek_ahead() returning events in wrong order and renamed from peak_ahead to peek_ahead (typo fix).

Bug

Fixes #3009

peek_ahead(n) was iterating directly over the internal heap list, which is NOT sorted. A binary heap only guarantees the minimum element at index 0.

Before: Events returned in heap order e.g., [5, 15, 8, 25, 20] (15 before 8 is wrong!)
After: Events returned in chronological order [5, 8, 10, 15, 20]

Implementation

  • Added nsmallest to heapq imports
  • Replaced direct heap iteration with heapq.nsmallest(n, valid_events)
  • Renamed peak_ahead → peek_ahead (peek = to look, peak = mountain top)

Testing

  • Added test verifying chronological ordering
  • All existing tests pass

Additional Notes

Breaking change: external code using peak_ahead() needs to rename to peek_ahead().

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.1% [+0.4%, +1.8%] 🔵 -0.9% [-1.0%, -0.8%]
BoltzmannWealth large 🔵 +1.4% [-0.6%, +4.5%] 🔵 -1.9% [-3.0%, -0.7%]
Schelling small 🔵 +0.7% [-0.2%, +1.7%] 🔵 +0.1% [-0.3%, +0.6%]
Schelling large 🔵 +1.1% [+0.1%, +2.7%] 🔵 -0.3% [-0.9%, +0.4%]
WolfSheep small 🔵 -0.8% [-1.7%, -0.2%] 🔵 -0.7% [-0.9%, -0.4%]
WolfSheep large 🔵 +0.4% [-3.1%, +2.4%] 🔵 +3.3% [+1.6%, +5.1%]
BoidFlockers small 🔵 +3.2% [+2.9%, +3.5%] 🔵 +1.8% [+1.6%, +2.0%]
BoidFlockers large 🔵 +2.6% [+2.1%, +3.2%] 🔵 +1.1% [+0.7%, +1.6%]

)
event_list.add_event(event)

events = event_list.peak_ahead(5)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please go ahead and change the name as part of this PR

assert event_times == sorted(event_times), (
f"peak_ahead should return events in chronological order, got {event_times}"
)
assert event_times == [5.0, 8.0, 10.0, 15.0, 20.0]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow this assertion.

events = event_list.peak_ahead(5)
event_times = [e.time for e in events]
# Events should be in chronological order: [5.0, 8.0, 10.0, 15.0, 20.0]
assert event_times == sorted(event_times), (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not test agains sorted(times)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated! Simplified to assert event_times == sorted(times)[:5] per your suggestion.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Dec 24, 2025

Can you please update the PR start post following one of the templates?

It looks good to me otherwise, so once updated, I'll merge it. Thanks!

@Nithin9585
Copy link
Copy Markdown
Contributor Author

@quaquel Updated the PR description with the bug template. Thanks!

@quaquel quaquel added the bug Release notes label label Dec 26, 2025
@quaquel quaquel merged commit 6232196 into mesa:main Dec 26, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EventList.peak_ahead() returns events in wrong order

2 participants