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)

    winner = pop.statistics.best_genome()

    # Validate winner.
    for g in pop.statistics.most_fit_genomes:
        assert winner.fitness >= g.fitness

    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)
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)

    winner = pop.statistics.best_genome()

    # Validate winner.
    for g in pop.statistics.most_fit_genomes:
        assert winner.fitness >= g.fitness

    statistics.save_stats(pop.statistics)
    statistics.save_species_count(pop.statistics)
    statistics.save_species_fitness(pop.statistics)
Exemple #3
0
    def train_network(self, checkpoint):

        self.checkpoint = checkpoint
        # Simulation

        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, 'config.txt')
        pop = population.Population(config_path)

        # Load checkpoint
        if self.checkpoint != None:
            pop.load_checkpoint(self.checkpoint)

        #pe = parallel.ParallelEvaluator(args.numCores,worker_evaluate_genome)
        pop.run(self.eval_fitness, self.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)

        #visualize.draw_net("config.txt", winner, view=False, node_names=None, filename=self.game_name + "net")
        '''
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():
    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]))
Exemple #6
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)
Exemple #7
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]))
Exemple #8
0
    def train_network(self, checkpoint):

        self.checkpoint = checkpoint

        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, 'config.txt')
        pop = population.Population(config_path)

        # Load checkpoint
        if self.checkpoint != None:
            pop.load_checkpoint(self.checkpoint)

        pop.run(self.eval_fitness, self.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)
Exemple #9
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)
Exemple #10
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)
    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)
Exemple #12
0
def train(checkpoint, config):
    print("Starting...")
    pop = population.Population(config)
    # HINT change checkpoints for new try or reloading
    try:
        pop.load_checkpoint(checkpoint)
    except:
        print("Checkpoint not found, starting from scratch: ", checkpoint)
    trainfunc = partial(eval_fitness_internalfight, num_runs=4, steplength=100, x=16, y=16)
    pop.run(trainfunc, 20)
    pop.save_checkpoint(checkpoint)

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

    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
def main():
    print("Starting...")
    pop = population.Population(
        os.path.join(os.path.dirname(__file__), 'nn_config'))
    #HINT change checkpoints for new try or reloading
    pop.load_checkpoint(
        os.path.join(os.path.dirname(__file__), 'checkpoints/popv1.cpt'))
    pop.run(eval_fitness_internalfight, 5)
    pop.save_checkpoint(
        os.path.join(os.path.dirname(__file__), 'checkpoints/popv1.cpt'))

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

    winner = pop.statistics.best_genome()
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = nn.create_recurrent_phenotype(winner)
    mooreNeighborhood = ca.Neighborhood(gocl.initNeighborhood)
    gameOfLife = ca.CellularAutomaton(mooreNeighborhood)
    gameOfLife.parameters = gameOfLife.world['parameters']
    newSpecies(gameOfLife, {
        'species': 'test',
        'color': 'Blue',
        'position': {
            'x': 0,
            'y': 0
        }
    })
    currentDecision = partial(netDecision, net=winner_net)
    evolve = gameOfLife.evolve
    c = 0
    while c < 20:
        dec = {}
        recursionDecision(gameOfLife.world['space'], dec, currentDecision)
        gameOfLife.decisions = dec
        evolve()
        c += 1
    print("Ended with: ", countSpecies(gameOfLife.world['space'], 'test'))
Exemple #14
0
            # Serial activation propagates the inputs through the entire network.
            output = net.serial_activate(inputs)
            sum_square_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 - sum_square_error


local_dir = os.path.dirname(__file__)
config_path = os.path.join(local_dir, 'xor2_config')
pop = population.Population(config_path)
pop.run(eval_fitness, 300)

# 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()
print('\nBest genome:\n{!s}'.format(winner))
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]))

Exemple #15
0
def test_concurrent_nn():
    """This is a stripped-down copy of the `memory` example."""

    # num_tests is the number of random examples each network is tested against.
    num_tests = 16
    # N is the length of the test sequence.
    N = 4

    def eval_fitness(genomes):
        for g in genomes:
            net = nn.create_recurrent_phenotype(g)

            error = 0.0
            for _ in range(num_tests):
                # Create a random sequence, and feed it to the network with the
                # second input set to zero.
                seq = [random.choice((0, 1)) for _ in range(N)]
                net.reset()
                for s in seq:
                    inputs = [s, 0]
                    net.activate(inputs)

                # Set the second input to one, and get the network output.
                for s in seq:
                    inputs = [0, 1]
                    output = net.activate(inputs)

                    error += (output[0] - s)**2

            g.fitness = -(error / (N * num_tests))**0.5

    # Demonstration of how to add your own custom activation function.
    def sinc(x):
        return 1.0 if x == 0 else math.sin(x) / x

    # This sinc function will be available if my_sinc_function is included in the
    # config file activation_functions option under the pheotype section.
    # Note that sinc is not necessarily useful for this example, it was chosen
    # arbitrarily just to demonstrate adding a custom activation function.
    activation_functions.add('my_sinc_function', sinc)

    local_dir = os.path.dirname(__file__)
    pop = population.Population(os.path.join(local_dir, 'recurrent_config'))
    pop.run(eval_fitness, 10)

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

    winner = pop.statistics.best_genome()
    repr(winner)
    str(winner)
    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)
Exemple #16
0
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)
Exemple #17
0
def test_concurrent_nn():
    """This is a stripped-down copy of the `memory` example."""

    # num_tests is the number of random examples each network is tested against.
    num_tests = 16
    # N is the length of the test sequence.
    N = 4

    def eval_fitness(genomes):
        for g in genomes:
            net = nn.create_recurrent_phenotype(g)

            error = 0.0
            for _ in range(num_tests):
                # Create a random sequence, and feed it to the network with the
                # second input set to zero.
                seq = [random.choice((0, 1)) for _ in range(N)]
                net.reset()
                for s in seq:
                    inputs = [s, 0]
                    net.activate(inputs)

                # Set the second input to one, and get the network output.
                for s in seq:
                    inputs = [0, 1]
                    output = net.activate(inputs)

                    error += (output[0] - s) ** 2

            g.fitness = -(error / (N * num_tests)) ** 0.5

    # Demonstration of how to add your own custom activation function.
    def sinc(x):
        return 1.0 if x == 0 else math.sin(x) / x

    # This sinc function will be available if my_sinc_function is included in the
    # config file activation_functions option under the pheotype section.
    # Note that sinc is not necessarily useful for this example, it was chosen
    # arbitrarily just to demonstrate adding a custom activation function.
    activation_functions.add('my_sinc_function', sinc)

    local_dir = os.path.dirname(__file__)
    pop = population.Population(os.path.join(local_dir, 'recurrent_config'))
    pop.run(eval_fitness, 10)

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

    winner = pop.statistics.best_genome()
    repr(winner)
    str(winner)
    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)