Repository navigation
Fix get_nearest_neighbors returning k+1 agents for co-located agents - #3899
Closed
Shivansh1205 wants to merge 4 commits into
Closed
Shivansh1205 wants to merge 4 commits into
Shivansh1205 wants to merge 4 commits into
Conversation
get_nearest_neighbors asks the space for the k+1 nearest agents and then drops the querying agent, assuming it is always among them. When more than k other agents share the querying agent's position, all of them are at distance 0 and argpartition may return k+1 of them without self, so nothing is dropped and k+1 neighbors are returned. Truncate the filtered result to k. This is correct because self is only excluded when all k+1 returned agents are at distance 0. Fixes mesa#3898
|
Performance benchmarks:
|
np.argpartition does not resolve ties deterministically, so truncating the k+1 nearest to k depended on argpartition internals. Select the k nearest by (distance, insertion index) and return them sorted, which matches the documented tie rule of get_k_nearest_agents and makes the truncation in get_nearest_neighbors well defined. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Removing an agent moves the last agent into the freed slot, so the internal index only matches insertion order until an agent is removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
quaquel
reviewed
Oct 20, 2026
| indices = np.concatenate([closer, tied]) | ||
| # sort by distance, with internal index as tie-breaker | ||
| indices = indices[np.argsort(dists[indices], kind="stable")] | ||
| agents = [agents[i] for i in indices] |
Member
There was a problem hiding this comment.
I think I understand what is going on, but what are the performance implications of this? Partition and argsort both involve a sort operation.
quaquel
reviewed
Oct 20, 2026
|
|
||
| # np.argpartition expects zero-based index (k-1) and returns the indices of the k smallest distances | ||
| indices = np.argpartition(dists, k - 1)[:k] | ||
| # np.argpartition does not resolve ties deterministically, so we find the k-th |
Member
There was a problem hiding this comment.
where is your evidence for this claim. All I can find is that the default selection algorithm is unstable, not that it is not deterministic.
Member
|
closing this as stale |
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.
Pre-PR Checklist
Summary
ContinuousSpaceAgent.get_nearest_neighbors(k)could returnk + 1neighbors when more thankother agents share the querying agent's position.Bug / Issue
Fixes #3898
The method requests the
k + 1nearest agents and dropsself, assumingselfis always among them. With co-located agents, more thank + 1agents are at distance 0.np.argpartitionmay then pickk + 1of them withoutself, so nothing is dropped and the caller getsk + 1neighbors.Implementation
Two parts:
ContinuousSpaceAgent.get_nearest_neighbors: after filtering outself, truncate both the agents list and the distances array tok.self(distance 0) can only be missing from thek + 1nearest when every returned agent is also at distance 0. Dropping the last one therefore still gives a correctk-nearest result. Whenselfis present, the slice is a no-op.ContinuousSpace.get_k_nearest_agents:np.argpartitiondoes not resolve ties deterministically, so which co-located agents came back could vary. The method now finds the k-th smallest distance, takes every agent strictly closer, and fills the remainder from the agents tied at that distance by internal index. It then sorts the result by distance with a stable sort. Results are now deterministic and sorted by distance. Ties break on the agent's internal index, which follows insertion order unless agents have been removed: removal moves the last agent into the freed slot. The docstring documents this.Testing
test_get_nearest_neighbors_colocated_agents: places 5 agents at the same point and checks, for every agent andkin 1-3, that exactlykneighbors andkdistances are returned, thatselfis excluded, that all distances are 0, and that the result is deterministic.assert 2 == 1for agents whoseselfwas crowded out.test_get_k_nearest_agents_ties_deterministic: 30 agents, mostly co-located, checks that ties resolve on internal index and that results are sorted by distance.ruff check/ruff format --checkclean.Additional Notes
get_k_nearest_agentsnow returns agents sorted by distance. Previously the order was unspecified. The set of agents returned is unchanged except for which tied agents are chosen.