Back to index

python-biopython  1.60
Public Member Functions | Private Attributes
Bio.GA.Selection.Tournament.TournamentSelection Class Reference
Inheritance diagram for Bio.GA.Selection.Tournament.TournamentSelection:
Inheritance graph
[legend]
Collaboration diagram for Bio.GA.Selection.Tournament.TournamentSelection:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def select

Private Attributes

 _num_competitors

Detailed Description

Implement tournament style selection.

Definition at line 14 of file Tournament.py.


Constructor & Destructor Documentation

def Bio.GA.Selection.Tournament.TournamentSelection.__init__ (   self,
  mutator,
  crossover,
  repairer,
  num_competitors = 2 
)
Initialize the tournament selector.

Arguments:

o num_competitors-- The number of individiuals that should be
involved in a selection round. By default we just have two
individuals (head to head!).

See AbstractSelection for a description of the arguments to
the initializer.

Definition at line 17 of file Tournament.py.

00017 
00018     def __init__(self, mutator, crossover, repairer, num_competitors = 2):
00019         """Initialize the tournament selector.
00020 
00021         Arguments:
00022 
00023         o num_competitors-- The number of individiuals that should be
00024         involved in a selection round. By default we just have two
00025         individuals (head to head!).
00026 
00027         See AbstractSelection for a description of the arguments to
00028         the initializer.
00029         """
00030         AbstractSelection.__init__(self, mutator, crossover, repairer)
00031 
00032         if num_competitors < 2:
00033             raise ValueError("Must have at least 2 competitors!")
00034         
00035         self._num_competitors = num_competitors

Here is the caller graph for this function:


Member Function Documentation

Perform selection on the population using the Tournament model.

Arguments:

o population -- A population of organisms on which we will perform
selection. The individuals are assumed to have fitness values which
are due to their current genome (ie. the fitness is up to date).

Definition at line 36 of file Tournament.py.

00036 
00037     def select(self, population):
00038         """Perform selection on the population using the Tournament model.
00039 
00040         Arguments:
00041 
00042         o population -- A population of organisms on which we will perform
00043         selection. The individuals are assumed to have fitness values which
00044         are due to their current genome (ie. the fitness is up to date).
00045         """
00046         # we want to create a new population of the same size as the original
00047         new_population = []
00048 
00049         while len(new_population) < len(population):
00050             # select two individuals using tournament selection
00051             new_orgs = []
00052             # select for two individuals
00053             for round_num in range(2):
00054                 competitors = []
00055                 while len(competitors) < self._num_competitors:
00056                     new_org = random.choice(population)
00057                     if new_org not in competitors:
00058                         competitors.append(new_org)
00059                                     
00060                 # sort the competitors by fitness, this will put them
00061                 # from lowest to highest
00062                 competitors.sort(key = lambda org: org.fitness)
00063 
00064                 # get the best organism
00065                 new_orgs.append(competitors[-1])
00066 
00067             assert len(new_orgs) == 2, "Expected two organisms to be selected"
00068 
00069             # do mutation and crossover to get the new organisms
00070             new_org_1, new_org_2 = self.mutate_and_crossover(new_orgs[0],
00071                                                              new_orgs[1])
00072 
00073             new_population.extend([new_org_1, new_org_2])
00074 
00075         return new_population
00076                 

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 34 of file Tournament.py.


The documentation for this class was generated from the following file: