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

examples: Fix boid_flockers viz #2492

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions mesa/examples/basic/boid_flockers/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ def __init__(
self.cohere_factor = cohere
self.separate_factor = separate
self.match_factor = match
self.neighbors = []

def step(self):
"""Get the Boid's neighbors, compute the new vector, and move accordingly."""
neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)
self.neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)

# If no neighbors, maintain current direction
if not neighbors:
if not self.neighbors:
new_pos = self.pos + self.direction * self.speed
self.model.space.move_agent(self, new_pos)
return
Expand All @@ -71,7 +72,7 @@ def step(self):
separation_vector = np.zeros(2) # Separation vector

# Calculate the contribution of each neighbor to the three behaviors
for neighbor in neighbors:
for neighbor in self.neighbors:
heading = self.model.space.get_heading(self.pos, neighbor.pos)
distance = self.model.space.get_distance(self.pos, neighbor.pos)

Expand All @@ -86,7 +87,7 @@ def step(self):
match_vector += neighbor.direction

# Weight each behavior by its factor and normalize by number of neighbors
n = len(neighbors)
n = len(self.neighbors)
cohere = cohere * self.cohere_factor
separation_vector = separation_vector * self.separate_factor
match_vector = match_vector * self.match_factor
Expand Down
5 changes: 1 addition & 4 deletions mesa/examples/basic/boid_flockers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@


def boid_draw(agent):
if not agent.neighbors: # Only for the first Frame
neighbors = len(agent.model.space.get_neighbors(agent.pos, agent.vision, False))
else:
neighbors = len(agent.neighbors)
neighbors = len(agent.neighbors)

Check warning on line 6 in mesa/examples/basic/boid_flockers/app.py

View check run for this annotation

Codecov / codecov/patch

mesa/examples/basic/boid_flockers/app.py#L6

Added line #L6 was not covered by tests

if neighbors <= 1:
return {"color": "red", "size": 20}
Expand Down
Loading