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
24 - def __init__(self):
25 raise NotImplementedError("Need to code this.")
26 27
28 -class GenerationEvolver(object):
29 """Evolve a population from generation to generation. 30 31 This implements a Generational GA, in which the population moves from 32 generation to generation. 33 """ 34
35 - def __init__(self, starting_population, selector):
36 """Initialize the evolver. 37 38 Arguments: 39 - starting_population - An initial set of individuals to begin 40 the evolution process from. This should be a list of Organism 41 objects. 42 - 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 """ 47 self._population = starting_population 48 self._selector = selector
49
50 - def evolve(self, stopping_criteria):
51 """Evolve the population through multiple generations. 52 53 Arguments: 54 - stopping_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 - The final evolved population. 60 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