aboutsummaryrefslogtreecommitdiff
path: root/lib/selection.py
blob: 48947cc3364ece06b0aaa5d4eed2922d6140a335 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# DAWKINS' BLIND WATCHMAKER -- "METHINKS IT IS LIKE A WEASEL" EXAMPLE
#
# Start with a random phrase.  Successively:
# Breed a next generation by copying with small mutations.
# Choose the member of the next generation that most closely resembles
# the target phrase to start breeding the next generation.
# How many generations will it take before the target is reached?

from whrandom import random

# Parameters determining the problem space
Target = 'methinks it is like a weasel'
Alphabet = ' abcdefghijklmnopqrstuvwxyz'

# Parameters to play with
GenerationSize = 100
MutationChance = 1.0/28.0

IndexSet = range(len(Target))	# Used to speed up loops

def choice(sequence):
	n = len(sequence)
	i = int(random() * float(n)) % n
	return sequence[i]

def random_phrase():
	phrase = ''
	for i in IndexSet:
		phrase = phrase + choice(Alphabet)
	return phrase

def mutate_phrase(phrase):
	mutant = phrase
	for i in range(int(0.5 + MutationChance*float(len(phrase)))):
		i = choice(IndexSet)
		c = choice(Alphabet)
		mutant = mutant[:i] + c + mutant[i+1:]
	#print `mutant`
	return mutant

def matching_factor(phrase):
	factor = 0
	for i in IndexSet:
		if phrase[i] = Target[i]: factor = factor + 1
	return factor

def breed_generation(phrase):
	generation = [phrase]
	while len(generation) < GenerationSize:
		generation.append(mutate_phrase(phrase))
	return generation

def select_best(generation):
	factor, selected = -1, ''
	for phrase in generation:
		f = matching_factor(phrase)
		if f > factor:
			factor, selected = f, phrase
	return selected

def main():
	gen = 0
	phrase = random_phrase()
	print gen, `phrase`
	while phrase <> Target:
		next_generation = breed_generation(phrase)
		#print next_generation
		phrase = select_best(next_generation)
		gen = gen+1
		print gen, `phrase`

main()