Programming: Implementing Dawkins' Weasel in Java

It has been reported by various sources that Uncommon Descent is offering a prize (Richard Dawkins' The Greatest Show on Earth or Stephen Meyer’s Signature in the Cell for a public copy of Richard Dawkins' Weasel program as described in one of his other books: The Blind Watchmaker. Now, I would love a copy Dawkins' new book so let's take a stab at it.

So what is the algorithm? My understanding from The Blind Watchmaker is that you set a target string which represents the genome of an organism perfectly adapted to some environment. Next, you'll need to determine two other conditions: how many children an organism has per generation and what the probability of any individual gene in the child mutating.

Once these factors are determined, an initial creature represented by a completely random string of equal length to the target string is created. This will be the parent of the next generation. For each of the children, copy the parent's genome. For each gene in that genome, determine if that gene mutates by comparing a random number and using the mutation probability you decided on. If the gene mutates, randomly assign another letter or space to that spot and move on to the next gene.

Once all the children are created, scan to see which child is the best fit for the environment as indicated by comparing how many genes in the child match position for position with the target. This fittest child then becomes the parent of the next generation, and the process starts over until a child with the perfect genome is created by random mutation with selection.

Based on the above, I created this code. To run, you need a Java runtime. If you have one, open a command prompt, type java -jar Weasel1.jar in the same folder where Weasel1.jar was downloaded, and press enter. The program should run with my defaults: 30 children per generation, 7% mutation rate, and the classic target string. These conditions tend to find the ideal genome in about 200-400 generations.

If you want to see the code, just open the jar file and read Weasel.java or click here. (Yes, it does have UNIX line endings.)

If you want to mess with the parameters, there is no need to edit the code. Just append them to the command line. For example, to have 500 children per generation with a 10% mutation rate with an ideal genome of "orange juice from concentrate" type java -jar Weasel1.jar 500 0.10 "orange juice from concentrate" and press enter.

If I get bored, I'll revise the code and make a Java applet that will run in your browser so you don't need to download it and run from a command line. However, if you want to do something like that, you can always check out Wesley R. Elsberry's JavaScript Weasel.

Now that I've handled the actual program, I'd like to make a few comments and observations. There have been some arguments about whether the program Dawkins original BASIC program used locking (i.e. once a gene was in the right place it wouldn't mutate any further). It appears that this is partially based on working backwards from the program output. This is problematic however. Taking my code which doesn't do locking, you can get the same appearance by using a large number of children with a low mutation rate.

In fact, the results you get from this program are pretty much controlled by generation size and mutation rate. If the mutation rate is high, getting the ideal genome becomes very hard. If the generation size is small, it takes many generations to find the target. To show how this works, I'm currently running several iterations of the program to create a 3-d graph of the parameter space (Cartesian: x=mutation rate, y=generation size, z=average number of generations needed). I'll try to get it up tomorrow.

There is a hidden factor in this program: the gene space. When mutating a gene, you randomly select from the gene space which consists of all letters from A-Z and a space. This gives 27 possible genes. If this space increased, it would increase the number of generations required to match the target.

However, before I write a term paper on this subject, I'd like to take a step back. What is Richard Dawkins trying to show with this program? It clearly isn't a thorough model for evolution. All it seems to show is that if selection pressure kills or prevents the mating of less fit creatures and there is some chance for mutation, then the offspring will approach and for some instances converge to the ideal fit for the environment. And indeed, as far as I understand it, that is all this program is supposed to show: mutation with selection can make things fit their environment as though designed.

In fact, if you run the program with a large mutation rate like 30%, you'll see that while it seems nearly impossible to converge to the ideal target; you can still get a significantly improved fitness over complete randomness. Now can I have the book, pretty please?

AttachmentSize
Weasel1.jar5.72 KB
Weasel1.java6.06 KB