Boids

One of the most beautiful algorithms in the field of artificial life is Boids, invented by Craig Reynolds in 1986. The algorithm models the behaviour of flocking or schooling animals such as birds or fish. The rules are very simple, yet lead to a very complex and life-like behaviour. There are 3 main rules:

The applet

The applet implements the 3 main rules, plus the code for avoiding walls of the "aquarium". To make the lives of boids less boring, i've added predators to the aquarium. A predator tries to move itself into the center of the flock, and when it has enough energy, it does a "sprint" towards the nearest boid (and eats it if it catches it).

The Legend:
Drive to center - how eager the boids are to move towards the center of the flock.
Av. speed - how eager the boids are to align their speed and direction with that of others.
Repel - how eagerly they avoid being within Min comf distance of another boid.
Min comf distance - the minimal distance of boid from another boid at which the boid is not yet repelled by its neighbour.
Max speed - the maximum speed of a boid/predator.
Max chase speed - the maximum speed at which the predator can hunt a boid.

The applet requires Java 1.4 to run. The source code can be downloaded from here.