Package Bio :: Package GA :: Module Evolver
[hide private]
[frames] | no frames]

Source Code for Module Bio.GA.Evolver

 1  # This code is part of the Biopython distribution and governed by its 
 2  # license.  Please see the LICENSE file that should have been included 
 3  # as part of this package. 
 4  # 
 5   
 6  """Evolution Strategies for a Population. 
 7   
 8  Evolver classes manage a population of individuals, and are responsible 
 9  for taking care of the transition from one generation to the next. 
10  """ 
11  # standard modules 
12  from __future__ import print_function 
13   
14  import sys 
15   
16   
17 -class SteadyStateEvolver(object):
18 """Evolve a population in place. 19 20 This implements a Steady State GA, where the population of individuals 21 is evolved in place. 22 """
23 - def __init__(self):
24 raise NotImplementedError("Need to code this.")
25 26
27 -class GenerationEvolver(object):
28 """Evolve a population from generation to generation. 29 30 This implements a Generational GA, in which the population moves from 31 generation to generation. 32 """
33 - def __init__(self, starting_population, selector):
34 """Initialize the evolver. 35 36 Arguments: 37 38 o starting_population -- An initial set of individuals to begin 39 the evolution process from. This should be a list of Organism 40 objects. 41 42 o selector -- A Selection object that implements selection, along 43 with mutation and crossover to select a new population from a 44 given population. 45 """ 46 self._population = starting_population 47 self._selector = selector
48
49 - def evolve(self, stopping_criteria):
50 """Evolve the population through multiple generations. 51 52 Arguments: 53 54 o stoppping_criteria -- A function which, when passed the current 55 individuals in the population, will determine when to stop 56 the evolution process. 57 58 Returns: 59 60 o The final evolved population. 61 """ 62 while not(stopping_criteria(self._population)): 63 try: 64 # perform selection, mutation, crossover on the population 65 self._population = self._selector.select(self._population) 66 67 # update the fitness of the new popultation 68 for organism in self._population: 69 organism.recalculate_fitness() 70 71 # dump out all of the organisms for debugging if the 72 # evolution process is broken with a Control-C 73 except KeyboardInterrupt: 74 # sort the population so we can look at duplicates 75 self._population.sort() 76 for org in self._population: 77 print(org) 78 sys.exit() 79 80 return self._population
81