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.
- Start with a random string of 28 characters.
- Make 100 copies of the string (reproduce).
- For each character in each of the 100 copies, with a probability of 5%, replace (mutate) the character with a new random character.
- 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).
- 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:
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:
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:
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:
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.
The Peppered Moth Simulation is here:
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:
Shakespeare would love it.