Advertisement
tyler569

EvolutionBase

Jan 30th, 2017
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.15 KB | None | 0 0
  1.  
  2. ######################################################################
  3. ####            chromosome.py
  4. ######################################################################
  5.  
  6. import random
  7. import copy
  8.  
  9. class Nucleotide(object):
  10.     def __init__(self, value = None):
  11.         self.value = value
  12.  
  13.     def __str__(self):
  14.         return "Nucleotide(" + str(self.value) + ")"
  15.    
  16.     def flip(self):
  17.         return NotImplemented
  18.  
  19.  
  20. class Chromosome(object):
  21.     """
  22.    Chromosome is intended to be subclassed and have logic implemented
  23.    there - iter and getitem are provided for convenience
  24.    """
  25.     def __init__(self):
  26.         print("Do not use this implementation - subclass this and implement logic")
  27.  
  28.     @classmethod
  29.     def new_from_parent(cls, parent):
  30.         x = copy.deepcopy(parent)
  31.         x.mutate()
  32.         return x
  33.  
  34.     def __iter__(self):
  35.         for n in self.raw_data:
  36.             yield n
  37.  
  38.     def __getitem__(self, key):
  39.         return "".join(item.value for item in self.raw_data[key])
  40.  
  41.     @property
  42.     def encoded_data(self):
  43.         return NotImplemented
  44.  
  45.     def fitness(self):
  46.         return NotImplemented
  47.        
  48.     def mutate(self, amount = None):
  49.         if amount is None:
  50.             amount = self.mutability
  51.         for nucleotide in self.raw_data:
  52.             if random.random() < amount:
  53.                 nucleotide.flip()
  54.  
  55.  
  56. ######################################################################
  57. ####            simple_implementation.py
  58. ######################################################################
  59. #!/usr/bin/env python3
  60.  
  61. import random
  62. import math
  63.  
  64. from chromosome import Nucleotide, Chromosome
  65. #from organism import Organism
  66.  
  67. class NumberNucleotide(Nucleotide):
  68.     def __init__(self, value = None):
  69.         if value is None:
  70.             value = random.randint(0,9)
  71.         self.value = value
  72.  
  73.     def flip(self):
  74.         delta = random.choice([-1,1])
  75.         self.value += delta
  76.         if self.value < 0:
  77.             self.value = 0
  78.         self.value %= 10
  79.  
  80. class NumberChromosome(Chromosome):
  81.     def __init__(self, length, mutability):
  82.         self.raw_data = [NumberNucleotide() for i in range(length)]
  83.         self.mutability = mutability
  84.  
  85.     @property
  86.     def encoded_data(self):
  87.         return int("".join([str(j.value) for j in self.raw_data]))
  88.  
  89.     def fitness(self):
  90.         return self.encoded_data
  91.  
  92.  
  93. def main():
  94.     # generations = []
  95.     length = 1000
  96.     keep = 500
  97.     convergeance = 20
  98.     conv_ctr = 0
  99.  
  100.     orgs = [NumberChromosome(length = 10, mutability = 0.1) for i in range(length)]
  101.    
  102.     while True:
  103.         orgs.sort(key = lambda org: org.fitness(), reverse = True)
  104.  
  105.         if orgs[keep-1].fitness() == orgs[0].fitness():
  106.             conv_ctr += 1
  107.         else:
  108.             conv_ctr = 0
  109.         if conv_ctr == convergeance:
  110.             print("Converged")
  111.             exit()
  112.  
  113.         print(orgs[0].fitness(), orgs[0].encoded_data)
  114.  
  115.         orgs = orgs[:keep]
  116.         for i in range(length - keep):
  117.             orgs.append(NumberChromosome.new_from_parent(orgs[random.randint(0, keep-1)]))
  118.  
  119.  
  120. if __name__ == "__main__":
  121.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement