def run():
    # load settings file
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'dpole_config'))

    # change the number of inputs accordingly to the type
    # of experiment: markov (6) or non-markov (3)
    # you can also set the configs in dpole_config as long
    # as you have two config files for each type of experiment
    config.input_nodes = 3

    pop = population.Population(config)
    pop.epoch(evaluate_population, 200, report=1, save_best=0)

    winner = pop.most_fit_genomes[-1]

    print('Number of evaluations: {0:d}'.format(winner.ID))
    print('Winner fitness: {0:f}'.format(winner.fitness))

    # save the winner
    with open('winner_chromosome', 'w') as f:
        cPickle.dump(winner, f)

    # Plots the evolution of the best/average fitness
    visualize.plot_stats(pop, ylog=True)
    # Visualizes speciation
    visualize.plot_species(pop)
    # visualize the best topology
    visualize.draw_net(winner, view=True)
예제 #2
0
def run():
    t0 = time.time()

    # Get the path to the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'xor2_config')

    # Use a pool of four workers to evaluate fitness in parallel.
    pe = parallel.ParallelEvaluator(fitness, 3)

    pop = population.Population(config_path)
    pop.run(pe.evaluate, 400)

    print("total evolution time {0:.3f} sec".format((time.time() - t0)))
    print("time per generation {0:.3f} sec".format(
        ((time.time() - t0) / pop.generation)))

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

    # Verify network output against training data.
    print('\nBest network output:')
    winner = pop.statistics.best_genome()
    net = nn.create_feed_forward_phenotype(winner)
    for i, inputs in enumerate(xor_inputs):
        output = net.serial_activate(inputs)  # serial activation
        print("{0:1.5f} \t {1:1.5f}".format(xor_outputs[i], output[0]))

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    visualize.draw_net(winner, view=True)
예제 #3
0
def run():
    # load settings file
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'dpole_config'))

    # change the number of inputs accordingly to the type
    # of experiment: markov (6) or non-markov (3)
    # you can also set the configs in dpole_config as long
    # as you have two config files for each type of experiment
    config.input_nodes = 3

    pop = population.Population(config)
    pop.epoch(evaluate_population, 200, report=1, save_best=0)

    winner = pop.most_fit_genomes[-1]

    print('Number of evaluations: {0:d}'.format(winner.ID))
    print('Winner fitness: {0:f}'.format(winner.fitness))

    # save the winner
    with open('winner_chromosome', 'w') as f:
        cPickle.dump(winner, f)

    # Plots the evolution of the best/average fitness
    visualize.plot_stats(pop, ylog=True)
    # Visualizes speciation
    visualize.plot_species(pop)
    # visualize the best topology
    visualize.draw_net(winner, view=True)
예제 #4
0
def run():
    t0 = time.time()

    # Get the path to the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'xor2_config')

    # Use a pool of four workers to evaluate fitness in parallel.
    pe = parallel.ParallelEvaluator(fitness,3)

    pop = population.Population(config_path)
    pop.run(pe.evaluate, 400)

    print("total evolution time {0:.3f} sec".format((time.time() - t0)))
    print("time per generation {0:.3f} sec".format(((time.time() - t0) / pop.generation)))

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

    # Verify network output against training data.
    print('\nBest network output:')
    winner = pop.statistics.best_genome()
    net = nn.create_feed_forward_phenotype(winner)
    outputs = net.array_activate(xor_inputs)
    
    print("Expected XOR output : ", xor_outputs)
    print("Generated output : ", outputs)
    
    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    visualize.draw_net(winner, view=True)
예제 #5
0
def run():
    local_dir = os.path.dirname(__file__)
    pop = population.Population(os.path.join(local_dir, 'nn_config'))
    pe = parallel.ParallelEvaluator(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)
