Skip to content

Fix insertion-order tie-breaking in k-nearest agents - #3946

Merged
quaquel merged 3 commits into
mesa:mainfrom
CraftierTen5:fix-k-nearest-agent-tie-breaking
Nov 4, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
CraftierTen5:fix-k-nearest-agent-tie-breaking

Conversation

@CraftierTen5

Copy link
Copy Markdown
Contributor

Summary

Fixes insertion-order tie-breaking in ContinuousSpace.get_k_nearest_agents() to ensure consistent ranking of agents at equal distances, in accordance with the documented behavior.

Bug / Issue

According to the documentation, get_k_nearest_agents() ranks agents by distance from the query position. However, when multiple agents have equal distances, the method does not reliably prioritize earlier inserted agents. This leads to inconsistent selection when ties occur at the k-nearest cutoff, particularly after agent removal changes the active agent array order.

Implementation

Updated get_k_nearest_agents() to handle equal-distance ties explicitly, prioritizing earlier-inserted agents.

Added a regression test in tests/experimental/test_distance_tie_bug.py covering equal-distance ties, cutoff selection, and tie-breaking after agent removal.

Testing

Regression test and existing tests in tests/experimental/test_continuous_space.py: 15 passed
Ruff checks and formatting: passed
git diff --check: passed

Additional Notes

The change is limited to the k-nearest agent selection logic and its regression tests. No new dependencies or API changes are introduced.

@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Oct 16, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.1% [-1.7%, -0.6%] 🔵 -1.6% [-1.8%, -1.3%]
BoltzmannWealth large 🔵 +1.1% [+0.6%, +1.7%] 🔴 +6.5% [+4.8%, +8.2%]
Schelling small 🔵 +0.5% [-0.0%, +1.0%] 🔵 -0.3% [-1.1%, +0.4%]
Schelling large 🔵 +0.5% [-0.3%, +1.2%] 🔵 +3.9% [+1.8%, +6.0%]
WolfSheep small 🔵 -0.4% [-0.9%, +0.1%] 🔵 +2.7% [-1.2%, +7.1%]
WolfSheep large 🔵 -1.1% [-2.3%, +0.1%] 🔵 -2.0% [-4.3%, +0.4%]
SugarscapeG1mt small 🔵 -2.2% [-2.6%, -1.7%] 🔵 +1.2% [-0.6%, +2.9%]
SugarscapeG1mt large 🔵 -0.2% [-1.5%, +1.2%] 🔵 +0.4% [-1.5%, +2.2%]
BoidFlockers small 🔵 +1.3% [+0.8%, +1.7%] 🔵 +0.2% [-0.2%, +0.6%]
BoidFlockers large 🔵 +2.0% [+1.5%, +2.5%] 🔵 -0.2% [-0.8%, +0.3%]
MultiLevelAllianceModel small 🔴 +9.7% [+7.9%, +11.5%] 🔵 -0.2% [-0.7%, +0.3%]
MultiLevelAllianceModel large 🔴 +7.7% [+5.5%, +9.6%] 🔴 +11.5% [+10.2%, +12.7%]

@quaquel

quaquel commented Oct 22, 2026

Copy link
Copy Markdown
Member

How does this interact with #3899?

@CraftierTen5

Copy link
Copy Markdown
Contributor Author

Hi, I checked PR #3899 and saw that it also modifies tie-breaking, using internal-index ordering, but my PR focuses on enforcing the existing get_k_nearest_agents() docstring contract that earlier-inserted agents rank higher when distances are tied.
From what I've seen, since removals can change internal indices, the two approaches can produce different results. The key difference between the PRs is that I'm preserving the documented insertion-order priority instead of relying on the current internal index.

@quaquel
quaquel merged commit e49bedc into mesa:main Nov 4, 2026
12 of 14 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
Co-authored-by: Jan Kwakkel <[email protected]>
(cherry picked from commit e49bedc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

experimental Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants