Created
January 7, 2011 22:13
-
-
Save maxcountryman/770206 to your computer and use it in GitHub Desktop.
Weasel program revisited with simple timing and testing functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import string, random, time | |
CHARS = string.uppercase + ' ' | |
TEST = True | |
class Biosphere(object): | |
'''Attempts to find `target` by producing generations of random strings that | |
mutate by `rate` percent. Generational population is determined by | |
`progeny`. | |
''' | |
def __init__(self, target='METHINKS IT IS LIKE A WEASEL', rate=0.05, progeny=100): | |
self.target = target | |
self.rate = rate | |
self.progeny = progeny | |
self._genotype = lambda s : [random.choice(s) for x in xrange(len(self.target))] | |
self.phenotypes = [''.join(self._genotype(CHARS)) for x in xrange(self.progeny)] | |
self.population = [''.join(self._mutate(l)) for l in self.phenotypes] | |
phenotype = '' | |
i = 0 | |
while phenotype != self.target: | |
points = [self._fitness(phenotype) for phenotype in self.population] | |
winner = max(points) | |
phenotype = self.population[points.index(winner)] | |
self.population = [''.join(self._mutate(phenotype)) for x in xrange(self.progeny)] | |
print('%s: %s -- score: %s' % (i, phenotype, winner)) | |
i += 1 | |
self.i = i | |
def _fitness(self, phenotype): | |
return sum([x == y for x,y in zip(phenotype, self.target)]) | |
def _mutate(self, phenotype): | |
return [(random.choice(CHARS) if random.random() < self.rate else c) for c in phenotype] | |
def prime(primer=5000): | |
k = 0 | |
while k < primer: | |
Biosphere('TEST') | |
k += 1 | |
def test(goal=10000): | |
'''Test goal numbers of times, print average number of iterations.''' | |
k = 0 | |
l = [] | |
while k < goal: | |
b = Biosphere('TEST') | |
l.append(b.i) | |
k += 1 | |
print('Average: ' + str(float(sum(l)) / len(l))) | |
print('Minimum: ' + str(min(l))) | |
print('Maximum: ' + str(max(l))) | |
def timeit(func, *args): | |
'''Simple timer wrapper for functions.''' | |
start = time.time() | |
func(*args) | |
elapsed = time.time() - start | |
return elapsed | |
if __name__ == '__main__': | |
if TEST: | |
#prime() | |
secs = timeit(test) | |
print('Time taken: %.2f secs' % secs) | |
else: | |
Biosphere() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment