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

Source Code for Module Bio.GA.Selection.Abstract

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