Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,21 @@
|
|||
from random import choice, random
|
||||
|
||||
target = list("METHINKS IT IS LIKE A WEASEL")
|
||||
alphabet = " ABCDEFGHIJLKLMNOPQRSTUVWXYZ"
|
||||
p = 0.05 # mutation probability
|
||||
c = 100 # number of children in each generation
|
||||
|
||||
def neg_fitness(trial):
|
||||
return sum(t != h for t,h in zip(trial, target))
|
||||
|
||||
def mutate(parent):
|
||||
return [(choice(alphabet) if random() < p else ch) for ch in parent]
|
||||
|
||||
parent = [choice(alphabet) for _ in xrange(len(target))]
|
||||
i = 0
|
||||
print "%3d" % i, "".join(parent)
|
||||
while parent != target:
|
||||
copies = (mutate(parent) for _ in xrange(c))
|
||||
parent = min(copies, key=neg_fitness)
|
||||
print "%3d" % i, "".join(parent)
|
||||
i += 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue