Back to index

python-biopython  1.60
Evolver.py
Go to the documentation of this file.
00001 """Evolution Strategies for a Population.
00002 
00003 Evolver classes manage a population of individuals, and are responsible
00004 for taking care of the transition from one generation to the next.
00005 """
00006 # standard modules
00007 import sys
00008 
00009 class SteadyStateEvolver(object):
00010     """Evolve a population in place.
00011 
00012     This implements a Steady State GA, where the population of individuals
00013     is evolved in place.
00014     """
00015     def __init__(self):
00016         raise NotImplementedError("Need to code this.")
00017         
00018 class GenerationEvolver(object):
00019     """Evolve a population from generation to generation.
00020 
00021     This implements a Generational GA, in which the population moves from
00022     generation to generation.
00023     """
00024     def __init__(self, starting_population, selector):
00025         """Initialize the evolver.
00026 
00027         Arguments:
00028 
00029         o starting_population -- An initial set of individuals to begin
00030         the evolution process from. This should be a list of Organism
00031         objects.
00032 
00033         o selector -- A Selection object that implements selection, along
00034         with mutation and crossover to select a new population from a
00035         given population.
00036         """
00037         self._population = starting_population
00038         self._selector = selector
00039 
00040     def evolve(self, stopping_criteria):
00041         """Evolve the population through multiple generations.
00042 
00043         Arguments:
00044 
00045         o stoppping_criteria -- A function which, when passed the current
00046         individuals in the population, will determine when to stop
00047         the evolution process.
00048 
00049         Returns:
00050 
00051         o The final evolved population.
00052         """
00053         while not(stopping_criteria(self._population)):
00054             try:
00055                 # perform selection, mutation, crossover on the population
00056                 self._population = self._selector.select(self._population)
00057 
00058                 # update the fitness of the new popultation
00059                 for organism in self._population:
00060                     organism.recalculate_fitness()
00061 
00062             # dump out all of the organisms for debugging if the
00063             # evolution process is broken with a Control-C
00064             except KeyboardInterrupt:
00065                 # sort the population so we can look at duplicates
00066                 self._population.sort()
00067                 for org in self._population:
00068                     print org
00069                 sys.exit()
00070             
00071         return self._population