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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
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'))
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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"))
Ejemplo n.º 7
0
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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
def main(config_file):
    # Get initial time.
    start_time = time()
    # Load configuration for NEAT-Python.
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_file)

    # Create population, core of NEAT algorithm.
    pop = neat.Population(config)

    # Create stdout reporter to print progress, currently not implemented.
    pop.add_reporter(neat.StdOutReporter(False))
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)

    # Run for unlimited generations max.
    para_eval = neat.ParallelEvaluator(4, play_game)
    winner = pop.run(para_eval.evaluate, 1000)

    # Display winning genome.
    print "\nBest Genome: " + str(winner)
    print "Running Time (in secs.): " + str(time() - start_time)

    visualize.draw_net(config, winner, True)
    visualize.plot_stats(stats, ylog=False, view=True)
Ejemplo n.º 12
0
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))

    global stats
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)

    winner = p.run(game_loop, 50)

    print('\nBest genome:\n{!s}'.format(winner))

    # 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)

    with open('winner-river', 'wb') as f:
        pickle.dump(winner, f)

    with open('my_winner-river', 'wb') as f:
        pickle.dump(best_yet[1], f)

    print('Minha Seleção:')
    print(best_yet)
    print(best_yet[0])
    print(best_yet[1])
Ejemplo n.º 13
0
def run(config_file):
    # Load configuration file
    config = neat.config.Config(neat.DefaultGenome, neat.DefaultReproduction,
                                neat.DefaultSpeciesSet, neat.DefaultStagnation,
                                config_file)
    p = neat.Population(config)

    # Report genome statistics
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)

    # Determine optimal neural network
    winner = p.run(eval_genomes, 100)  # Evolve 100 generations

    # Plot/print the most successful neural network
    node_names = {
        -1: 'obst_dist',
        -2: 'obst_height',
        -3: 'obst_width',
        -4: 'obst_y',
        -5: 'game_speed',
        -6: 'dino.y',
        -7: 'is_cactus',
        0: 'Jump',
        1: 'Duck'
    }
    visualize.draw_net(config, winner, True, node_names=node_names)
    print('\nBest genome:\n{!s}'.format(winner))
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 )
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
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 evaluation():
    pop_test = neat.Checkpointer.restore_checkpoint(
        'neat-checkpoint-{}'.format(CHECKPOINT))

    # find the winner in restored population
    winner = pop_test.run(eval_genomes, 1)

    # show winner net
    node_names = {
        -1: 'In0',
        -2: 'In1',
        -3: 'In3',
        -4: 'In4',
        0: 'act1',
        1: 'act2'
    }
    visualize.draw_net(pop_test.config, winner, True, node_names=node_names)

    net = neat.nn.RecurrentNetwork.create(winner, pop_test.config)
    while True:
        s = ENV.reset()
        while True:
            ENV.render()
            a = np.argmax(net.activate(s))
            s, r, done, _ = ENV.step(a)
            if done: break
Ejemplo n.º 19
0
def eval_parallel(genomes, config):
    global current_level
    global gen_counter
    current_level = levels[gen_counter % len(levels)]

    print("Testing generation number {} on level {}...".format(gen_counter, current_level))

    evaluator = neat.parallel.ParallelEvaluator(12, eval_single, 2 * 60) # quit after 2 min if stuck
    evaluator.evaluate(genomes, config)

    with open('stats.obj', 'wb') as stats_file:
        pickle.dump(stats, stats_file)
    visualize.plot_stats(stats, ylog=False, view=False)

    if len(stats.best_genomes(1)) == 0:
        print("There is no best genome")
    else:
        visualize.draw_net(config, stats.best_genome())

    if gen_counter > 1:
        stats.save()
        with open('{}.csv'.format(current_level.replace('/', '-')), 'a') as f:
            print('{}, {}, {}, {}'.format(gen_counter, stats.get_fitness_stat(max)[-1], stats.get_fitness_mean()[-1], stats.get_fitness_stdev()[-1]), file=f)
    
    with open('gen_counter.txt', 'w') as f:
        f.write(str(gen_counter))

    gen_counter += 1
Ejemplo n.º 20
0
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)
Ejemplo n.º 21
0
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)
Ejemplo n.º 22
0
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)

    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)
Ejemplo n.º 24
0
def run(generations):
    '''
	Runs DHN

	generations -- number of generations to evolve for
	'''
    pop = population.Population(config)

    # Run the population under a simple evaluation scheme
    winner = pop.run(evaluate_xor, generations)
    cppn = FeedForwardNetwork.create(winner, config)
    substrate = decode(cppn, input_dimensions, num_outputs, sheet_dimensions)
    print("WINNER: Genome {0}\n".format(winner.key))
    print("Winner Fitness is {0}".format(winner.fitness))
    for node in winner.nodes:
        print("\t{0}: {1}".format(winner.nodes[node].key,
                                  winner.nodes[node].activation))
    for connection in winner.connections:
        print("\t{0},{1},{2}".format(connection,
                                     winner.connections[connection].weight,
                                     winner.connections[connection].enabled))
    sum_square_error = 0.0
    for inputs, expected in zip(xor_inputs, expected_outputs):
        print("Expected Output: {0}".format(expected))
        actual_output = substrate.activate(inputs)[0]
        print("Actual Output: {0}".format(actual_output))
        sum_square_error += ((actual_output - expected)**2.0) / 4.0
        print("Loss: {0}".format(sum_square_error))
    draw_net(cppn, filename="dhn_cppn_winner")
    draw_net(substrate, filename="dhn_substrate_winner")
    for node in substrate.node_evals:
        print("Node: \t{0}:".format(node))
    for connection in substrate.values:
        print("Value:\t{0}".format(connection))
Ejemplo n.º 25
0
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"))
Ejemplo n.º 26
0
def run(config_path):
    """
    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_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)

    # run for up to 50 generations
    winner = p.run(eval_genomes, 50)

    # visualize neural network of winning agent
    visualize.draw_net(config, winner, True)  #node_names=node_names)

    # show final stats
    print('\nBest genome:\n{!s}'.format(winner))
Ejemplo n.º 27
0
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)
Ejemplo n.º 28
0
def eval_genomes(genomes, config):
    global env
    global MAX_STEPS
    global count
    global reward_list

    max_episode_fitness = -500
    for genome_id, genome in genomes:
        genome.fitness = 0
        net = neat.nn.FeedForwardNetwork.create(genome, config)
        observation = env.reset()
        episode_reward = 0
        for _ in range(5):
            while True:
                action = net.activate(observation)
                #print("observation",observation)
                #print("action",action)
                action = np.clip(action, -1, 1)
                #env.render()
                observation, reward, done, info = env.step(action)
                episode_reward += reward
                #if (-4.8  > observation[0]) or (observation[0] > 4.8) or (0.017453292519943 < observation[3] < -0.017453292519943) or (episode_reward >= MAX_STEPS):
                if done:
                    #print(episode_reward)
                    env.reset()
                    break
            #print(episode_reward)
        genome.fitness = episode_reward / 5
        if max_episode_fitness <= episode_reward / 5:
            max_episode_fitness = episode_reward / 5
            winner = genome
    #print(max_episode_fitness)
    reward_list.append(max_episode_fitness / 5)
    if count % 10 == 0:
        winner_net = neat.nn.FeedForwardNetwork.create(genome, config)
        #env = wrappers.Monitor(env, '/home/bc/Documents/EA/experiment/Bipedalwalker/neat/movies', force=True)
        #observation = env.reset()
        #for i in range(500):
        #action = winner_net.activate(observation)
        #env.render()
        #action = np.clip(action,-1,1)
        #observation,reward,done,info = env.step(action)
        #episode_reward += reward
        #if (-4.8  > observation[0]) or (observation[0] > 4.8) or (0.017453292519943 < observation[3] < -0.017453292519943) or (episode_reward >= MAX_STEPS):
        #if done:
        #observation = env.reset()
        #break

        for n, g in enumerate([winner]):
            name = '2winner-{0}'.format(n)
            with open(name + '.pickle', 'wb') as f:
                pickle.dump(g, f)

            #visualize.draw_net(config, g, view=False, filename=str(count)+name + "-net.gv")
            visualize.draw_net(config,
                               g,
                               view=False,
                               filename=str(count) + "net-enabled.gv",
                               show_disabled=False)
    count += 1
Ejemplo n.º 29
0
    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)
Ejemplo n.º 30
0
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)
Ejemplo n.º 31
0
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 evaluation():
    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)

    net = neat.nn.FeedForwardNetwork.create(winner, p.config)
    while True:
        s = env.reset()
        while True:
            env.render()
            a = np.argmax(net.activate(s))
            s, r, done, _ = env.step(a)
            if done: break