Back to index

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

List of all members.

Public Member Functions

def __init__
def select

Private Member Functions

def _set_up_wheel

Detailed Description

Roulette wheel selection proportional to individuals fitness.

The implements a roulette wheel selector that selects individuals
from the population, and performs mutation and crossover on
the selected individuals.

Definition at line 14 of file RouletteWheel.py.


Constructor & Destructor Documentation

def Bio.GA.Selection.RouletteWheel.RouletteWheelSelection.__init__ (   self,
  mutator,
  crossover,
  repairer = None 
)
Initialize the selector.

Arguments:

o mutator -- A Mutation object which will perform mutation
on an individual.

o crossover -- A Crossover object which will take two
individuals and produce two new individuals which may
have had crossover occur.

o repairer -- A class which can do repair on rearranged genomes
to eliminate infeasible individuals. If set at None, so repair
will be done.

Definition at line 21 of file RouletteWheel.py.

00021 
00022     def __init__(self, mutator, crossover, repairer = None):
00023         """Initialize the selector.
00024 
00025         Arguments:
00026 
00027         o mutator -- A Mutation object which will perform mutation
00028         on an individual.
00029 
00030         o crossover -- A Crossover object which will take two
00031         individuals and produce two new individuals which may
00032         have had crossover occur.
00033 
00034         o repairer -- A class which can do repair on rearranged genomes
00035         to eliminate infeasible individuals. If set at None, so repair
00036         will be done.
00037         """
00038         AbstractSelection.__init__(self, mutator, crossover, repairer)

Here is the caller graph for this function:


Member Function Documentation

def Bio.GA.Selection.RouletteWheel.RouletteWheelSelection._set_up_wheel (   self,
  population 
) [private]
Set up the roulette wheel based on the fitnesses.

This creates a fitness proportional 'wheel' that will be used for
selecting based on random numbers.

Returns:

o A dictionary where the keys are the 'high' value that an
individual will be selected. The low value is determined by
the previous key in a sorted list of keys. For instance, if we
have a sorted list of keys like:

[.1, .3, .7, 1]

Then the individual whose key is .1 will be selected if a number
between 0 and .1 is chosen, the individual whose key is .3 will
be selected if the number is between .1 and .3, and so on.

The values of the dictionary are the organism instances.

Definition at line 85 of file RouletteWheel.py.

00085 
00086     def _set_up_wheel(self, population):
00087         """Set up the roulette wheel based on the fitnesses.
00088 
00089         This creates a fitness proportional 'wheel' that will be used for
00090         selecting based on random numbers.
00091 
00092         Returns:
00093         
00094         o A dictionary where the keys are the 'high' value that an
00095         individual will be selected. The low value is determined by
00096         the previous key in a sorted list of keys. For instance, if we
00097         have a sorted list of keys like:
00098 
00099         [.1, .3, .7, 1]
00100 
00101         Then the individual whose key is .1 will be selected if a number
00102         between 0 and .1 is chosen, the individual whose key is .3 will
00103         be selected if the number is between .1 and .3, and so on.
00104 
00105         The values of the dictionary are the organism instances.
00106         """
00107         # first sum up the total fitness in the population
00108         total_fitness = 0
00109         for org in population:
00110             total_fitness += org.fitness
00111 
00112         # now create the wheel dictionary for all of the individuals
00113         wheel_dict = {}
00114         total_percentage = 0
00115         for org in population:
00116             org_percentage = float(org.fitness) / float(total_fitness)
00117 
00118             # the organisms chance of being picked goes from the previous
00119             # percentage (total_percentage) to the previous percentage
00120             # plus the organisms specific fitness percentage
00121             wheel_dict[total_percentage + org_percentage] = copy.copy(org)
00122 
00123             # keep a running total of where we are at in the percentages
00124             total_percentage += org_percentage
00125 
00126         return wheel_dict

Here is the caller graph for this function:

Perform selection on the population based using a Roulette 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.

Definition at line 39 of file RouletteWheel.py.

00039 
00040     def select(self, population):
00041         """Perform selection on the population based using a Roulette model.
00042 
00043         Arguments:
00044 
00045         o population -- A population of organisms on which we will perform
00046         selection. The individuals are assumed to have fitness values which
00047         are due to their current genome.
00048         """
00049         # set up the current probabilities for selecting organisms
00050         # from the population
00051         prob_wheel = self._set_up_wheel(population)
00052         probs = prob_wheel.keys()
00053         probs.sort()
00054         
00055         # now create the new population with the same size as the original
00056         new_population = []
00057 
00058         for pair_spin in range(len(population) // 2):
00059             # select two individuals using roulette wheel selection
00060             choice_num_1 = random.random()
00061             choice_num_2 = random.random()
00062 
00063             # now grab the two organisms from the probabilities
00064             chosen_org_1 = None
00065             chosen_org_2 = None
00066             prev_prob = 0
00067             for cur_prob in probs:
00068                 if choice_num_1 > prev_prob and choice_num_1 <= cur_prob:
00069                     chosen_org_1 = prob_wheel[cur_prob]
00070                 if choice_num_2 > prev_prob and choice_num_2 <= cur_prob:
00071                     chosen_org_2 = prob_wheel[cur_prob]
00072 
00073                 prev_prob = cur_prob
00074 
00075             assert chosen_org_1 is not None, "Didn't select organism one"
00076             assert chosen_org_2 is not None, "Didn't select organism two"
00077 
00078             # do mutation and crossover to get the new organisms
00079             new_org_1, new_org_2 = self.mutate_and_crossover(chosen_org_1,
00080                                                              chosen_org_2)
00081             
00082             new_population.extend([new_org_1, new_org_2])
00083 
00084         return new_population

Here is the call graph for this function:

Here is the caller graph for this function:


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