Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 70 lines (50 sloc) 1.526 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#! /usr/bin/env python

"""
Very simple demo in which organisms try to minimise
the output value of a function
"""

from pygene.gene import FloatGene, FloatGeneMax
from pygene.organism import Organism, MendelOrganism
from pygene.population import Population

class CvGene(FloatGeneMax):
    """
Gene which represents the numbers used in our organism
"""
    # genes get randomly generated within this range
    randMin = -100.0
    randMax = 100.0
    
    # probability of mutation
    mutProb = 0.1
    
    # degree of mutation
    mutAmt = 0.1


class Converger(MendelOrganism):
    """
Implements the organism which tries
to converge a function
"""
    genome = {'x':CvGene, 'y':CvGene}
    
    def fitness(self):
        """
Implements the 'fitness function' for this species.
Organisms try to evolve to minimise this function's value
"""
        return self['x'] ** 2 + self['y'] ** 2

    def __repr__(self):
        return "<Converger fitness=%f x=%s y=%s>" % (
            self.fitness(), self['x'], self['y'])


# create an empty population

pop = Population(species=Converger, init=2, childCount=50, childCull=20)


# now a func to run the population

def main():
    try:
        while True:
            # execute a generation
            pop.gen()

            # get the fittest member
            best = pop.best()
            
            # and dump it out
            print best

    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.