Back to index

python-biopython  1.60
Public Member Functions | Private Member Functions | Private Attributes
Bio.GA.Crossover.GeneralPoint.GeneralPointCrossover Class Reference
Inheritance diagram for Bio.GA.Crossover.GeneralPoint.GeneralPointCrossover:
Inheritance graph
[legend]

List of all members.

Public Member Functions

def __init__
def do_crossover

Private Member Functions

def _generate_locs
def _crossover

Private Attributes

 _crossover_prob
 _sym
 _npoints

Detailed Description

Perform n-point crossover between genomes at some defined rates.

   Ideas on how to use this class:
       - Call it directly ( construct, do_crossover )
       - Use one of the provided subclasses
       - Inherit from it:
           * replace _generate_locs with a more domain 
             specific technique
           * replace _crossover with a more efficient 
             technique for your point-count

Definition at line 35 of file GeneralPoint.py.


Constructor & Destructor Documentation

def Bio.GA.Crossover.GeneralPoint.GeneralPointCrossover.__init__ (   self,
  points,
  crossover_prob = .1 
)
Initialize to do crossovers at the specified probability.

Definition at line 47 of file GeneralPoint.py.

00047 
00048     def __init__(self, points, crossover_prob = .1):
00049         """Initialize to do crossovers at the specified probability.
00050         """
00051         self._crossover_prob = crossover_prob
00052 
00053         self._sym     = points % 2 # odd n, gets a symmetry flag
00054         self._npoints = (points + self._sym)//2 # (N or N+1)//2
    

Here is the caller graph for this function:


Member Function Documentation

def Bio.GA.Crossover.GeneralPoint.GeneralPointCrossover._crossover (   self,
  x,
  no,
  locs 
) [private]
Generalized Crossover Function:
    
   arguments: 
       x (int)        - genome number [0|1]
       no (organism,organism)
              - new organisms
       locs (int list, int list)
              - lists of locations, 
                [0, +n points+, bound]
                for each genome (sync'd with x)

    return type: sequence (to replace no[x])

Reimplemented in Bio.GA.Crossover.GeneralPoint.InterleaveCrossover, and Bio.GA.Crossover.GeneralPoint.TwoCrossover.

Definition at line 113 of file GeneralPoint.py.

00113 
00114     def _crossover( self, x, no, locs ):
00115         """Generalized Crossover Function:
00116             
00117            arguments: 
00118                x (int)        - genome number [0|1]
00119                no (organism,organism)
00120                               - new organisms
00121                locs (int list, int list)
00122                               - lists of locations, 
00123                                 [0, +n points+, bound]
00124                                 for each genome (sync'd with x)
00125 
00126             return type: sequence (to replace no[x])
00127         """
00128         s = no[ x ].genome[ :locs[ x ][1] ]
00129         for n in range(1,self._npoints):
00130             # flipflop between genome_0 and genome_1
00131             mode = (x+n)%2
00132             # _generate_locs gives us [0, +n points+, bound]
00133             #  so we can iterate: { 0:loc(1) ... loc(n):bound }
00134             t = no[ mode ].genome[ locs[mode][n]:locs[mode][n+1] ]
00135             if (s): s = s + t
00136             else:   s = t
00137         return s

Here is the caller graph for this function:

Generalized Location Generator:
    
   arguments:
       bound (int)   - upper bound 
    
   returns: [0]+x_0...x_n+[bound]
     where n=self._npoints-1
       and 0 < x_0 < x_1 ... < bound

Reimplemented in Bio.GA.Crossover.GeneralPoint.InterleaveCrossover, and Bio.GA.Crossover.GeneralPoint.TwoCrossover.

Definition at line 94 of file GeneralPoint.py.

00094 
00095     def _generate_locs(self, bound):
00096         """Generalized Location Generator:
00097             
00098            arguments:
00099                bound (int)   - upper bound 
00100             
00101            returns: [0]+x_0...x_n+[bound]
00102              where n=self._npoints-1
00103                and 0 < x_0 < x_1 ... < bound
00104         """
00105         results = []
00106         for increment in range(self._npoints):
00107             x = random.randint(1,bound-1)
00108             while (x in results):  # uniqueness
00109                 x = random.randint(1,bound-1)
00110             results.append( x )
00111         results.sort()             # sorted
00112         return [0]+results+[bound] # [0, +n points+, bound]
            

Here is the caller graph for this function:

Potentially do a crossover between the two organisms.

Definition at line 55 of file GeneralPoint.py.

00055 
00056     def do_crossover(self, org_1, org_2):
00057         """Potentially do a crossover between the two organisms.
00058         """
00059         new_org = ( org_1.copy(), org_2.copy() )
00060         
00061         # determine if we have a crossover
00062         crossover_chance = random.random()
00063         if crossover_chance <= self._crossover_prob:
00064             
00065             # pre-compute bounds (len(genome))
00066             bound  = (len(new_org[0].genome), len(new_org[1].genome))
00067             
00068             mbound = min(bound)
00069             # can't have more than 0,x_0...x_n,bound locations
00070             if (self._npoints == 0 or self._npoints > mbound-2):
00071                 self._npoints = mbound-2
00072                 
00073             y_locs = []
00074             # generate list for the shortest of the genomes
00075             x_locs = self._generate_locs( mbound )
00076 
00077             if (self._sym != 0):  
00078                 y_locs = x_locs
00079             else:
00080                 # figure out which list we've generated 
00081                 # for, and generate the other
00082                 if (mbound == bound[0]):
00083                     y_locs = self._generate_locs( bound[1] )
00084                 else:
00085                     y_locs = x_locs
00086                     xlocs  = self._generate_locs( bound[0] )
00087               
00088             # copy new genome strings over
00089             tmp = self._crossover(0, new_org, (x_locs,y_locs))
00090             new_org[1].genome = self._crossover(1, new_org, (x_locs,y_locs))
00091             new_org[0].genome = tmp
00092 
00093         return new_org

Here is the call graph for this function:


Member Data Documentation

Definition at line 50 of file GeneralPoint.py.

Definition at line 53 of file GeneralPoint.py.

Definition at line 52 of file GeneralPoint.py.


The documentation for this class was generated from the following file: