Repository navigation
Fix ValueError in get_k_nearest_agents when k >= number of agents - #3671
Merged
Merged
Conversation
|
Performance benchmarks:
|
Collaborator
|
This description is unclear and looks AI-generated, Please update it |
Contributor
Author
fixed |
Collaborator
|
Please write a proper description following our PR template |
quaquel
reviewed
Apr 4, 2026
| self.agent_positions: ( | ||
| np.array | ||
| ) # a view on _agent_positions containing all active positions | ||
| self.agent_positions = self._agent_positions[0:0] |
quaquel
reviewed
Apr 4, 2026
quaquel
reviewed
Apr 4, 2026
| display(space_viz) | ||
| page_session.wait_for_selector("img") | ||
| changed_space = page_session.locator("img").first.screenshot() | ||
| changed_space = page_session.locator("img").last.screenshot() |
Member
There was a problem hiding this comment.
please remove this change. It is unrelated to the PR.
quaquel
reviewed
Apr 4, 2026
Comment on lines
+498
to
+500
| model = Model() | ||
| space = ContinuousSpace([[0, 10], [0, 10]], random=Random(42)) | ||
|
|
Member
There was a problem hiding this comment.
Suggested change
| model = Model() | |
| space = ContinuousSpace([[0, 10], [0, 10]], random=Random(42)) | |
| model = Model(rng=42) | |
| space = ContinuousSpace([[0, 10], [0, 10]], random=model.random) | |
also remove import random from the top.
quaquel
reviewed
Apr 4, 2026
|
|
||
| def test_continuous_space_k_empty_space(): | ||
| """Test that empty space returns no agents.""" | ||
| space = ContinuousSpace([[0, 10], [0, 10]], random=Random(42)) |
quaquel
reviewed
Apr 4, 2026
Comment on lines
+525
to
+527
| model = Model() | ||
| space = ContinuousSpace([[0, 10], [0, 10]], random=Random(42)) | ||
|
|
quaquel
reviewed
Apr 4, 2026
Comment on lines
+539
to
+541
| model = Model() | ||
| space = ContinuousSpace([[0, 10], [0, 10]], random=Random(42)) | ||
|
|
…unrelated viz change
Contributor
Author
fixed |
quaquel
approved these changes
May 19, 2026
EwoutH
pushed a commit
that referenced
this pull request
Oct 28, 2026
) * Fix run_model_test capturing initial image instead of updated visualization * Fix ValueError in get_k_nearest_agents when k >= number of agents * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix k>=n and empty space handling in ContinuousSpace * Address review feedback: add warning for k>n, fix test setup, remove unrelated viz change --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Jan Kwakkel <[email protected]> (cherry picked from commit 9780c9f)
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
get_k_nearest_agentscrashed whenk >= n(number of agents) or the space was empty, due tonp.argpartitionreceiving an out-of-range index.Bug / Issue
Two problems:
k >= nor empty space, causingnp.argpartitionto raise aValueErrornp.argpartitiontakes a zero-based index so it should bek-1, notkImplementation
k <= 0ktonwhenk > n, with aUserWarningso the caller knowsnp.argpartition(dists, k-1)k > nbehaviourTesting
Four new tests:
k > n(checks warning too), empty space,k=0, andkexactly equal to population. All existing tests pass.Additional Notes
Also removed an unrelated line that had crept into
test_examples_viz.py, and aligned new tests to useModel(rng=42)/model.randomper reviewer feedback.