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)
Пример #2
0
def run():
    # Determine path to configuration file.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config')
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_path)

    # Demonstration of saving a configuration back to a text file.
    config.save('test_save_config.txt')

    # Demonstration of how to add your own custom activation function.
    # This sinc function will be available if my_sinc_function is included in the
    # config file activation_options option under the DefaultGenome section.
    config.genome_config.add_activation('my_sinc_function', sinc)

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))

    if 1:
        pe = neat.ParallelEvaluator(4, eval_genome)
        winner = pop.run(pe.evaluate, 1000)
    else:
        winner = pop.run(eval_genomes, 1000)


    # Log statistics.
    stats.save()

    # Show output of the most fit genome against a random input.
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = neat.nn.RecurrentNetwork.create(winner, config)
    num_correct = 0
    for n in range(num_tests):
        print('\nRun {0} output:'.format(n))
        seq = [random.choice((0.0, 1.0)) for _ in range(N)]
        winner_net.reset()
        for s in seq:
            inputs = [s, 0.0]
            winner_net.activate(inputs)
            print('\tseq {0}'.format(inputs))

        correct = True
        for s in seq:
            output = winner_net.activate([0, 1])
            print("\texpected {0:1.5f} got {1:1.5f}".format(s, output[0]))
            correct = correct and round(output[0]) == s
        print("OK" if correct else "FAIL")
        num_correct += 1 if correct else 0

    print("{0} of {1} correct {2:.2f}%".format(num_correct, num_tests, num_correct/num_tests))

    node_names = {-1: 'input', -2: 'gate', 0: 'output'}
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=True)
Пример #3
0
 def eval_genomes(self, genomes, config):
     for genome_id, genome in genomes:
         net = neat.nn.FeedForwardNetwork.create(genome, config)
         genome.fitness = self.evalNet(net)
         if self.bestFitness < genome.fitness:
             self.bestFitness = genome.fitness
             print("New Best Fitness: " + str(self.circuit.getFitness()))
             self.circuit.drawResult()
             visualize.plot_stats(self.stats, ylog=False, view=True)
Пример #4
0
def main(config_file='config.txt', save_dir='nets'):

    # Parse command-line arguments
    parser = argparse.ArgumentParser()
    parser.add_argument('-g',
                        '--ngen',
                        type=int,
                        required=False,
                        help='Number of generations to run')
    parser.add_argument('-r',
                        '--reps',
                        type=int,
                        default=10,
                        required=False,
                        help='Number of repetitions per genome')
    parser.add_argument('-v', '--viz', dest='visualize', action='store_true')
    parser.add_argument('-s',
                        '--seed',
                        type=int,
                        required=False,
                        help='Seed for random number generator')
    args = parser.parse_args()

    # Set random seed if indicated
    random.seed(args.seed)

    # Make directory for pickling nets, if it doesn't already exist
    _makedir(save_dir)

    # Load configuration.
    config = CopterConfig(config_file, args.reps)

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

    # Create a parallel fitness evaluator
    pe = neat.ParallelEvaluator(multiprocessing.cpu_count(), eval_genome)

    # Runn for number of generations specified in config file
    winner = p.run(pe.evaluate) if args.ngen is None else p.run(
        pe.evaluate, args.ngen)

    # Pickle the winner
    filename = '%s/%f.dat' % (save_dir, winner.fitness)
    print('Saving %s' % filename)
    pickle.dump((winner, config), open(filename, 'wb'))

    # Visualize results if indicated
    if args.visualize:
        visualize.plot_stats(stats, ylog=False, view=True)
        visualize.plot_species(stats, view=True)
Пример #5
0
def run(config_file):
    global env
    # 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-9')
    # 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(10))
    reward_list = []
    #for j in range(20):
    # Run for up to 300 generations.
    winner = p.run(eval_genomes, 300)
    print(reward_list)
    # Display the winning genome.
    print('\nBest genome:\n{!s}'.format(winner))

    # Show output of the most fit genome against training data.
    print('\nOutput:')

    visualize.draw_net(config, winner, True)
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)
    visualize.draw_net(config,
                       winner,
                       view=True,
                       filename="winner-feedforward-evabled-pruneg.gv",
                       show_disabled=False,
                       prune_unused=True)
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    final_reward = 0
    #env = wrappers.Monitor(env, '/mnt/c/Users/bc/Documents/EA/neat/BipedalWalker/movies', force=True)
    observation = env.reset()
    while True:
        action = winner_net.activate(observation)
        action = np.clip(action, -1, 1)
        observation, reward, done, info = env.step(action)
        final_reward += reward
        if done:
            print("final_reward :", final_reward)
            break
        #winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    for n, g in enumerate([winner]):
        visualize.draw_net(config,
                           g,
                           view=False,
                           filename=str(j) + "-net-enabled-pruned.gv",
                           show_disabled=False,
                           prune_unused=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.
    # True == show all species, False == don't show species
    p.add_reporter(neat.StdOutReporter(False))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)
    p.add_reporter(neat.Checkpointer(5))

    # Stop running after n=n_generations
    # if n=None runs until solution is found
    winner = p.run(eval_genomes, n=n_generations)

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

    # Show output of the most fit genome against testing data.
    print('\nTest Output, Actual, Diff:')
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)

    predicted = []
    for xi, xo in zip(test_x, test_y):
        output = winner_net.activate(xi)
        predicted.append(output)

    node_names = {
        -1: '4',
        -2: '3',
        -3: '2',
        -4: '1',
        -5: '0',
        0: 'Predict Change'
    }
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)

    # ? save?
    #p = neat.Checkpointer.restore_checkpoint('neat-checkpoint')
    #p.run(eval_genomes, n_evaluate)

    # plot predictions vs actual
    plt.plot(test_y, 'g', label='Actual')
    plt.plot(predicted, 'r-', label='Predicted')
    plt.title('Test Data')
    plt.legend()
    plt.show()
Пример #7
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)

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

    # 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))
    
    
    df=pd.read_csv(inputs + 'bpusd_test_X.csv', sep=' ',header=None)
    new_test_X = df.values
    X_test_inputs = []
    for i in range(len(new_test_X)):
        X_test_inputs.append(tuple(new_test_X[i]))

    predictions_enn = []
    for xi in X_test_inputs:
        output = winner_net.activate(xi)
        predictions_enn.append(output)

    np.savetxt(inputs + 'predictions_enn.csv', np.array(predictions_enn), delimiter=',')
    real_y=pd.read_csv(inputs + 'bpusd_test_Y.csv', sep=' ',header=None)
    mse = np.sum((np.array(real_y) - predictions_enn)**2)/(len(predictions_enn))
    mae = np.average(np.abs(np.array(real_y) - predictions_enn))
    print("MSE:", mse)
    print("MAE:", mae)

    node_names = {-1: "t-7", -2: "t-6", -3: "t-5", -4:"t-4", -5:"t-3", -6:"t-2", -7:"t-1",0: "Target"}
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=False)
    visualize.plot_species(stats, view=False)
Пример #8
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(300))

    # Run for up to 300 generations.
    start_time = time.time()

    winner = p.run(eval_genomes, 5000)

    stats.save()

    print(stats.best_genomes(5))
    # 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 , expected output {!r}, got {!r}".format(xo, output))
    # test on testset
    fitness_test = 0

    for xi_test, xo_test in zip(test_inputs, test_outputs):
        output_test = winner_net.activate(xi_test)

        if abs(output_test[0] - xo_test[0]) < 0.1:
            fitness_test += 1
        else:
            fitness_test += 0

    test_evaluation = fitness_test / float(len(test_outputs))
    print("\nTest evaluation: {!r}".format(test_evaluation))
    print("--- %s seconds ---" % (time.time() - start_time))

    node_names = {0: 'Output'}
    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)
