Simulating Evolution


This page is an introduction to several scripts I wrote to simulate evolution.

Richard Dawkins (author of ‘The Selfish Gene’) has in several of his books used computer programs to illustrate how natural selection works. In ‘The Blind Watchmaker’ he describes a computer algorithm that demonstrates the power of ‘selection’.

This ‘Weasel Program’ (Dawkins’ weasel) generates a sentence written by Shakespeare by random variation combined with non-random cumulative selection. The sentence is: “METHINKS IT IS LIKE A WEASEL”. Dawkins did not include the code of the program, but on Wikipedia page Weasel program it is suggested that a ‘Weasel’ style algorithm could run as follows.

  1. Start with a random string of 28 characters.
  2. Make 100 copies of the string (reproduce).
  3. For each character in each of the 100 copies, with a probability of 5%, replace (mutate) the character with a new random character.
  4. Compare each new string with the target string “METHINKS IT IS LIKE A WEASEL”, and give each a score (the number of letters in the string that are correct and in the correct position).
  5. If any of the new strings has a perfect score (28), halt. Otherwise, take the highest scoring string, and go to step 2.

I wrote such a script (using Javascript) and called it “Wiki Weasel Script”. You find it here:

Wiki Weasel Script

A weakness in this algorithm is the method used to generate a score for the sentences. A letter is either ‘good’ or ‘wrong’. That makes many sentences get an equal score (for example sentences with 3 letters correct) while some of them could be ‘better’ or ‘worse’ than others.

So I wrote another version of the program, which I called “My Weasel Script”, where sentences are scored for their ‘distance’ to the Weasel sentence. With ‘distance’, I mean distance in the alphabet, so the letter ‘A’ is closer to ‘B’ than to any other letter. You find the “My Weasel Script” here:

My Weasel Script

To illustrate that ‘distance’ matters, I then wrote a version of that script, where I use the sentences as a genetic code for the color of an evolving animal. If a sentence is closer to the Weasel sentence, its color will be also closer to the color coded by the Weasel sentence. This I called “My Color Weasel Script” and you can see it here:

My Color Weasel Script

The Weasel algorithm is rather simple and only used to illustrated the advantage of ‘cumulative selection’. It does not simulate natural selection. Dawkins therefore wrote another program which generates ‘biomorphs’. The Wikipedia page about the Bind Watchmaker describes this Biomorph program like this:

The program displayed a two-dimensional shape (a ‘biomorph’) made up of straight black lines, the length, position, and angle of which were defined by a simple set of rules and instructions (analogous to a genome). Adding new lines (or removing them) based on these rules offered a discrete set of possible new shapes (mutations), which were displayed on screen so that the user could choose between them. The chosen mutation would then be the basis for another generation of biomorph mutants to be chosen from, and so on. Thus, the user, by selection, could steer the evolution of biomorphs. This process often produced images which were reminiscent of real organisms for instance beetles, bats, or trees.


In the Biomorph program, it is the user who steers the selection process; it is a bit like a plant breeder taking seed from a good tomato or an animal breeder using his best cows to breed with. So this algorithm is not simulating ‘natural selection’.

Dawkins, in another book, then discusses a computer script that generates spiderwebs. Here the algorithm shoots ‘random flies’ at the webs, and the web that catches most flies is used to start the next generation. This simulates some kind of natural selection.

What is still unnatural about all these scripts is that in each generation only one individual is selected to start the next generation. All other mutations are ‘killed’ by the script.

In real evolution it is not only the best individual that is allowed to multiply. Each individual has a different ‘fitness’ and the fitter ones contribute more to the future than the less fit ones. Only the real bad individuals fail to get offspring. So I decided to write a script that allows more survivors to multiply and pass their different genes to the next generation.

I didn’t want to make an improved spiderweb script, because a spiderweb is an extended phenotype. I decided to have a script with genes that directly act on the phenotype of the simulated animals. That brought me to the well documented evolution of the Peppered Moth. This moth evolves to a darker or lighter color depending on the color of the bark of trees. A moth with a better matching color is less visible to predatory birds and has a higher chance to survive and have offspring. Read more about this on Wikipedia page: Peppered moth evolution.

Peppered moth
Peppered moth

The Peppered Moth Simulation is here:

Peppered Moth Simulation

The Pepper Moth script has a lot of different settings to play with.

More scripts to come. Please send me your comments.

Oh, you don’t like weasels? I have another script where you can pick other animals. It is here:

Other Animals

Shakespeare would love it.

Scroll to Top