def environment_selection(self, args): v_list = [] indi_list = [] for indi in self.pops.individuals: indi_list.append(indi) v_list.append(indi.acc) for indi in self.parent_pops.individuals: indi_list.append(indi) v_list.append(indi.acc) #add log # find the largest one's index max_index = np.argmax(v_list) selection = Selection() selected_index_list = selection.RouletteSelection(v_list, k=args.pop_size) if max_index not in selected_index_list: first_selectd_v_list = [v_list[i] for i in selected_index_list] min_idx = np.argmin(first_selectd_v_list) selected_index_list[min_idx] = max_index next_individuals = [indi_list[i] for i in selected_index_list] next_gen_pops = Population(args, self.pops.gen_no+1) next_gen_pops.create_from_offspring(next_individuals) self.pops = next_gen_pops
def environment_selection(self): v_list = [] indi_list = [] # 将此时的种群acc和父种群acc记录在一起 for indi in self.pops.individuals: indi_list.append(indi) v_list.append(indi.acc) for indi in self.parent_pops.individuals: indi_list.append(indi) v_list.append(indi.acc) _str = [] for _, indi in enumerate(self.pops.individuals): _t_str = 'Indi-%s-%.5f-%s' % (indi.id, indi.acc, indi.uuid()[0]) _str.append(_t_str) for _, indi in enumerate(self.parent_pops.individuals): _t_str = 'Pare-%s-%.5f-%s' % (indi.id, indi.acc, indi.uuid()[0]) _str.append(_t_str) # add log # find the largest one's index # 选出max个体并筛选下一代 max_index = np.argmax(v_list) selection = Selection() # 采用轮盘赌选择算法 selected_index_list = selection.RouletteSelection( v_list, k=self.params['pop_size']) if max_index not in selected_index_list: # 若max个体不在筛选结果中,那么从筛选结果中替换掉其中的min个体 first_selectd_v_list = [v_list[i] for i in selected_index_list] min_idx = np.argmin(first_selectd_v_list) selected_index_list[min_idx] = max_index next_individuals = [indi_list[i] for i in selected_index_list] """Here, the population information should be updated, such as the gene no and then to the individual id""" # 更新种群基本信息 next_gen_pops = Population(self.pops.params, self.pops.gen_no + 1) next_gen_pops.create_from_offspring(next_individuals) self.pops = next_gen_pops for _, indi in enumerate(self.pops.individuals): _t_str = 'new -%s-%.5f-%s' % (indi.id, indi.acc, indi.uuid()[0]) _str.append(_t_str) _file = './populations/ENVI_%2d.txt' % (self.pops.gen_no) Utils.write_to_file('\n'.join(_str), _file) Utils.save_population_at_begin(str(self.pops), self.pops.gen_no)