Skip to content

fix(continuous_space): return at most k agents from get_nearest_neigh… - #3995

Merged
quaquel merged 3 commits into
mesa:mainfrom
Shriraj1901:fix-3898-truncate-k
Nov 14, 2026
Merged

quaquel merged 3 commits into
mesa:mainfrom
Shriraj1901:fix-3898-truncate-k

Conversation

@Shriraj1901

Copy link
Copy Markdown
Contributor

…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 Preview tab and select a PR template:

Feature/enhancement PRs require prior maintainer approval in an issue or discussion before they are accepted.

…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
@github-actions github-actions Bot added experimental Release notes label testing Release notes label labels Oct 26, 2026
@quaquel quaquel added the trigger-benchmarks Special label that triggers the benchmarking CI label 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]

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. 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.

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).

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.

this is not in line with the current implementation which removed argpartition.

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.0% [-2.5%, -1.3%] 🔵 +0.7% [+0.3%, +1.0%]
BoltzmannWealth large 🔵 -3.8% [-5.5%, -1.5%] 🔵 +0.8% [-4.3%, +6.4%]
Schelling small 🟢 -4.5% [-5.1%, -3.9%] 🔵 -2.7% [-3.6%, -1.9%]
Schelling large 🔵 -1.1% [-2.0%, -0.2%] 🔵 +1.7% [-2.5%, +5.4%]
WolfSheep small 🔵 -2.1% [-2.7%, -1.5%] 🔵 -0.1% [-4.1%, +4.3%]
WolfSheep large 🟢 -7.9% [-8.7%, -7.2%] 🟢 -13.9% [-15.2%, -12.6%]
SugarscapeG1mt small 🟢 -5.6% [-6.4%, -4.7%] 🔵 -0.7% [-2.6%, +1.1%]
SugarscapeG1mt large 🟢 -7.9% [-9.5%, -6.3%] 🔵 -3.6% [-4.8%, -2.1%]
BoidFlockers small 🟢 -4.8% [-5.3%, -4.3%] 🔵 +0.1% [-0.2%, +0.5%]
BoidFlockers large 🟢 -4.6% [-5.3%, -3.9%] 🔵 -0.2% [-0.5%, +0.1%]
MultiLevelAllianceModel small 🟢 -8.9% [-10.4%, -7.6%] 🔵 -1.6% [-2.2%, -1.1%]
MultiLevelAllianceModel large 🟢 -6.0% [-7.1%, -5.0%] 🟢 -8.8% [-11.8%, -5.9%]

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
quaquel merged commit 89dd9ca into mesa:main Nov 14, 2026
15 of 16 checks passed
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)
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 trigger-benchmarks Special label that triggers the benchmarking CI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ContinuousSpaceAgent.get_nearest_neighbors returns k+1 neighbors when agents share a position

2 participants