def run(config_path): config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(True)) # p.add_reporter(neat.StatisticsReporter()) stats = neat.StatisticsReporter() p.add_reporter(stats) winner = p.run(eval_genomes, 100) # Save the winner. with open('winner-feedforward', 'wb') as f: pickle.dump(winner, f) visualize.plot_stats(stats, ylog=True, view=True, filename="feedforward-fitness.svg") visualize.plot_species(stats, view=True, filename="feedforward-speciation.svg")
def evaluation(): CHECKPOINT = 1759 p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-%i' % CHECKPOINT) winner = p.run(eval_genomes, 1) # find the winner in restored population # show winner net # node_names = {-1: 'In0', -2: 'In1', -3: 'In3', -4: 'In4', 0: 'act1', 1: 'act2'} # visualize.draw_net(p.config, winner, True, node_names=node_names) node_names = { -1: 'A', -2: 'B', -3: 'C', -4: 'D', 0: 'a', 1: 'b', 2: 'c', 3: 'd' } visualize.draw_net(p.config, winner, True, node_names=node_names) stats = neat.StatisticsReporter() visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) net = neat.nn.FeedForwardNetwork.create(winner, p.config) # net = neat.nn.RecurrentNetwork.create(winner, p.config) while True: s = env.reset() done = False while not done: env.render() # a = np.argmax(net.activate(s)) a = net.activate(s) s, r, done, _ = env.step(a)
def run_experiment(config_file): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) p = neat.Population(config) p.add_reporter(neat.StdOutReporter(show_species_detail=True)) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter( neat.Checkpointer(generation_interval=5, filename_prefix=os.path.join( local_dir, 'out/neat-checkpoint-'))) best_genome = p.run(fitness_function=eval_genomes, n=300) best_net = neat.nn.FeedForwardNetwork.create(best_genome, config) best_genome_fitness = eval_fitness(best_net) if best_genome_fitness > config.fitness_threshold: print('\n\nSuccess: The XOR problem solver found!!!') else: print('\n\nFailure: Failed to find XOR problem solver!!!') node_names = {-1: 'A', -2: 'B', 0: 'A XOR B'} visualize.draw_net(config, best_genome, True, node_names=node_names, directory=out_dir) 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, candle_inputs, candle_outputs): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) global inputs, outputs inputs = candle_inputs outputs = candle_outputs p = neat.Population(config) 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)) print('\nOutput:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) for xi, xo in zip(candle_inputs, candle_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.run(eval_genomes, 10)
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) print("Train Starting in 3 sec !") time.sleep(3) # Run for up to 50 generations. winner = p.run(eval_genomes, 50) pickle.dump(winner, open('best-genomes/winner.pkl', 'wb')) # Visualize winner node_names = {-1: 'Distance', -2: 'Gap', -3: 'Speed', 0: 'Duck', 1: 'Jump'} 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(): local_dir = os.path.dirname(__file__) pop = population.Population(os.path.join(local_dir, 'nn_config')) pe = parallel.ParallelEvaluator(4, eval_fitness) pop.run(pe.evaluate, 1000) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Display the most fit genome. print('\nBest genome:') winner = pop.statistics.best_genome() print(winner) # Verify network output against a few randomly-generated sequences. winner_net = nn.create_recurrent_phenotype(winner) for n in range(4): print('\nRun {0} output:'.format(n)) seq = [random.choice((0, 1)) for _ in range(N)] winner_net.reset() for s in seq: winner_net.activate([s, 0]) for s in seq: output = winner_net.activate([0, 1]) print("expected {0:1.5f} got {1:1.5f}".format(s, output[0])) # Visualize the winner network and plot/log statistics. visualize.draw_net(winner, view=True, filename="nn_winner.gv") visualize.draw_net(winner, view=True, filename="nn_winner-enabled.gv", show_disabled=False) visualize.draw_net(winner, view=True, filename="nn_winner-enabled-pruned.gv", show_disabled=False, prune_unused=True) visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics)
def run(): # Load the file, witch is assumed to live in the same directory as this script local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'conf_ff.txt') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) pop = neat.Population(config) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.StdOutReporter(True)) winner = pop.run(eval_genomes, 300) node_names = { -1: 'cPos', -2: 'cVel', -3: 'pAng', -4: 'pAngVel', 0: 'esq ou dir' } visualize.draw_net(config, winner, False, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) with open('winner_ff.p', 'wb') as f: pickle.dump(winner, f)
def main(gen, save_ckpt, load_ckpt, save_path): config_file = 'config-feedforward.txt' config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create population if load_ckpt is None: p = neat.Population(config) else: p = neat.Checkpointer.restore_checkpoint(load_ckpt) save_ckpt = load_ckpt.split('_G')[0] # 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(generation_interval=10, filename_prefix=f"{save_ckpt}_G")) # Evolve winner = p.run(eval_genomes, gen) print('\nBest genome:\n{}'.format(winner)) print('\nTime Elapsed:', time() - START) visualize.draw_net(config, winner, False) visualize.plot_stats(stats, ylog=False, view=False) visualize.plot_species(stats, view=False) # Save Genome pickle.dump(winner, open(save_path, "wb"))
def run(): t0 = time.time() # Get the path to 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, 'xor2_config') # Use a pool of four workers to evaluate fitness in parallel. pe = parallel.ParallelEvaluator(4, fitness) pop = population.Population(config_path) pop.run(pe.evaluate, 400) print("total evolution time {0:.3f} sec".format((time.time() - t0))) print("time per generation {0:.3f} sec".format( ((time.time() - t0) / pop.generation))) print('Number of evaluations: {0:d}'.format(pop.total_evaluations)) # Verify network output against training data. print('\nBest network output:') winner = pop.statistics.best_genome() net = nn.create_feed_forward_phenotype(winner) for i, inputs in enumerate(xor_inputs): output = net.serial_activate(inputs) # serial activation print("{0:1.5f} \t {1:1.5f}".format(xor_outputs[i], output[0])) # Visualize the winner network and plot statistics. visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) visualize.draw_net(winner, view=True)
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) pe = neat.ParallelEvaluator(4, eval_genome) winner = p.run(pe.evaluate, 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)
def run(config_file, restore_file=None): # 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 = None if restore_file is None: p = neat.Population(config) else: p = neat.Checkpointer.restore_checkpoint(restore_file) p.add_reporter(neat.Checkpointer(generation_interval=10, time_interval_seconds=None)) p.add_reporter(trackers.AssistanceRequestTracker(p.generation)) p.add_reporter(trackers.ReportBestTracker(p.generation)) # Run for up to 300 generations. winner = None if debug: winner = p.run(eval_genomes, 300) else: pe = neat.ParallelEvaluator(4, eval_genome) winner = p.run(pe.evaluate) print('\nBest genome:\n{!s}'.format(winner)) print("Saving as winner-genome-{}.pkl".format(winner.fitness)) with open('winner-genome-{}.pkl'.format(winner.fitness), 'wb') as output: pickle.dump(winner, output, 1) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_file): config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create the population 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 x generations. The Generations parameter tells how many # NEAT calls the eval_genomes fitness function in order to evaluate the population winner = p.run(eval_genomes, generations) if (showGraph == "n"): #Visualization of the winner NN node_names = {-1: 'Pos', -2: 'Vel', -3: 'Angle', -4: 'VelTip'} visualize.draw_net(config, winner, True, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) # show final stats print('\nBest genome:\n{!s}'.format(winner)) #test winner winner_net = neat.nn.FeedForwardNetwork.create(winner, config) score = test_model(winner_net) #Tests model 100 times and prints result return score
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, 'configs/neat_config.cfg') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) pop = neat.Population(config) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.StdOutReporter(True)) pe = neat.ParallelEvaluator(6, eval_genome) winner = pop.run(pe.evaluate) # Save the winner. with open('models/winner-feedforward.model', 'wb') as f: pickle.dump(winner, f) visualize.plot_stats(stats, ylog=True, view=True, filename="models/feedforward-fitness.svg") visualize.plot_species(stats, view=True, filename="models/feedforward-speciation.svg") visualize.draw_net(config, winner, True)
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 50 generations. winner = p.run(trainingFunction, 50) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) visualize.draw_net(config, winner, True) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) winner_net = neat.nn.FeedForwardNetwork.create(winner, config) with open ('backupFile','wb') as backupFile : pickle.dump( winner_net, backupFile )
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(300)) # 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. winner_net = neat.nn.FeedForwardNetwork.create(winner, config) # Use the winner_net to run through a course in slow-motion run_course(winner_net, final=True) visualize.draw_net(config, winner, True) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_path): """ Sets up NEAT and displays statistics Creates visualizations after last generation """ global p config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) p = neat.Population(config) stats = neat.StatisticsReporter() p.add_reporter(neat.StdOutReporter(True)) p.add_reporter(stats) winner = p.run(main, 50) print('\nBest genome:\n{!s}'.format(winner)) node_names = { -1: 'Player y-value', -2: 'Player distance from enemy', 0: 'Jump' } 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 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. 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:') 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-49') p.run(eval_genomes, 10)
def run(args): # Create the population, which is the top-level object for a NEAT run. p = neat.Population(load_config()) if args.load is not None: p = neat.checkpoint.Checkpointer.restore_checkpoint(args.load) # Add a stdout reporter to show progress in the terminal. p.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() p.add_reporter(stats) graph_reporter = GraphReporter(stats) p.add_reporter(graph_reporter) saver = neat.checkpoint.Checkpointer(generation_interval=100, time_interval_seconds=None, filename_prefix=args.save) p.add_reporter(saver) # Run for at least 1 generations. generations_to_run = max(SETTINGS['GENERATIONS'] - p.generation, 1) winner = p.run(eval_genomes, generations_to_run) if args.save is not None: saver.save_checkpoint(p.config, p.population, p.species, p.generation) graph_reporter.close() print('\nBest genome:\n{!s}'.format(winner)) render_game_with_NN(winner) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
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:') 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): 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, filename_prefix= 'checkpoints\\reproduce_image\\feedforward-checkpoint-')) 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:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) visualize.draw_net(config, winner, True) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_file): """ runs the NEAT algorithm to train a neural network to play flappy bird. :param config_file: location of config file :return: None """ config = neat.config.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 50 generations. print("pre run (with coupled eval_genomes") winner = p.run(eval_genomes, 150) # show final stats print('\nBest genome:\n{!s}'.format(winner)) #4 node_names = {-1:'Bird Y', -2: 'Top Pipe Y', -3:"Bottom Pipe Y", 0:'Jump'} visualize.draw_net(config, winner, True, 'test', node_names=node_names) print ('left draw_net') visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_file): # Load configuration. config = neat.Config(maps.MapGenome, 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(100)) # 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:') winner_net = maps.MapNetwork.create(winner, config, MAP_SIZE) 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'} #The following visualization is not accurate visualize.draw_net(config, winner, True, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) pickle.dump(winner_net, open("mapsw.bin", "wb"))
def run(config_file, it, cores): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) # Create the population 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 x generations pe = neat.ParallelEvaluator(cores, eval_genome) winner = p.run(pe.evaluate, it) # Display and save the winning genome print('\nBest genome:\n{!s}'.format(winner)) with open('winner/winner-parallel.pkl', 'wb') as output: pickle.dump(winner, output, 1) visualize.draw_net(config, winner, view=True, filename="draw_net") visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_file): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_file) population = neat.Population(config) population.add_reporter(neat.StdOutReporter(True)) stats = neat.StatisticsReporter() population.add_reporter(stats) population.add_reporter(neat.Checkpointer(5)) parallelEval = neat.ParallelEvaluator(cpu_count, eval_genomes) print('Evaluating on {} CPUs'.format(cpu_count)) winner = population.run(parallelEval.evaluate, 1000) print('\n Best Genome : \n {}'.format(winner)) print('\n\n Output : ') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) visualize.draw_net(config, winner, True) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) environment(winner_net)
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') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) pop = neat.Population(config) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.StdOutReporter(True)) # runs evaluation functions in parallel subprocesses in order to evaluate multiple genomes at once. pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome) winner = pop.run(pe.evaluate) # Save the winner. with open('winner-NEAT-pickle', 'wb') as f: pickle.dump(winner, f) print(winner) visualize.plot_stats(stats, ylog=True, view=True, filename="winner-NEAT-fitness") visualize.plot_species(stats, view=True, filename="winner-NEAT-speciation")
def run(): pop = Checkpointer.restore_checkpoint('neat-checkpoint-3') winner = Checkpointer. stats = neat.StatisticsReporter() pe = neat.ParallelEvaluator(1, eval_genome) winner = pop.run(pe.evaluate) print(winner) #print(pop.statistics.best_genome()) net = neat.nn.FeedForwardNetwork.create(genome, config) driver = main(TestNeatDriver(logdata=False, net=net)) 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 train(self, config_file): local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, config_file) # Load Config 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(50)) # Run for up to 300 generations pe = neat.ParallelEvaluator(24, self.play) winner = p.run(pe.evaluate, 1000) #save winnning genome with open("winner.pkl", "wb") as output: pickle.dump(winner, output, 1) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(self, 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 MAX_GENERATIONS generations. p_evaluator = ParallelEvaluatorSims(Const.N_SIMS, run_sim) winner = p.run(p_evaluator.evaluate, Const.MAX_GENERATIONS) # 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)) # node_names = {-1:'A', -2: 'B', 0:'A XOR B'} visualize.draw_net(config, winner, True) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)
def run(config_file): global p, stats, winner # 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-27') # 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)) # Run for up to 300 generations. winner = p.run(evalGenomes, 20) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) node_names = {0: 'RIGHT', 1: 'UP', 2: 'LEFT', 3: 'DOWN', 4: 'NONE'} 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_scenario(): # Create configuration object config = Config(genome.DefaultGenome, reproduction.DefaultReproduction, species.DefaultSpeciesSet, stagnation.DefaultStagnation, "../config files/config_walking_V2") # Set maximum number of episodes episodes = 50 # Create a population of genomes from the set configuration pop = Population(config) # Add reporters to keep track of performance of the genomes pop.add_reporter(StdOutReporter(True)) stats = StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(Checkpointer(5, 5, "../checkpoints/walking2/walking-V2.2-checkpoint-")) # Run NEAT and retrieve the highest performing genome winner = pop.run(fitness_function, episodes) # Save the highest performing genome save_genome(winner, "moving-genome-PT2.2") # Display the fitness of the highest performing genome print("highest fitness value: {!r}".format(winner.fitness)) # Display the genome's network structure and fitness and species graphs node_names = {-9: 'Right 1', -1: 'Left 1', -8: 'Right 2', -2: 'Left 2', -7: 'Right 3', -3: 'Left 3', -6: 'Right 4', -4: 'Left 4', -5: 'Centre', 0: 'Turn right', 1: 'Turn left', 2:'Move Forward'} visualize.draw_net(config, winner, True, node_names=node_names) visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True) # Close game once complete game.close()
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)) winner = p.run(eval_genomes, 5) # Run for up to 300 generations. # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) visualize.plot_stats(stats, view=True, filename="trialstats") visualize.plot_species(stats, view=True, filename="trialspecies") winner_net = neat.nn.FeedForwardNetwork.create(winner, config) while (True): winner_flag = not (input() == "False") if (winner_flag == True): winner_gif(winner_net) else: break
def run(): config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, CONFIG) pop = neat.Population(config) # recode history stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.StdOutReporter(True)) pop.add_reporter(neat.Checkpointer(5)) pop.run(eval_genomes, 10) # train 10 generations # visualize training visualize.plot_stats(stats, ylog=False, view=True) visualize.plot_species(stats, view=True)