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

Boid: Rename velocity to direction_vector #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 9 additions & 8 deletions examples/boid_flockers/boid_flockers/boid.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Boid(mesa.Agent):
- Alignment: try to fly in the same direction as the neighbors.

Boids have a vision that defines the radius in which they look for their
neighbors to flock with. Their speed (a scalar) and velocity (a vector)
neighbors to flock with. Their speed (a scalar) and direction (a vector)
define their movement. Separation is their desired minimum distance from
any other Boid.
"""
Expand All @@ -23,7 +23,7 @@ def __init__(
model,
pos,
speed,
velocity,
direction_vector,
vision,
separation,
cohere=0.025,
Expand All @@ -37,6 +37,7 @@ def __init__(
unique_id: Unique agent identifyer.
pos: Starting position
speed: Distance to move per step.
direction_vector: numpy vector for the Boid's direction of movement. It is a unit vector (a vector with length 1) that determines the direction of the Boid's movement.
heading: numpy vector for the Boid's direction of movement.
vision: Radius to look around for nearby Boids.
separation: Minimum distance to maintain from other Boids.
Expand All @@ -47,7 +48,7 @@ def __init__(
super().__init__(unique_id, model)
self.pos = np.array(pos)
self.speed = speed
self.velocity = velocity
self.direction_vector = direction_vector
self.vision = vision
self.separation = separation
self.cohere_factor = cohere
Expand Down Expand Up @@ -84,7 +85,7 @@ def match_heading(self, neighbors):
match_vector = np.zeros(2)
if neighbors:
for neighbor in neighbors:
match_vector += neighbor.velocity
match_vector += neighbor.direction_vector
match_vector /= len(neighbors)
return match_vector

Expand All @@ -94,11 +95,11 @@ def step(self):
"""

neighbors = self.model.space.get_neighbors(self.pos, self.vision, False)
self.velocity += (
self.direction_vector += (
self.cohere(neighbors) * self.cohere_factor
+ self.separate(neighbors) * self.separate_factor
+ self.match_heading(neighbors) * self.match_factor
) / 2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The / 2 affects the result. This is self.velocity +=, not self.velocity =.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you're totally right. I will reply in the main thread more extensively.

self.velocity /= np.linalg.norm(self.velocity)
new_pos = self.pos + self.velocity * self.speed
)
self.direction_vector /= np.linalg.norm(self.direction_vector)
new_pos = self.pos + self.direction_vector * self.speed
self.model.space.move_agent(self, new_pos)
4 changes: 2 additions & 2 deletions examples/boid_flockers/boid_flockers/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ def make_agents(self):
x = self.random.random() * self.space.x_max
y = self.random.random() * self.space.y_max
pos = np.array((x, y))
velocity = np.random.random(2) * 2 - 1
direction_vector = np.random.random(2) * 2 - 1
boid = Boid(
i,
self,
pos,
self.speed,
velocity,
direction_vector,
self.vision,
self.separation,
**self.factors,
Expand Down