Пример #9
0
def run():
    # get config
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config-feedforward-icegame')
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_path)

    # set population and set reporting options
    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))
    # Checkpoint every x gen or y minutes
    pop.add_reporter(
        neat.Checkpointer(500, 900,
                          "checkpoints/" + fn_results + "-checkpoint"))

    #winner = pop.run(eval_genomes, NUM_GEN) # non-parallel
    pe = neat.ParallelEvaluator(NUM_WORKERS, eval_genome)  # parallel
    winner = pop.run(pe.evaluate, NUM_GEN)

    # save network
    with open("results/winner-pickle-" + fn_results, 'wb') as f:
        pickle.dump(winner, f)

    #print(winner)

    visualize.plot_stats(stats,
                         ylog=True,
                         view=True,
                         filename="results/" + fn_results + "-fitness.svg")
    visualize.plot_species(stats,
                           view=True,
                           filename="results/" + fn_results +
                           "-speciation.svg")

    if DRAW_NETS:
        visualize.draw_net(config,
                           winner,
                           view=True,
                           filename="results/winner-" + fn_results + ".net")
        visualize.draw_net(config,
                           winner,
                           view=True,
                           filename="results/winner-" + fn_results +
                           "-enabled.net",
                           show_disabled=False)
        visualize.draw_net(config,
                           winner,
                           view=True,
                           filename="results/winner-" + fn_results +
                           "-pruned.net",
                           show_disabled=False,
                           prune_unused=True)
