diff --git a/examples/boid_flockers/boid_flockers/boid.py b/examples/boid_flockers/boid_flockers/boid.py index f427f9dd..2390ff1d 100644 --- a/examples/boid_flockers/boid_flockers/boid.py +++ b/examples/boid_flockers/boid_flockers/boid.py @@ -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. """ @@ -23,7 +23,7 @@ def __init__( model, pos, speed, - velocity, + direction_vector, vision, separation, cohere=0.025, @@ -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. @@ -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 @@ -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 @@ -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 - 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) diff --git a/examples/boid_flockers/boid_flockers/model.py b/examples/boid_flockers/boid_flockers/model.py index 3f7428f0..a0ec1cfd 100644 --- a/examples/boid_flockers/boid_flockers/model.py +++ b/examples/boid_flockers/boid_flockers/model.py @@ -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,