Repository navigation
fix(continuous_space): return at most k agents from get_nearest_neigh… - #3995
Merged
Merged
Conversation
…bors get_nearest_neighbors requests the k + 1 nearest agents and filters out self. When more than k other agents share the querying agent's position, self may not be among the k + 1, so k + 1 agents were returned. Keep at most k. Also correct the get_k_nearest_agents docstring: ties at the k-th distance are resolved deterministically by np.argpartition, which is not stable, so they do not follow insertion order. Fixes mesa#3898
quaquel
reviewed
Nov 7, 2026
Comment on lines
+98
to
+100
| # If more than k other agents share this agent's position, self may not be | ||
| # among the k+1 nearest, so keep at most k. | ||
| return agents[:k], dists[logical][:k] |
Member
There was a problem hiding this comment.
I don't follow this. It seems you suggest that the return from self.space. get_k_nearest_agents might not contains self if we have multiple agents on the same position?
Also, the code here can be made more efficient. In essence we are looping over the agents and distances 3 times. Can't this be expressed in a single loop using zip? This might require a little bit of performance checking.
quaquel
reviewed
Nov 7, 2026
Comment on lines
+290
to
+292
| This method returns exactly k agents, ignoring ties. Which of several agents | ||
| tied at the k-th distance are returned is deterministic, but does not follow | ||
| insertion order (np.argpartition is not stable). |
Member
There was a problem hiding this comment.
this is not in line with the current implementation which removed argpartition.
|
Performance benchmarks:
|
Stop at k neighbors in one pass instead of filtering and slicing three times, and drop the docstring change since argpartition is no longer used.
quaquel
approved these changes
Nov 14, 2026
EwoutH
pushed a commit
that referenced
this pull request
Nov 22, 2026
#3995) * fix(continuous_space): return at most k agents from get_nearest_neighbors get_nearest_neighbors requests the k + 1 nearest agents and filters out self. When more than k other agents share the querying agent's position, self may not be among the k + 1, so k + 1 agents were returned. Keep at most k. Also correct the get_k_nearest_agents docstring: ties at the k-th distance are resolved deterministically by np.argpartition, which is not stable, so they do not follow insertion order. Fixes #3898 * fix(continuous_space): collect nearest neighbors in a single loop Stop at k neighbors in one pass instead of filtering and slicing three times, and drop the docstring change since argpartition is no longer used. --------- Co-authored-by: Jan Kwakkel <[email protected]> (cherry picked from commit 89dd9ca)
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.
…bors
get_nearest_neighbors requests the k + 1 nearest agents and filters out self. When more than k other agents share the querying agent's position, self may not be among the k + 1, so k + 1 agents were returned. Keep at most k.
Also correct the get_k_nearest_agents docstring: ties at the k-th distance are resolved deterministically by np.argpartition, which is not stable, so they do not follow insertion order.
Fixes #3898
Thanks for opening a PR! Please click the
Previewtab and select a PR template:Feature/enhancement PRs require prior maintainer approval in an issue or discussion before they are accepted.