CS010 Introduction to Computer Science I
Term Project
Deliverable Two

Swarm and Flocking Behavior:
Neighbors and Iterative Refinement
(last updated 4/3/2005)

For the second deliverable, you will start implementing the elements of actual swarm behavior.  Swarming or flocking behavior relies on a population of identical critters that are aware of and respond to other critters in their respective neighborhoods.  Each critter steers left or right or accelerates or slows down based on the collective behaviors of its neighbors.  This week, we will define neighborhoods for critters and the features over neighborhoods that will be needed to determine the appropriate steering adjustments to come.  To supplement the descriptions below, you will want to read the sections on neighbors, separation, cohesion and alignment from this paper.

In addition, we will refine our representation of a critter.  Our initial representation of critters as locations, headings and speeds, made for a simple and straight-forward process when controlling an individual critter.  However, when interacting with other critters and making steering adjustments based on other critters, it will be convenient to represent speed and heading within a velocity vector, instead of two separate fields.  Thus, for this week, revise your data definition for critters so that a critter consists of a location (posn), and a velocity (posn).  We will maintain the heading field although it will become less important.  Finally, since we are anticipating critters of different types (e.g., sheep and sheep dogs), your critter data type should include (numeric) fields max-speed and max-accel for limiting the top-speed and the rate-of-change in speed that a given critter can achieve.

Neighbors.  The neighbors of a particular critter make up a sub-set of the critters from the total swarm that are close-enough to the critter in question.  You should define a parameter NEIGHBOR-RADIUS that defines a circle (centered on the given critter's location), within which critters are considered to be close-enough.  In addition, you should define a parameter FIELD-OF-VIEW as an angel (in radians) that defines how much of the circle is perceived by the critter.  Essentially, critters within the wedge of the circle directly behind the critter are ignored.

Based on a set of neighbors for a given critter, we will want to implement three behaviors: separation, cohesion, and alignment. 

Separation.  In order to implement separation, we will need to find the position offset from the given critter for each critter in the neighborhood.  These vectors act as repelling forces are scaled by the inverse-squares of the respective offsets.  The vectors are combined into a single separation steering vector. 

Cohesion.  In order to implement cohesion, we need to find the average position for all critters in the neighborhood.  From this position, we can derive a cohesion steering vector by subtracting the location of the given critter.

Alignment.  To implement alignment, we simply find the average velocity vector, and then generate an alignment steering vector by stracting the given critter's current velocity vector.

For this week's project deliverable, you should (minimally) write functions:
get-neighbors: critter swarm -> swarm,
get-separation-vector: critter swarm -> posn
get-cohesion-vector: criter swarm -> posn
and get-alignment-vector: critter swarm -> posn
where posns are used to represent the vectors of interest and where the lists of critters naturally are the collections of neighbors found by get-neighbors with a critter and the full swarm.

To grade this deliverable, I will review your code (including contracts, purpose statements, indentation, etc.) and I will run the four functions with my own swarm.  It is important that you follow the instructions carefully.