Ejemplo n.º 1
0
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def test_minimal():
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'test_configuration')

    pop = Population(config_path)
    pe = parallel.ParallelEvaluator(4, eval_fitness)
    pop.run(pe.evaluate, 400)
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
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)

    # Log statistics.
    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Show output of the most fit genome against a random input.
    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    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]))
Ejemplo n.º 6
0
def train_network(env):
    def evaluate_genome(g):
        net = nn.create_feed_forward_phenotype(g)
        return simulate_species(net,
                                env,
                                args.episodes,
                                args.max_steps,
                                render=args.render)

    def eval_fitness(genomes):
        for g in genomes:
            fitness = evaluate_genome(g)
            g.fitness = fitness

    # Simulation
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'gym_config')
    pop = population.Population(config_path)
    # Load checkpoint
    if args.checkpoint:
        pop.load_checkpoint(args.checkpoint)
    # Start simulation
    if args.render:
        pop.run(eval_fitness, args.generations)
    else:
        pe = parallel.ParallelEvaluator(args.numCores, worker_evaluate_genome)
        pop.run(pe.evaluate, args.generations)

    pop.save_checkpoint("checkpoint")

    # Log statistics.
    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Show output of the most fit genome against training data.
    winner = pop.statistics.best_genome()

    # Save best network
    import pickle
    with open('winner.pkl', 'wb') as output:
        pickle.dump(winner, output, 1)

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

    raw_input("Press Enter to run the best genome...")
    winner_net = nn.create_feed_forward_phenotype(winner)
    for i in range(100):
        simulate_species(winner_net, env, 1, args.max_steps, render=True)
Ejemplo n.º 7
0
    def __init__(self, env, config, threads: int = 4):
        print("\n------------------------------------------")
        print("               NEAT USING THE               ")
        print("            NEAT-PYTHON LIBRARY             ")
        print("------------------------------------------\n")

        self.env = env

        # Simulation
        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, config)
        pop = population.Population(config_path)  # Get the config file

        # Load checkpoint
        if args.checkpoint:
            pop.load_checkpoint(args.checkpoint)

        # Start simulation
        try:
            if threads == 1:
                pop.run(self.eval_fitness, args.generations)
            else:
                pe = parallel.ParallelEvaluator(threads, self.evaluate_genome)
                pop.run(pe.evaluate, args.generations)
        except KeyboardInterrupt:
            print("\nExited.")

        pop.save_checkpoint("checkpoint")  # Save the current checkpoint

        # Log statistics.
        statistics.save_stats(pop.statistics)
        statistics.save_species_count(pop.statistics)
        statistics.save_species_fitness(pop.statistics)

        print('Number of evaluations: {0}'.format(pop.total_evaluations))

        # Show output of the most fit genome against training data.
        winner = pop.statistics.best_genome()

        # Save best network
        name = input("Net name: ")
        import pickle
        with open(name + '.pkl', 'wb') as output:
            pickle.dump(winner, output, 1)
        print("Net saved as " + name + '.pkl')

        print('\nBest genome:\n{!s}'.format(winner))
        input("Press enter to run the best genome...")

        winner_net = nn.create_feed_forward_phenotype(winner)
        self.simulate_species(winner_net, env, 1, args.max_steps, render=True)
Ejemplo n.º 8
0
def run():

    random.seed()
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'lab_config')

    pe = parallel.ParallelEvaluator(9, fitness)

    pop = population.Population(config_path)
    for i in range(100):
        pop.run(pe.evaluate, 10)
        winner = pop.statistics.best_genome()

        with open("ress/res_%d.net" % i, "wb") as f:
            pickle.dump(winner, f)
Ejemplo n.º 9
0
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)
    genJump = 10

    for numGen in range(0, 1000, genJump):
        pop.run(pe.evaluate, genJump)

        print('Number of evaluations: {0:d}'.format(pop.total_evaluations))

        with open('nn_pop' + str(numGen), 'wb') as f:
            pickle.dump(pop, f)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
def run():
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'neat_tetris_config')
    pe = parallel.ParallelEvaluator(50, eval_fitness)
    pop = population.Population(config_path)
    pop.run(pe.evaluate, 1000000)
    #pop.run(eval_fitness_genomes, 100)

    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    # Display the most fit genome.
    winner = pop.statistics.best_genome()
    f = open("best_genome.pkl", "wb")
    pickle.dump(winner, f)
    f.close()
    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]))
    '''
Ejemplo n.º 12
0
    global renderer
    for g in gs:
        p = nnPlayer(g, renderer)
        game.setPlayer(p)
        g.fitness = game.run()
        print(g.fitness)


pop = None
parallelExec = raw_input("Parallel Evaluation? (y/n) ")
if parallelExec == 'y':
    print("Go for Threading stuffs")
    nThread = int(raw_input("N Threads? "))
    # The population stuff
    pop = population.Population('GorNnConfig')
    pe = parallel.ParallelEvaluator(nThread, parallelEvalFitness)
    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)
Ejemplo n.º 13
0
            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, 'nn_config'))

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('nn_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="nn_fitness.svg")
# Visualizes speciation
visualize.plot_species(pop.statistics, filename="nn_speciation.svg")
# Visualize the best network.