March 12, 2005

Biology simulations

My first simulations were in the field of biology, which at first glance is a dumb place to start. Isn't biology more complex, and thus more difficult to simulate?

In truth, it's just as easy to write biology simulations as it is to write simulations in any other field. The only difference is that when you write a biology simulation, you abstract out a whole lot more underlying detail. Here's how the simulation I wrote for my grade 7 science fair project, way back in 1990, worked:

In an M by N world with no wraparound, a population of P critters was maintained, so that every time one died, a new one was born. Internally, the only data maintained was the location and other characteristics of each critter, which cut the memory requirements from M*N down to P, and made it easier to code (keep in mind I was using BASIC and I was only 12). Critters were given a lifetime of 75 timesteps, after which they would immediately die and a new critter would be created at a random position. During their life, critters would perform a random walk, with bounds checking so that they didn't wander off the edge of the world.

Starting with this very simple framework, you can add all kinds of details to test various theories. That year, I did a disease simulation, with each critter being either uninfected, infectious, or immune. Infectious critters would occasionally infect nearby critters, and would eventually die, or recover and become immune. By tweaking various parameters such as P, the lethality of the disease, the length of the infectious stage, and so on, I was able to create low level persistent diseases that would always keep a small portion of the population infected, plagues that would rip through the population until practically all individuals were immune then come close to dying out until the next generation of uninfected individuals was born, and diseases so lethal that they killed off all nearby hosts and died off completely.

By the next year's science fair project, the population was no longer being artificially maintained at P. Critters had an energy level, which was depleted by movement and reproduction, and increased by eating food. Critters would still die of old age, but they would also die if they ran out of energy. Instead of being immediately replaced, critters were only created when an existing critter accumulated enough energy (and a reserve) to allow it to reproduce. Food consisted of unmoving "plankton" that was being scattered across the world at a constant rate, and was eaten whenever a critter happened upon some. The experiment that year was to create two different "species" of critter with different characteristics (energy needs, lifespan, etc.) and have them compete for the food. The result was that no matter how carefully I tweaked the variables, one type would always die out. This turned out to support (in a way) a well known hypothesis that only one species can occupy a niche at a time.

This very simplistic model can be extended to perform all kinds of little experiments, and with enough work can become a pretty complex ecology simulation. Tomorrow, I'll describe how to add a simple element of genetics, and therefore evolution, to the mix.

0 Comments:

Post a Comment

<< Home