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