# Weasel Implementation in C#

In the book The Blind Watchmaker, Richard Dawkins explains how evolution happens through accumulated small random variations (or mutations) filtered by the natural selection, as opposed to the mainstream misinterpretation that evolution happens in big leaps. He proposes a computer program, called weasel, to demonstrate how the accumulated small improvements (mutations that bring a benefit to the individual so that it is “chosen” by the natural selection) produce faster results.

I don’t know who it was first pointed out that, given enough time, a monkey bashing away at random on a typewriter could produce all the works of Shakespeare. The operative phrase is, of course, given enough time. Let us limit the task facing our monkey somewhat. Suppose that he has to produce, not the complete works of Shakespeare but just the short sentence ‘Methinks it is like a weasel’, and we shall make it relatively easy by giving him a typewriter with a restricted keyboard, one with just the 26 (capital) letters, and a space bar. How long will he take to write this one little sentence?

[…]

We again use our computer monkey, but with a crucial difference in its program. It again begins by choosing a random sequence of 28 letters, just as before … it duplicates it repeatedly, but with a certain chance of random error β ‘mutation’ β in the copying. The computer examines the mutant nonsense phrases, the ‘progeny’ of the original phrase, and chooses the one which, however slightly, most resembles the target phrase, METHINKS IT IS LIKE A WEASEL.

The phrase “METHINKS IT IS LIKE A WEASEL” is from Hamlet:

Hamlet: Do you see yonder cloud that’s almost in shape of a camel?
Polonius: By the mass, and ’tis like a camel, indeed.
Hamlet: Methinks it is like a weasel.

The algorithm as described on Wikipedia, is as follows:

2. Make 100 copies of this string, with a 5% chance per character of that character being replaced with a random character.
3. Compare each new string with the target “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).
4. If any of the new strings has a perfect score (28), halt.
5. Otherwise, take the highest scoring string, and go to step 2.

A character is an upper case letter or space, the number of copies per iteration could be 100 and the mutation rate 5%.

The following C# code is an implementation of the algorithm.

Function Fitness() computes how similar an offspring is to the parent, function Mutate() makes a copy of the parent with a 5% chance for each character to mutate. Function Run() runs the algorithm until the target string is reached. A possible output of the program is shown below. Different runs require different number of iterations (even more than 100).

We can change this algorithm to randomize the chance of mutation. The changes to the code are small (basically function Mutate changes to take not only the parent string but only the chance for the mutation rate), but this increases the number of iterations needed to reach the target string.

Here is the output, but because of the bigger number of iterations, only every 20th iteration is shown:

Notice that matching letters are not immutable. A letter, even if it matches one in the target string can change at any time.

You can find similar implementations in several programming languages on the Rosetta Code website.

CategoriesC#

### 2 Replies to “Weasel Implementation in C#”

1. I have only 1 question: I’m wondering why humans with 4 hands, which were very likely it existed at some point, were not a better fit than us, and nature decided they should disapear? π They couldn’t multiply themselves as fast as 2 hands humanoids? π

2. mariusbancila says:

And on what do you base your statement that its very likely that humanoids with 4 hands lived at some point on Earth? I don’t think so. Organism evolve from simple to complex. A 4 hands humanoid is more complex than a 2 hands humanoid. Even if there was a mutation that could cause 4 hands to appear, I don’t see why it should have been passed the filter of natural selection. 2 hands are good enough for us to live a long life. What great advantage would 2 additional hands bring? I don’t see that advantage, and that’s why I think it is very unlikely that such a specie ever existed.

This site uses Akismet to reduce spam. Learn how your comment data is processed.