Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify ContinuousSpace.get_neighbors behavior with multiple agents at same position #2599

Merged
merged 7 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mesa/examples/basic/boid_flockers/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ def __init__(

def step(self):
"""Get the Boid's neighbors, compute the new vector, and move accordingly."""
self.neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)
neighbors = self.model.space.get_neighbors(self.pos, self.vision, True)
self.neighbors = [n for n in neighbors if n is not self]

# If no neighbors, maintain current direction
if not self.neighbors:
Expand Down
7 changes: 7 additions & 0 deletions mesa/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,13 @@ def get_neighbors(
coordinates. i.e. if you are searching for the
neighbors of a given agent, True will include that
agent in the results.

Notes:
If 1 or more agents are located on pos, include_center=True will remove these agents
from the results. So, if you really want to get the neighbors of a given agent,
quaquel marked this conversation as resolved.
Show resolved Hide resolved
you should set include_center to true and filter the list of agents to remove
the given agent.

"""
if self._agent_points is None:
self._build_agent_cache()
Expand Down
Loading