Пример #10
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:')
    #Show the winning Snake
    winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    champ = Snake(winner_net)
    champ.play()
    '''
    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',
        -3: 'C',
        -4: 'D',
        -5: 'E',
        -6: 'F',
        -7: 'G',
        -8: 'H',
        -9: 'I',
        -10: 'J',
        -11: 'K',
        -12: 'L',
        0: 'DOWN',
        1: 'UP',
        2: 'LEFT',
        3: 'RIGHT'
    }
    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():
    # 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, 'config-ctrnn')
    config = neatfast.Config(neatfast.DefaultGenome,
                             neatfast.DefaultReproduction,
                             neatfast.DefaultSpeciesSet,
                             neatfast.DefaultStagnation, config_path)

    pop = neatfast.Population(config)
    stats = neatfast.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neatfast.StdOutReporter(True))

    if 0:
        winner = pop.run(eval_genomes)
    else:
        pe = neatfast.ParallelEvaluator(4, eval_genome)
        winner = pop.run(pe.evaluate)

    # Save the winner.
    with open('winner-ctrnn', 'wb') as f:
        pickle.dump(winner, f)

    print(winner)

    visualize.plot_stats(stats,
                         ylog=True,
                         view=True,
                         filename="ctrnn-fitness.svg")
    visualize.plot_species(stats, view=True, filename="ctrnn-speciation.svg")

    node_names = {-1: 'x', -2: 'dx', -3: 'theta', -4: 'dtheta', 0: 'control'}
    visualize.draw_net(config, winner, True, node_names=node_names)

    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-ctrnn.gv")
    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-ctrnn-enabled.gv",
                       show_disabled=False)
    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-ctrnn-enabled-pruned.gv",
                       show_disabled=False,
                       prune_unused=True)
Пример #12
0
def run():

    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config-feedforward')
    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(4, eval_genome)
    # winner = pop.run(pe.evaluate)
    winner = pop.run(eval_genomes, 50)

    # Save the winner.
    with open("my_network.data", 'wb') as fd:
        pickle.dump((winner, config, stats), fd)
    # with open("my_network.data", 'rb') as fd:
    #     winner, config, stats = pickle.load(fd)

    print(winner)

    visualize.plot_stats(stats,
                         ylog=True,
                         view=True,
                         filename="feedforward-fitness.svg")
    visualize.plot_species(stats,
                           view=True,
                           filename="feedforward-speciation.svg")

    node_names = {-1: 'x', -2: 'dx', -3: 'theta', -4: 'dtheta', 0: 'control'}
    visualize.draw_net(config, winner, True, node_names=node_names)

    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-feedforward.gv")
    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-feedforward-enabled.gv",
                       show_disabled=False)
    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-feedforward-enabled-pruned.gv",
                       show_disabled=False,
                       prune_unused=True)
Пример #13
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))
    stats = neat.statistics.StatisticsReporter()
    p.add_reporter(stats)
    winner = p.run(main, 100)
    visualize.plot_stats(stats)
    visualize.plot_score(score_arr)
Пример #14
0
def run():
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         CONFIG)
    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))
    pop.add_reporter(neat.Checkpointer(5))
    pop.run(eval_genomes, 10)
    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(1000 - 1))

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

    # 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(fizzbuzz_inputs, fizzbuzz_outputs):
        output = winner_net.activate(xi)
        print("input {!r}, expected output {!r}, got {!r}".format(
            xi, xo, output))

    outputList = [
        fizz_buzz(i, winner_net.activate(binary_encode(i, bitLength)))
        for i in range(2**bitLength)
    ]

    print(outputList)

    node_names = {
        -1: 'A',
        -2: 'B',
        -3: 'C',
        -4: 'D',
        -5: 'E',
        -6: 'F',
        -7: 'G',
        0: 'Number',
        1: 'fizz',
        2: 'buzz',
        3: 'fizzbuzz'
    }
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=True)
def run(config_file, generations, binary_file, drawfile, progressfile,
        statsfile):

    #Configuring the agent and the evaluation function
    from eval import eval_one_to_one_3x3
    eval_func = eval_one_to_one_3x3
    #Preprocessing for inputs: none
    in_func = reorder_inputs
    from solve import convert_to_action
    out_func = convert_to_action
    #Preprocessing for output - convert float to boolean

    # Load configuration.
    config = neat.Config(GuidedMapGenome, 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 = Reporters.StatReporterv2()
    p.add_reporter(stats)
    p.add_reporter(Reporters.ProgressTracker(progressfile))
    #p.add_reporter(Reporters.NetRetriever())
    #p.add_reporter(neat.Checkpointer(5))

    # Run for up to 300 generations.
    winner = p.run(make_eval_fun(eval_func, in_func, out_func), 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 = create(winner, config, MAP_SIZE)
    winner_agent = Agent(winner_net, in_func, out_func)
    print("Score in task: {}".format(eval_func(winner_agent)))

    print("Input function: Reorder_inputs")
    print("Output function: convert_to_action")
    render_network.draw_net(winner_net, filename=drawfile)

    #Log the maximum fitness over generations
    from visualize import plot_stats
    plot_stats(stats, False, view=False, filename=statsfile)

    #Uncomment the following if you want to save the network in a binary file
    fp = open(binary_file, 'wb')
    pickle.dump(winner_net, fp)
    fp.close()
Пример #17
0
    def __init__(self):
    
        # Setup the ROS sensor subscriptions
        self.sensor_data = {}
        
        rospy.init_node('neat', anonymous=True)
        
        self.mobility_pub = rospy.Publisher('mobility_type', Bool)
        imu_sub = rospy.Subscriber('/mobile_base/sensors/imu_data', Imu, self.callback, ('imu'))
        cam_sub = rospy.Subscriber('/cam1/raw_image', Image, self.callback, ('camera'))
        lidar_sub = rospy.Subscriber('/scan', LaserScan, self.callback, ('lidar'))
        
        #self.sensor_data['imu'] = 1
        #self.sensor_data['lidar'] = 2
        
        print('Waiting for sensor data to come in...')
        while 'imu' not in self.sensor_data and 'lidar' not in self.sensor_data:
            pass
        print('Ready')
        # Setup NEAT
        local_dir = os.path.dirname(__file__)
        config_path = os.path.join(local_dir, 'config-ann')

        config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                     neat.DefaultSpeciesSet, neat.DefaultStagnation,
                     config_path)

        population = neat.Population(config)

        population.add_reporter(neat.StdOutReporter(True))
        stats = neat.StatisticsReporter()
        population.add_reporter(stats)
        population.add_reporter(neat.Checkpointer(5)) # Saves every 5 generations as checkpoints to fall back on

        # Pass the eval_genomes function to the population object
        # 'eval_genomes' will be called on each individual in the population to determine their
        # fitness each generation
        # 300 is the number of generations to run. This should be configured manually and tweaked for best results
        most_fit_genome = population.run(self.eval_genomes, 300)
        most_fit_network = neat.nn.FeedForwardNetwork.create(most_fit_genome, config)

        # Should probably do a test run with the winning genome's network here to see how well it learned

        # Write the genome to a file maybe to save it for later
        # That way when we are not in a learning period, we can just load up the genome,
        # create the ANN from it, and use it to modify the mobility mechanism

        # Outputting some graphs with the network, and some graphs on fitness and speciation
        visualize.draw_net(config, most_fit_genome, True)
        visualize.plot_stats(stats, ylog=False, view=True)
        visualize.plot_species(stats, view=True)
Пример #18
0
def evolutionary_driver(n=5):
    # Load configuration.
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         'config')

    # 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 until we achive n.
    winner = p.run(eval_genomes, n=n)

    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)

    # winner_nn = neat.nn.RecurrentNetwork.create(winner, config)
    #
    # test_suite = testing.testSuite[0]
    # test_suite_to_coverage = []
    #
    # for test_case in test_suite:
    #     input = (
    #         int(test_case[1][0]),
    #         int(test_case[1][1]),
    #         int(test_case[1][2]),
    #         (int(test_case[1][0]) * int(test_case[1][0])),
    #         (int(test_case[1][1]) * int(test_case[1][1])),
    #         (int(test_case[1][2]) * int(test_case[1][2])),
    #         (int(test_case[1][0]) * int(test_case[1][1])),
    #         (int(test_case[1][1]) * int(test_case[1][2])),
    #         (int(test_case[1][2]) * int(test_case[1][0])),
    #     )
    #     output_from_nn = winner_nn.activate(input)
    #     output = TriangleApp.TriangleTester(int(output_from_nn[0]), int(output_from_nn[1]), int(output_from_nn[2]))
    #
    #     output_from_nn = ['TriangleTester', (int(output_from_nn[0]), int(output_from_nn[1]), int(output_from_nn[2])),
    #                       output]
    #     test_suite_to_coverage.append(output_from_nn)
    #
    # print(test_suite_to_coverage)
    # result = Coverage.report(test_suite_to_coverage)
    # print(result)

    # dump winning genome
    pickle.dump(winner, open('winner.pkl', 'wb'))
Пример #19
0
def run(config_file):
    # Load configuration.
    config = neat.Config(multiarm.BanditGenome, multiarm.BanditReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_file)

    # Create the population, which is the top-level object for a NEAT run.
    p = neat.Population(config)

    # Add bandit here
    # bandit = bandit3.RandomMutator(rates=[0.2, 0.1, 0.8, 0.5, 0.2, 0.9])
    # bandit = bandit4.PrMutator(rates=[0.2, 0.1, 0.8, 0.5, 0.2, 0.9], single=True)
    # bandit = bandit4.EpsMutator(plays=[1])
    bandit = bandit4.TSMutator()
    p.reproduction.bandit_type(bandit)

    # Add a stdout reporter to show progress in the terminal.
    p.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)
    br = custom_reporter.BanditPrintReporter()
    p.add_reporter(br)
    # p.add_reporter(neat.Checkpointer(5))

    # Run for up to 200 generations.
    winner = p.run(eval_genomes, 100)

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

    # for _ in range(10):
    #     x,y = (random(), random())
    #     output = winner_net.activate((x,y))
    #     print("input {!r}, expected output {!r}, got {!r}".format((x,y), (true_xor(x,y),), output))

    # node_names = {-1:'A', -2: 'B', 0:'A XOR B'}
    # visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.draw_net(config, winner, view=True)
    visualize.draw_net(config,
                       stats.best_genome(),
                       view=True,
                       filename="best_genome")
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)
Пример #20
0
def run():
    # Determine path to configuration file.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config-gru')
    config = neat.Config(neat.gru.GRUGenome, 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))

    if 1:
        pe = neat.ParallelEvaluator(4, eval_genome)
        winner = pop.run(pe.evaluate)
    else:
        winner = pop.run(eval_genomes)

    # Log statistics.
    stats.save()

    # Show output of the most fit genome against a random input.
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = nw.create(winner, config)
    num_correct = 0
    for n in range(num_tests):
        print('\nRun {0} output:'.format(n))
        seq = [random.choice((0.0, 1.0)) for _ in range(N)]
        winner_net.reset()
        for s in seq:
            inputs = [s, 0.0]
            winner_net.activate(inputs)
            print('\tseq {0}'.format(inputs))

        correct = True
        for s in seq:
            output = winner_net.activate([0, 1])
            print("\texpected {0:1.5f} got {1:1.5f}".format(s, output[0]))
            correct = correct and round(output[0]) == s
        print("OK" if correct else "FAIL")
        num_correct += 1 if correct else 0

    print("{0} of {1} correct {2:.2f}%".format(num_correct, num_tests,
                                               num_correct / num_tests))

    node_names = {-1: 'input', -2: 'gate', 0: 'output'}
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=True)
Пример #21
0
def run_scenario():
    # Create configuration object
    config = Config(genome.DefaultGenome, reproduction.DefaultReproduction,
                    species.DefaultSpeciesSet, stagnation.DefaultStagnation,
                    "../config files/config_traverse")

    # Create a population of genomes using the configuration
    pop = Population(config)

    # Set maximum number of episodes
    episodes = 50

    # Add reporters to keep track of the performance of the genomes
    pop.add_reporter(StdOutReporter(True))
    stats = StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(
        Checkpointer(5, 5, "../checkpoints/walking2/walking-checkpoint-"))

    # Run NEAT and retrieve the highest performing genomes
    winner = pop.run(fitness_function, episodes)

    # Save the highest performing genome
    save_genome(winner, "moving-genome")

    # 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 the game once complete
    game.close()
Пример #22
0
def run():
    # Determine path to configuration file.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config')
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         config_path)

    # Demonstration of saving a configuration back to a text file.
    # config.save('test_save_config.txt')

    pop = neat.Population(config)
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))

    winner = pop.run(eval_genomes, NUM_OF_GENERATIONS)

    # Log statistics.
    stats.save()

    # Show output of the most fit genome against a random input.
    print('\nBest genome:\n{!s}'.format(winner))
    # print('\nOutput:')
    # #winner_net = neat.nn.RecurrentNetwork.create(winner, config)
    # num_correct = 0
    # for n in range(num_tests):
    #     print('\nRun {0} output:'.format(n))
    #     seq = [random.choice((0.0, 1.0)) for _ in range(N)]
    #     winner_net.reset()
    #     for s in seq:
    #         inputs = [s, 0.0]
    #         winner_net.activate(inputs)
    #         print('\tseq {0}'.format(inputs))

    #     correct = True
    #     for s in seq:
    #         output = winner_net.activate([0, 1])
    #         print("\texpected {0:1.5f} got {1:1.5f}".format(s, output[0]))
    #         correct = correct and round(output[0]) == s
    #     print("OK" if correct else "FAIL")
    #     num_correct += 1 if correct else 0

    # print("{0} of {1} correct {2:.2f}%".format(num_correct, num_tests, num_correct/num_tests))

    # node_names = {-1: 'input', -2: 'gate', 0: 'output'}
    # visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=False)
    visualize.plot_species(stats, view=False)
Пример #23
0
def main():
    # 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, 'config-feedforward')
    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(4, eval_genome)
    winner = pop.run(pe.evaluate, 10000)

    # Save the winner.
    with open('winner-feedforward', 'wb') as f:
        pickle.dump(winner, f)

    print(winner)

    visualize.plot_stats(stats,
                         ylog=True,
                         view=True,
                         filename="feedforward-fitness.svg")
    visualize.plot_species(stats,
                           view=True,
                           filename="feedforward-speciation.svg")

    node_names = {
        -1: 'x',
        -2: 'y',
        -3: 'l',
        -4: 'f',
        -5: 'r',
        -6: 'dir_x',
        -7: 'dir_y',
        0: 'forward',
        1: 'left',
        2: 'right'
    }

    visualize.draw_net(config,
                       winner,
                       view=True,
                       node_names=node_names,
                       filename="winner-feedforward.gv")
Пример #24
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-44')
    # 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, 50)

    # 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)
    output = play_game(winner_net, 50)
    print("Winner Net results:", output)
    node_names = {
        -1: 'Prob',
        -2: 'Blind',
        -3: 'Pot',
        -4: 'Tocall',
        -5: 'River',
        -6: 'Turn',
        -7: 'Flop',
        -8: 'Open',
        -9: 'OppoRaise',
        -10: 'OppoCall',
        -11: 'Oppocheck',
        0: 'Fold',
        1: 'Call',
        2: 'Raise'
    }
    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)
    stats.save()
Пример #25
0
def plot_graphs(config, stats, display=False, winner=None):
    print("Plotting graphs")
    visualize.plot_stats(stats,
                         ylog=False,
                         view=display,
                         filename=params.testid + "-fitness.svg")
    visualize.plot_species(stats,
                           view=display,
                           filename=params.testid + "-species.svg")
    if winner is not None:
        visualize.draw_net(config,
                           winner,
                           view=display,
                           node_names=None,
                           filename=params.testid + "-net.svg")
Пример #26
0
def generate_stat_plots(stats, winner):
    if GENERATE_PLOTS:
        print("Plotting stats...")
        visualize.draw_net(config,
                           winner,
                           view=False,
                           node_names=None,
                           filename=PLOT_FILENAME_PREFIX + "net")
        visualize.plot_stats(stats,
                             ylog=False,
                             view=False,
                             filename=PLOT_FILENAME_PREFIX + "fitness.svg")
        visualize.plot_species(stats,
                               view=False,
                               filename=PLOT_FILENAME_PREFIX + "species.svg")
Пример #27
0
def run():
    # Determine path to configuration file.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'config')
    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(multiprocessing.cpu_count(), eval_genome)
    winner = pop.run(pe.evaluate, 1000)

    # Log statistics.
    stats.save()

    # Show output of the most fit genome against a random input.
    print('\nBest genome:\n{!s}'.format(winner))
    print('\nOutput:')
    winner_net = neat.nn.RecurrentNetwork.create(winner, config)
    num_correct = 0
    for n in range(num_tests):
        print('\nRun {0} output:'.format(n))

        num_inputs = random.randint(1, max_inputs)
        num_ignore = random.randint(0, max_ignore)

        seq = [random.choice((0.0, 1.0)) for _ in range(num_inputs)]
        winner_net.reset()
        outputs = test_network(winner_net, seq, num_ignore)

        correct = True
        for i, o in zip(seq, outputs):
            print("\texpected {0:1.5f} got {1:1.5f}".format(i, o[0]))
            correct = correct and round(o[0]) == i
        print("OK" if correct else "FAIL")
        num_correct += 1 if correct else 0

    print("{0} of {1} correct {2:.2f}%".format(num_correct, num_tests, 100.0 *
                                               num_correct / num_tests))

    node_names = {-1: 'input', -2: 'record', -3: 'play', 0: 'output'}
    visualize.draw_net(config, winner, True, node_names=node_names)
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)
Пример #28
0
def run(config_file):
    config = neat.Config(
        neat.DefaultGenome,
        neat.DefaultReproduction,
        neat.DefaultSpeciesSet,
        neat.DefaultStagnation,
        config_file
    )

    # crete population, which is the top-level object for a NEAT run
    pop = neat.Population(config)
    # add a stdout reporter to show progress in terminal
    pop.add_reporter(neat.StdOutReporter(True))
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.Checkpointer(5))

    # run for up to 500 generations
    winner = pop.run(eval_genomes, 10000)

    # display the winning genome
    print(f"\nBest genome:\n{winner}")

    win_net = neat.nn.FeedForwardNetwork.create(winner, config)
    holdings = 1
    cash = 0
    for day in tsla:
        close = day["close"]
        macd = day["macd"]
        signal = day["signal"]
        diff = day["diff"]

        buy, hold, sell = win_net.activate((holdings, macd, signal, diff))

        if buy > hold and buy > sell:
            if holdings == 0:
                cash -= close
                holdings += 1
                print(f"bought at {close}, total: {cash + (holdings * close)}")
        if sell > hold and sell > buy:
            if holdings == 1:
                cash += close
                holdings -= 1
                print(f"sold at {close}, total: {cash + (holdings * close)}")

    visualize.draw_net(config, winner, True)
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)
Пример #29
0
def bot(config_path):
    global game_state
    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)
    # p.add_reporter(neat.Checkpointer(5))

    # Run for up to 50 generations.
    game_state = "Playing"
    winner = p.run(playing, generations)

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

    import os
    os.environ[
        "PATH"] += os.pathsep + r'C:\Program Files (x86)\Graphviz2.38\bin'

    node_names = {1: "velx", 2: "vely", 3: "x", 4: "y", 5: "grounded"}
    visualize.draw_net(config, winner, view=False)
    # visualize.draw_net(config, winner, node_names=node_names, view=False)
    visualize.plot_stats(stats, ylog=False, view=False)
    visualize.plot_species(stats, view=False)

    with open("saved_net", 'wb') as pickle_file:
        pickle.dump(winner, pickle_file)

    # with open("saved_net", 'rb') as pickle_file:
    #     saved_net = pickle.load(pickle_file)
    #
    # net = neat.nn.feed_forward.create(saved_net, config)
    # playing(net=net)
    #

    # neat.Checkpointer.save_checkpoint(config, p, winner, generation=5)
    # # neat.checkpoint.
    # p = neat.Checkpointer.restore_checkpoint('saved_net')
    # p.run(bot, 10)

    game_state = "Menu"
Пример #30
0
def eval_parallel(genomes, config):
    global gen_counter

    print("Testing generation number {}...".format(gen_counter))

    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 gen_counter > 2:
        stats.save()

    gen_counter += 1
Пример #31
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(False))
    stats = neat.StatisticsReporter()
    p.add_reporter(stats)
    p.add_reporter(neat.Checkpointer(5))
    winner = p.run(eval_genomes, GENERATIONS)
    print "Best Genome {!s}".format(winner)
    # winner_net = neat.nn.FeedForwardNetwork.create(winner, config)
    raw_input('Press ENTER to run the simulation')
    fitness(SnakeEnv2(grid=np.array(GRID)), winner, config, render=True)
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)
def run():
    config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation, CONFIG)
    pop = neat.Population(config)

    # recode history
    stats = neat.StatisticsReporter()
    pop.add_reporter(stats)
    pop.add_reporter(neat.StdOutReporter(True))
    pop.add_reporter(neat.Checkpointer(5))

    pop.run(eval_genomes, 10)       # train 10 generations

    # visualize training
    visualize.plot_stats(stats, ylog=False, view=True)
    visualize.plot_species(stats, view=True)