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

Source Code for Module Bio.GA.Selection.Diversity

 1  """Select individuals into a new population trying to maintain diversity. 
 2   
 3  This selection mechanism seeks to try and get both high fitness levels 
 4  and high diversity levels in the population. 
 5  """ 
 6  # standard modules 
 7  import random 
 8  import math 
 9   
10  # biopython 
11  from Bio.Seq import MutableSeq 
12   
13  # local modules 
14  from Abstract import AbstractSelection 
15  from Tournament import TournamentSelection 
16   
17   
18 -class DiversitySelection(AbstractSelection):
19 """Implement diversity selection. 20 21 Diversity selection is performed by trying to select individuals 22 from the population that aren't already in the new_population. A group 23 of selected individuals is then subjected to selection using 24 a passed selection routine. 25 26 If new individuals can not be selected, new individuals will be 27 randomly generated and inserted into the population. 28 """
29 - def __init__(self, internal_selector, genome_generator):
30 """Initialize a diversity selector. 31 32 Arguments: 33 34 o internal_selector - A selection object that will be used to select 35 individuals based on fitness, perform crossover, mutation and repair. 36 37 o genome_generator - A function that, when called, will return a 38 genome to be used for a new organism. The genome returned must 39 be a MutableSeq() object. 40 """ 41 self._internal_selector = internal_selector 42 self._genome_generator = genome_generator 43 44 self.sub_pop_percent = .1 45 self.random_tries = 10
46
47 - def _get_new_organism(self, new_pop, old_pop):
48 """Get a new organism from old_pop that isn't in new_pop. 49 50 This attempts to select an organism from old_pop that isn't in 51 new_pop. If we can't do this in the number of tries specified 52 by the class attribute random_tries, we generate a new random 53 organism and return that. 54 """ 55 # try to pick an organism that isn't in the population 56 new_org = None 57 num_tries = 0 58 while new_org is None and num_tries < self.random_tries: 59 chance_org = random.choice(old_pop) 60 61 if chance_org not in new_pop: 62 new_org = chance_org 63 64 num_tries += 1 65 66 # if we don't get an organism, generate a random one 67 if new_org is None: 68 new_org = old_pop[0].copy() 69 random_genome = self._genome_generator() 70 new_org.genome = random_genome 71 new_org.recalculate_fitness() 72 73 return new_org
74
75 - def select(self, population):
76 """Perform selection on the current population, encouraging diversity. 77 """ 78 new_population = [] 79 80 while len(new_population) < len(population): 81 # generate a sub population 82 sub_pop_size = int(math.ceil(len(population) * 83 self.sub_pop_percent)) 84 sub_pop = [] 85 for individual in range(sub_pop_size): 86 new_org = self._get_new_organism(new_population, population) 87 sub_pop.append(new_org) 88 89 # put the new sub population through selection, mutation 90 # and all of that good stuff 91 new_sub_pop = self._internal_selector.select(sub_pop) 92 93 new_population.extend(new_sub_pop) 94 95 # return the new population, which should have the same number 96 # of individuals we started with. 97 return new_population[:len(population)]
98