def test_minimal(): # sample fitness function def eval_fitness(population): for individual in population: individual.fitness = 1.0 # creates the population local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) pop = Population(config) # run the simulation for up to 20 generations pop.run(eval_fitness, 20) # get statistics avg_fitness = pop.statistics.get_average_fitness() assert len(avg_fitness) == 1 assert all(f == 1 for f in avg_fitness) # Change fitness threshold and do another run. config.max_fitness_threshold = 1.1 pop = Population(config) # runs the simulation for 20 generations pop.run(eval_fitness, 20) # get statistics avg_fitness = pop.statistics.get_average_fitness() assert len(avg_fitness) == 20 assert all(f == 1 for f in avg_fitness)
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 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 check_self_crossover(genome_type): # Check that self-crossover produces a genetically identical child (with a different ID). indexer = InnovationIndexer(0) local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) c = genome_type.create_unconnected(1, config) c.connect_full(indexer) c.fitness = 0.0 cnew = c.crossover(c, 2) assert cnew.ID != c.ID assert len(cnew.conn_genes) == len(c.conn_genes) for kold, vold in cnew.conn_genes.items(): print(kold, vold) print(c.conn_genes) assert kold in c.conn_genes vnew = c.conn_genes[kold] assert vold.is_same_innov(vnew) assert vnew.weight == vold.weight assert vnew.in_node_id == vold.in_node_id assert vnew.out_node_id == vold.out_node_id assert vnew.enabled == vold.enabled assert len(cnew.node_genes) == len(c.node_genes) for kold, vold in cnew.node_genes.items(): assert kold in c.node_genes vnew = c.node_genes[kold] assert vnew.ID == vold.ID assert vnew.type == vold.type assert vnew.bias == vold.bias assert vnew.response == vold.response assert vnew.activation_type == vold.activation_type
def __init__(self, config): """ :param config: Either a config.Config object or path to a configuration file. """ # If config is not a Config object, assume it is a path to the config file. if not isinstance(config, Config): config = Config(config) # Configure statistics and reporting as requested by the user. self.reporters = ReporterSet() if config.collect_statistics: self.statistics = StatisticsReporter() self.add_reporter(self.statistics) else: self.statistics = None if config.report: self.add_reporter(StdOutReporter()) self.config = config print(self.config.genotype.__name__) ## Check if we have a society directory defined and may be this a continuation on an existing run. if self.config.society_directory != None: ## Check if latest society file is available if os.path.isdir(self.config.society_directory): print("Society Directory") self.species_indexer = Indexer(1) self.genome_indexer = Indexer(1) self.innovation_indexer = InnovationIndexer(0) self.reproduction = config.reproduction_type(self.config, self.reporters, self.genome_indexer, self.innovation_indexer) self.species = [] self.generation = -1 self.total_evaluations = 0 # Create a population if one is not given, then partition into species. self.population = self._create_population() self._speciate(self.population)
def __init__(self, config): """ :param config: Either a config.Config object or path to a configuration file. """ # If config is not a Config object, assume it is a path to the config file. if not isinstance(config, Config): config = Config(config) # Configure statistics and reporting as requested by the user. self.reporters = ReporterSet() if config.collect_statistics: self.statistics = StatisticsReporter() self.add_reporter(self.statistics) else: self.statistics = None if config.report: self.add_reporter(StdOutReporter()) self.config = config ## Check if we have a society directory defined and may be this a continuation on an existing run. if self.config.society_directory != None: ## Check if latest society file is available if os.path.isdir(self.config.society_directory): print("Society Directory") self.species_indexer = Indexer(1) self.genome_indexer = Indexer(1) self.innovation_indexer = InnovationIndexer(0) self.reproduction = config.reproduction_type(self.config, self.reporters, self.genome_indexer, self.innovation_indexer) self.species = [] self.generation = -1 self.total_evaluations = 0 # Create a population if one is not given, then partition into species. self.population = self._create_population() self._speciate(initial_population)
def test_config_options(): # sample fitness function def eval_fitness(population): for individual in population: individual.fitness = 1.0 local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) for hn in (0, 1, 2): config.hidden_nodes = hn for fc in (0, 1): config.fully_connected = fc for act in activation_functions.functions.keys(): config.allowed_activation = [act] for ff in (0, 1): config.feedforward = ff pop = Population(config) pop.run(eval_fitness, 250)
def check_add_connection(genome_type, feed_forward): indexer = InnovationIndexer(0) local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) config.input_nodes = 3 config.output_nodes = 4 config.hidden_nodes = 5 config.feedforward = feed_forward N = config.input_nodes + config.hidden_nodes + config.output_nodes connections = {} for a in range(100): g = genome_type.create_unconnected(a, config) g.add_hidden_nodes(config.hidden_nodes) for b in range(1000): g.mutate_add_connection(indexer) for c in g.conn_genes.values(): connections[c.key] = connections.get(c.key, 0) + 1 # TODO: The connections should be returned to the caller and checked # against the constraints/assumptions particular to the network type. for i in range(N): values = [] for j in range(N): values.append(connections.get((i, j), 0)) print("{0:2d}: {1}".format(i, " ".join("{0:3d}".format(x) for x in values)))
def check_simple(genome_type): indexer = InnovationIndexer(0) local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) c1 = genome_type.create_unconnected(1, config) c1.connect_full(indexer) # add two hidden nodes c1.add_hidden_nodes(2) # apply some mutations c1.mutate_add_node(indexer) c1.mutate_add_connection(indexer)
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 test_checkpoint(): # sample fitness function def eval_fitness(population): for individual in population: individual.fitness = 0.99 # creates the population local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'test_configuration')) pop = Population(config) pop.run(eval_fitness, 20) t = tempfile.NamedTemporaryFile(delete=False) t.close() pop.save_checkpoint(t.name) pop2 = Population(config) pop2.load_checkpoint(t.name)
# without exceeding these limits. if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians: break fitness += 1.0 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(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.
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()
raise Exception('''This example is currently broken, it will be fixed soon. In the meantime, try the single_pole or xor examples.''') import os import sys import cPickle from cart_pole import CartPole from neatsociety.config import Config filename = 'winner_chromosome' if len(sys.argv) > 1: filename = sys.argv[1] # load genome print("loading genome {0!s}".format(filename)) with open(filename) as f: c = cPickle.load(f) # load settings file local_dir = os.path.dirname(__file__) config = Config(os.path.join(local_dir, 'dpole_config')) print("Loaded genome:\n{0!s}".format(c)) # starts the simulation simulator = CartPole([c], markov=False) simulator.run(testing=True)