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