예제 #6
0
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

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

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()
    node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'}
    visualize.draw_net(winner, view=True, node_names=node_names)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    sum_square_error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12, 12))
    for r, (inputData, outputData, t0, t1, v0, v1, neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title("Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData), fontsize=12)
        for i, s in neuron_data.items():
            if i in net.outputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()
예제 #7
0
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)

    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    winner = pop.statistics.best_genome()

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

    visualize.draw_net(winner, view=False, filename="xor2-all.gv")
    visualize.draw_net(winner,
                       view=False,
                       filename="xor2-enabled.gv",
                       show_disabled=False)
    visualize.draw_net(winner,
                       view=False,
                       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)
예제 #8
0
def train_model(features, num_generations):
    timestamp = time.strftime("%Y%m%d-%H%M%S")
    print("########################## Time Stamp ==== " + timestamp)
    t0 = time.time()

    print("## Train a NEAT model")
    timestr = time.strftime("%Y%m%d-%H%M%S")

    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'bnp_config')

    # Use a pool of four workers to evaluate fitness in parallel.
    pe = parallel.ParallelEvaluator(fitness, 3, progress_bar=True, verbose=1)

    pop = population.Population(config_path)
    pop.run(pe.evaluate, num_generations)

    print("total evolution time {0:.3f} sec".format((time.time() - t0)))
    print("time per generation {0:.3f} sec".format(
        ((time.time() - t0) / pop.generation)))
    print('Number of evaluations: {0:d}'.format(pop.total_evaluations))

    # Verify network output against training data.
    print("## Test against verification data.")
    winner = pop.statistics.best_genome()

    net = nn.create_feed_forward_phenotype(winner)
    p_train = net.array_activate(X_train[features].values)
    p_valid = net.array_activate(X_valid[features].values)

    score_train = sklearn.metrics.log_loss(y_train, p_train[:, 0])
    score_valid = sklearn.metrics.log_loss(y_valid, p_valid[:, 0])
    print("Score based on training data set = ", score_train)
    print("Score based on validating data set = ", score_valid)

    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    visualize.draw_net(winner, view=True)

    print("## Predicting test data")
    preds = net.array_activate(test[features].values)
    test[test_col_name] = preds
    test[[id_col_name,
          test_col_name]].to_csv("../predictions/pred_" + timestr + ".csv",
                                 index=False)
예제 #9
0
def train_model(features,num_generations):
    timestamp = time.strftime("%Y%m%d-%H%M%S")
    print("########################## Time Stamp ==== " + timestamp)
    t0 = time.time()
    
    print("## Train a NEAT model")
    timestr = time.strftime("%Y%m%d-%H%M%S")
    
    local_dir = os.path.dirname(__file__)
    config_path = os.path.join(local_dir, 'bnp_config')
    
    # Use a pool of four workers to evaluate fitness in parallel.
    pe = parallel.ParallelEvaluator(fitness,3,progress_bar=True,verbose=1)

    pop = population.Population(config_path)
    pop.run(pe.evaluate, num_generations)
    
    print("total evolution time {0:.3f} sec".format((time.time() - t0)))
    print("time per generation {0:.3f} sec".format(((time.time() - t0) / pop.generation)))
    print('Number of evaluations: {0:d}'.format(pop.total_evaluations))
    
    # Verify network output against training data.
    print("## Test against verification data.")
    winner = pop.statistics.best_genome()
    
    net = nn.create_feed_forward_phenotype(winner)
    p_train = net.array_activate(X_train[features].values)
    p_valid = net.array_activate(X_valid[features].values)
    
    score_train = sklearn.metrics.log_loss(y_train, p_train[:,0])
    score_valid = sklearn.metrics.log_loss(y_valid, p_valid[:,0])
    print("Score based on training data set = ", score_train)
    print("Score based on validating data set = ", score_valid)

    
    # Visualize the winner network and plot statistics.
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)
    visualize.draw_net(winner, view=True)
    
    print("## Predicting test data")
    preds = net.array_activate(test[features].values)
    test[test_col_name] = preds
    test[[id_col_name,test_col_name]].to_csv("../predictions/pred_" + timestr + ".csv", index=False)
