Here’s another classic from the 80’s: an artificial life simulation, where bugs move on a virtual Petri dish, hunting for bacteria. If they manage to survive until adulthood, and accumulate enough energy from bacteria, the bugs reproduce and generate two copies of themselves. In the reproduction process, the genetic code undergoes small mutations, so that the baby-bugs are not exact copies of their mother.
The genetic code of bugs determines the way they move around. It consists of six numbers that give the probability that the bug will move in one of six directions (forward, soft/hard right, backwards, soft/hard left) at any point in time. For example, a bug with code [5, 0, 5, 0, 0, 0] would move forward 50% of the time (relative to his current direction), and for the rest of the time it would take a hard turn right (120 degrees on its right) and move. Mutations change one of the numbers in the code by +/- 2.
Individual with a genetic code unfit to deal with competition for food eventually die away, and by the law of natural selection the population of bugs adapts to efficiently navigate their environment to collect bacteria. The optimal strategy will depend on the environment: if the bacteria are randomly scattered around, the optimal behavior is to “slide” forward for some time before taking a turn. If instead bacteria are concentrated on a small patch, a surer way for a bug to survive is to rotate on itself, to make sure not to get too far.
This idea was described by A.K. Dewdney in in the article “Simulated evolution: wherein bugs learn to hunt bacteria” in 1989 in Scientific American (May, pp. 138-141). The flash application above is my version of Dewdney’s simulation, implemented in ActionScript 3 (click here to download the code). It is based on the SpatialDatabase class described in the previous post, so you might want to have a look at the code if you’re curious about how it can be used in practice.
As you might have guessed, the yellow circles are bacteria, while the green ones are bugs. Bugs start to fade when their energy is low; if they don’t find food fast enough, they eventually disappear into nothing. The button “Garden of Eden” activates a small region with high bacterial growth, you can switch it on to see how fast the bugs adapt to the new environment. It usually takes around 20 generations for them to show a highly specialized behavior.
Have fun!
