def run(config_file): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) winner = p.run(eval_genomes, 300) print('\nBest genome:\n{!s}'.format(winner)) 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)) 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) p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4') p.run(eval_genomes, 10)
def run(config_file): """ Main function that runs the neat platform - Loads the config file - Initializes the population according to the parameters of the config file - Add a Reporter to collect statistics of the runs - Add a Checkpointer to save (using picke): generation, config, population, species_set, rndstate """ # 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. custom_stats = custom_report.StdOutReporter() p.add_reporter(custom_stats) # save a checkpointer each generation p.add_reporter(neat.Checkpointer(1)) # Run using parallelizing with thenumber of processors for NUM_GENERATIONS pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome) winner = p.run(pe.evaluate, NUM_GENERATIONS) # save the results of the Statistics collected by the Reporter into a csv custom_stats.save_table('stats_table')
def run(config, base_dir): """Performs a single evolutionary run. Args: config (Config): The experiment configuration file. base_dir (str): The base directory to store the results for this run. """ global best global stats # Configure algorithm population = Population(config) # Add reporters stats = neat.StatisticsReporter() population.add_reporter(stats) population.add_reporter(neat.StdOutReporter(True)) population.add_reporter( neat.Checkpointer(generation_interval=config.checkpoint_interval, time_interval_seconds=None, filename_prefix=base_dir + 'checkpoints/' + 'neat-checkpoint-')) # Set generation limit max_generations = config.max_generations generation = 0 while generation < max_generations: batch_size = 1 best = population.run(fitness_function=evaluate_genomes, n=batch_size) visualize.plot_stats(stats, ylog=False, view=False, filename=base_dir + "fitness.svg") mfs = sum(stats.get_fitness_mean()[-5:]) / 5.0 print("Average mean fitness over last 5 generations: {0}".format(mfs)) mfs = sum(stats.get_fitness_stat(min)[-5:]) / 5.0 print("Average min fitness over last 5 generations: {0}".format(mfs)) # Check if a solution has been found evaluate_genomes([(0, best)], config, True) print(f'Current best genome lasted {best.fitness} time steps.') # if best.fitness >= config.fitness_threshold: # # Solved # break # Save current best save_object(best, base_dir + f'solution_{generation}.pickle') generation += batch_size # Save best genome and stats reporter if best: print('Saving best genome...') save_object(best, base_dir + 'solution.pickle') save_object(stats, base_dir + 'stats_reporter.pickle')
def run(config_file, data_test, file_name_stats): # 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(5)) ## v number of generations winner = p.run(eval_genomes, 50) # 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(data_inputs, data_outputs): output = winner_net.activate(xi) print("\ninput {!r}, expected output {!r}, got {!r}".format(xi, xo, output)) """ node_names = { -1: 'A', -2: 'B', -3: 'C', -4: 'D', 0:'0', 1: '1', 2: '2'} #visualize.draw_net(config, winner, True, filename = file_name_stats, node_names=node_names) #visualize.plot_stats(stats, ylog=False, view=True, filename = "avg_fitness" + file_name_stats + ".svg") #visualize.plot_species(stats, view=True, filename = "speciation" + file_name_stats + ".svg") # Running again 10 first generations #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4') # lleva a biblioteca random #p.run(eval_genomes, 10) #print(winner_net.node_evals) file_node = open("weights_" + file_name_stats, "w") file_node.write(str(winner_net.node_evals)) file_node.close() y_pred = [] for xi in X: out = winner_net.activate(xi) max_out = max(out) out_index = out.index(max_out) y_pred.append(out_index) return y_pred
def run(path, prev_model=False): # Check if we have a previous model to evaluate if prev_model: # Create a list of checkpoints checkpoints = [] # Loop through the files in the root directory for file in os.listdir(path): # If the file is a neat-checkpoint if file.find('neat-checkpoint') is not -1: # add it to our list of checkpoints checkpoints.append(file) if len(checkpoints) > 0: # Order the checkpoints from highest number (most recent) to lowest number checkpoints.sort(reverse=True) # Print a message for the checkpoint that we will be using print("Found a checkpoint file:", checkpoints[0]) # Load the checkpoint into a population (called 'pop') pop = neat.Checkpointer.restore_checkpoint(checkpoints[0]) else: # Otherwise .. print("No checkpoint file found. Loading a new population.") # The config file is loaded in with the specific headers we are using # All this can be found in the 'neat-config.txt' file config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, path + "/neat-config.txt") # create a new population with the config file pop = neat.Population(config) else: # Otherwise .. # The config file is loaded in with the specific headers we are using # All this can be found in the 'neat-config.txt' file config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, path + "/neat-config.txt") # create a new population with the config file pop = neat.Population(config) # Create a reporter to show statistics and add it to the population pop.add_reporter(neat.StdOutReporter(True)) # Create our statistics reporter stats = neat.StatisticsReporter() # Add our stats reporter to the population pop.add_reporter(stats) # Add a checkpointer, which will save checkpoints every 25 generations pop.add_reporter(neat.Checkpointer(generation_interval=25)) # Create a ParallelEvaluator to allow multiple instances of the network # to run at once. These numbers may vary from system to system. par_eval = neat.ParallelEvaluator(num_workers=15, eval_function=eval_genomes) # The winner (the highest fitness after the fitness goal has been achieved) # is saved into a variable winner = pop.run(par_eval.evaluate) # and then written to a model file for safe keeping pickle.dump(winner, open("model.pk", "wb"))
def run_experiment(config_file, n_generations=100): """ The function to run XOR experiment against hyper-parameters defined in the provided configuration file. The winner genome will be rendered as a graph as well as the important statistics of neuroevolution process execution. Arguments: config_file: the path to the file with experiment configuration """ # 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(5, filename_prefix='out/spb-neat-checkpoint-')) # Run for up to N generations. best_genome = p.run(eval_genomes, n=n_generations) # Display the best genome among generations. print('\nBest genome:\n{!s}'.format(best_genome)) # Check if the best genome is a winning Sinle-Pole balancing controller net = neat.nn.FeedForwardNetwork.create(best_genome, config) print("\n\nEvaluating the best genome in random runs") success_runs = evaluate_best_net(net, config, additional_num_runs) print("Runs successful/expected: %d/%d" % (success_runs, additional_num_runs)) if success_runs == additional_num_runs: print("SUCCESS: The stable Sinle-Pole balancing controller found!!!") else: print( "FAILURE: Failed to find the stable Sinle-Pole balancing controller!!!" ) # Visualize the experiment results node_names = {-1: 'x', -2: 'dot_x', -3: 'θ', -4: 'dot_θ', 0: 'action'} visualize.draw_net(config, best_genome, True, node_names=node_names, directory=out_dir, fmt='svg') visualize.plot_stats(stats, ylog=False, view=True, filename=os.path.join(out_dir, 'avg_fitness.svg')) visualize.plot_species(stats, view=True, filename=os.path.join(out_dir, 'speciation.svg'))
def run(config_file): # extract details from the config file config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # directory for storing checkpoints checkpoint_dir = os.listdir("checkpoints/") # if directory is empty if not checkpoint_dir: # start new population pop = neat.Population(config) # if directory is not empty else: # initialize empty list checkpoint_list = list() # iterate through each file for checkpoint in checkpoint_dir: # append to list the indices of the checkpoints checkpoint_list.append(checkpoint[16:]) # descending sort the checkpoint list and get the max value checkpoint = sorted(checkpoint_list, reverse=True)[0] # restore population from last checkpoint pop = neat.Checkpointer().restore_checkpoint( "checkpoints/neat-checkpoint-" + str(checkpoint)) # print which checkpoint is loaded print("Loaded last checkpoint: ", checkpoint) # uses print to output information about the run method pop.add_reporter(neat.StdOutReporter(True)) # gathers and provides the most-fit genomes and info on genome and species fitness and species sizes. pop.add_reporter(neat.StatisticsReporter()) # performs checkpointing, saving and restoring the simulation state. pop.add_reporter( neat.Checkpointer(generation_interval=1, time_interval_seconds=1200, filename_prefix='checkpoints/neat-checkpoint-')) # find the winner genome by running the main_game method for 20 generations winner = pop.run(main_game, 20) # display the characteristics of the winner genome print('\n\nBest genome: {!s}'.format(winner)) # create a file for winner model with open("winner.pickle", 'wb') as model_file: # save the model pickle.dump(winner, model_file)
def run(config_file): #run requires a population object that must contain a config object config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) #allows for reporting on the terminal stats = neat.StatisticsReporter(); p.add_reporter(stats) #adds a stats reporter p.add_reporter(neat.Checkpointer(5)) #adds a checkpointer reporter winner = p.run(eval_genomes,50) #runs upto as many generations or uptil the fitness threshold in the config file return winner
def run(): # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) # config_path = os.path.join(local_dir, 'config-feedforward') config_path = os.path.join(local_dir, 'config') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) pop = neat.Population(config) # Add a stdout reporter to show progress in the terminal. pop.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.Checkpointer(5)) winner = pop.run(eval_genomes, 200) # Save the winner. with open('winner-feedforward', 'wb') as f: pickle.dump(winner, f) print(winner) pygame.quit() visualize.plot_stats(stats, ylog=True, view=True, filename="feedforward-fitness.svg") visualize.plot_species(stats, view=True, filename="feedforward-speciation.svg") node_names = {-1: 'x', -2: 'dx', -3: 'theta', -4: 'dtheta', 0: 'control'} visualize.draw_net(config, winner, True, node_names=node_names) visualize.draw_net(config, winner, view=True, node_names=node_names, filename="winner-feedforward.gv") visualize.draw_net(config, winner, view=True, node_names=node_names, filename="winner-feedforward-enabled.gv", show_disabled=False) visualize.draw_net(config, winner, view=True, node_names=node_names, filename="winner-feedforward-enabled-pruned.gv", show_disabled=False, prune_unused=True)
def evolutionary_driver(config, show): p = neat.Population(config) p.add_reporter(neat.StdOutReporter(False)) p.add_reporter(neat.Checkpointer(10)) if show: network = p.run(eval_genomes_visible) else: network = p.run(eval_genomes_non_visible) return network
def _setup_neat(self): p_ = neat.Population(self.config_) self.stats_ = neat.StatisticsReporter() p_.add_reporter(self.stats_) if self.create_checkpoints: p_.add_reporter(neat.Checkpointer(self.checkpoint_frequency)) if self.statistic_reporter: p_.add_reporter(neat.StdOutReporter(self.statistic_reporter)) return p_
def run(neatConfig): neatPopulation = neat.population.Population(neatConfig) neatPopulation.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() neatPopulation.add_reporter(stats) neatPopulation.add_reporter(neat.Checkpointer(5)) winner = neatPopulation.run(eval_genomes, 20)
def main(): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, 'config-feedforward.txt') pop = neat.Population(config) pop.add_reporter(neat.Checkpointer(10)) winner = pop.run(eval_genomes) with open('winner_pinball_1.pkl', 'wb') as output: pickle.dump(winner, output, 1)
def __init__(self, config): pop = neat.Population(config) self.stats = neat.StatisticsReporter() pop.add_reporter(self.stats) pop.add_reporter(neat.StdOutReporter(True)) # Checkpoint every 10 generations or 900 seconds. pop.add_reporter(neat.Checkpointer(10, 900)) self.config = config self.population = pop self.pool = multiprocessing.Pool()
def run(config_file): # 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. # True == show all species, False == don't show species p.add_reporter(neat.StdOutReporter(False)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) # Stop running after n=n_generations # if n=None runs until solution is found winner = p.run(eval_genomes, n=n_generations) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Show output of the most fit genome against testing data. print('\nTest Output, Actual, Diff:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) predicted = [] for xi, xo in zip(test_x, test_y): output = winner_net.activate(xi) predicted.append(output) node_names = { -1: '4', -2: '3', -3: '2', -4: '1', -5: '0', 0: 'Predict Change' } visualize.draw_net(config, winner, True, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) # ? save? #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint') #p.run(eval_genomes, n_evaluate) # plot predictions vs actual plt.plot(test_y, 'g', label='Actual') plt.plot(predicted, 'r-', label='Predicted') plt.title('Test Data') plt.legend() plt.show()
def run(config_file): # 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(300)) # Run for up to 300 generations. start_time = time.time() winner = p.run(eval_genomes, 5000) stats.save() print(stats.best_genomes(5)) # 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 , expected output {!r}, got {!r}".format(xo, output)) # test on testset fitness_test = 0 for xi_test, xo_test in zip(test_inputs, test_outputs): output_test = winner_net.activate(xi_test) if abs(output_test[0] - xo_test[0]) < 0.1: fitness_test += 1 else: fitness_test += 0 test_evaluation = fitness_test / float(len(test_outputs)) print("\nTest evaluation: {!r}".format(test_evaluation)) print("--- %s seconds ---" % (time.time() - start_time)) node_names = {0: 'Output'} visualize.draw_net(config, winner, True, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4') p.run(eval_genomes, 10)
def run(): # get config local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'config-feedforward-icegame') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) # set population and set reporting options pop = neat.Population(config) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.StdOutReporter(True)) # Checkpoint every x gen or y minutes pop.add_reporter( neat.Checkpointer(500, 900, "checkpoints/" + fn_results + "-checkpoint")) #winner = pop.run(eval_genomes, NUM_GEN) # non-parallel pe = neat.ParallelEvaluator(NUM_WORKERS, eval_genome) # parallel winner = pop.run(pe.evaluate, NUM_GEN) # save network with open("results/winner-pickle-" + fn_results, 'wb') as f: pickle.dump(winner, f) #print(winner) visualize.plot_stats(stats, ylog=True, view=True, filename="results/" + fn_results + "-fitness.svg") visualize.plot_species(stats, view=True, filename="results/" + fn_results + "-speciation.svg") if DRAW_NETS: visualize.draw_net(config, winner, view=True, filename="results/winner-" + fn_results + ".net") visualize.draw_net(config, winner, view=True, filename="results/winner-" + fn_results + "-enabled.net", show_disabled=False) visualize.draw_net(config, winner, view=True, filename="results/winner-" + fn_results + "-pruned.net", show_disabled=False, prune_unused=True)
def run(config_file): global env # 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) #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-9') # 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(10)) reward_list = [] #for j in range(20): # Run for up to 300 generations. winner = p.run(eval_genomes, 300) print(reward_list) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Show output of the most fit genome against training data. print('\nOutput:') visualize.draw_net(config, winner, True) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) visualize.draw_net(config, winner, view=True, filename="winner-feedforward-evabled-pruneg.gv", show_disabled=False, prune_unused=True) winner_net = neat.nn.FeedForwardNetwork.create(winner, config) final_reward = 0 #env = wrappers.Monitor(env, '/mnt/c/Users/bc/Documents/EA/neat/BipedalWalker/movies', force=True) observation = env.reset() while True: action = winner_net.activate(observation) action = np.clip(action, -1, 1) observation, reward, done, info = env.step(action) final_reward += reward if done: print("final_reward :", final_reward) break #winner_net = neat.nn.FeedForwardNetwork.create(winner, config) for n, g in enumerate([winner]): visualize.draw_net(config, g, view=False, filename=str(j) + "-net-enabled-pruned.gv", show_disabled=False, prune_unused=True)
def main(): p = neat.Checkpointer().restore_checkpoint('top_genomes') env.reset() 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.run(eval_genomes, 2)
def runn(config_file, pop): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter()) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) winner = p.run(main, pop) print('\nBest genome:\n{!s}'.format(winner))
def run(config_file): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-7') p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(2)) winner = p.run(eval_genomes, 200)
def load_checkpoint(ckp_name, max_iter): global stats, p p = neat.Checkpointer.restore_checkpoint(ckp_name) p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter( neat.Checkpointer(generation_interval=10, time_interval_seconds=None)) p.run(eval_genomes, max_iter)
def play_mario(self, config_file, w): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,neat.DefaultSpeciesSet, neat.DefaultStagnation,config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) p.add_reporter(neat.Checkpointer(5)) stats_reporter = neat.StatisticsReporter() p.add_reporter(stats_reporter) winner = p.run(self.eval_genomes, w) real_winner = p.best_genome pickle.dump(winner, open('winner.pkl', 'wb')) pickle.dump(real_winner, open('real_winner.pkl', 'wb'))
def run_pop(task, gens): pop = neat.population.Population(task.config) checkpoints = neat.Checkpointer(generation_interval=1, time_interval_seconds=None, filename_prefix='./binance_champs_2/tradegod-checkpoint-') stats = neat.statistics.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(checkpoints) pop.add_reporter(neat.reporting.StdOutReporter(True)) winner = pop.run(task.eval_fitness, gens) print("es trade god summoned") return winner, stats
def run(config_file): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) winner = p.run(fitness_function, 30) winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
def run(config_file): # 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(5)) # Run for up to 300 generations. winner = p.run(eval_genomes, 300) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) # Show output of the most fit genome against training data. print('\nOutput:') #Show the winning Snake winner_net = neat.nn.FeedForwardNetwork.create(winner, config) champ = Snake(winner_net) champ.play() ''' 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)) ''' node_names = { -1: 'A', -2: 'B', -3: 'C', -4: 'D', -5: 'E', -6: 'F', -7: 'G', -8: 'H', -9: 'I', -10: 'J', -11: 'K', -12: 'L', 0: 'DOWN', 1: 'UP', 2: 'LEFT', 3: 'RIGHT' } 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(self, config_file, n): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) pop = neat.Population(config) pop.add_reporter(neat.StdOutReporter(True)) pop.add_reporter(neat.Checkpointer(5)) stats = neat.StatisticsReporter() pop.add_reporter(stats) winner = pop.run(self._eval_genomes, n) pickle.dump(winner, open('best_genome', 'wb'))
def run(config_file): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5)) p.run(eval_genomes, 10) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def simulation_runner_parallel(neat_config_file, game, state, scenario, network_type, color_mode, num_generations, controller_actions=False, checkpoint_file=None, display_reports=True, checkpoint_interval=1, num_threads=None): # Determine path to configuration file. This path manipulation is # here so that the script will run successfully regardless of the # current working directory. # local_dir = os.path.dirname(__file__) # config_path = os.path.join(local_dir, 'config-neat') # NEAT configuration, all defaults except a config file is provided config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, neat_config_file) # NEAT output population = PopulationExtended(config) # using custom PopulationExtended if checkpoint_file: population = neat.Checkpointer.restore_checkpoint(checkpoint_file) if display_reports: population.add_reporter(neat.StdOutReporter(True)) population.add_reporter(neat.StatisticsReporter()) if checkpoint_interval > 0: population.add_reporter(neat.Checkpointer(checkpoint_interval)) if num_threads: threads = num_threads else: threads = multiprocessing.cpu_count() # create a parallel evaluator that will spawn workers pe = ParallelEvaluatorExtended(threads, evaluate_genome) # the winning network up to x generations winner = population.runParallel(pe.evaluate, num_generations, game, state, scenario, network_type, color_mode, controller_actions) # save the winning network to a binary file to reload later with open(str(population.best_genome.fitness) + '.pkl', 'wb') as output: pickle.dump(winner, output, 1) return population.best_genome.fitness
def neat_illusion(output_dir, model_name, config_path, structure, w, h, channels, c_dim=3, checkpoint=None, gradient=1): repeat = 6 limit = 1 half_h = int(h / 2) size = [w, h] gpu = 0 best_dir = output_dir if not os.path.exists(best_dir): os.makedirs(best_dir) # Load configuration. config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) def eval_genomes(genomes, config): get_fitnesses_neat(structure, genomes, model_name, config, w, h, channels, c_dim=c_dim, best_dir=best_dir, gradient=gradient) checkpointer = neat.Checkpointer(100) # Create the population, which is the top-level object for a NEAT run. if not checkpoint: p = neat.Population(config) else: p = checkpointer.restore_checkpoint(checkpoint) # 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(checkpointer) # Run for up to x generations. winner = p.run(eval_genomes, 300)