예제 #10
0
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)

    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    winner = pop.statistics.best_genome()

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

    visualize.draw_net(winner, view=False, filename="xor2-all.gv")
    visualize.draw_net(winner, view=False, filename="xor2-enabled.gv", show_disabled=False)
    visualize.draw_net(winner, view=False, 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)
예제 #11
0
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)
예제 #12
0
def run(output_dir, neat_config=None, generations=20, port=3001, frequency=None, unstuck=False, evaluation=None, checkpoint=None, configuration=None, timelimit=None):

    if output_dir is None:
        print('Error! No output dir has been set')
        return
    
    if neat_config is None:
        neat_config = os.path.join(output_dir, 'nn_config')
    
    
    if evaluation is None:
        fitness_function = get_fitness_function(os.path.join(output_dir, 'fitness.py'))
    else:
        fitness_function = get_fitness_function(evaluation)
        
    
    results_path, models_path, debug_path, checkpoints_path, EVAL_FUNCTION = simulation.initialize_experiments(output_dir, configuration=configuration, unstuck=unstuck, port=port)
    
    best_model_file = os.path.join(output_dir, 'best.pickle')
    
    if frequency is None:
        frequency = generations
    
    pop = population.Population(neat_config)
    
    if checkpoint is not None:
        print('Loading from ', checkpoint)
        pop.load_checkpoint(checkpoint)
    
    for g in range(1, generations+1):
        
        pop.run(lambda individuals: eval_fitness(individuals,
                                                 fitness_function=fitness_function,
                                                 evaluate_function=lambda g : EVAL_FUNCTION(g, pop.generation > 13),
                                                 cleaner=lambda: simulation.clean_temp_files(results_path, models_path),
                                                 timelimit=timelimit
                                                ),
                1)
        
        if g % frequency == 0:
            print('Saving best net in {}'.format(best_model_file))
            best_genome = get_best_genome(pop)
            pickle.dump(nn.create_recurrent_phenotype(best_genome), open(best_model_file, "wb"))
            
            new_checkpoint = os.path.join(checkpoints_path, 'neat_gen_{}.checkpoint'.format(pop.generation))
            print('Storing to ', new_checkpoint)
            pop.save_checkpoint(new_checkpoint)
            
            print('Plotting statistics')
            visualize.plot_stats(pop.statistics, filename=os.path.join(output_dir, 'avg_fitness.svg'))
            visualize.plot_species(pop.statistics, filename=os.path.join(output_dir, 'speciation.svg'))
            
            print('Save network view')
            visualize.draw_net(best_genome, view=False,
                               filename=os.path.join(output_dir, "nn_winner-enabled-pruned.gv"),
                               show_disabled=False, prune_unused=True)

            visualize.draw_net(best_genome, view=False, filename=os.path.join(output_dir, "nn_winner.gv"))
            visualize.draw_net(best_genome, view=False, filename=os.path.join(output_dir, "nn_winner-enabled.gv"),
                               show_disabled=False)
                
                
    print('Number of evaluations: {0}'.format(pop.total_evaluations))

    print('Saving best net in {}'.format(best_model_file))
    pickle.dump(nn.create_recurrent_phenotype(get_best_genome(pop)), open(best_model_file, "wb"))
    
    # Display the most fit genome.
    #print('\nBest genome:')
    winner = pop.statistics.best_genome()
    #print(winner)

    

    # Visualize the winner network and plot/log statistics.
    visualize.draw_net(winner, view=True, filename=os.path.join(output_dir, "nn_winner.gv"))
    visualize.draw_net(winner, view=True, filename=os.path.join(output_dir, "nn_winner-enabled.gv"), show_disabled=False)
    visualize.draw_net(winner, view=True, filename=os.path.join(output_dir, "nn_winner-enabled-pruned.gv"), show_disabled=False, prune_unused=True)
    visualize.plot_stats(pop.statistics, filename=os.path.join(output_dir, 'avg_fitness.svg'))
    visualize.plot_species(pop.statistics, filename=os.path.join(output_dir, 'speciation.svg'))
    statistics.save_stats(pop.statistics, filename=os.path.join(output_dir, 'fitness_history.csv'))
    statistics.save_species_count(pop.statistics, filename=os.path.join(output_dir, 'speciation.csv'))
    statistics.save_species_fitness(pop.statistics, filename=os.path.join(output_dir, 'species_fitness.csv'))
