Evolutionary Cline Simulator

I recently had time to code a biological cline simulator.

It runs locally on your machine, and is written in JavaScript. For this reason, don’t expect usable performance from phones and tablets!


Here’s the result of one run after a few thousand steps.

evolution run showing cline

Selection Pressure
The selection pressure image describes the selection pressure within the environment. In a red region, a red coloured organism will do well. In a green region, a green organism will do well. Of the 3 colours in the RGB colour system, blue is non-selective. This means a purple (blue + red) organism will do just as well as a red one, and a turquoise one (blue + green) will do just as well as a green one. Regions in black cannot be entered by the organisms.

All genes are expressed. No alleles are recessive.

The environment canvas is where the organisms are rendered. They are rendered by their phenotype. For this model, their phenotype is simply an RGB colour. The difference between the organism’s colour and the colour of the corresponding pixel on the selection pressure image gives a value of how well the organism is faring – the best possible value would be a perfect match, i.e. a difference of 0.

For simplicity, organisms are hermaphroditic. They move randomly a short distance each step. Whenever they encounter each other, either by landing in the same pixel, or in one of the 8 adjacent pixels, they mate.

The simulated organisms are diploid, and are capable of being heterozygous, receiving one chromosomefrom each parent. Each chromosome is implemented as an RGB colour. The chromosome has 3 genes (red, green, blue), each with 256 alleles (potential values of 0-255).

Organisms are generated in a random location within a perimeter, and all have identical genomes.

The organism’s phenotype is calculated by averaging both “chromosomes”, producing a colour halfway between each chromosome colour.

If the environment is unfavourable for an individual (for example, a green organism in a location in the environment that corresponds to a red pixel in the selection pressure image), it typically moves further each processing step than if it’s in a favourable environment. This gives organisms a very limited quantity of behavioural “intelligence”. They are more likely to spend more time in a favourable environment, than an unfavourable one.

Matings produce 0-1 offspring – 0 offspring if the two parents’ gametes are incompatible, otherwise 1.

Each parent organism generates a single gamete. A gamete is a single chromosome. Gametes are generated by randomly selecting 3 genes from the parent’s available 6, 1 each of red, green and blue. There is a chance that a small mutation may be applied to each gene at this point, altering 0-3 alleles by a small amount.

Genetic compatibility is measured by comparing the difference between all 3 genes of each of the parent’s gametes. If the gametes are sufficiently similar, 1 organism is produced by the mating. If not, 0 organisms are produced – the 2 potential parents are too genetically different; they have speciated.

Injury and death
Each organism cannot live for more than 1000 processing steps. Invariably, the environment, and/or competition with their peers kills them before old age can.

Organisms have a value of “health” when this reaches 0, the organism dies. Every processing step, the organism’s phenotype is compared with the selection pressure of its pixel, and the difference (modified by a “hostility” factor), subtracted from its health.

To prevent population growing exponentially, there is a small health hit each time an organism encounters another organism; in highly populated areas, this has the effect of shortening organism’s lifespans, and preventing a population explosion. This could be imagined as multiple animals competing for resources, and spreading diseases with each other.

An organism’s health can only ever stay stable and decline. There is no way an organism can increase it’s health value.