From 49b88646235cca7ad405bc8498b44522c59444ff Mon Sep 17 00:00:00 2001 From: Jan Kwakkel Date: Mon, 6 Jan 2025 16:24:23 +0100 Subject: [PATCH] Basic bugfix for bug ContinuousSpace.get_neighbors (#2599) Co-authored-by: Ewout ter Hoeven --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Ewout ter Hoeven --- mesa/examples/basic/boid_flockers/agents.py | 3 ++- mesa/space.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mesa/examples/basic/boid_flockers/agents.py b/mesa/examples/basic/boid_flockers/agents.py index 2ff00cbaef2..a8f23f915aa 100644 --- a/mesa/examples/basic/boid_flockers/agents.py +++ b/mesa/examples/basic/boid_flockers/agents.py @@ -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: diff --git a/mesa/space.py b/mesa/space.py index 6e15426bf34..d503d42e41b 100644 --- a/mesa/space.py +++ b/mesa/space.py @@ -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=False will remove all these agents + from the results. So, if you really want to get the neighbors of a given agent, + you should set include_center=True, and then filter the list of agents to remove + the given agent (i.e., self when calling it from an agent). + """ if self._agent_points is None: self._build_agent_cache()