예제 #13
0
        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return min(fitnesses)


# Load the config file, which is assumed to live in
# the same directory as this script.
local_dir = os.path.dirname(__file__)
config = Config(os.path.join(local_dir, 'ctrnn_config'))
config.node_gene_type = ctrnn.CTNodeGene

pop = population.Population(config)
pe = parallel.ParallelEvaluator(evaluate_genome)
pop.run(pe.evaluate, 2000)

# Save the winner.
print('Number of evaluations: {0:d}'.format(pop.total_evaluations))
winner = pop.statistics.best_genome()
with open('ctrnn_winner_genome', 'wb') as f:
    pickle.dump(winner, f)

print(winner)

# Plot the evolution of the best/average fitness.
visualize.plot_stats(pop.statistics, ylog=True, filename="ctrnn_fitness.svg")
# Visualizes speciation
visualize.plot_species(pop.statistics, filename="ctrnn_speciation.svg")
# Visualize the best network.
visualize.draw_net(winner, view=True, filename="ctrnn_winner.gv")
예제 #14
0
        # its maximum value of 1.0.
        g.fitness = 1 - sum_square_error


pop = population.Population('xor2_config')
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)

예제 #15
0
def run():
    # Load the config file, which is assumed to live in
    # the same directory as this script.
    local_dir = os.path.dirname(__file__)
    config = Config(os.path.join(local_dir, 'xor2_config'))

    # For this network, we use two output neurons and use the difference between
    # the "time to first spike" to determine the network response.  There are
    # probably a great many different choices one could make for an output encoding,
    # and this choice may not be the best for tackling a real problem.
    config.output_nodes = 2

    pop = population.Population(config)
    pop.run(eval_fitness, 200)

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

    # Visualize the winner network and plot statistics.
    winner = pop.statistics.best_genome()
    node_names = {0: 'A', 1: 'B', 2: 'Out1', 3: 'Out2'}
    visualize.draw_net(winner, view=True, node_names=node_names)
    visualize.plot_stats(pop.statistics)
    visualize.plot_species(pop.statistics)

    # Verify network output against training data.
    print('\nBest network output:')
    net = iznn.create_phenotype(winner, *iz_params)
    sum_square_error, simulated = simulate(winner)

    # Create a plot of the traces out to the max time for each set of inputs.
    plt.figure(figsize=(12, 12))
    for r, (inputData, outputData, t0, t1, v0, v1,
            neuron_data) in enumerate(simulated):
        response = compute_output(t0, t1)
        print("{0!r} expected {1:.3f} got {2:.3f}".format(
            inputData, outputData, response))

        axes = plt.subplot(4, 1, r + 1)
        plt.title(
            "Traces for XOR input {{{0:.1f}, {1:.1f}}}".format(*inputData),
            fontsize=12)
        for i, s in neuron_data.items():
            if i in net.outputs:
                t, v = zip(*s)
                plt.plot(t, v, "-", label="neuron {0:d}".format(i))

        # Circle the first peak of each output.
        circle0 = patches.Ellipse((t0, v0), 1.0, 10.0, color='r', fill=False)
        circle1 = patches.Ellipse((t1, v1), 1.0, 10.0, color='r', fill=False)
        axes.add_artist(circle0)
        axes.add_artist(circle1)

        plt.ylabel("Potential (mv)", fontsize=10)
        plt.ylim(-100, 50)
        plt.tick_params(labelsize=8)
        plt.grid()

    plt.xlabel("Time (in ms)", fontsize=10)
    plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
    plt.savefig("traces.png", dpi=90)
    plt.show()