Flockers

Flocking

In the final part of the assignment you will implement and explore the flocking algorithm.

The skeleton code Flocker.java is again based on the Follower class. There are two important additions to the skeleton code file, which take the form of inner classes.

Your job is to implement the different forces, then create scenarios that illustrate typical flocking behavior. Extensions allow you to experiment with the expressive capabilities of the simulation.

Programming

Your Flocker should potentially reflect a balance among five different priorities. They are:

The way a Flocker balances these priorities is using the WeightedForce object. In particular, the basic algorithm for Flocker decision making goes like this.

This amounts to the pseudocode for the new deliberate method in your flocker.

Here is a brief explanation of how to calculate each force.

Clearance. Find all the obstacles that the flocker can see (and predators and whatever else you think this flocker will want to avoid, besides lights and other flockers). If the obstacle is closer than the distance flocker.clearance and lies within the angle bounded by flocking.cone on either side of the flocker's direct heading, add in a force to avoid that obstacle. That force should:

See the picture:

Note: There are lots of different directions you could imagine steering to avoid an obstacle, as Braitenberg already points out. You could imagine steering backwards, or directly to one side or another. You might want to play with the different strategies. The strategy above, more or less, is the one Craig Reynolds recommends in his original paper. It balances better with the other flocking forces, which will tend to keep a group of agents flying more or less forward. A directly opposed clearance force would need to be very strong and would lead to unstable and, perhaps, unlikely behaviors.

Separation Find all the nearby boids that the flocker can see. If they are closer than the distance flocking.separation, then add in a force to avoid that boid. The force should:

See the picture:

Note: In this case, we do steer away from the other boid, because we imagine this force typically being applied in the middle of crowds, where the boids are ''jockeying for position'' with subtle movements to one side or another.

Alignment Find the nearby boids that the flocker can see that are closer than the distance flocking.detection but further than flocking.separation. Create a force with a weight of flocking.alignmentWeight that points in the average orientation of these boids. (To average, add all the forces together and then reweight by 1/n where n is the number of relevant boids.) See the picture:

Note: You want the force to depend on the average orientation because that way there is the same alignment force on each flocker regardless of the size of the flock around it. That means you only have to tune the balance among the different priorities once for flocks of many different sizes.

Centering Consider the same nearby boids as the alignment force. Create a force with a weight of flocking.centeringWeight that points to the average position of these boids. See the picture:

Following Find the closest light, and create a force towards it. The force should have a weight of followWeight.

Note: You could imagine having the force vary with the distance to the light. If the force increases with distance, it steers the flock as a whole towards lights; if it increases with the inverse of distance, it makes sure that individual agents near lights are sure to go out of their way to eat them.

Testing

The best way to test the flocker is to implement the forces one at a time, visualizing them as you go. These test cases showcase the effects of individual forces.

Once you have the forces working individually, then you can start to look at how they interact with one another in simple cases. For example, external link: worksite:/Homework/Examples/fl1.xml shows three boids going around an obstacle to reach a light. external link: sample run

Finally you can work with fancier files that show more interesting behavior, like external link: this one which shows a predator chasing a big flock. external link: sample run

Analysis

Create three specific files, showing some of the strengths and weaknesses of flocking.

Extensions

Make the behavior of your flockers more sophisticated, interesting or realistic. As always the extensions should get turned on only if the XML property with-extensions and therefore the internal variable form.withExtensions are set.

Possible examples include:

Another option is to create an auxiliary program that makes interesting random worlds for flockers to navigate through.

Hand in

Navigation

That is the end.