One of the criticisms of evolution is the concept that all the order that we see in the world could emerge from nondirected natural processes. Some critics compare this notion to the notion that a monkey with a typewriter could randomly type a work of Shakespeare. It's possible, but infinitely unlikely if the monkey had to type it all in one go. But with mutation and natural selection the monkey doesn't.
Natural selection picks the best adaptations, and mutation keeps things shaken up so that a marginally better-adapted animal can be selected in the next generation.
The
Weasel Program appears in Chapter 3 of "
The Blind Watchmaker" as an example of how mutation and natural selection can increase the efficiency of order emerging from a random process. The program starts with a bunch of random strings and picks the one that's closest to its target phrase, in this case Hamlet's "methinks it is like a weasel."
Unfortunately Dawkins didn't provide any source code, making the demonstration unsatisfyingly anecdotal.
Denyse O'Leary has issued
a challenge: "Provide the code for Dawkins' WEASEL program." It seemed simple enough so I coded one up:
weasel.py It's possible to write a smaller or more efficient program but I wrote this one to be readable and easy to modify. You can easily change the phrase you're looking for, the allowable characters that will generate it, the max mutations per generation, the number of children per generation, and whether to allow a nonmutated representative of the last generation. I have no idea whether this is what Dawkins wrote but it pretty much works as advertised, converging on the solution in about 40 generations. I also print out the number of mutations per generation that happened to be selected.
Then I found
this guy's page. He's written up a much better example showing variants where the good values become locked vs. unlocked, "harmful" mutations vs "beneficial" mutations, etc. Or
this guy who uses selections of random polygons to generate the Mona Lisa. OK never mind I think I just wasted an hour reinventing the wheel.