def evolve(self, organisms_old, gen): self.enemy_group = set() self.dead = set() elitism_num = int( np.floor(self.settings['elitism'] * self.settings['pop_size'])) new_orgs = self.settings['pop_size'] - elitism_num # --- GET STATS FROM CURRENT GENERATION ----------------+ stats = defaultdict(int) for org in organisms_old: if org.score > stats['BEST'] or stats['BEST'] == 0: stats['BEST'] = org.score if org.score < stats['WORST'] or stats['WORST'] == 0: stats['WORST'] = org.score stats['SUM'] += org.score stats['COUNT'] += 1 stats['AVG'] = stats['SUM'] / stats['COUNT'] # --- ELITISM (KEEP BEST PERFORMING ORGANISMS) ---------+ orgs_sorted = sorted(organisms_old, key=operator.attrgetter('score'), reverse=True) self.population = [] for i in range(0, elitism_num): aux_dict = {"wih": orgs_sorted[i].wih, "woh": orgs_sorted[i].who, "pos": [random.randrange(0, self.settings['WIDTH']), \ random.randrange(0, self.settings['HEIGHT'])]} self.population.append( Auxiliars.merge_two_dicts(orgs_sorted[i].pars, aux_dict)) # --- GENERATE NEW ORGANISMS ---------------------------+ for w in range(elitism_num - 1, self.settings["pop_size"]): aux_dict = dict() # SELECTION (TRUNCATION SELECTION) canidates = range(0, elitism_num) random_index = random.sample(canidates, 2) org_1 = orgs_sorted[random_index[0]] org_2 = orgs_sorted[random_index[1]] # CROSSOVER crossover_weight = random.random() aux_dict["wih"] = (crossover_weight * org_1.wih) + \ ((1 - crossover_weight) * org_2.wih) aux_dict["who"] = (crossover_weight * org_1.who) + \ ((1 - crossover_weight) * org_2.who) aux_dict["pos"] = [ random.randrange(0, self.settings['WIDTH']), random.randrange(0, self.settings['HEIGHT']) ] aux_dict["v_max"] = (crossover_weight * org_1.pars["v_max"]) + \ ((1 - crossover_weight) * org_2.pars["v_max"]) aux_dict["ang_rang"] = (crossover_weight * org_1.pars["ang_rang"]) + \ ((1 - crossover_weight) * org_2.pars["ang_rang"]) aux_dict["acc"] = (crossover_weight * org_1.get_acc()) + \ ((1 - crossover_weight) * org_2.get_acc()) aux_dict["ang_acc"] = (crossover_weight * org_1.get_ang_acc()) + \ ((1 - crossover_weight) * org_2.get_ang_acc()) aux_dict["vel"] = [0, 0] aux_dict["ang_vel"] = float(self.settings["ang_vel_median"] * np.random.randn(1) + self.settings["ang_vel_std_dev"]) aux_dict["ang"] = float(self.settings["ang_median"] * np.random.randn(1) + self.settings["ang_std_dev"]) aux_dict["WIDTH"] = self.settings["WIDTH"] aux_dict["HEIGHT"] = self.settings["HEIGHT"] aux_dict["score"] = 0 aux_dict["fr"] = self.settings["fr"] aux_dict["lives"] = self.settings["lives"] # MUTATION mutate = random.random() if mutate <= self.settings['mutate']: # PICK WHICH WEIGHT MATRIX TO MUTATE mat_pick = random.randint(0, 1) # MUTATE: WIH WEIGHTS if mat_pick == 0: index_row = random.randint(0, self.settings['hnodes'] - 1) aux_dict["wih"][index_row] = aux_dict["wih"][index_row] * \ random.uniform(0.9, 1.1) if aux_dict["wih"][index_row].any() > 1: aux_dict["wih"][index_row] = 1 if aux_dict["wih"][index_row].any() < - \ 1: aux_dict["wih"][index_row] = -1 # MUTATE: WHO WEIGHTS if mat_pick == 1: index_row = random.randint(0, self.settings['onodes'] - 1) index_col = random.randint(0, self.settings['hnodes'] - 1) aux_dict["who"][index_row][index_col] = aux_dict["who"][index_row][index_col] * \ random.uniform(0.9, 1.1) if aux_dict["who"][index_row][index_col] > 1: aux_dict["who"][index_row][index_col] = 1 if aux_dict["who"][index_row][index_col] < -1: aux_dict["who"][index_row][index_col] = -1 self.population.append(aux_dict)
def spawn_ships(self, player_pos): self.enemy_group = set([ EnemyShip(Auxiliars.merge_two_dicts(org, self.p), player_pos, org["wih"], org["who"]) for org in self.population ])