def run(config_file): 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(OwnReporter()) stats = neat.StatisticsReporter() p.add_reporter(stats) p.add_reporter(neat.Checkpointer(5, filename_prefix="./checkpoints/ckpt-")) #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4') # Run for up to 300 generations. winner = p.run(fitness_function, generations) # Display the winning genome. print('\nBest genome:\n{!s}'.format(winner)) winner_net = neat.nn.FeedForwardNetwork.create(winner, config) node_names = {-1:'x_t', -2: 'y_t', -3: 'x_act', -4:'y_act', 0:'x_t+1', 1: 'y_t+1'} visualize.draw_net(config, winner, True, node_names=node_names, filename="./reports/Digraph.gv") test(winner_net) visualize.plot_stats(stats, ylog=False, view=True, filename="./reports/avg_fitness.svg") visualize.plot_species(stats, view=True, filename="./reports/species.svg")
def main(): global novSearch # myworld = World("Simulator", 1080, 280, 40) # myworld.readWorldConfigFile("testConfig.txt") myworld = World("Simulator", 2000, 400, 40) myworld.readWorldConfigFile("finalWorld.txt") myworld.getValidStand() myworld.getAirspace() # mario = Mario(myworld, "Mario", 0, 5) mario = Mario(myworld, "Mario", 0, 8) # for final world # set up NEAT config.load("marNovelty_config") chromosome.node_gene_type = genome.NodeGene # use the noveltyFitness function to determine fitness scores population.Population.evaluate = noveltyFitness pop = population.Population() # set how many generations you want evolution to run generations = 10 pop.epoch(generations, report=True) # After evolution completes... # Plot the best/average fitness across the evolution visualize.plot_stats(pop.stats) # Create a visualization of speciation that occurred visualize.plot_species(pop.species_log) # save the archive and growth data from novelty search novSearch.saveArchive("mario") novSearch.saveGrowth("mario") # Save the best coverage chormosomes found print "\nFound behaviors with the following coverage scores:" for i in range(len(bestChromos)): print bestChromos[i][1] f = open("bestChromo%d" % (i), "w") pickle.dump(bestChromos[i][0], f) f.close() # Save the most recently added chromosomes in the archive print "\nNovelty scores for 10 most recently added behaviors:" i = 0 for saved in novSearch.archive[-10:]: print saved[1] f = open("novelty%d" % (i), "w") pickle.dump(saved[2], f) f.close() i += 1
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(): 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(): t0 = time.time() # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'xor2_config')) num_workers = 6 pool = Pool(num_workers) print "Starting with %d workers" % num_workers def fitness(chromosomes): return eval_fitness(chromosomes, pool) pop = population.Population(config) pop.epoch(fitness, 400) print "total evolution time %.3f sec" % (time.time() - t0) print "time per generation %.3f sec" % ((time.time() - t0) / pop.generation) winner = pop.most_fit_genomes[-1] print 'Number of evaluations: %d' % winner.ID # Verify network output against training data. print '\nBest network output:' net = nn.create_fast_feedforward_phenotype(winner) for i, inputs in enumerate(INPUTS): output = net.sactivate(inputs) # serial activation print "%1.5f \t %1.5f" % (OUTPUTS[i], output[0]) # Visualize the winner network and plot statistics. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores) visualize.plot_species(pop.species_log) visualize.draw_net(winner, view=True)
def run(): # load settings file local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'dpole_config')) # change the number of inputs accordingly to the type # of experiment: markov (6) or non-markov (3) # you can also set the configs in dpole_config as long # as you have two config files for each type of experiment config.input_nodes = 3 pop = population.Population(config) pop.epoch(evaluate_population, 200, report=1, save_best=0) winner = pop.most_fit_genomes[-1] print('Number of evaluations: {0:d}'.format(winner.ID)) print('Winner fitness: {0:f}'.format(winner.fitness)) # save the winner with open('winner_chromosome', 'w') as f: cPickle.dump(winner, f) # Plots the evolution of the best/average fitness visualize.plot_stats(pop, ylog=True) # Visualizes speciation visualize.plot_species(pop) # visualize the best topology visualize.draw_net(winner, view=True)
def main(): local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'grid_pacai_config') pe = parallel.ParallelEvaluator(2, runPacman) pop = population.Population(config_path) pop.run(pe.evaluate, 400) winner = pop.statistics.best_genome() print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Visualize the winner network and plot/log statistics. visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) visualize.draw_net(winner, view=True, filename="xor2-all.gv") visualize.draw_net(winner, view=True, filename="xor2-enabled.gv", show_disabled=False) visualize.draw_net(winner, view=True, filename="xor2-enabled-pruned.gv", show_disabled=False, prune_unused=True)
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 = Config(os.path.join(local_dir, 'xor2_config')) # For spiking networks config.output_nodes = 2 pop = population.Population(config) pop.epoch(eval_fitness, 500) winner = pop.most_fit_genomes[-1] print 'Number of evaluations: %d' % winner.ID # Verify network output against training data. print '\nBest network output:' net = iznn.create_phenotype(winner) for inputData, outputData in zip(INPUTS, OUTPUTS): for j in range(MAX_TIME): output = net.advance([x * 10 for x in inputData]) if output != [False, False]: break if output[0] and not output[1]: # Network answered 1 print "%r expected %d got 1" % (inputData, outputData) elif not output[0] and output[1]: # Network answered 0 print "%r expected %d got 0" % (inputData, outputData) else: print "%r expected %d got ?" % (inputData, outputData) # Visualize the winner network and plot statistics. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores) visualize.plot_species(pop.species_log) visualize.draw_net(winner, view=True)
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(): # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'xor2_config')) # For this network, we use two output neurons and use the difference between # the "time to first spike" to determine the network response. There are # probably a great many different choices one could make for an output encoding, # and this choice may not be the best for tackling a real problem. config.output_nodes = 2 pop = population.Population(config) pop.run(eval_fitness, 200) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Visualize the winner network and plot statistics. winner = pop.statistics.best_genome() node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'} visualize.draw_net(winner, view=True, node_names=node_names) visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) # Verify network output against training data. print('\nBest network output:') net = iznn.create_phenotype(winner, *iz_params) sum_square_error, simulated = simulate(winner) # Create a plot of the traces out to the max time for each set of inputs. plt.figure(figsize=(12, 12)) for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated): response = compute_output(t0, t1) print("{0!r} expected {1:.3f} got {2:.3f}".format(inputData, outputData, response)) axes = plt.subplot(4, 1, r + 1) plt.title("Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData), fontsize=12) for i, s in neuron_data.items(): if i in net.outputs: t, v = zip(*s) plt.plot(t, v, "-", label="neuron {0:d}".format(i)) # Circle the first peak of each output. circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False) circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False) axes.add_artist(circle0) axes.add_artist(circle1) plt.ylabel("Potential (mv)", fontsize=10) plt.ylim(-100, 50) plt.tick_params(labelsize=8) plt.grid() plt.xlabel("Time (in ms)", fontsize=10) plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) plt.savefig("traces.png", dpi=90) plt.show()
def main(): population.Population.evaluate = eval_fitness pop = population.Population() generations = 23 pop.epoch(generations, report=True, save_best=True, \ checkpoint_interval=None, checkpoint_generation=11) stop = time.time() elapsed = stop - start print "Total time to evolve:", elapsed print "Time per generation:", elapsed / float(generations) visualize.plot_stats(pop.stats) visualize.plot_species(pop.species_log)
def main(): # set up NEAT config.load("mar2_config") chromosome.node_gene_type = genome.NodeGene population.Population.evaluate = coverageFitness #function name pop = population.Population() # set how many generations you want evolution to run generations = 30 pop.epoch(generations, report=True, save_best=True) # Plots the best/average fitness across the evolution visualize.plot_stats(pop.stats) # Creates a visualization of speciation that occurred visualize.plot_species(pop.species_log)
def main(): # set up NEAT config.load("NEAT_config") chromosome.node_gene_type = genome.NodeGene population.Population.evaluate = coverageFitness #function name pop = population.Population() # set how many generations you want evolution to run generations = 30 pop.epoch(generations, report=True, save_best=True) # Plots the best/average fitness across the evolution visualize.plot_stats(pop.stats) # Creates a visualization of speciation that occurred visualize.plot_species(pop.species_log)
def main(): population.Population.evaluate = eval_fitness pop = population.Population() generations = 23 pop.epoch(generations, report=True, save_best=True, \ checkpoint_interval=None, checkpoint_generation=11) stop = time.time() elapsed = stop - start print "Total time to evolve:", elapsed print "Time per generation:", elapsed/float(generations) visualize.plot_stats(pop.stats) visualize.plot_species(pop.species_log)
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 = Config(os.path.join(local_dir, 'xor2_config')) # For this network, we use two output neurons and use the difference between # the "time to first spike" to determine the network response. There are # probably a great many different choices one could make for an output encoding, # and this choice may not be the best for tackling a real problem. config.output_nodes = 2 pop = population.Population(config) pop.epoch(eval_fitness, 200) winner = pop.most_fit_genomes[-1] print('Number of evaluations: %d' % winner.ID) # Visualize the winner network and plot statistics. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores) visualize.plot_species(pop.species_log) visualize.draw_net(winner, view=True) # Verify network output against training data. print('\nBest network output:') net = iznn.create_phenotype(winner, *iz_params) error, simulated = simulate(winner) # Create a plot of the traces out to the max time for each set of inputs. plt.figure(figsize=(12,12)) for r, (inputData, outputData, t0, t1, neuron_data) in enumerate(simulated): response = compute_output(t0, t1) print("%r expected %.3f got %.3f" % (inputData, outputData, response)) plt.subplot(4, 1, r + 1) plt.title("Traces for XOR input {%.1f, %.1f}" % inputData, fontsize=12) for i, s in neuron_data.items(): if i not in net.inputs: t, v = zip(*s) plt.plot(t, v, "-", label="neuron %d" % i) plt.ylabel("Potential (mv)", fontsize=10) plt.ylim(-100, 50) plt.tick_params(axis='both', labelsize=8) plt.grid() plt.xlabel("Time (in ms)", fontsize=10) plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) plt.savefig("traces.png", dpi=90) plt.show()
def main(argv = None): if argv is None: argv = sys.argv[1:] chromo_file = None log_file = None if len(argv) == 0: pass elif len(argv) == 1: chromo_gen = argv[0] chromo_file = "blue_best_chromo_" + chromo_gen elif len(argv) == 2: chromo_gen = argv[0] chromo_file = "blue_best_chromo_" + chromo_gen log_file = argv[1] else: print "Usage: python blueEat.py [chromo_gen] [log_file]" return if chromo_file: if log_file: logFP = open(log_file, "w") else: logFP = None # Test an already evolved network fp = open(chromo_file, "r") chromo = pickle.load(fp) fp.close() print chromo result = eval_individual(chromo, int(chromo_gen), logFP) print "Fitness:", result if log_file: logFP.write("Fitness %f" % result) logFP.close() else: # Start the evolutionary process population.Population.evaluate = eval_fitness pop = population.Population() pop.epoch(23, report=True, save_best=True, checkpoint_interval=4, \ checkpoint_generation=None, name="blue") # changed from 4, None, 2 # Plots the evolution of the best/average fitness (requires Biggles) visualize.plot_stats(pop.stats, name="blue") # Visualizes speciation visualize.plot_species(pop.species_log, name="blue") winner = pop.best # Visualize the winner network (requires PyDot) visualize.draw_net(winner, "blue") # best chromosome print "NEAT evolution complete"
def main(argv=None): if argv is None: argv = sys.argv[1:] chromo_file = None log_file = None if len(argv) == 0: pass elif len(argv) == 1: chromo_gen = argv[0] chromo_file = "blue_best_chromo_" + chromo_gen elif len(argv) == 2: chromo_gen = argv[0] chromo_file = "blue_best_chromo_" + chromo_gen log_file = argv[1] else: print "Usage: python blueEat.py [chromo_gen] [log_file]" return if chromo_file: if log_file: logFP = open(log_file, "w") else: logFP = None # Test an already evolved network fp = open(chromo_file, "r") chromo = pickle.load(fp) fp.close() print chromo result = eval_individual(chromo, int(chromo_gen), logFP) print "Fitness:", result if log_file: logFP.write("Fitness %f" % result) logFP.close() else: # Start the evolutionary process population.Population.evaluate = eval_fitness pop = population.Population() pop.epoch(23, report=True, save_best=True, checkpoint_interval=4, \ checkpoint_generation=None, name="blue") # changed from 4, None, 2 # Plots the evolution of the best/average fitness (requires Biggles) visualize.plot_stats(pop.stats, name="blue") # Visualizes speciation visualize.plot_species(pop.species_log, name="blue") winner = pop.best # Visualize the winner network (requires PyDot) visualize.draw_net(winner, "blue") # best chromosome print "NEAT evolution complete"
def save_stats(self, file_prefix): ''' Save info about the network to a number of files with the given file prefix file_prefix: string returns: None ''' best_genome = self.pop.statistics.best_genome() visualize.plot_stats(self.pop.statistics, filename="%s_stats.png" % file_prefix) visualize.plot_species(self.pop.statistics, filename="%s_stats.png" % file_prefix) visualize.draw_net(best_genome, view=False, filename="%s_stats.gv" % file_prefix) statistics.save_stats(self.pop.statistics, filename="%s_stats.csv" % file_prefix) statistics.save_species_count(self.pop.statistics, filename="%s_species_count.csv" % file_prefix) statistics.save_species_fitness(self.pop.statistics, filename="%s_species_fitness.csv" % file_prefix)
def main(): local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'pacai_config') pe = parallel.ParallelEvaluator(8, runPacman) pop = population.Population(config_path) for i in range(0, 1): pop.run(pe.evaluate, 1) winner = pop.statistics.best_genome() print winner filename = "winner{0}".format(i) with open(filename, 'wb') as f: pickle.dump(winner, f) visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) visualize.draw_net(winner, view=True)
def run(): pop = population.Population('xor2_config') pop.epoch(eval_fitness, 300) winner = pop.most_fit_genomes[-1] print('Number of evaluations: %d' % winner.ID) # Verify network output against training data. print('\nBest network output:') net = nn.create_feed_forward_phenotype(winner) for inputs, expected in zip(INPUTS, OUTPUTS): output = net.serial_activate(inputs) print("expected %1.5f got %1.5f" % (expected, output[0])) print(nn.create_feed_forward_function(winner)) # Visualize the winner network and plot statistics. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores) visualize.plot_species(pop.species_log) visualize.draw_net(winner, view=True)
def run(): local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, "spole_ctrnn_config")) pop = population.Population(config, node_gene_type=genome.CTNodeGene) pop.epoch(evaluate_population, 2000, report=1, save_best=0) # saves the winner winner = pop.most_fit_genomes[-1] print "Number of evaluations: %d" % winner.ID with open("winner_chromosome", "w") as f: cPickle.dump(winner, f) print winner # Plot the evolution of the best/average fitness. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores, ylog=True, view=True) # Visualizes speciation visualize.plot_species(pop.species_log) # Visualize the best network. visualize.draw_net(winner, view=True)
def test_run(): xor_inputs = [[0, 0], [0, 1], [1, 0], [1, 1]] xor_outputs = [0, 1, 1, 0] def eval_fitness(genomes): for g in genomes: net = nn.create_feed_forward_phenotype(g) error = 0.0 for inputs, expected in zip(xor_inputs, xor_outputs): # Serial activation propagates the inputs through the entire network. output = net.serial_activate(inputs) error += (output[0] - expected) ** 2 # When the output matches expected for all inputs, fitness will reach # its maximum value of 1.0. g.fitness = 1 - error local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) pop = population.Population(config) pop.run(eval_fitness, 10) visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) winner = pop.statistics.best_genome() # Validate winner. for g in pop.statistics.most_fit_genomes: assert winner.fitness >= g.fitness visualize.draw_net(winner, view=False, filename="xor2-all.gv") visualize.draw_net(winner, view=False, filename="xor2-enabled.gv", show_disabled=False) visualize.draw_net(winner, view=False, filename="xor2-enabled-pruned.gv", show_disabled=False, prune_unused=True) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics)
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 = Config(os.path.join(local_dir, 'xor2_config')) pop = population.Population(config) pop.epoch(eval_fitness, 300) winner = pop.most_fit_genomes[-1] print 'Number of evaluations: %d' % winner.ID # Verify network output against training data. print '\nBest network output:' net = nn.create_fast_feedforward_phenotype(winner) for i, inputs in enumerate(INPUTS): output = net.sactivate(inputs) # serial activation print "%1.5f \t %1.5f" % (OUTPUTS[i], output[0]) # Visualize the winner network and plot statistics. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores) visualize.plot_species(pop.species_log) visualize.draw_net(winner, view=True)
def __init__(self): #config = Config('drop7_config') pop = population.Population('drop7_config') pop.run(self.eval_fitness, 100) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Display the most fit genome. self.winner = pop.statistics.best_genome() print('\nBest genome:\n{!s}'.format(self.winner)) # Verify network output against training data. #print('\nOutput:') self.winner_net = nn.create_feed_forward_phenotype(self.winner) #for inputs, expected in zip(xor_inputs, xor_outputs): # output = winner_net.serial_activate(inputs) # print("expected {0:1.5f} got {1:1.5f}".format(expected, output[0])) visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) visualize.draw_net(self.winner, view=True, filename="drop7-all.gv") visualize.draw_net(self.winner, view=True, filename="drop7-enabled.gv", show_disabled=False) visualize.draw_net(self.winner, view=True, filename="drop7-enabled-pruned.gv", show_disabled=False, prune_unused=True)
pop.run(eval_fitness, 300) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Display the most fit genome. winner = pop.statistics.best_genome() print('\nBest genome:\n{!s}'.format(winner)) # Verify network output against training data. print('\nOutput:') winner_net = nn.create_feed_forward_phenotype(winner) for inputs, expected in zip(xor_inputs, xor_outputs): output = winner_net.serial_activate(inputs) print("expected {0:1.5f} got {1:1.5f}".format(expected, output[0])) # Visualize the winner network and plot/log statistics. visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) visualize.draw_net(winner, view=True, filename="xor2-all.gv") visualize.draw_net(winner, view=True, filename="xor2-enabled.gv", show_disabled=False) visualize.draw_net(winner, view=True, filename="xor2-enabled-pruned.gv", show_disabled=False, prune_unused=True) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics)
#chromo.fitness = (4.0 - error_stanley)**2 # (Stanley p. 43) chromo.fitness = 1 - math.sqrt(error/len(OUTPUTS)) population.Population.evaluate = eval_fitness pop = population.Population() pop.epoch(300, report=True, save_best=False) winner = pop.stats[0][-1] print 'Number of evaluations: %d' %winner.id # Visualize the winner network (requires PyDot) visualize.draw_net(winner) # best chromosome # Plots the evolution of the best/average fitness (requires Biggles) visualize.plot_stats(pop.stats) # Visualizes speciation visualize.plot_species(pop.species_log) # Let's check if it's really solved the problem print '\nBest network output:' brain = nn.create_ffphenotype(winner) for i, inputs in enumerate(INPUTS): output = brain.sactivate(inputs) # serial activation print "%1.5f \t %1.5f" %(OUTPUTS[i], output[0]) # saves the winner #file = open('winner_chromosome', 'w') #pickle.dump(winner, file) #file.close()
#print('\nBest genome:\n{!s}'.format(winner)) testGenome(winner,v=True,summary=False) #%% #pop = backup # Verify network output against training data. print('\nOutput:') winner = pop.statistics.best_genome() testGenome(winner,summary=True) #%% winner_net = nn.create_feed_forward_phenotype(winner) for i in range(tests): random.shuffle(letters) output = (winner_net.serial_activate(letters)) #print(output) output = decrypt(output) if output in words:print('%s- GOOD'%output) else:print('%s- BAD'%output) # Visualize the winner network and plot/log statistics. visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) visualize.draw_net(winner, view=True, filename="xor2-all.gv") visualize.draw_net(winner, view=True, filename="xor2-enabled.gv", show_disabled=False) visualize.draw_net(winner, view=True, filename="xor2-enabled-pruned.gv", show_disabled=False, prune_unused=True) statistics.save_stats(pop.statistics) statistics.save_species_count(pop.statistics) statistics.save_species_fitness(pop.statistics) #%%
pop.run(pe.evaluate, 300) else: print("Go for single Thread") viz = raw_input("Visulisation (y/n) ") renderer = None if viz == 'y': from GORLibrary import display as GORDisplay renderer = GORDisplay.pygameRenderer() game = GORGame.GOR(740, 580, 10, None, renderer) game.addRobot(20, 200) game.addFood() pop = population.Population('GorNnConfig') pop.run(evalFitness, 300) # Save the winner. print('Number of evaluations: {0:d}'.format(pop.total_evaluations)) winner = pop.most_fit_genomes[-1] with open('nn_winner_genome', 'wb') as f: pickle.dump(winner, f) print(winner) # Plot the evolution of the best/average fitness. visualize.plot_stats(pop, ylog=True, filename="nn_fitness.svg") # Visualizes speciation visualize.plot_species(pop, filename="nn_speciation.svg") print("done")
# the cart/pole has run/inclined out of the limits break chromo.fitness = fitness print 'Spikes:', spikes, ', Firing rate:', spikes / MAX_TIME, '(spikes/ms)' if __name__ == "__main__": config.load('spole_config') # Temporary workaround chromosome.node_gene_type = genome.NodeGene population.Population.evaluate = evaluate_population pop = population.Population() pop.epoch(200, report=1, save_best=0) #pop.epoch(500, report=1, save_best=0) print 'Number of evaluations: %d' % (pop.stats[0][-1]).id # visualize the best topology visualize.draw_net(pop.stats[0][-1]) # best chromosome # Plots the evolution of the best/average fitness visualize.plot_stats(pop.stats) visualize.plot_species(pop.species_log) # saves the winner file = open('winner_chromosome', 'w') pickle.dump(pop.stats[0][-1], file) file.close()
pop = neat.population.Population(config) stats = neat.statistics.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.reporting.StdOutReporter(True)) winner = pop.run(eval_fitness, generations) return winner, stats # If run as script. if __name__ == '__main__': result = run(1000) winner = result[0] stats = result[1] print('\nBest genome:\n{!s}'.format(winner)) # Verify network output against training data. print('\nOutput:') winner_net = neat.nn.FeedForwardNetwork.create(winner, config) for inputs, expected in zip(xor_inputs, xor_outputs): new_input = inputs output = winner_net.activate(new_input) print(" input {!r}, expected output {!r}, got {!r}".format( inputs, expected, output)) # Save net if wished reused and draw it to a file. with open('winner_neat_xor.pkl', 'wb') as output: pickle.dump(winner_net, output, pickle.HIGHEST_PROTOCOL) draw_net(winner_net, filename="neat_xor_winner") plot_stats(stats, ylog=False, view=True, filename='avg_fitness_neat.svg') plot_species(stats, view=True, filename='speciation_neat.svg')
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 = Config(os.path.join(local_dir, 'xor2_config')) # For this network, we use two output neurons and use the difference between # the "time to first spike" to determine the network response. There are # probably a great many different choices one could make for an output encoding, # and this choice may not be the best for tackling a real problem. config.output_nodes = 2 pop = population.Population(config) pop.run(eval_fitness, 200) print('Number of evaluations: {0}'.format(pop.total_evaluations)) # Visualize the winner network and plot statistics. winner = pop.statistics.best_genome() node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'} visualize.draw_net(winner, view=True, node_names=node_names) visualize.plot_stats(pop.statistics) visualize.plot_species(pop.statistics) # Verify network output against training data. print('\nBest network output:') net = iznn.create_phenotype(winner, *iz_params) sum_square_error, simulated = simulate(winner) # Create a plot of the traces out to the max time for each set of inputs. plt.figure(figsize=(12, 12)) for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated): response = compute_output(t0, t1) print("{0!r} expected {1:.3f} got {2:.3f}".format( inputData, outputData, response)) axes = plt.subplot(4, 1, r + 1) plt.title( "Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData), fontsize=12) for i, s in neuron_data.items(): if i in net.outputs: t, v = zip(*s) plt.plot(t, v, "-", label="neuron {0:d}".format(i)) # Circle the first peak of each output. circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False) circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False) axes.add_artist(circle0) axes.add_artist(circle1) plt.ylabel("Potential (mv)", fontsize=10) plt.ylim(-100, 50) plt.tick_params(labelsize=8) plt.grid() plt.xlabel("Time (in ms)", fontsize=10) plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0) plt.savefig("traces.png", dpi=90) plt.show()
from cart_pole import discrete_actuator_force from fitness import evaluate_population # Use the CTRNN network phenotype and the discrete actuator force function. def fitness_function(genomes): evaluate_population(genomes, ctrnn.create_phenotype, discrete_actuator_force) # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'ctrnn_config')) pop = population.Population(config, node_gene_type=genes.CTNodeGene) pop.epoch(fitness_function, 2000, report=1, save_best=0) # Save the winner. winner = pop.most_fit_genomes[-1] print('Number of evaluations: %d' % winner.ID) with open('ctrnn_winner_genome', 'wb') as f: pickle.dump(winner, f) print(winner) # Plot the evolution of the best/average fitness. visualize.plot_stats(pop.most_fit_genomes, pop.avg_fitness_scores, ylog=True, filename="ctrnn_fitness.svg") # Visualizes speciation visualize.plot_species(pop.species_log, filename="ctrnn_speciation.svg") # Visualize the best network. visualize.draw_net(winner, view=True, filename="ctrnn_winner.gv")
# Test the performance of the best genome produced by nn_evolve.py. # Test the performance of the best genome produced by nn_evolve.py. from __future__ import print_function import pickle from neat import nn, parallel, population, visualize # load the winner with open('nn_pop0', 'rb') as f: pop = pickle.load(f) winner = pop.statistics.best_genome() # Plot the evolution of the best/average fitness. visualize.plot_stats(pop.statistics, ylog=True, filename="nn_fitness.svg") # Visualizes speciation visualize.plot_species(pop.statistics, filename="nn_speciation.svg") # Visualize the best network. visualize.draw_net(winner, view=False, filename="nn_winner.gv") visualize.draw_net(winner, view=False, filename="nn_winner-enabled.gv", show_disabled=False) visualize.draw_net(winner, view=False, filename="nn_winner-enabled-pruned.gv", show_disabled=False, prune_unused=True)
fitnesses.append(fitness) # The genome's fitness is its worst performance across all runs. return min(fitnesses) # Load the config file, which is assumed to live in # the same directory as this script. local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'ctrnn_config')) config.node_gene_type = ctrnn.CTNodeGene pop = population.Population(config) pe = parallel.ParallelEvaluator(4, evaluate_genome) pop.run(pe.evaluate, 2000) # Save the winner. print('Number of evaluations: {0:d}'.format(pop.total_evaluations)) winner = pop.statistics.best_genome() with open('ctrnn_winner_genome', 'wb') as f: pickle.dump(winner, f) print(winner) # Plot the evolution of the best/average fitness. visualize.plot_stats(pop.statistics, ylog=True, filename="ctrnn_fitness.svg") # Visualizes speciation visualize.plot_species(pop.statistics, filename="ctrnn_speciation.svg") # Visualize the best network. visualize.draw_net(winner, view=True, filename="ctrnn_winner.gv")