def test_evolve(): test_values = [random.random() for _ in range(10)] def evaluate_genome(genomes): for g in genomes: net = ctrnn.create_phenotype(g) fitness = 0.0 for t in test_values: net.reset() output = net.serial_activate([t]) expected = t ** 2 error = output[0] - expected fitness -= error ** 2 g.fitness = fitness # 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 config.prob_mutate_time_constant = 0.1 config.checkpoint_time_interval = 0.1 config.checkpoint_gen_interval = 1 pop = population.Population(config) pop.run(evaluate_genome, 10) # Save the winner. print('Number of evaluations: {0:d}'.format(pop.total_evaluations)) winner = pop.statistics.best_genome() with open('winner_genome', 'wb') as f: pickle.dump(winner, f) repr(winner) str(winner) for g in winner.node_genes: repr(g) str(g) for g in winner.conn_genes: repr(g) str(g)
def main(): local_dir = os.path.dirname(__file__) config = Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, os.path.join(local_dir, 'train_config.txt')) config.save_best = True config.checkpoint_time_interval = 3 pop = population.Population(config) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.StdOutReporter(True)) pop.add_reporter(neat.StatisticsReporter()) pop.add_reporter(neat.Checkpointer(2)) winner = pop.run(eval_fitness, 100) with open('winner.pkl', 'wb') as f: pickle.dump(winner, f)
def main(): local_dir = os.path.dirname(__file__) config = Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, os.path.join(local_dir, 'train_config.txt')) config.save_best = True config.checkpoint_time_interval = 3 initial_pop = [] # initial_pop = [138, 74, 40, 84, 97, 133, 127, 60, 102, 70, 0, 1, 2] for root, dirs, files in os.walk('best'): for file_name in files: with open(os.path.join('best', file_name), 'rb') as f: initial_pop.append(pickle.load(f)) if not initial_pop: initial_pop = None pop = population.Population(config, initial_pop) stats = neat.StatisticsReporter() pop.add_reporter(stats) pop.add_reporter(neat.Checkpointer(5)) pop.run(eval_fitness, 100)