Advertisement
Sp3000

Genetic Hello World

Sep 9th, 2014
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.92 KB | None | 0 0
  1. import random
  2. from collections import namedtuple
  3.  
  4. def evolve(initialise_population_f, fitness_f, breed_f, num_generations=100):
  5.     """
  6.    initialise_population: Returns an initial population represented as a list
  7.    fitness_f            : Fitness function
  8.    breed_f              : Takes two elements and breeds them, possibly introducing mutations
  9.    num_generations      : Number of generations to run the evolver for
  10.    """
  11.  
  12.     Individual = namedtuple("Individual", ["fitness", "gene"])
  13.  
  14.     population = [Individual(fitness_f(x), x) for x in initialise_population_f()]
  15.     pop_size = len(population)
  16.  
  17.     def rand_index():
  18.         return max(pop_size - 1, int(pop_size * random.triangular(0, 1, 1)))
  19.  
  20.     for generation in range(num_generations):
  21.         new_population = []
  22.         population.sort()
  23.  
  24.         for i in range(pop_size):
  25.             index1 = rand_index()
  26.             index2 = rand_index()
  27.  
  28.             new_indiv = breed_f(population[index1].gene, population[index2].gene)
  29.             new_population.append(Individual(fitness_f(new_indiv), new_indiv))
  30.  
  31.         population = new_population
  32.        
  33.     population.sort()
  34.     return population[-1]
  35.  
  36. alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ, !"
  37.  
  38. def rand_string(n, alphabet):
  39.         return "".join([random.choice(alphabet) for i in range(n)])
  40.    
  41. def initialise_population():
  42.     return [rand_string(13, alphabet) for i in range(1000)]
  43.  
  44. def fitness_function(string):
  45.     return sum((1 if string[i] == "Hello, World!"[i] else 0) for i in range(len(string)))
  46.  
  47. def breed(string1, string2):
  48.     global alphabet
  49.    
  50.     index = random.randint(0, len(string1) - 1)
  51.     child = string1[:index] + string2[index:]
  52.  
  53.     if random.random() < 0.1:
  54.         mutation_index = random.randint(0, len(child) - 1)
  55.         child = child[:mutation_index] + random.choice(alphabet) + child[mutation_index+1:]
  56.        
  57.     return child
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement