Repository navigation
Fix insertion-order tie-breaking in k-nearest agents - #3946
Merged
Merged
Conversation
|
Performance benchmarks:
|
Member
|
How does this interact with #3899? |
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. |
quaquel
approved these changes
Nov 4, 2026
EwoutH
pushed a commit
that referenced
this pull request
Nov 22, 2026
Co-authored-by: Jan Kwakkel <[email protected]> (cherry picked from commit e49bedc)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.