weasel program

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

weasel program

Postby pedros » Fri Feb 22, 2013 10:40 am

this is a python (2.x) version of the code for a tutorial i originally wrote in javascript for codecademy as an introduction to genetic algorithms. if i get time i'll add the explanations that goes with each step later but for further reading see http://en.wikipedia.org/wiki/Weasel_program

Code: Select all
#!/usr/bin/env python

"""
weasel.py

Copyright (C) 2013 Peter Somerville

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
-------------------------------------------------

The weasel program, Dawkins' weasel, or the Dawkins weasel is a thought
experiment and a variety of computer simulations illustrating it. Their aim
is to demonstrate that the process that drives evolutionary systems -- random
variation combined with non-random cumulative selection -- is different from
pure chance.

The thought experiment was formulated by Richard Dawkins, and the first
simulation written by him; various other implementations of the program have
been written by others.

http://en.wikipedia.org/wiki/Weasel_program

"""
__author__ = "Peter Somerville"
__email__ = "peterwsomerville@gmail.com"
__version__ = "1.0.0"
__date__ = "22/2/13"

import random

TARGET = "METHINKS IT IS LIKE A WEASEL"
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ "
MUT_PROB = 2000


def generate_offspring(num=1):
    pool = []
    for i in xrange(num):
        offspring = [random.choice(ALPHABET) for i in xrange(28)]
        pool.append(offspring)
    return pool


def clone_fittest(fittest, num=50):
    return [fittest for i in xrange(num)]


def get_fitness(child):
    fitness = 0
    for i, char in enumerate(child):
        if char == TARGET[i]:
            fitness += 1
    return fitness


def get_fittest(pool):
    fitnesses = [get_fitness(child) for child in pool]
    return pool[fitnesses.index(max(fitnesses))]


def do_mutation(offspring, locking):
    for i, char in enumerate(offspring):
        if random.randint(1, MUT_PROB) == 1:
            if (locking and offspring[i] != TARGET[i]) or not locking:
                offspring[i] = random.choice(ALPHABET)
    return offspring


def main():
    print "A Weasel Program in python\n"
    print "http://en.wikipedia.org/wiki/Weasel_program"
    print "*" * 66
    print "Hamlet: Do you see yonder cloud that's almost in shape of a camel?"
    print "Polonius: By the mass, and 'tis like a camel, indeed."
    print "Hamlet: Methinks it is like a weasel."
    print "*" * 66
    print
    evolve(raw_input("Use locking? ").lower().startswith("y"))


def evolve(locking):
    num_generations = 0
    pool = generate_offspring()
    fittest = get_fittest(pool)
    while get_fitness(fittest) != 28:
        num_generations += 1
        pool = clone_fittest(fittest, 1)
        pool = [do_mutation(offspring, locking) for offspring in pool]
        fittest = get_fittest(pool)
        print num_generations, "".join(fittest), get_fitness(fittest)


if __name__ == "__main__":
    main()

pedros
 
Posts: 3
Joined: Mon Feb 11, 2013 10:55 am

Return to Completed Scripts

Who is online

Users browsing this forum: W3C [Linkcheck] and 3 guests