HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Python genetic algorithm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
geneticpythonalgorithm

Problem

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)

Solution

That is, more or less, a genetic algorithm, though it only implements mutation and not recombination:

@classmethod
def recombine(cls, parentA, parentB):
    traits = {}
    for t in ("r", "g", "b"):
        traits[t] = (getattr(parentA, t) + getattr(parentB, t))/2
    return cls(**traits)


There are, of course, many ways to implement recombination. For example, instad of the simple averaging I do above, you could instead choose a random parent to take each trait from. The best solution depends on your problem space.

To learn more, I'd suggest googling and finding tutorials or examples, and then trying to solve some more realistic problems with them. Can you adapt them to solve any ProjectEuler problems? Or find another application where they're useful?

Code Snippets

@classmethod
def recombine(cls, parentA, parentB):
    traits = {}
    for t in ("r", "g", "b"):
        traits[t] = (getattr(parentA, t) + getattr(parentB, t))/2
    return cls(**traits)

Context

StackExchange Code Review Q#140811, answer score: 6

Revisions (0)

No revisions yet.