def test_threaded_evaluation(): """Tests a neat evolution using neat.threaded.ThreadedEvaluator""" if not HAVE_THREADING: raise unittest.SkipTest("Platform does not have threading") # Load configuration. local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'test_configuration') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) # Create the population, which is the top-level object for a NEAT run. p = neat.Population(config) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(1, 5)) # Run for up to 19 generations. pe = neat.ThreadedEvaluator(4, eval_dummy_genome_nn) p.run(pe.evaluate, 19) stats.save()
def train(self, config_path: str): """ Initializes training for every genome """ self.data: List[Request] = load_dataset() shuffle(self.data) if not self.p: self.p = self.__initialize__(config_path) pe = neat.ThreadedEvaluator(multiprocessing.cpu_count() - 1, self.__eval_genome__) winner = self.p.run(pe.evaluate, self.generations) node_names = { -1: "method", -2: "headers", -3: "protocol", -4: "body", -5: "query", 0: "Is not a hack", 1: "Is a hack" } visualize.draw_net(self.__get_config__(config_path), winner, True, node_names=node_names) visualize.plot_stats(self.stats, ylog=False, view=True) visualize.plot_species(self.stats, view=True) return winner
def test_threaded_evaluator(): """Tests general functionality of neat.threaded.ThreadedEvaluator""" if not HAVE_THREADING: raise unittest.SkipTest("Platform does not have threading") n_workers = 3 e = neat.ThreadedEvaluator(n_workers, eval_dummy_genome_nn) try: # ensure workers are not started if (len(e.workers) > 0) or (e.working): raise Exception("ThreadedEvaluator started on __init__()!") # ensure start() starts the workers e.start() if (len(e.workers) != n_workers) or (not e.working): raise Exception("ThreadedEvaluator did not start on start()!") w = e.workers[0] if not w.is_alive(): raise Exception("Workers did not start on start()") # ensure a second call to start() does nothing when already started e.start() if (len(e.workers) != n_workers) or (not e.working): raise Exception( "A second ThreadedEvaluator.start() call was not ignored!" ) w = e.workers[0] if not w.is_alive(): raise Exception("A worker died or stopped!") # ensure stop() works e.stop() if (len(e.workers) != 0) or (e.working): raise Exception( "ThreadedEvaluator.stop() did not work!" ) if w.is_alive(): raise Exception("A worker is still alive!") # ensure a second call to stop() does nothing when already stopped e.stop() if (len(e.workers) != 0) or (e.working): raise Exception( "A second ThreadedEvaluator.stop() call was not ignored!" ) if w.is_alive(): raise Exception("A worker is still alive or was resurrected!") # ensure a restart is possible # ensure start() starts the workers e.start() if (len(e.workers) != n_workers) or (not e.working): raise Exception("ThreadedEvaluator did not restart on start()!") w = e.workers[0] if not w.is_alive(): raise Exception("Workers did not start on start()") finally: # try to close if KeyboardInterrupt or similar if len(e.workers) or e.working: e.stop() # ensure del stops workers del e
def run(self): """ Run until the fitness threshold is reached. :return: """ p_th = neat.ThreadedEvaluator(self.instances, self.eval_genome) p_par = neat.ParallelEvaluator(self.instances, self.eval_genome) while not self.p.best_genome or self.p.best_genome.fitness < self.p.config.fitness_threshold: print( "\n\n=================== NEATGym Running Generation %d ===================\n\n" % self.generation) if self.mode == 'threaded': print("Mode: Threaded") self.p.run(p_th.evaluate, 1) self.record_fitness() elif self.mode == 'parallel': print("Mode: Parallel") self.p.run(p_par.evaluate, 1) self.record_fitness() else: print("Mode: Default") self.p.run(self.eval_genomes, 1) if self.generation % self.checkpoint_freq == 0: with open( self.get_log_name() + '_population_' + str(self.generation) + '.dat', 'wb') as file: pickle.dump(self.p, file) with open(self.get_log_name() + '_flog.dat', 'wb') as file: pickle.dump(self.f_record, file) self.generation += 1 with open( self.get_log_name() + '_best_' + str(self.generation) + '.dat', 'wb') as file: pickle.dump(self.p.best_genome, file) with open(self.get_log_name() + '_flog.dat', 'wb') as file: pickle.dump(self.f_record, file) # Display the winning genome. print('\nBest genome:\n{!s}'.format(self.p.best_genome)) return self.p.best_genome
def run_pop(self, checkpoint=""): if (checkpoint == ""): pop = neat.population.Population(self.config) else: pop = neat.Checkpointer.restore_checkpoint( "./pkl_pops/kraken/pop-checkpoint-" + checkpoint) checkpoints = neat.Checkpointer( generation_interval=2, time_interval_seconds=None, filename_prefix='./pkl_pops/kraken/pop-checkpoint-') stats = neat.statistics.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(checkpoints) pop.add_reporter(neat.reporting.StdOutReporter(True)) pe = neat.ThreadedEvaluator(4, self.eval_fitness) winner = pop.run(pe.evaluate, self.num_gens) return winner, stats
def test_threaded_evaluator(): """tests generall functionality of neat.threaded.ThreadedEvaluator""" n_workers = 3 e = neat.ThreadedEvaluator(n_workers, eval_dummy_genome_nn) # ensure workers are not started if (len(e.workers) > 0) or (e.working): raise Exception("ThreadedEvaluator started on __init__()!") # ensure start() starts the workers e.start() if (len(e.workers) != n_workers) or (not e.working): raise Exception("ThreadedEvaluator did not start on start()!") w = e.workers[0] if not w.is_alive(): raise Exception("Workers did not start on start()") # ensure a second call to start() does nothing when already started e.start() if (len(e.workers) != n_workers) or (not e.working): raise Exception( "A second ThreadedEvaluator.start() call was not ignored!") w = e.workers[0] if not w.is_alive(): raise Exception("A worker died or stopped!") # ensure stop() works e.stop() if (len(e.workers) != 0) or (e.working): raise Exception("ThreadedEvaluator.stop() did not work!") if w.is_alive(): raise Exception("A worker is still alive!") # ensure a second call to stop() does nothing when already stopped e.stop() if (len(e.workers) != 0) or (e.working): raise Exception( "A second ThreadedEvaluator.stop() call was not ignored!") if w.is_alive(): raise Exception("A worker is still alive or was resurrected!") # ensure a restart is possible # ensure start() starts the workers e.start() if (len(e.workers) != n_workers) or (not e.working): raise Exception("ThreadedEvaluator did not restart on start()!") w = e.workers[0] if not w.is_alive(): raise Exception("Workers did not start on start()") # ensure del stops workers del e
def run(config_file, last_factor_winner): # Load configuration. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) config.genome_config.add_activation('my_activation', my_activation) config.genome_config.add_activation('my_sigmoid', my_sigmoid) # Create the population, which is the top-level object for a NEAT run. if last_factor_winner != None: print(f"Load the last best factor network!") p = neat.Population(config, initial_state=(0, 0, 0)) population = create_pop(last_factor_winner, 10) species = config.species_set_type(config.species_set_config, p.reporters) generation = 0 species.speciate(config, population, generation) p.population = population p.species = species p.generation = generation else: p = neat.Population(config) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) # Run for up to 1000 generations. # pe = neat.ThreadedEvaluator(multiprocessing.cpu_count(), evalGenome) #Threading pe = neat.ThreadedEvaluator(20, evalGenome) # pe = neat.ParallelEvaluator(4, evalGenome) winner = p.run(pe.evaluate, GENERATION_NUM) pe.stop() # Threading # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Show output of the most fit genome against training data. print('\nOutput:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) return winner_net, winner
def test_threaded_evaluation(): """tests a neat evolution using neat.threaded.ThreadedEvaluator""" # Load configuration. local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'test_configuration') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) # Create the population, which is the top-level object for a NEAT run. p = neat.Population(config) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) # Run for up to 300 generations. pe = neat.ThreadedEvaluator(4, eval_dummy_genome_nn) p.run(pe.evaluate, 300) stats.save()
def run(config_file): """load the config, create a population, evolve and show the result""" # Load configuration. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create the population, which is the top-level object for a NEAT run. p = neat.Population(config) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) # Run for up to 300 generations. pe = neat.ThreadedEvaluator(4, eval_genome) winner = p.run(pe.evaluate, 300) pe.stop() # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Show output of the most fit genome against training data. print('\nOutput:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) for xi, xo in zip(xor_inputs, xor_outputs): output = winner_net.activate(xi) print( "input {!r}, expected output {!r}, got {!r}".format(xi, xo, output) ) if visualize is not None: node_names = {-1: 'A', -2: 'B', 0: 'A XOR B'} visualize.draw_net(config, winner, True, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_file): """load the config, create a population, evolve and show the result""" # Load configuration. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create the population, which is the top-level object for a NEAT run. p = neat.Population(config) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(50)) # Run for up to 300 generations. pe = neat.ThreadedEvaluator(8, eval_genome) winner = p.run(pe.evaluate, 1000) filehandler = open("./winner.pkl", 'wb', pickle.HIGHEST_PROTOCOL) pickle.dump(winner, filehandler) pe.stop() # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner))
def run_es(gens, _env, _max_steps, config, _params, _substrate, _max_trials=100, output=True, mode='parallel'): global env, substrate, params, max_trails, trials, max_steps env = _env substrate = _substrate params = _params max_trials = _max_trials max_steps = _max_steps trials = 1 # Create population and train the network. Return winner of network running 100 episodes. stats_one = neat.statistics.StatisticsReporter() pop = ini_pop(None, stats_one, config, output) stats_ten = neat.statistics.StatisticsReporter() pop = ini_pop((pop.population, pop.species, 0), stats_ten, config, output) trials = 10 # eval_genomes(pop.population, config) p_th = neat.ThreadedEvaluator(8, eval_fitness) p_par = neat.ParallelEvaluator(8, eval_fitness) gen = 0 while not pop.best_genome or pop.best_genome.fitness < pop.config.fitness_threshold: if mode == 'threaded': print("Mode: Threaded") winner_ten = pop.run(p_th.evaluate, 1) elif mode == 'parallel': print("Mode: Parallel") winner_ten = pop.run(p_par.evaluate, 1) else: print("Mode: Default") winner_ten = pop.run(eval_genomes, 1) if gen % 10 == 0: if not os.path.exists('./logs'): os.mkdir('./logs') with open('./logs/' + str(gen), 'wb') as f: pickle.dump(pop, f) gen += 1 if max_trials is 0: return winner_ten, (stats_one, stats_ten) stats_hundred = neat.statistics.StatisticsReporter() pop = ini_pop((pop.population, pop.species, 0), stats_hundred, config, output) trials = max_trials winner_hundred = pop.run(eval_fitness, gens) return winner_hundred, (stats_one, stats_ten, stats_hundred)
def run_neat(gens, env, max_steps, config, max_trials=100, output=True, mode='parallel'): trials = 1 def eval_fitness(g, config): net = neat.nn.FeedForwardNetwork.create(g, config) fitnesses = [] for i in range(trials): ob = env.reset() total_reward = 0 for j in range(max_steps): o = net.activate(ob) action = np.argmax(o) ob, reward, done, info = env.step(action) total_reward += reward if done: break fitnesses.append(total_reward) g.fitness = np.array(fitnesses).mean() # Create population and train the network. Return winner of network running 100 episodes. stats_one = neat.statistics.StatisticsReporter() pop = ini_pop(None, stats_one, config, output) pop.run(eval_fitness, gens) stats_ten = neat.statistics.StatisticsReporter() pop = ini_pop((pop.population, pop.species, 0), stats_ten, config, output) trials = 10 p_th = neat.ThreadedEvaluator(8, eval_fitness) p_par = neat.ParallelEvaluator(8, eval_fitness) gen = 0 while not pop.best_genome or pop.best_genome.fitness < pop.config.fitness_threshold: if mode == 'threaded': print("Mode: Threaded") winner_ten = pop.run(p_th.evaluate, 1) elif mode == 'parallel': print("Mode: Parallel") winner_ten = pop.run(p_par.evaluate, 1) else: print("Mode: Default") winner_ten = pop.run(eval_genomes, 1) if gen % 10 == 0: if not os.path.exists('./logs'): os.mkdir('./logs') with open('./logs/' + str(gen), 'wb') as f: pickle.dump(pop, f) gen += 1 if max_trials is 0: return winner_ten, (stats_one, stats_ten) stats_hundred = neat.statistics.StatisticsReporter() pop = ini_pop((pop.population, pop.species, 0), stats_hundred, config, output) trials = max_trials winner_hundred = pop.run(eval_fitness, gens) return winner_hundred, (stats_one, stats_ten, stats_hundred)
fitness_current = -1 return fitness_current def eval_genomes(genome, config): worky = Worker(genome, config) return worky.work() config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, 'config-feedforward') p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(10)) #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-572') pe = neat.ThreadedEvaluator(5, eval_genomes) pe.stop() winner = p.run(pe.evaluate) with open('winner.pkl', 'wb') as output: pickle.dump(winner, output, 1)