Back to index
Implement diversity selection. Diversity selection is performed by trying to select individuals from the population that aren't already in the new_population. A group of selected individuals is then subjected to selection using a passed selection routine. If new individuals can not be selected, new individuals will be randomly generated and inserted into the population.
Initialize a diversity selector. Arguments: o internal_selector - A selection object that will be used to select individuals based on fitness, perform crossover, mutation and repair. o genome_generator - A function that, when called, will return a genome to be used for a new organism. The genome returned must be a MutableSeq() object.
00028 00029 def __init__(self, internal_selector, genome_generator): 00030 """Initialize a diversity selector. 00031 00032 Arguments: 00033 00034 o internal_selector - A selection object that will be used to select 00035 individuals based on fitness, perform crossover, mutation and repair. 00036 00037 o genome_generator - A function that, when called, will return a 00038 genome to be used for a new organism. The genome returned must 00039 be a MutableSeq() object. 00040 """ 00041 self._internal_selector = internal_selector 00042 self._genome_generator = genome_generator 00043 00044 self.sub_pop_percent = .1 00045 self.random_tries = 10
Get a new organism from old_pop that isn't in new_pop. This attempts to select an organism from old_pop that isn't in new_pop. If we can't do this in the number of tries specified by the class attribute random_tries, we generate a new random organism and return that.
00046 00047 def _get_new_organism(self, new_pop, old_pop): 00048 """Get a new organism from old_pop that isn't in new_pop. 00049 00050 This attempts to select an organism from old_pop that isn't in 00051 new_pop. If we can't do this in the number of tries specified 00052 by the class attribute random_tries, we generate a new random 00053 organism and return that. 00054 """ 00055 # try to pick an organism that isn't in the population 00056 new_org = None 00057 num_tries = 0 00058 while new_org is None and num_tries < self.random_tries: 00059 chance_org = random.choice(old_pop) 00060 00061 if chance_org not in new_pop: 00062 new_org = chance_org 00063 00064 num_tries += 1 00065 00066 # if we don't get an organism, generate a random one 00067 if new_org is None: 00068 new_org = old_pop.copy() 00069 random_genome = self._genome_generator() 00070 new_org.genome = random_genome 00071 new_org.recalculate_fitness() 00072 00073 return new_org
Perform selection on the current population, encouraging diversity.
00074 00075 def select(self, population): 00076 """Perform selection on the current population, encouraging diversity. 00077 """ 00078 new_population =  00079 00080 while len(new_population) < len(population): 00081 # generate a sub population 00082 sub_pop_size = int(math.ceil(len(population) * 00083 self.sub_pop_percent)) 00084 sub_pop =  00085 for individual in range(sub_pop_size): 00086 new_org = self._get_new_organism(new_population, population) 00087 sub_pop.append(new_org) 00088 00089 # put the new sub population through selection, mutation 00090 # and all of that good stuff 00091 new_sub_pop = self._internal_selector.select(sub_pop) 00092 00093 new_population.extend(new_sub_pop) 00094 00095 # return the new population, which should have the same number 00096 # of individuals we started with. 00097 return new_population[:len(population)] 00098 00099