Showing posts with label weasel. Show all posts
Showing posts with label weasel. Show all posts

Sunday, July 26, 2009

I kinda get Python. Kinda.

Phew, long week and long weekend. I had my weekend WTF moment when I caved to my daughter's pleas for tonic water. I figured that there was no way a 19-month year old would have more than one sip. She proved me wrong, downing all that I gave her. There's nothing quite like watching a toddler wander around with a rock glass full of tonic. That should be on a Hallmark card or something.

Anyway, I've been slllooowwwwllly progressing with Python and MatPlotLib. There are some concepts that are still beyond me.

I've continued to play with the Weasel program; it's interesting enough to keep me programming while giving me plenty of output to visualise. I've always wanted to see how changing the number of offspring per generation AND the mutation rate would affect the number of iterations needed to finish. This requires a lot of runs- not only do you need to simulate many mutation-rate & number-of-offspring combinations, you also need to perform many simulations for each combination to get an average number of total iterations (because it's random, the results can differ quite a bit.)

This plot shows the variability in similar runs (and shows that I can do whisker plots now.) The Y-axis is number of iterations (LOG SCALE!) to completion, the x-axis is number of offspring set for the simulation. Each point represents the average of 100 simulations using the same inputs; the whiskers show the standard deviation. As the number of offspring increases, both the standard deviation and number of iterations decreases. This makes sense- with more offspring, you have a greater chance of a "good" mutation. The log scale makes the change look small, but it's order magnitudes!


I repeated this, but now I varied both mutation rate AND number of offspring. With 10,721 combinations, I lowered the number of runs per combination to 30 from 100. This left me with 321,630 weasel runs (two days of calculations!) This contour plot shows the variation in average number of iterations to completion when the two controls are changed. Note again a log scale, and also note that I had a very, very hard time setting up a proper log scale color bar. The bar goes from the low 10's to the high 400's.

What is interesting here is that if you follow the contours closely, you see a rebound in the number of iterations required as the mutation rate goes up high enough. This means that for a given number of offspring, there is an optimal mutation rate for the fastest convergence: too low and the process drags on forever, too high and we cannot converge because there is so much variation. Although I expected this result, it was still neat to see visualised like this.

The big personal accomplishment here was building these results - from the weasel runs, the scripting, all the way to the plotting - in Python. I would like to start using this language for work, but I don't want to waste research time learning an extra language.

Wednesday, July 8, 2009

Methinks it is like a Python

On Monday, my second book on Python programming arrived in the mail. My first, Learning Python (O'Reilly), is well written but slow- I learn best by plowing through some examples and then filling in the gory details. Learning Python works in the opposite direction, so I grabbed Python Scripting for Computational Science (Springer). This book has been great for getting up to speed post-haste.

Now that I've had some free time, I've spent the past two evenings playing with the Python language and was able to recreate Richard Dawkin's "Weasel" program. This program takes a target phrase, "Methinks it is like a weasel" (Shakespeare, anyone?), and a random group of letters of the same length. Over each iteration, an offspring of the jumbled phrase is created by randomly changing some of the letters. Of X offspring, the one nearest the target phrase (the "fittest") is chosen and another generation is made. Eventually, and rather quickly, you achieve the target phrase through a set of random mutations guided by a judgment of fitness -- a neat demonstration of the principles of evolution.

Although I was able to create this program quickly using Perl, I thought it would be an excellent Python exercise. I was able to hack together a version pretty quickly, but given the powerful matplotlib package, was able to go a step further and visualize some of the results.

Convergence to the target phrase in 8332 iterations. Each generation produced 10 children phrases; each letter for each child phrase had a 0.1% chance of mutation.

Not too shabby for a few hours work. Compared to my Perl version, the source code is shorter but feels much less elegant. They both run very quickly. Python gets points for the ease of visualization through matplotlib; Perl gets points for easily integrating into a CGI script for web browser interface (though Python supposedly does this equally as well.) The comparisons won't be fair until I've given Python more than a few days' worth of work.

Here's a good online version of Weasel.
Here's another.