def post_speciation(self, speciation, regulation, complexity): self.mean_complexity.append(mean(complexity)) self.min_complexity.append(min(complexity)) self.max_complexity.append(max(complexity)) details = { 'Id': [], 'Size': [], 'Fitness': [], 'AdjFitness': [], 'LastImproved': [] } for s_id, specie in speciation.species.items(): details['Id'].append(s_id) details['Size'].append(len(specie)) details['Fitness'].append(specie.fitness) details['AdjFitness'].append(specie.adjusted_fitness) details['LastImproved'].append(specie.last_improved) if self.best_genome is None or speciation.best_genome.fitness > self.best_genome.fitness: self.best_genome = speciation.best_genome self.current_best = speciation.best_genome print("\nTotal number of species {0}".format(len(speciation.species))) print("Lifetime best fitness {0} " "@ complexity {1}".format(self.best_genome.fitness, self.best_genome.complexity)) print("Current best fitness {0}" " @ complexity {1}".format(self.current_best.fitness, self.current_best.complexity)) print(tabulate(details, headers="keys", numalign="right")) print(regulation)
def main(): config = Config() config.initialize('config/config_1.ini') reproduction_factory = TraditionalReproduction() speciation_factory = TraditionalFixedSpeciation() regulation_factory = NoRegulation(config) reporting_factory = reporter.Reporter() reporting_factory.add_report(stdout.StdOut()) reporting_factory.add_report(statistics.Statistics()) population = Population(reproduction_factory, speciation_factory, regulation_factory, reporting_factory) parallel_evaluator = ParallelEvaluator(num_workers=4, eval_function=evaluate) population.initialize(parallel_evaluator.evaluate, config) while True: population.fit(25) best_genome = population.best_genome print(best_genome) env = gym.make('LunarLander-v2') # [1] env = gym.wrappers.Monitor(env, 'results', force=True) network = FeedForwardNetwork(best_genome, config) network.initialize(Activations(), Aggregations()) fitness = [] for e_idx in range(10): episode_reward = 0 observation = env.reset() network.reset(hard=True) while True: env.render() action = network.activate(observation.tolist()) action = action.index(max(action)) observation, reward, done, info = env.step(action) episode_reward += reward if done: break fitness.append(episode_reward) env.close() env.env.close() fitness_reward = mean(fitness) print("Examination mean fitness [{}]".format(fitness_reward)) if fitness_reward >= config.neat.fitness_threshold: break
def evaluate(attributes): g_id, genome, config = attributes network = FeedForwardNetwork(genome, config) network.initialize(Activations(), Aggregations()) fitness = [] env = gym.make('CartPole-v1') # [1], [2] for e_idx in range(100): network.reset(hard=True) observation = env.reset() episode_reward = 0 while True: action = network.activate(observation.tolist())[0] action = 0 if action < 0.5 else 1 observation, reward, done, info = env.step(action) episode_reward += reward if done: break fitness.append(episode_reward) return mean(fitness)
def evaluate(attributes): g_id, genome, config = attributes network = RecurrentNetwork(genome, config) network.initialize(Activations(), Aggregations()) fitness = [] env = gym.make('MountainCar-v0') # [1], [2] for e_idx in range(10): episode_reward = 0 observation = env.reset() network.reset(hard=True) while True: action = network.activate(observation.tolist()) action = action.index(max(action)) observation, reward, done, info = env.step(action) episode_reward += reward if done: break fitness.append(episode_reward) return mean(fitness)
def main(): config = Config() config.initialize('config/config_1.ini') reproduction_factory = TraditionalReproduction() speciation_factory = TraditionalSpeciation() regulation_factory = PhasedComplexityRegulation(config) reporting_factory = reporter.Reporter() reporting_factory.add_report(stdout.StdOut()) reporting_factory.add_report(statistics.Statistics()) population = Population(reproduction_factory, speciation_factory, regulation_factory, reporting_factory) parallel_evaluator = ParallelEvaluator(num_workers=4, eval_function=evaluate) population.initialize(parallel_evaluator.evaluate, config) population.fit() best_genome = population.best_genome print(best_genome) # Champion solution env = gym.make('CartPole-v1') # [1] network = FeedForwardNetwork(best_genome, config) network.initialize(Activations(), Aggregations()) fitness = [] for e_idx in range(100): network.reset(hard=True) observation = env.reset() episode_reward = 0 while True: if e_idx % 10 == 0: env.render() time.sleep(0.075) action = network.activate(observation.tolist())[0] action = 0 if action < 0.5 else 1 observation, reward, done, info = env.step(action) episode_reward += reward if done: break fitness.append(episode_reward) print("Examination mean fitness [{}]".format(mean(fitness)))
def calc_specie_stats(self, generation, population_size, config): target_size_sum = 0 mean_fitness_sum = 0.0 for specie in self.species.values(): members_true_fitness, members_fitness = [], [] for member in specie.members: members_true_fitness.append(member.fitness) members_fitness.append(member.adjusted_fitness) specie.members_fitness = members_fitness specie.fitness = specie.fitness_criterion(members_true_fitness) specie.adjusted_fitness = specie.fitness_criterion(members_fitness) specie.adjusted_fitness_mean = mean(members_fitness) self._purge_stagnant_species(generation, config) for specie in self.species.values(): mean_fitness_sum += specie.adjusted_fitness_mean if mean_fitness_sum == 0.0: target_size_float = population_size / len(self.species) for specie in self.species.values(): specie.target_size_float = target_size_float specie.target_size = probabilistic_round(target_size_float) target_size_sum += specie.target_size else: for s_id, specie in self.species.items(): specie.target_size_float = ( specie.adjusted_fitness_mean / mean_fitness_sum) * population_size target_size = probabilistic_round(specie.target_size_float) if target_size == 0 and s_id == self.best_specie_idx: target_size = 1 specie.target_size = target_size target_size_sum += specie.target_size target_size_delta = target_size_sum - population_size if target_size_delta < 0: if target_size_delta == -1: self.species[self.best_specie_idx].target_size += 1 else: specie_idxs = list(self.species.keys()) for _ in range(abs(target_size_delta)): probabilities = np.array([max(0.0, specie.target_size_float - specie.target_size) for specie in self.species.values()]) probabilities = probabilities / np.sum(probabilities) self.species[np.random.choice(specie_idxs, 1, p=probabilities)[0]].target_size += 1 elif target_size_delta > 0: specie_idxs = list(self.species.keys()) i = 0 while i < target_size_delta: probabilities = np.array([max(0.0, specie.target_size - specie.target_size_float) for specie in self.species.values()]) probabilities = probabilities / np.sum(probabilities) specie_idx = np.random.choice(specie_idxs, 1, p=probabilities)[0] if self.species[specie_idx].target_size != 0: if not (specie_idx == self.best_specie_idx and self.species[specie_idx].target_size == 1): self.species[specie_idx].target_size -= 1 i += 1 for s_id, specie in self.species.items(): if specie.target_size == 0: specie.elites = 0 continue elites = probabilistic_round(len(specie.members) * config.species.elitism) specie.elites = min(elites, specie.target_size) if s_id == self.best_specie_idx and elites == 0: specie.elites = 1 specie.off_springs = specie.target_size - specie.elites specie.off_spring_asexual = probabilistic_round(specie.off_springs * config.species.off_spring_asexual_rate) specie.off_spring_sexual = specie.off_springs - specie.off_spring_asexual specie.survivors = max(1, probabilistic_round(len(specie.members) * config.species.survivor_rate))
def fit(self, n=None): if self.config.neat.no_fitness_termination and (n is None): raise RuntimeError('Cannot have no generational limit with no fitness termination') k = 0 while n is None or k < n: k += 1 self.reporter.start_generation(self.generation) self.reporter.pre_reproduction() self.population = self.reproduction.reproduce(species=self.speciation.species, generation=self.generation, regulation=self.regulation, population_size=self.population_size, config=self.config) self.reporter.post_reproduction() self.reporter.pre_evaluation() self.fitness_function(list(self.population.items()), self.config) self.reporter.post_evaluation() best = None damaged_members = [] members_fitness = [] members_complexity = [] for g_id, member in self.population.items(): if member.is_damaged: damaged_members.append(g_id) else: members_fitness.append(member.fitness) members_complexity.append(member.complexity) if best is None or member.fitness > best.fitness: best = member for g_id in damaged_members: del self.population[g_id] if self.best_genome is None or best.fitness > self.best_genome.fitness: self.best_genome = best min_fitness, max_fitness = min(members_fitness), max(members_fitness) if min_fitness == max_fitness: for member in self.population.values(): member.adjusted_fitness = 0.0 else: for member in self.population.values(): member.adjusted_fitness = normalize(min_fitness, max_fitness, member.fitness, 0.0, 1.0) self.reporter.pre_speciation() self.speciation.speciate(self.population, self.generation, self.config) self.speciation.reset_specie_stats() self.speciation.sort_specie_genomes() self.speciation.calc_best_stats() self.speciation.calc_specie_stats(self.generation, self.population_size, self.config) self.reporter.post_speciation(self.speciation, self.regulation, members_complexity) if not self.config.neat.no_fitness_termination: fv = self.fitness_criterion(members_fitness) if fv >= self.config.neat.fitness_threshold: break self.regulation.determine_mode(mean_complexity=mean(members_complexity), generation=self.generation, current_best=self.speciation.best_genome) self.reporter.end_generation() self.generation += 1 return self.best_genome
def mean_aggregation(x): return mean(x)