def check(self, indi): # is the individual that was just evaluated the last in the generation? if int(indi) == model.get_max_indi_id_by_gen(model.get_cur_gen(int(indi))): # check if termination requirements are met if model.get_cur_gen(indi) >= model.get_max_num_gen(): # they are, end ga raise web.seeother('/terminate') else: # they are not, move onto selection select().select(int(indi)) # are there more to evaluate? elif int(indi) <= model.get_max_indi_id_by_gen(model.get_cur_gen(int(indi))): raise web.seeother('/fitness/' + str(int(indi) + 1)) # just in case exit condition else: raise web.seeother('/terminate')
def tournament(self, k, indi): ''' tournament selection k = subset size indi = current indi id -- used for future generation and id number 1. a random subset of size, k, from the given generation is extracted 2. sort the pool by fitness value 3. return the winner, the individual with the highest fitness value ''' # from each individual in the current generation: get the indi_id and fitness value tmp = model.get_all_indi_id_by_gen(model.get_cur_gen(indi - 1)) all = [] for i in tmp: # put each indi and fitness in tuple all += [(i.indi_id, i.fitness)] # randomly select k individuals to create pool pool = [] for i in range(0, k): if random.choice(all) not in pool: pool += [random.choice(all)] print 'pool == ', pool # select indi with highest fitness as winner winner = sorted(pool, key=lambda x:-x[1])[0][0] print 'the winner id is ', winner return winner
def GET(self, indi): title = 'Fitness' ff = self.fitness_form() ff.indi_id.set_value(indi) song_name = self.convert_midi(self.create_pheno(indi), indi) l = model.get_num_indi(0)[0].id cur_gen = model.get_cur_gen(indi) all_indi = model.get_all_indi_id_by_gen(cur_gen) song_names = [] for i in all_indi: song_names += [self.convert_midi(self.create_pheno(i.indi_id), i.indi_id)] return render.fitness(title, ff, song_name, l)