Back to index

python-biopython  1.60
Abstract.py
Go to the documentation of this file.
00001 """Base selection class from which all Selectors should derive.
00002 """
00003 
00004 class AbstractSelection(object):
00005     """Base class for Selector classes.
00006 
00007     This classes provides useful functions for different selector classes
00008     and also defines the functions that all selector classes must
00009     implement.
00010 
00011     This class should not be used directly, but rather should be subclassed.
00012     """
00013     def __init__(self, mutator, crossover, repairer = None):
00014         """Initialize a selector.
00015 
00016         Arguments:
00017 
00018         o mutator -- A Mutation object which will perform mutation
00019         on an individual.
00020 
00021         o crossover -- A Crossover object which will take two
00022         individuals and produce two new individuals which may
00023         have had crossover occur.
00024 
00025         o repairer -- A class which can do repair on rearranged genomes
00026         to eliminate infeasible individuals. If set at None, so repair
00027         will be done.
00028         """
00029         self._mutator = mutator
00030         self._crossover = crossover
00031         self._repairer = repairer
00032 
00033     def mutate_and_crossover(self, org_1, org_2):
00034         """Perform mutation and crossover on the two organisms.
00035 
00036         This uses the classes mutator and crossover functions to
00037         perform the manipulations.
00038 
00039         If a repair class is available, then the rearranged genomes will
00040         be repaired to make them feasible.
00041         
00042         The newly created individuals are returned.
00043         """
00044         # first crossover the two organisms
00045         cross_org_1, cross_org_2 = self._crossover.do_crossover(org_1, org_2)
00046 
00047         # now perform mutation on the two organisms
00048         final_org_1 = self._mutator.mutate(cross_org_1)
00049         final_org_2 = self._mutator.mutate(cross_org_2)
00050 
00051         # if we have a repair class, perform repair
00052         if self._repairer is not None:
00053             final_org_1 = self._repairer.repair(final_org_1)
00054             final_org_2 = self._repairer.repair(final_org_2)
00055 
00056         return final_org_1, final_org_2
00057 
00058     def select(self, population):
00059         raise NotImplementedError("Derived classes must implement.")