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 initialize_population(self): ''' 种群初始化 ''' # utils 中.ini [IS_RUNNING] 参数置1 StatusUpdateTool.begin_evolution() pops = Population(params, 0) pops.initialize() self.pops = pops Utils.save_population_at_begin(str(pops), 0)
def initialize_population(self): ''' 种群初始化 ''' # ini文件IS_RUNNING参数置1 StatusUpdateTool.begin_evolution() # 初始化种群,传入ini文件的参数 pops = Population(params, 0) pops.initialize() self.pops = pops # 建立并写入种群begin文件 Utils.save_population_at_begin(str(pops), 0)
def do_genetic(population, population_propogation_function): iterations = 0 last_n = [float('Inf')] * Params.stop_genetic_after_count best_gene = None while True: iterations += 1 max_evaluation = population.get_max_evaluation(graph) last_n.pop(0) last_n.append(max_evaluation) flag = False for i in range(len(last_n)): if i != 0 and last_n[i] != last_n[i - 1]: flag = True if not flag: break print(max_evaluation) best_gene = population.best_n(1, graph)[0] print(best_gene) if Params.show_plot: plot_figure(best_gene, graph) new_population = population_propogation_function(population, graph) population = Population(new_population) Graph.no_colours = len(set(best_gene.array)) eval = best_gene.evaluate(graph) no_colors = len(set(best_gene.array)) conflicts = -1 * (eval + (no_colors * Params.penalty_per_color_used) ) / Params.penalty_same_color return iterations, best_gene, conflicts
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)
def __init__(self, screen, size): # Display attributes self.screen = screen self.size = size self.font = pygame.font.Font('ressources/font/DroidSansMono.ttf', 20) self.font_small = pygame.font.Font('ressources/font/DroidSansMono.ttf', 12) # Genetic attributes self.training_enabled = False self.pop = Population() self.pool = [] # The population of individuals self.gen_number = 1 # The generation number self.best_score = 0 # The best score for an individual # pop scores. self.best_pop_score = 0 self.gens_without_hs = 0
def create_population(individual_num, genes_num): ''' Creates a new population. individual_num: Number of individuals of the population. genes_num: Number of genes of each individual. ''' individual_list = [] for i in range(individual_num): individual = create_individual(genes_num) individual_list.append(individual) return Population(individuals=individual_list)
def get_next_population(current_population, mutator, fitnessFunction): ''' Generates the next new population. The new population is composed by the fittest genes of the current population. ''' individual_list = current_population.individuals next_individual_list = [] next_individual_list.append(individual_list[0]) while len(next_individual_list) < len(individual_list): parent1, i = pick_individual(individual_list, -1) parent2, i = pick_individual(individual_list, i) next_individual = reproduce(parent1, parent2, mutator) next_individual_list.append(next_individual) next_individual_list = sorted(next_individual_list, key=lambda individual: fitnessFunction.calculate_fitness(individual), reverse=True) return Population(individuals=next_individual_list)
def load_population(cls, prefix, gen_no): ''' 读取 .txt 文件,解析字符串 ''' file_name = './populations/%s_%02d.json' % (prefix, np.min(gen_no)) params = StatusUpdateTool.get_init_params() pop = Population(params, gen_no) f = open(file_name, 'r') # *** strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。 info = json.load(f) # individuals = info["populations"] for i in info["populations"]: individual_item = info individual_item["net"] = i indi_no = i["indi_no"] indi = Individual(individual_item, indi_no) # indi_no = indi_start_line[5:] # indi = Individual(params, indi_no) pop.individuals.append(indi) f.close() # load the fitness to the individuals who have been evaluated, only suitable for the first generation if gen_no == 0: after_file_path = './populations/after_%02d.json' % (gen_no) if os.path.exists(after_file_path): # 从after_文件中取出已训练好的accuracy数据 fitness_map = {} f = open(after_file_path, 'r') info = json.load(f) for i in info["populations"]: if "accuracy" in i: fitness_map[i["indi_no"]] = i["accuracy"] f.close() for indi in pop.individuals: if indi.id in fitness_map: indi.acc = fitness_map[indi.id] return pop
class Genetic: def __init__(self, screen, size): # Display attributes self.screen = screen self.size = size self.font = pygame.font.Font('ressources/font/DroidSansMono.ttf', 20) self.font_small = pygame.font.Font('ressources/font/DroidSansMono.ttf', 12) # Genetic attributes self.training_enabled = False self.pop = Population() self.pool = [] # The population of individuals self.gen_number = 1 # The generation number self.best_score = 0 # The best score for an individual # pop scores. self.best_pop_score = 0 self.gens_without_hs = 0 # Enable or disable the training. def toggle_training(self, enabled): if enabled: self.training_enabled = True else: self.training_enabled = False # Create a default population of stupid bunnies. def init(self): # Create a default population. self.pop.create_default_pop() self.pool = self.pop.population # Reset the pool def next_pop(self): # Get the last gen survivor best_individual = self.pop.get_best_individual(self.pool) # Count how many gens could not improve the high score. if self.best_pop_score < best_individual.score: self.best_pop_score = best_individual.score self.gens_without_hs = 0 else: self.gens_without_hs += 1 # Reset the pool. self.pool = [] self.gen_number += 1 # Prepare next population. self.pop.breed(best_individual, self.stupidity_factor(self.gens_without_hs)) self.pool = self.pop.population # Define the supidity factor of a pop depending on how many gens didn't beat high score. def stupidity_factor(self, gens_without_hs): if gens_without_hs == 5: print('Population is maybe stupid') if gens_without_hs > 10: print('Population is really stupid') # Maybe tweak this return result. return gens_without_hs def gen_watcher(self): best_individual = self.pop.get_best_individual(self.pool) # Display population data self.display_gen_data(best_individual.score) # Display the neural network self.display_neural_network(best_individual) def think(self, player, display): # Get the inputs. inputs = self.get_inputs(player, display) # Feed the inputs to a function determined_output = self.determine_output(inputs, player) # print(determined_output) if determined_output > 0.5: self.output_jump(player) # From 0 to 1 def determine_output(self, inputs, player): # The 4 inputs i1 = int(inputs[0]) i2 = int(inputs[1]) i3 = inputs[2] i4 = inputs[3] # Inputs to layer weights w_i1_j1 = player.synapses[0] w_i1_j2 = player.synapses[1] w_i1_j3 = player.synapses[2] w_i1_j4 = player.synapses[3] w_i2_j1 = player.synapses[2] w_i2_j2 = player.synapses[3] w_i2_j3 = player.synapses[4] w_i2_j4 = player.synapses[5] w_i3_j1 = player.synapses[4] w_i3_j2 = player.synapses[5] w_i3_j3 = player.synapses[6] w_i3_j4 = player.synapses[7] w_i4_j1 = player.synapses[6] w_i4_j2 = player.synapses[7] w_i4_j3 = player.synapses[8] w_i4_j4 = player.synapses[9] # Layer to output weights w_j1_k1 = player.synapses[10] w_j2_k1 = player.synapses[11] w_j3_k1 = player.synapses[12] w_j4_k1 = player.synapses[13] bias = 2 j1 = player.sigmoid((i1 * w_i1_j1 + i2 * w_i2_j1 + i3 * w_i3_j1 + i4 * w_i4_j1) + bias) j2 = player.sigmoid((i1 * w_i1_j2 + i2 * w_i2_j2 + i3 * w_i3_j2 + i4 * w_i4_j2) + bias) j3 = player.sigmoid((i1 * w_i1_j3 + i2 * w_i2_j3 + i3 * w_i3_j3 + i4 * w_i4_j3) + bias) j4 = player.sigmoid((i1 * w_i1_j4 + i2 * w_i2_j4 + i3 * w_i3_j4 + i4 * w_i4_j4) + bias) k1 = j1 * w_j1_k1 + j2 * w_j2_k1 + j3 * w_j3_k1 + j4 * w_j4_k1 return player.sigmoid(k1) # player status, obstacle distances. def get_inputs(self, player, display): obstacles = display.om.get_obstacles() obstacle_positions = self.get_obstacle_positions(obstacles) # We need to know the distance from the first obstacle if len(obstacle_positions) > 0: next_obstacle_dist = obstacle_positions[0] - (player.pos_x + player.size_x) else: next_obstacle_dist = 1 # We need to know how far is the next obstacle if len(obstacle_positions) > 1: obstacle_spacing = obstacle_positions[1] - obstacle_positions[0] else: obstacle_spacing = 1 jump_fall = 0 obstacle_spacing = 0 if player.jumping: if player.falling: jump_fall = 1 inputs = [ jump_fall, # If player is already jumping player.pos_y, next_obstacle_dist, # The distance between the player and next obstacle obstacle_spacing, # The spacing between 1st and 2nd obstacle ] return inputs # Get the x coords of obstacles. def get_obstacle_positions(self, obstacles): obstacles_pos = [] for obstacle in obstacles: obstacles_pos.append(obstacle.pos_x) return obstacles_pos # Make the bunny jump. def output_jump(self, individual): individual.do_jump() # Use the om from display to reset obstacles. def reset_obstacles(self, display): display.om.clear_all_obstacles() def display_gen_data(self, player_score): color = 255, 171, 0 # Display alive individuals. gen_string = 'Bunnies alive : {:05d}'.format(len(self.pool)) text = self.font.render(gen_string, True, color) text_rect = text.get_rect() text_rect.top = 20 text_rect.right = self.size[0] - 20 self.screen.blit(text, text_rect) # Display current gen. gen_string = 'Generation : {:05d}'.format(self.gen_number) text = self.font.render(gen_string, True, color) text_rect = text.get_rect() text_rect.top = 40 text_rect.right = self.size[0] - 20 self.screen.blit(text, text_rect) # Display the best score an individual could achieve. if player_score > self.best_score: self.best_score = player_score gen_string = 'Best Score : {:05d}'.format(self.best_score) text = self.font.render(gen_string, True, color) text_rect = text.get_rect() text_rect.top = 60 text_rect.right = self.size[0] - 20 self.screen.blit(text, text_rect) gen_string = 'Stupidity factor : {:05d}'.format(self.gens_without_hs) text = self.font.render(gen_string, True, color) text_rect = text.get_rect() text_rect.top = 80 text_rect.right = self.size[0] - 20 self.screen.blit(text, text_rect) # Display the neural network of an individual. (the best in this case) def display_neural_network(self, player): green = 0, 170, 0 white = 255, 255, 255 black = 0, 0, 0 red = 255, 0, 0 orange = 230, 82, 0 i1_pos = (30, 30) i2_pos = (30, 70) i3_pos = (30, 110) i4_pos = (30, 150) j1_pos = (120, 30) j2_pos = (120, 70) j3_pos = (120, 110) j4_pos = (120, 150) k1_pos = (160, 90) circles = [ i1_pos, # I1 i2_pos, # I2 i3_pos, # I3 i4_pos, # I4 j1_pos, # J1 j2_pos, # J2 j3_pos, # J3 j4_pos, # J4 k1_pos, # K1 ] # Draw circles for circle in circles: pygame.draw.circle(self.screen, (255, 255, 255), circle, 8, 1) # Build lines data lines = [] for synapse in player.synapses: if synapse > 2: color = white elif synapse > 0: color = green elif synapse > -1: color = orange elif synapse > -2: color = red else: color = black lines.append(color) lines_coord = [ # Lines from i1 [i1_pos, j1_pos], # w_i1_j1 [i1_pos, j2_pos], # w_i1_j2 [i1_pos, j3_pos], # w_i1_j3 [i1_pos, j4_pos], # w_i1_j4 # Lines from i2 [i2_pos, j1_pos], # w_i2_j1 [i2_pos, j2_pos], # w_i2_j2 [i2_pos, j3_pos], # w_i2_j3 [i2_pos, j4_pos], # w_i2_j4 # Lines from i3 [i3_pos, j1_pos], # w_i3_j1 [i3_pos, j2_pos], # w_i3_j2 [i3_pos, j3_pos], # w_i3_j3 [i3_pos, j4_pos], # w_i3_j4 # Lines from i4 [i4_pos, j1_pos], # w_i4_j1 [i4_pos, j2_pos], # w_i4_j2 [i4_pos, j3_pos], # w_i4_j3 [i4_pos, j4_pos], # w_i4_j4 # Lines from J layer to K [j1_pos, k1_pos], # w_j1_k1 [j2_pos, k1_pos], # w_j2_k1 [j3_pos, k1_pos], # w_j3_k1 [j4_pos, k1_pos], # w_j4_k1 ] # Draw lines i = 0 for line in lines: pygame.draw.line(self.screen, line, lines_coord[i][0], lines_coord[i][1], 2) i += 1
def initialize_population(self, args): pops = Population(args, 0) pops.re_initialize(args) self.pops = pops
} evaluator = ANNCoverageEvaluator(**evaluator_args) population_args = { "pop_size": POP_SIZE, "genome_size": evaluator.get_genome_size(), "eval_func": evaluator.evaluate, "init_func": np.random.normal, "weight_scale": 1.0, "mutation_rate": 0.1, "mutation_scale": 0.1, "selection_rate": 0.9 } population = Population(pop_size=population_args["pop_size"], genome_size=population_args["genome_size"], eval_func=population_args["eval_func"], mutation_rate=population_args["mutation_rate"], mutation_scale=population_args["mutation_scale"], init_func=np.random.normal) # Train iterations = 200 # ann, history = train(iterations=iterations, generator=generator, evaluator=evaluator, population=population) ann, history = train(iterations=iterations, generator=generator, evaluator=evaluator, population=population, world_name=world_name, evaluator_args=evaluator_args, population_args=population_args) g = visualize.show_history(history)
def initialize_population(self): StatusUpdateTool.begin_evolution() pops = Population(params, 0) pops.initialize() self.pops = pops Utils.save_population_at_begin(str(pops), 0)
bot = Bot(settings.BOT_SKINS[0], 75, 200, 300, 290, "Bot") obs_player = Observer(player, [wall]) obs_bot = Observer(bot, [wall]) player.obs = obs_player bot.set_target(wall) bot.obs = obs_bot _game.player = player _game.add_object(bot) if __name__ == "__main__": pygame.display.set_caption("Smart Square") game = Game(400, 500) wall = Wall(settings.SKINS[0], -100, 500, 250, "Wall") wall.game = game game.add_object(wall) # ------------------------------------------------------------ env = Environment() env.population = Population(10, 0.2) ai_list, env.observers = create_ai(env.population, target=wall) game.objects.extend(ai_list) game.env = env # ------------------------------------------------------------ game.start()
df = pd.DataFrame({"fitness": fitness, "diversity": diversity}) df["Generation"] = df.index df = pd.melt(df, id_vars=['Generation'], value_vars=["fitness", "diversity"]) return df if __name__ == "__main__": functions = [rosenbrock, rastrigin] function_num = 1 fn = functions[function_num] population = Population( 100, 2, lambda x: -fn(x), mutation_scale=0.2, init_func=lambda size: np.random.uniform(low=-2, high=2, size=size)) history = evolve(100, population) df = process_history(history) g = sns.FacetGrid(data=df, row="variable", height=3, sharey="row") g = g.map(plt.plot, "Generation", "value") # sns.lineplot(data=df, x="Generation", y="value", hue="variable") plt.show() extent = [-2, 2, -2, 2] file_name = "ae" img_dir = "" if (function_num == 0):
def initialize_population(): n = Params.initial_population_size population = [] for i in range(n): population.append(get_random_parent(Graph.no_colours, no_points)) return Population(population)
def load_population(cls, prefix, gen_no): file_name = './populations/%s_%02d.txt' % (prefix, np.min(gen_no)) params = StatusUpdateTool.get_init_params() pop = Population(params, gen_no) f = open(file_name) indi_start_line = f.readline().strip() while indi_start_line.startswith('indi'): indi_no = indi_start_line[5:] indi = Individual(params, indi_no) for line in f: line = line.strip() if line.startswith('--'): indi_start_line = f.readline().strip() break else: if line.startswith('Acc'): indi.acc = float(line[4:]) elif line.startswith('[conv'): data_maps = line[6:-1].split(',', 5) conv_params = {} for data_item in data_maps: _key, _value = data_item.split(":") if _key == 'number': indi.number_id = int(_value) conv_params['number'] = int(_value) elif _key == 'in': conv_params['in_channel'] = int(_value) elif _key == 'out': conv_params['out_channel'] = int(_value) else: raise ValueError( 'Unknown key for load conv unit, key_name:%s' % (_key)) conv = ResUnit(conv_params['number'], conv_params['in_channel'], conv_params['out_channel']) indi.units.append(conv) elif line.startswith('[pool'): pool_params = {} for data_item in line[6:-1].split(','): _key, _value = data_item.split(':') if _key == 'number': indi.number_id = int(_value) pool_params['number'] = int(_value) elif _key == 'type': pool_params['max_or_avg'] = float(_value) else: raise ValueError( 'Unknown key for load pool unit, key_name:%s' % (_key)) pool = PoolUnit(pool_params['number'], pool_params['max_or_avg']) indi.units.append(pool) else: print( 'Unknown key for load unit type, line content:%s' % (line)) pop.individuals.append(indi) f.close() # load the fitness to the individuals who have been evaluated, only suitable for the first generation if gen_no == 0: after_file_path = './populations/after_%02d.txt' % (gen_no) if os.path.exists(after_file_path): fitness_map = {} f = open(after_file_path) for line in f: if len(line.strip()) > 0: line = line.strip().split('=') fitness_map[line[0]] = float(line[1]) f.close() for indi in pop.individuals: if indi.id in fitness_map: indi.acc = fitness_map[indi.id] return pop
sort_a = a[idx] sum_a = np.sum(a).astype(np.float) selected_index = [] for i in range(k): u = np.random.rand() * sum_a sum_ = 0 for i in range(sort_a.shape[0]): sum_ += sort_a[i] if sum_ > u: selected_index.append(idx[i]) break return selected_index[0] if __name__ == '__main__': from genetic.population import Population import logging FORMAT = '%(asctime)-15s [%(levelname)s] %(message)s' logging.basicConfig(format=FORMAT, level=logging.DEBUG) _params = StatusUpdateTool.get_init_params() _gen_no = 20 pop = Population(_params, _gen_no) pop.initialize() _prob = 0.2 # Mutation Probability m = Mutation(pop.individuals, _prob, logging) m.do_mutation()