Пример #1
0
 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
Пример #2
0
 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)