creeky belly
Posts: 205 Joined: June 2006
|
I went for the numerical alphabet for convenience and wrote it up in python. It has a variable alphabet length, phrase length, number of generations, population size, and flat mutation rate per letter (plus a random 'phrase' every time). Anyone enjoy python? (insert Randall Munroe joke here)
Quote | import pylab as p import numpy as N import copy as c
# main function - runs WEASEL simulation def main(let=27,lin=20,pop=50,mut=0.05,gen=100):
# 'let' = length of alphabet # 'lin' = 'phrase' length # 'pop' = number of offspring # 'mut' = probability of mutation of any letter in offspring # 'gen' = number of generations to simulate #create an array of 'lin' elements from a 'let' length alphabet target = N.array([int(let*p.rand()) for x in N.arange(0,lin,1)]) #create initial population of completely random lines (offspring) offsp = [] for i in N.arange(0,pop,1): offsp.append(N.array([int(let*p.rand()) for x in N.arange(0,lin,1)])) #store best fit curbest=-1 ##loop over generations for i in N.arange(0,gen,1):
#initialize best fit offspring parameters bestfit=0 bestoff=offsp[int(p.rand()*(pop-1))] newoff=[]
##loop over offspring for off in offsp: #initialize fitness currentfit = 0
##loop over 'letters' in 'phrase' for pos in N.arange(0,lin,1): #letters match => (fitness+1) if target[pos]==off[pos]: currentfit=currentfit+1 ##end letter loop #found a more fit individual, use it to populate next generation if currentfit>bestfit: bestoff=off bestfit=currentfit
##end offspring loop
#only output if there's a change in fitness between generations if curbest != bestfit: print 'Best fit candidate %s out of %s in generation %s'% (bestfit,lin,i) curbest = bestfit
##create next generation from the most fit candidate from previous generation for k in N.arange(0,pop,1): #make a copy of 'fittest' offspring coff = c.copy(bestoff) #create an array of uniform random numbers [0,1) of the 'phrase' length probs = p.rand(lin)
##loop over random number array for z in N.arange(0,lin,1):
#mutate corresponding letter if random number is less than the mutation rate if probs[z]<mut: coff[z]=int(let*p.rand()) ##end random array loop #add offspring to next generation newoff.append(coff)
#set new generation offsp=newoff
##end 'reproduction' loop
##end generation loop print 'Target',target print 'Best ',bestoff |
Here's some sample output:
Quote | In [106]: we.main(lin=25,let=27,pop=100,mut=0.05,gen=50) Best fit candidate 4 out of 25 in generation 0 Best fit candidate 5 out of 25 in generation 1 Best fit candidate 6 out of 25 in generation 2 Best fit candidate 8 out of 25 in generation 3 Best fit candidate 9 out of 25 in generation 4 Best fit candidate 10 out of 25 in generation 5 Best fit candidate 11 out of 25 in generation 6 Best fit candidate 12 out of 25 in generation 7 Best fit candidate 13 out of 25 in generation 8 Best fit candidate 14 out of 25 in generation 9 Best fit candidate 15 out of 25 in generation 10 Best fit candidate 16 out of 25 in generation 13 Best fit candidate 17 out of 25 in generation 14 Best fit candidate 18 out of 25 in generation 16 Best fit candidate 19 out of 25 in generation 22 Best fit candidate 20 out of 25 in generation 27 Best fit candidate 21 out of 25 in generation 28 Best fit candidate 22 out of 25 in generation 34 Best fit candidate 23 out of 25 in generation 35 Target [ 2 1 25 14 6 10 3 10 21 16 17 10 5 19 5 10 11 1 18 23 24 3 21 8 8] Best [ 2 1 25 14 6 10 3 10 21 16 12 15 5 19 5 10 11 1 18 23 24 3 21 8 8] |
|