CS010 Introduction to Computer Science I
Term Project
Deliverable Four

Swarm and Flocking Behavior:
Efficiency and Other Concerns
(last updated 4/17/2005)

For the fourth deliverable, we will revisit earlier ground with the intention of improving the efficiency of our programs and allowing us to handle larger swarms.  We will also be adding a few new features and enforcing max-speed and max-accel (if you are not already doing that).

Efficiency
We will want to think of a state-variable, of type world, that will contain critters that are moving around based on their neighbors and the edges.  To start off, we will introduce the data definition for a world
A world is a (vectorof (vectorof (listof critter))).
Remember that these vectors are Scheme vectors -- not posns.  (Refer back to Intermezzo 5 or pages 426-438.) 

We will use this as follows.  We will take our canvas and partition it into grids based on our NEAR-RADIUS defined parameter.  Whatever our WIDTH and HEIGHT, our swarm will consist of a vector having HEIGHT/NEAR-RADIUS elements, where each element is a vector having WIDTH/NEAR-RADIUS elements, where each of these elements is a (listof critter).  Now, a critter in our swarm will exist on the list of critters in the cell that covers its location.  Let's say a critter's location is (43, 11), our WIDTH and HEIGHT are 100, and our NEAR-RADIUS is 25.  In this case, we would have a four-by-four grid (actually a vector of  4 vectors, each with 4 elements).  The critter in question would be found in the second element of the first vector of our grid.  Its y value is between 0 and 25 (the first row) and its x value is between 25 and 50 (the second column or cell of the first vector).

As I hope you already realized, the reason for going to this trouble is that when considering neighbors, we only need consider the lists of critters in the critter's own cell and its eight neighboring cells.  For my four-by-four example, that is not a very big deal; but if we have a canvas 1200 by 800 and 400 critters, I hope you can see the potential savings.

In addition to this efficiency improvement, I am sending you on a treasure hunt for other efficiency improvements.  One thing to look for are repeated computations -- especially computations that involve recursion or maps, folds, etc.  When you make an efficiency improvement, be sure to place a comment starting with ";E: " and followed by a short note of what you changed.  I will compare your code to that of Project 3 for such notes.  You can eyeball your progress by the visual flash as you animate a swarm of a given size.  You can also use the time function for your satisfaction.  (I will use time to compare your time with mine on the same swarm.)  You are free to alter or extend the data definitions of world and/or critter if you think it will improve your efficiency.  However, you should provide a function, animate-swarm: (listof critter) N -> anything, that I can call and will (behind the scenes) set up anything that needs to be set up and transform my critters into your critters.  (My critters will reflect the data definition given below.)

Other Features
You will need to check the change in velocity and the resulting velocity with respect to the max-accel and max-speed of the critter, respectively.  The idea is that a desired steering vector may exceed max-accel, and is then truncated; the resulting vector is added to the existing velocity vector and the resulting vector is also truncated if it exceeds max-speed.

You should modify your critter data definition and structure definition so that a sixth field is type, and a seventh is color.  That is, the new structure definition for a critter should be: (define-struct critter (location velocity max-speed max-accel heading type color))

You should modify your steering functions so that alignment, cohesion, and separation consider only critters of the same type.  You should also provide a steering vector (perhaps with a different weight) that causes critters to steer away from critters that are not of the same type. 

You should also modify your critter drawing functions to render a critter based on the critter-color field.


For this week's project deliverable, you should have working code that implements:
  1. Everything from deliverable 3 (i.e., swarming and edge avoidance)
  2. Be able to handle swarms of 100 critters without significant slow-downs.
  3. Two different critter species that swarm independently and avoid each other.
To grade this deliverable, I will review your code (including contracts, purpose statements, indentation, etc.) and I will run the animate-swarm with my own swarm.  It is important that you follow the instructions carefully.