Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2009
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. import random
  2.  
  3. target = 'methinks it is a weasle'
  4. offsprings = 10
  5. pop_size = 100
  6. mutation_rate = 100
  7.  
  8. def turn_str(l):
  9.     s=''
  10.     for c in l:
  11.         s+=c
  12.     return s
  13.  
  14. def abiogenesis():
  15.     return [[' ' for n in range(len(target))] for m in range(pop_size)]
  16.  
  17. # mutates an individual's string
  18. # [string] -> [string]
  19. def mutate_string(individual):
  20.     l=list(individual)
  21.     gene_position=0
  22.     while gene_position < len(target):
  23.         if random.randint(1,mutation_rate)==1:
  24.             l[gene_position] = random.choice([chr(c+ord('a')) for c in range(26)] + [' '])
  25.         gene_position+=1
  26.     return turn_str(l)
  27.  
  28. def fitness(individual):
  29.     fitness_amount = 0
  30.     fitness_amount = sum([individual[n] == target[n] for n in range(len(target))])
  31.     return fitness_amount
  32.  
  33. def best_fit(population):
  34.     fittest_score = [fitness(population[0]) for n in range(offsprings)]
  35.     fittest_individuals = [population[0] for n in range(offsprings)]
  36.     i = 1
  37.     while i < pop_size:
  38.         individual_fitness = fitness(population[i])
  39.         if sum([individual_fitness > n for n in fittest_score]):
  40.             fittest_score.pop()
  41.             fittest_score.insert(0,individual_fitness)
  42.             fittest_individuals.pop()
  43.             fittest_individuals.insert(0, population[i])
  44.         i+=1
  45.     return fittest_individuals
  46.  
  47. def procreate(fittest_individuals):
  48.     new_population = []
  49.     for m in fittest_individuals:
  50.         for n in range(pop_size/offsprings):
  51.             new_population.append(m)
  52.     return new_population
  53.  
  54. def teh_main():
  55.     generation=1
  56.     stay_in = True
  57.     population = abiogenesis()
  58.     while stay_in:
  59.         population=map(mutate_string, population)
  60.         fittest=best_fit(population)
  61.         population=procreate(fittest)
  62.         generation+=1
  63.         for n in range(len(fittest)):
  64.             print('fittest: ', fittest[n], 'fitness: ', fitness(fittest[n]))
  65.         if sum([n == target for n in fittest]):
  66.             print generation
  67.             stay_in=False
  68.  
  69. def try_fitness():
  70.     print(fitness("methinks it is a weasla"))
  71.     print(fitness("                       "))    
  72.  
  73. def try_best_fit():
  74.     population=abiogenesis()
  75.     print(best_fit(population))
  76.  
  77. def try_procreate():
  78.     l=procreate(['abc','def','ghi'])
  79.     print l
  80.  
  81. teh_main()
  82.  
  83. pop_size = 3
  84. mutation_rate = 3
  85.  
  86. #try_fitness()
  87. #try_best_fit()
  88. #try_procreate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement