I haven't done any research into how you actually write a genetic algorithm, so this is my best guess. What I'm really curious to know is two-fold:
- Have I created a genetic algorithm?
- If I have, how can I continue to explore the subject more?
I hope this code and docstrings are enough explanation for how the code works.
from random import randint
import time
class Organism(object):
def __init__(self, r=0, g=0, b=0):
"""Initialize the class with the RGB color values."""
self.r = r
self.g = g
self.b = b
@property
def fitness(self):
"""The lower the fitness the better."""
total = self.r + self.g + self.b
return abs(765 - total)
@classmethod
def spawn(cls, parent):
"""Return a mutated generation of ten members."""
generation = []
for number in range(0, 10):
r = cls.mutate(parent.r)
g = cls.mutate(parent.g)
b = cls.mutate(parent.b)
generation.append(cls(r, g, b))
return generation
@staticmethod
def mutate(value):
"""Mutate the value by 10 points in either direction."""
min_, max_ = value - 10, value + 10
return randint(min_, max_)
def breed_and_select_fittest(individual):
"""Return the fittest member of a generation."""
generation = Organism.spawn(individual)
fittest = generation[0]
for member in generation:
if member.fitness < fittest.fitness:
fittest = member
return fittest
if __name__ == '__main__':
individual = Organism() # abiogenesis!
while True:
individual = breed_and_select_fittest(individual)
print individual.fitness
time.sleep(0.2)