Back to index

python-biopython  1.60
Uniform.py
Go to the documentation of this file.
00001 """Perform uniform crossovers between the genomes of two organisms.
00002 
00003 
00004 genome 1 --       A B C D E F G
00005                   .   . .   .
00006 genome 2 --       a b c d e f g
00007 
00008 After crossover:
00009 
00010 new genome 1 --  a B c d E f G
00011 new genome 2 --  A b C D e F g
00012 
00013 Uniform Crossover is a standard crossover technique for
00014 rapid mutation-behavior.  
00015 """
00016 # standard modules
00017 import random
00018 
00019 class UniformCrossover(object):
00020     """Perform single point crossover between genomes at some defined rates.
00021 
00022     This performs a single crossover between two genomes at some
00023     defined frequency. The location of the crossover is chosen randomly
00024     if the crossover meets the probability to occur.
00025     """
00026     def __init__(self, crossover_prob = .1, uniform_prob = 0.7):
00027         """Initialize to do uniform crossover at the specified probability and frequency.
00028         """
00029         self._crossover_prob = crossover_prob
00030         self._uniform_prob   = uniform_prob
00031         return
00032         
00033     def do_crossover(self, org_1, org_2):
00034         """Potentially do a crossover between the two organisms.
00035         """
00036         new_org_1 = org_1.copy()
00037         new_org_2 = org_2.copy()
00038         
00039         # determine if we have a crossover
00040         crossover_chance = random.random()
00041         if crossover_chance <= self._crossover_prob:
00042             minlen = min(len(new_org_1.genome),len(new_org_2.genome))
00043             for i in range( minlen ):
00044                 uniform_chance = random.random()
00045                 if uniform_chance <= self._uniform_prob:
00046                     # cycle element
00047                     temp = new_org_1.genome[i]
00048                     new_org_1.genome[i] = new_org_2.genome[i]
00049                     new_org_2.genome[i] = temp
00050             
00051         return new_org_1, new_org_2