I've always been interested in the emergent behaviour that can come from systems with simple rules. Some of the first projects I undertook when learning to code involved simulating the evolution of chaotic systems of equations.
The evolution of biologial systems is another area where emergence, complexity, and unexpected behaviour come into play. We do not need to directly optimize behaviour when using an evolutionary approach, we can see let the algorithm find the behaviour that leads to the best fitness and survival. Setting up an environment where survival leads to novel behaviour can be a challenge but I find it compelling when it works. On long train journeys I would sometimes create simulations of little blobs that would move around an area eating, fighting, and reproducing. A blob's "stats" were determined by an array of genes and through the generations you could see how the fitter creatures were allocating their genes more appropriately.
The boids algorithm was deveolped by Craig Reynolds in 1986. The basic idea is to control the movement of simulated creatures such that they produce coordinated motion similar to flocks of birds or schools of fish. There are three main steering behaviours that govern boid motion: separation, alignment, and cohesion. To see the effect of these behaviours, let's set up some proto-boids below that move around a central point. My boids have a minimum speed that is half of their maximum speed, and their acceleration is limited to a small value so they can't turn on a dime but instead they swoop around in a similar manner to real birds.
Let's turn on cohesion. This attracts boids towards others within a detection radius. We can see that the boids form local clusters that attract and absorb loose boids.
After cohesion, we can add alignment. This behaviour means that boids try to match the velocity vectors of nearby boids. With alignment the boids form sets of rings that traverse across the page.
Finally, we can add separation. This acts as a repulsive force for boids that get too close together and prevent them from significantly overlapping in an unphysical way. These three rules work together to produce a nice set of boids gently flocking around the central point.
These rules can produce motion that mimics herding and flocking, but they do not tell us why this exists in nature. Concentrating organisms in one location is likely to introduce more competition for resources and one might naively expect it to be a suboptimal behaviour. The answer is that flocking is a response to predation. Being in such large group makes in unlikely that an individual bird will be the victim of a bird-of-prey when compared to being alone.
We can introduce a predator into our boid simulation. Here the red dot represents a boid-of-prey, chasing boids around and scattering them. I aim to show that flocking behaviour can evolve in a simple system such as this as a response to predation, but before we get to that there are a few more steps.
There is no need to have every boid following the same set of rules. Each boid can react to its environment and its neighbours in its own way. We can use this to see if flocking behaviour is appropriate in various scenarios and how the evolved flocking dynamics vary with different selection effects. We can give each boid its own weights for cohesion, alignment, and separation, along with their respective active distances and see which flocking behaviour leads to boid survival.
To make this interesting we can add several types of danger for the boids, such that no naive strategy ends up dominating. Firstly, the predators themselves can kill boids. Successfully evading or avoiding interactions with predators will be a necessary feature of appropriate flocking behaviour. The predator will chase the nearest boid and can reach a higher top speed so there's no outmanoeuvering the predator. Secondly, leaving the observable window on the monitor screen is punishable by death. If a boid could just run off to infinity then it would not produce very interesting behaviour. Finally, we can also add danger from collisions between boids, this will stop the boids from collecting in a point mass and prevent the flocking behaviour from leading to many overlaps.
I have provided a boid evolution sandbox in a separate tab that you can find here. From this you can set up your own boid evolution simulation and let it run in your browser. We can get interesting behaviour depending on the rules that we set up. Some examples are below.
We can start with a relatively lenient collision danger, only high speed head-on collisions will kill a pair of boids. If we fix the separation force and distance, and only let the cohesion, alignment, central attraction, and predator avoidance behaviour vary then the boids end up settling into a pattern.
A whirling maelstrom of boids with extremely strong cohesion pulling them together in the same direction, similar to a galactic disc. Here the boids don't care about actively avoiding predators that much, staying within the boid cloud provides enough protection by itself and the angle of rotation prevents head-on collisions. Actively avoiding predators too strongly could take a boid away from the flock and increase the risk of head-on collisions.
Turning collision danger off means that the successful boids form a tight pod that moves around avoiding predators.
Increase the danger from collisions (by including side-on and lower speed collisions) means that alignment becomes more important to survival. The boids below have high alignment and separation which avoids collisions, but they still try to move as a flock in order to avoid predators.
Allowing the boids to accelerate much faster and removing the minimum speed produces swarm-like effects as the separation force is able to avoid collisions more effectively. However there is no longer large-scale cohesion between the boids as the selection pressures have siginificantly reduced the alignment and cohesion distances.
There aren't many grand conclusions that we can draw from this experiment beyond "flocking works to avoid predators". It would be interesting to explore the full range of strategies that develop across a large variety of experimental parameters to build up a "phase-space" of flocking strategies that respond to different pressures.
There is much in the literature about the boids algorithm, but relatively little that I could find about using an evolutionary approach to explain flocking behaviour. One paper investigated whether flocking would form naturally in a plant/herbivore/predator simulation with movement controlled by a neural net, however they had to artificially reward being near to other herbivores in order to induce flocking. The project most similar to mine is this one (pdf) by Dan Sayers. It explored evolved predator and prey motion dynamics including flocking and how robust the flocking was to various changes in sensory information and constraints on both sets of creatures.
In the process of reviewing the literature for this project I also found the following illustration for a conference proceedings which was too interesting not to share.
In any case, hopefully my sandbox proves to be of interest to someone else interested in the same problem, feel free to get in touch if you make anything cool with it!