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

Source Code for Module Bio.GA.Selection.Abstract

 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  """Base selection class from which all Selectors should derive.""" 
 7   
 8   
9 -class AbstractSelection(object):
10 """Base class for Selector classes. 11 12 This classes provides useful functions for different selector classes 13 and also defines the functions that all selector classes must 14 implement. 15 16 This class should not be used directly, but rather should be subclassed. 17 """ 18
19 - def __init__(self, mutator, crossover, repairer=None):
20 """Initialize a selector. 21 22 Arguments: 23 - mutator - A Mutation object which will perform mutation 24 on an individual. 25 - crossover - A Crossover object which will take two 26 individuals and produce two new individuals which may 27 have had crossover occur. 28 - repairer - A class which can do repair on rearranged genomes 29 to eliminate infeasible individuals. If set at None, so repair 30 will be done. 31 32 """ 33 self._mutator = mutator 34 self._crossover = crossover 35 self._repairer = repairer
36
37 - def mutate_and_crossover(self, org_1, org_2):
38 """Perform mutation and crossover on the two organisms. 39 40 This uses the classes mutator and crossover functions to 41 perform the manipulations. 42 43 If a repair class is available, then the rearranged genomes will 44 be repaired to make them feasible. 45 46 The newly created individuals are returned. 47 """ 48 # first crossover the two organisms 49 cross_org_1, cross_org_2 = self._crossover.do_crossover(org_1, org_2) 50 51 # now perform mutation on the two organisms 52 final_org_1 = self._mutator.mutate(cross_org_1) 53 final_org_2 = self._mutator.mutate(cross_org_2) 54 55 # if we have a repair class, perform repair 56 if self._repairer is not None: 57 final_org_1 = self._repairer.repair(final_org_1) 58 final_org_2 = self._repairer.repair(final_org_2) 59 60 return final_org_1, final_org_2
61
62 - def select(self, population):
63 raise NotImplementedError("Derived classes must implement.")
64