def on_finish(self, generation: Generation) -> None: logger.info("OnFinish called with generation {}".format( generation.number)) # Get the best agent agent = fitness_evaluation_utils.get_best_agent(generation.agents) # Print genome text_visualization.print_agent(agent) # Plot fitness values reporter_visualization.plot_fitness_reporter( self.fitness_reporter.data, plot=True) # Plot species sizes reporter_visualization.plot_species_reporter( self.species_reporter.data, plot=True) # Plot required time values reporter_visualization.plot_time_reporter(self.time_reporter.data, plot=True) # Plot genome genome_visualization.draw_genome_graph(agent.genome, draw_labels=False) plt.show()
def get_best_genomes_from_species(species_list: List[Species], min_species_size) -> List[Genome]: """ Get the best genomes from each species with more members then the given min_species_size. The agent, with the best fitness value will be included, even if the species has less members then the given min_species_size :param species_list: a list of species, from which the best members should be extraced :param min_species_size: the min size if each species, that the best agent will be copied :return: a list with the best genomes """ best_generation_agent = None best_genomes_list = [] for species in species_list: best_species_agent = fitness_evaluation_utils.get_best_agent( species.members) # Check if the species champions is the generation champion if best_generation_agent is None or best_generation_agent.fitness < best_species_agent.fitness: best_generation_agent = best_species_agent # Check if species agent should be added if len(species.members) >= min_species_size: best_genomes_list.append(best_species_agent.genome) # Check if best generation agent is in best genomes list if best_generation_agent is not None and best_generation_agent.genome not in best_genomes_list: best_genomes_list.append(best_generation_agent.genome) return best_genomes_list
def on_finish(self, generation: Generation, reporters: List[NeatReporter]) -> None: logger.info("OnFinish called with generation {}".format( generation.number)) self.solved_generation_number = generation.number # Get the best agent agent = fitness_evaluation_utils.get_best_agent(generation.agents) # Print genome text_visualization.print_agent(agent) # # Plot fitness curve # reporter_visualization.plot_fitness_reporter(self.fitness_reporter.data, plot=True) # # # Plot species sizes # reporter_visualization.plot_species_reporter(self.species_reporter.data, plot=True) # # # Plot required time # reporter_visualization.plot_time_reporter(self.time_reporter.data, plot=True) # # # Draw genome # genome_visualization.draw_genome_graph(agent.genome, draw_labels=False) # plt.show() # Visualize results self.visualize_genome(agent.genome)
def on_finish(self, generation: Generation, reporters: List[NeatReporter]) -> None: """ Store the best genome every time :param generation: the last generation evaluated :param reporters: a list with all reporters :return: None """ best_agent = fitness_evaluation_utils.get_best_agent(generation.agents) file_name = self.storage_path + self.name_prefix + "finish" + str( generation.number) + ".data" # Create dictionary for reporters reporter_dict = {} for reporter in reporters: should_save, key, val = reporter.store_data() if should_save: reporter_dict[key] = val file_save.save_genome_and_reporter(file_name, best_agent.genome, reporter_dict) logger.info( "Saved best genome and reporter of generation {} into file {}". format(generation.number, file_name))
def on_generation_evaluation_end(self, generation: Generation, reporters: List[NeatReporter]) -> None: best_agent = fitness_evaluation_utils.get_best_agent(generation.agents) logger.info("Finished evaluation of generation {}".format( generation.number)) logger.info("Best Fitness in Agent {}: {}".format( best_agent.id, best_agent.fitness)) logger.info("Amount species: {}".format(len(generation.species_list)))
def on_generation_evaluation_end(self, generation: Generation) -> None: self.progressbar.finish() best_agent = fitness_evaluation_utils.get_best_agent(generation.agents) logger.info("Finished evaluation of generation {}".format( generation.number)) logger.info("Best Fitness in Agent {}: {}, AdditionalInfo: {}".format( best_agent.id, best_agent.fitness, best_agent.additional_info)) logger.info("Amount species: {}".format(len(generation.species_list)))
def on_finish(self, generation: Generation, reporters: List[NeatReporter]) -> None: logger.info("OnFinish called with generation {}".format( generation.number)) # Get the best agent agent = fitness_evaluation_utils.get_best_agent(generation.agents) # Print genome text_visualization.print_agent(agent)
def on_generation_evaluation_end(self, generation: Generation, reporters: List[NeatReporter]) -> None: required_time = round(time.time() - self.start_time_generation, 4) logger.info( "Finished evaluation of generation {}, Required Time: {}".format( generation.number, required_time)) best_agent = fitness_evaluation_utils.get_best_agent(generation.agents) logger.info("Best Fitness in Agent {}: {}, AdditionalInfo: {}".format( best_agent.id, best_agent.fitness, best_agent.additional_info)) logger.info("Amount species: {}".format(len(generation.species_list)))
def update_fitness_species(generation: Generation) -> Generation: """ Update the max fitness and the corresponding generation of each species with the values from its members. :param generation: which contains the species :return: the updated generation """ for species in generation.species_list: best_fitness_current_generation = fitness_evaluation_utils.get_best_agent( species.members).fitness # If fitness is none or lower, update the value if species.max_species_fitness is None or species.max_species_fitness < best_fitness_current_generation: species.max_species_fitness = best_fitness_current_generation species.generation_max_species_fitness = generation.number return generation
def finish_evaluation(self, generation: Generation) -> bool: best_agent = fitness_evaluation_utils.get_best_agent(generation.agents) return best_agent.additional_info["solved"]
def test_get_best_agent(self): expected_best_agent = self.generation.agents[len(self.generation.agents) - 1] best_agent = get_best_agent(self.generation.agents) self.assertEqual(expected_best_agent, best_agent) self.assertEqual(self.config.population_size - 1, best_agent.fitness)