Exemplo n.º 1
0
    def draw_net(net, filename, node_names):

        # Create PDF using PUREPLES function
        draw_net(net, filename=filename, node_names=node_names)

        # Delete text
        os.remove(filename)
Exemplo n.º 2
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)
    p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-4')

    # 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(1000))
    #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-299')

    # Run for up to 300 generations.
    pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome)
    winner = p.run(pe.evaluate, 300)

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

    # Show output of the most fit genome against training data.
    print('\nOutput:')
    cppn = neat.nn.RecurrentNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    winner_net = network.create_phenotype_network(
        filename='es_hyperneat_winner.png')
    input("Winner is found")
    for i in range(10):
        train(winner_net, network, True)

    draw_net(cppn, filename="es_hyperneat")
Exemplo n.º 3
0
    def _draw_net(net, filename, node_names):

        # Create PDF
        draw_net(net, filename=filename, node_names=node_names)

        # Delete text
        os.remove(filename)
Exemplo n.º 4
0
def run(config_file):
    # Load configuration.
    config = neat.config.Config(neat.genome.DefaultGenome,
                                neat.reproduction.DefaultReproduction,
                                neat.species.DefaultSpeciesSet,
                                neat.stagnation.DefaultStagnation, config_file)

    # Create the population, which is the top-level object for a NEAT run.
    #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-15')

    # Add a stdout reporter to show progress in the terminal.
    pop = neat.population.Population(config)
    stats = neat.statistics.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))
    pop.add_reporter(neat.Checkpointer(999))
    #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-299')

    # Run for up to 300 generations.
    winner = pop.run(eval_multipleGenomes, 300)

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

    # Show output of the most fit genome against training data.
    print('\nOutput:')
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    winner_net = network.create_phenotype_network(
        filename='es_hyperneat_winner.png')
    input("Winner is found")
    for i in range(10):
        train(winner_net, network, True)

    draw_net(cppn, filename="es_hyperneat")
Exemplo n.º 5
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)
    #
    # p = neat.Checkpointer.restore_checkpoint('neat-checkpoint-100')
    #
    #
    # thenet = neat.nn.FeedForwardNetwork.create(p.best_genome, config)
    # output = thenet.activate(i)
    #
    # 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(0))

    # Run for up to 300 generations.
    winner = p.run(eval_genomes, 150)

    # import gzip
    #
    # try:
    #     import pickle as pickle  # pylint: disable=import-error
    # except ImportError:
    #     import pickle  # pylint: disable=import-error
    #
    # def save_object(obj, filename):
    #     with gzip.open(filename, 'w', compresslevel=5) as f:
    #         pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
    #
    # def load_object(filename):
    #     with gzip.open(filename) as f:
    #         obj = pickle.load(f)
    #         return obj

    # save_object(winner,"neat.pkl")
    # winner = load_object("neat.pkl")

    # 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.RecurrentNetwork.create(winner, config)
    draw_net(winner_net, filename="neat_torcs_winner_steering")
    with open('neat_torcs_steering.pkl', 'wb') as output:
        pickle.dump(winner_net, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 6
0
# Config for FeedForwardNetwork.
config = neat.config.Config(neat.genome.DefaultGenome,
                            neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_neat_pole_balancing')


# Use the gym_runner to run this experiment using NEAT.
def run(gens, env):
    winner, stats = run_neat(gens, env, 500, config)
    print("neat_pole_balancing done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("CartPole-v1")

    # Run!
    winner = run(500, env)[0]

    # Save net if wished reused and draw it + winner to file.
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    draw_net(winner_net, filename="neat_pole_balancing_winner")
    with open('neat_pole_balancing_winner.pkl', 'wb') as output:
        pickle.dump(winner_net, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 7
0
import pickle as pickle
# from __future__ import print_function

import copy
import os

import pandas as pd
from pureples.shared.visualize import draw_net

from sklearn.model_selection import train_test_split
import numpy as np
import visualize
import sklearn.metrics
import neat

# winner_net = neat.nn.RecurrentNetwork.create(winner, config)
filename = 'network_brake'
with open(filename + '.pkl', 'rb') as input:
    winner_net = pickle.load(input)
draw_net(winner_net, filename=filename)
Exemplo n.º 8
0
                            neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_cppn_mountain_car')


def run(gens, env):
    winner, stats = run_es(gens, env, 200, config, params, sub, max_trials=0)
    print("es_hyperneat_mountain_car_small done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("MountainCar-v0")

    # Run!
    winner = run(200, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    net = network.create_phenotype_network(
        filename="es_hyperneat_mountain_car_small_winner")
    draw_net(cppn, filename="es_hyperneat_mountain_car_small_cppn")
    with open('es_hyperneat_mountain_car_small_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 9
0
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("es_hyperneat_xor_medium done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    winner = run(300)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    winner_net = network.create_phenotype_network(filename='es_hyperneat_xor_medium_winner.png')  # This will also draw winner_net.
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs + (1.0,)
        winner_net.reset()
        for i in range(network.activations):
            output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(inputs, expected, output))

    # Save CPPN if wished reused and draw it to file.
    draw_net(cppn, filename="es_hyperneat_xor_medium_cppn")
    with open('es_hyperneat_xor_medium_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)

Exemplo n.º 10
0
    # Create population and train the network. Return winner of network running 100 episodes.
    stats_one = neat.statistics.StatisticsReporter()
    pop = ini_pop(None, stats_one, config)
    pop.run(eval_fitness, gens)

    stats_ten = neat.statistics.StatisticsReporter()
    pop = ini_pop((pop.population, pop.species, 0), stats_ten, config)
    trials = 10
    winner_ten = pop.run(eval_fitness, gens)

    if max_trials is 0:
        return winner_ten, (stats_one, stats_ten)

    stats_hundred = neat.statistics.StatisticsReporter()
    pop = ini_pop((pop.population, pop.species, 0), stats_hundred, config)
    trials = max_trials
    winner_hundred = pop.run(eval_fitness, gens)
    return winner_hundred, (stats_one, stats_ten, stats_hundred)


logger = logging.getLogger()
logger.setLevel(logging.INFO)

winner = run(100, 1000000, config, params, sub)[0]

cppn = neat.nn.FeedForwardNetwork.create(winner, config)
network = ESNetwork(sub, cppn, params)
net = network.create_phenotype_network(filename="es_hyperneat_2048_winner")
draw_net(cppn, filename="es_hyperNEAT_2048_cppn")
with open('es_hyperNEAT_2048_cppn.pkl', 'wb') as output:
    pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 11
0
                            neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_cppn_pole_balancing')


# Use the gym_runner to run this experiment using HyperNEAT.
def run(gens, env):
    winner, stats = run_hyper(gens, env, 500, config, sub, activations)
    print("hyperneat_polebalancing done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("CartPole-v1")

    # Run!
    winner = run(100, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    winner_net = create_phenotype_network(cppn, sub)
    draw_net(cppn, filename="hyperneat_pole_balancing_cppn")
    with open('hyperneat_pole_balancing_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
    draw_net(winner_net, filename="hyperneat_pole_balancing_winner")
Exemplo n.º 12
0
def run(gens, env):
    """
    Run the pole balancing task using the Gym environment
    Returns the winning genome and the statistics of the run.
    """
    winner, stats = run_hyper(gens, env, 500, CONFIG, SUBSTRATE, ACTIVATIONS)
    print("hyperneat_polebalancing done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    LOGGER = logging.getLogger()
    LOGGER.setLevel(logging.INFO)
    ENVIRONMENT = gym.make("CartPole-v1")

    # Run! Only relevant to look at the winner.
    WINNER = run(100, ENVIRONMENT)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    CPPN = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    WINNER_NET = create_phenotype_network(CPPN, SUBSTRATE)
    draw_net(
        CPPN, filename="pureples/experiments/pole_balancing/hyperneat_pole_balancing_cppn")
    with open('pureples/experiments/pole_balancing/hyperneat_pole_balancing_cppn.pkl', 'wb') as output:
        pickle.dump(CPPN, output, pickle.HIGHEST_PROTOCOL)
    draw_net(
        WINNER_NET, filename="pureples/experiments/pole_balancing/hyperneat_pole_balancing_winner")
Exemplo n.º 13
0
    #winner = pop.run(eval_fitness, gens)
    try:
        winner = pop.run(eval_parallel, gens)
        return winner, stats
    except multiprocessing.context.TimeoutError:
        os.system('make die')
        os._exit()


def run(gens):
    winner, stats = run_hyper(gens, config, sub, activations)
    print("hyperneat_mario done")
    return winner, stats


if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)

    # Run!
    winner = run(700)[0]

    # Save CPPN and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    net = create_phenotype_network(cppn, sub)
    draw_net(cppn, filename="hyperneat_mario_cppn")
    draw_net(net, filename="hyperneat_mario_winner")
    with open('hyperneat_mario_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 14
0
    winner, stats = run_es(gens,
                           env,
                           200,
                           CONFIG,
                           params(version),
                           SUBSTRATE,
                           max_trials=0)
    print(f"es_hyperneat_mountain_car_{VERSION_TEXT} done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    LOGGER = logging.getLogger()
    LOGGER.setLevel(logging.INFO)
    ENVIRONMENT = gym.make("MountainCar-v0")

    # Run! Only relevant to look at the winner.
    WINNER = run(200, ENVIRONMENT, VERSION)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    CPPN = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    NETWORK = ESNetwork(SUBSTRATE, CPPN, params)
    NET = NETWORK.create_phenotype_network(
        filename=f"es_hyperneat_mountain_car_{VERSION_TEXT}_winner")
    draw_net(CPPN, filename=f"es_hyperneat_mountain_car_{VERSION_TEXT}_cppn")
    with open(f'es_hyperneat_mountain_car_{VERSION_TEXT}_cppn.pkl',
              'wb') as output:
        pickle.dump(CPPN, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 15
0
    winner = pop.run(eval_fitness, gens)
    print("hyperneat_xor done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    WINNER = run(300)[0]  # Only relevant to look at the winner.
    print('\nBest genome:\n{!s}'.format(WINNER))

    # Verify network output against training data.
    print('\nOutput:')
    CPPN = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    WINNER_NET = create_phenotype_network(CPPN, SUBSTRATE)

    for inputs, expected in zip(XOR_INPUTS, XOR_OUTPUTS):
        new_input = inputs + (1.0,)
        WINNER_NET.reset()

        for i in range(ACTIVATIONS):
            output = WINNER_NET.activate(new_input)

        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save CPPN if wished reused and draw it to file along with the winner.
    with open('pureples/experiments/xor/hyperneat_xor_cppn.pkl', 'wb') as output:
        pickle.dump(CPPN, output, pickle.HIGHEST_PROTOCOL)
    draw_net(CPPN, filename="pureples/experiments/xor/hyperneat_xor_cppn")
    draw_net(WINNER_NET, filename="pureples/experiments/xor/hyperneat_xor_winner")
Exemplo n.º 16
0
# fitness function.
def run(gens):
    pop = neat.population.Population(config)
    stats = neat.statistics.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("neat_xor done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    winner = run(300)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs + (1.0, )
        output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save net if wished reused and draw it to a file.
    with open('winner_neat_xor.pkl', 'wb') as output:
        pickle.dump(winner_net, output)
    draw_net(winner_net, filename="neat_xor_winner")
Exemplo n.º 17
0
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("es_hyperneat_xor_large done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    winner = run(300)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    winner_net = network.create_phenotype_network(filename='es_hyperneat_xor_large_winner.png')  # This will also draw winner_net.
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs + (1.0,)
        winner_net.reset()
        for i in range(network.activations):
            output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(inputs, expected, output))

    # Save CPPN if wished reused and draw it to file.
    draw_net(cppn, filename="es_hyperneat_xor_large_cppn")
    with open('es_hyperneat_xor_large_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)

Exemplo n.º 18
0
    print(f"es_hyperneat_polebalancing_{VERSION_TEXT} done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    LOGGER = logging.getLogger()
    LOGGER.setLevel(logging.INFO)
    ENVIRONMENT = gym.make("CartPole-v1")

    # Run! Only relevant to look at the winner.
    WINNER = run(100, ENVIRONMENT, VERSION)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    CPPN = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    NETWORK = ESNetwork(SUBSTRATE, CPPN, params(VERSION))
    NET = NETWORK.create_phenotype_network(
        filename=
        f"pureples/experiments/pole_balancing/es_hyperneat_pole_balancing_{VERSION_TEXT}_winner"
    )
    draw_net(
        CPPN,
        filename=
        f"pureples/experiments/pole_balancing/es_hyperneat_pole_balancing_{VERSION_TEXT}_cppn"
    )
    with open(
            f'pureples/experiments/pole_balancing/es_hyperneat_pole_balancing_{VERSION_TEXT}_cppn.pkl',
            'wb') as output:
        pickle.dump(CPPN, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 19
0
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_cppn_pole_balancing')


# Use the gym_runner to run this experiment using ES-HyperNEAT.
def run(gens, env):
    winner, stats = run_es(gens, env, 500, config, params, sub)
    print("es_hyperneat_polebalancing_large done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("CartPole-v1")

    # Run!
    winner = run(100, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    net = network.create_phenotype_network(
        filename="es_hyperneat_pole_balancing_large_winner")
    draw_net(cppn, filename="es_hyperneat_pole_balancing_large_cppn")
    with open('es_hyperneat_pole_balancing_large_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 20
0
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("es_hyperneat_xor_small done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    winner = run(300)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    winner_net = network.create_phenotype_network(filename='es_hyperneat_xor_small_winner.png')  # This will also draw winner_net.
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs + (1.0,)
        winner_net.reset()
        for i in range(network.activations):
            output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(inputs, expected, output))

    # Save CPPN if wished reused and draw it to file.
    draw_net(cppn, filename="es_hyperneat_xor_small_cppn")
    with open('es_hyperneat_xor_small_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)

Exemplo n.º 21
0
# Config for NEAT.
config = neat.config.Config(neat.genome.DefaultGenome,
                            neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_neat_mountain_car')


def run(gens, env):
    winner, stats = run_neat(gens, env, 200, config, max_trials=0)
    print("neat_mountain_car done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("MountainCar-v0")

    # Run!
    winner = run(20, env)[0]

    # Save net if wished reused and draw it to file.
    net = neat.nn.RecurrentNetwork.create(winner, config)
    draw_net(net, filename="neat_mountain_car_winner")
    with open('neat_mountain_car_winner.pkl', 'wb') as output:
        pickle.dump(net, output, pickle.HIGHEST_PROTOCOL)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(task.eval_fitness, gens)
    print("es trade god summoned")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    task = PurpleTrader(8)
    #print(task.trial_run())
    winner = run_pop(task, 89)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    [cppn] = create_cppn(winner, task.config, task.leaf_names, ['cppn_out'])
    network = ESNetwork(task.subStrate, cppn, task.params)
    with open('es_trade_god_cppn_3d.pkl', 'wb') as output:
        pickle.dump(winner, output)
    #draw_net(cppn, filename="es_trade_god")
    winner_net = network.create_phenotype_network_nd(
        'dabestest.png')  # This will also draw winner_net.

    # Save CPPN if wished reused and draw it to file.
    draw_net(cppn, filename="es_trade_god")
    '''
    for x in range(len(task.hs.hist_shaped[0])):
        print(task.hs.hist_shaped[1][x][3],task.hs.hist_shaped[0][x][3])
    '''
Exemplo n.º 23
0
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("hyperneat_xor done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    winner = run(300)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    winner_net = create_phenotype_network(cppn, sub)
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs + (1.0, )
        winner_net.reset()
        for i in range(activations):
            output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save CPPN if wished reused and draw it to file along with the winner.
    with open('hyperneat_xor_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
    draw_net(cppn, filename="hyperneat_xor_cppn")
    draw_net(winner_net, filename="hyperneat_xor_winner")
                            neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_cppn_mountain_car')


def run(gens, env):
    winner, stats = run_es(gens, env, 200, config, params, sub, max_trials=0)
    print("es_hyperneat_mountain_car_large done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("MountainCar-v0")

    # Run!
    winner = run(200, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    net = network.create_phenotype_network(
        filename="es_hyperneat_mountain_car_large_winner")
    draw_net(cppn, filename="es_hyperneat_mountain_car_large_cppn")
    with open('es_hyperneat_mountain_car_large_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 25
0
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_cppn_pole_balancing')


# Use the gym_runner to run this experiment using ES-HyperNEAT.
def run(gens, env):
    winner, stats = run_es(gens, env, 500, config, params, sub)
    print("es_hyperneat_polebalancing_medium done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("CartPole-v1")

    # Run!
    winner = run(100, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    net = network.create_phenotype_network(
        filename="es_hyperneat_pole_balancing_medium_winner")
    draw_net(cppn, filename="es_hyperneat_pole_balancing_medium_cppn")
    with open('es_hyperneat_pole_balancing_medium_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output)
Exemplo n.º 26
0
                            neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet,
                            neat.stagnation.DefaultStagnation,
                            'config_cppn_mountain_car')


def run(gens, env):
    winner, stats = run_es(gens, env, 200, config, params, sub, max_trials=0)
    print("es_hyperneat_mountain_car_medium done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("MountainCar-v0")

    # Run!
    winner = run(200, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    net = network.create_phenotype_network(
        filename="es_hyperneat_mountain_car_medium_winner")
    draw_net(cppn, filename="es_hyperneat_mountain_car_medium_cppn")
    with open('es_hyperneat_mountain_car_medium_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 27
0
    Returns the winning genome and the statistics of the run.
    """
    winner, stats = run_hyper(gens,
                              env,
                              200,
                              CONFIG,
                              SUBSTRATE,
                              ACTIVATIONS,
                              max_trials=0)
    print("hyperneat_mountain_car done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    LOGGER = logging.getLogger()
    LOGGER.setLevel(logging.INFO)
    ENVIRONMENT = gym.make("MountainCar-v0")

    # Run! Only relevant to look at the winner.
    WINNER = run(200, ENVIRONMENT)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    NET = create_phenotype_network(cppn, SUBSTRATE)
    draw_net(cppn, filename="hyperneat_mountain_car_cppn")
    draw_net(NET, filename="hyperneat_mountain_car_winner")
    with open('hyperneat_mountain_car_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 28
0
config = neat.config.Config(neat.genome.DefaultGenome, neat.reproduction.DefaultReproduction,
                            neat.species.DefaultSpeciesSet, neat.stagnation.DefaultStagnation,
                            'config_cppn_pole_balancing')


# Use the gym_runner to run this experiment using ES-HyperNEAT.
def run(gens, env):
    winner, stats = run_es(gens, env, 500, config, params, sub)
    print("es_hyperneat_polebalancing_small done") 
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    env = gym.make("CartPole-v1")

    # Run!
    winner = run(100, env)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    network = ESNetwork(sub, cppn, params)
    net = network.create_phenotype_network(filename="es_hyperneat_pole_balancing_small_winner")
    draw_net(cppn, filename="es_hyperneat_pole_balancing_small_cppn")
    with open('es_hyperneat_pole_balancing_small_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)

Exemplo n.º 29
0
    WINNER = run(300, VERSION)[0]  # Only relevant to look at the winner.
    print('\nBest genome:\n{!s}'.format(WINNER))

    # Verify network output against training data.
    print('\nOutput:')
    CPPN = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    NETWORK = ESNetwork(SUBSTRATE, CPPN, DYNAMIC_PARAMS)
    # This will also draw winner_net.
    WINNER_NET = NETWORK.create_phenotype_network(
        filename=
        f'pureples/experiments/xor/es_hyperneat_xor_{VERSION_TEXT}_winner.png')

    for inputs, expected in zip(XOR_INPUTS, XOR_OUTPUTS):
        new_input = inputs + (1.0, )
        WINNER_NET.reset()

        for i in range(NETWORK.activations):
            output = WINNER_NET.activate(new_input)

        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save CPPN if wished reused and draw it to file.
    draw_net(CPPN,
             filename=
             f"pureples/experiments/xor/es_hyperneat_xor_{VERSION_TEXT}_cppn")
    with open(
            f'pureples/experiments/xor/es_hyperneat_xor_{VERSION_TEXT}_cppn.pkl',
            'wb') as output:
        pickle.dump(CPPN, output, pickle.HIGHEST_PROTOCOL)
Exemplo n.º 30
0
    """
    pop = neat.population.Population(CONFIG)
    stats = neat.statistics.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("neat_xor done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    WINNER = run(300)[0]  # Only relevant to look at the winner.
    print('\nBest genome:\n{!s}'.format(WINNER))

    # Verify network output against training data.
    print('\nOutput:')
    WINNER_NET = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)

    for inputs, expected in zip(XOR_INPUTS, XOR_OUTPUTS):
        new_input = inputs + (1.0, )
        output = WINNER_NET.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save net if wished reused and draw it to a file.
    with open('pureples/experiments/xor/winner_neat_xor.pkl', 'wb') as output:
        pickle.dump(WINNER_NET, output, pickle.HIGHEST_PROTOCOL)
    draw_net(WINNER_NET, filename="pureples/experiments/xor/neat_xor_winner")