Advertisement
Madmouse

an example of a genetic algoritm

Apr 30th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.01 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import numpy as np
  4. import time, random
  5.  
  6. class GeneticALG():
  7.     def __init__(self, chromosome_size, population_size, fitness_test, crossover=0.7, mutation=0.001, elite_pool=0.10):
  8.         self.chromosome_size = chromosome_size
  9.         self.crossover       = crossover
  10.         self.mutation        = mutation
  11.         self.elite_pool      = elite_pool
  12.         self.population_size = population_size
  13.         self.fitness_test    = fitness_test
  14.         self.population      = list()
  15.         self.generation      = 0
  16.         np.random.seed([int(time.time() * 1e9) % 4294967296])
  17.         random.seed(time.time())
  18.         for i in xrange(population_size):
  19.             self.population.append([
  20.                 np.random.choice([0, 1], size=(chromosome_size,)),
  21.                 0
  22.              ])
  23.    
  24.    
  25.     def test_fitness(self):
  26.         # test fitness
  27.         for c in self.population:
  28.             c[1] = self.fitness_test(c[0])
  29.    
  30.     def breed(self):
  31.         # choose two mates
  32.         op = sorted(self.population, key=lambda x: x[1], reverse=True)
  33.         for i in xrange(int(self.population_size * self.crossover)):
  34.             male   = random.randint(0, self.population_size - 1)
  35.             female = random.choice(op[:int(self.population_size * self.elite_pool)])
  36.             splice = random.randint(0, self.chromosome_size - 1)
  37.            
  38.             for p in xrange(self.chromosome_size - splice):
  39.                 index = p + splice
  40.                 self.population[male][0][index] = female[0][index]
  41.        
  42.         # mutate
  43.         for i in xrange(int(self.population_size * self.mutation)):
  44.             cancer   = random.randint(0, self.chromosome_size - 1)
  45.             victim   = random.randint(0, self.population_size - 1)
  46.             self.population[victim][0][cancer] = 0 if self.population[victim][0][cancer] else 1
  47.        
  48.         # test fitness
  49.         self.test_fitness()
  50.         self.generation += 1
  51.  
  52. if __name__ == "__main__":
  53.     from numpy import array_equal
  54.     def fitness(x):
  55.         r = 0
  56.         if np.array_equal(x & np.array([0,0,0,0,0,0,0,0,1,0,1,1]), np.array([0,0,0,0,0,0,0,0,1,0,1,1])):
  57.             r += 1
  58.         if np.array_equal(x & np.array([0,1,0,0,1,0,0,0,0,0,0,0]), np.array([0,1,0,0,1,0,0,0,0,0,0,0])):
  59.             r += 2
  60.         if np.array_equal(x & np.array([0,0,0,0,0,1,1,0,0,0,0,0]), np.array([0,0,0,0,0,1,1,0,0,0,0,0])):
  61.             r += 3
  62.         if np.array_equal(x , np.array([0,1,0,0,1,1,1,0,1,0,1,1])):
  63.             r *= 7
  64.         return r
  65.        
  66.     species = GeneticALG(12, 500, fitness)
  67.    
  68.     print "[*] Starting population generated:"
  69.    
  70.     while True:
  71.         print "[+] Generation %i breeding:" % species.generation
  72.         species.breed()
  73.         print "[+] Top 5 census:"
  74.         census = sorted(species.population, key=lambda x: x[1], reverse=True)
  75.         for i in xrange(5):
  76.             print "\t%s" % census[i]
  77.             if census[i][1] > 6:
  78.                 exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement