예제 #1
0
def test_evolve():
    test_values = [random.random() for _ in range(10)]

    def evaluate_genome(genomes):
        for g in genomes:
            net = ctrnn.create_phenotype(g)

            fitness = 0.0
            for t in test_values:
                net.reset()
                output = net.serial_activate([t])

                expected = t ** 2

                error = output[0] - expected
                fitness -= error ** 2

            g.fitness = fitness

    # 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
    config.prob_mutate_time_constant = 0.1
    config.checkpoint_time_interval = 0.1
    config.checkpoint_gen_interval = 1

    pop = population.Population(config)
    pop.run(evaluate_genome, 10)

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

    repr(winner)
    str(winner)

    for g in winner.node_genes:
        repr(g)
        str(g)
    for g in winner.conn_genes:
        repr(g)
        str(g)
예제 #2
0
                    sim.theta) >= sim.angle_limit_radians:
                break

            fitness += 1.0

        fitnesses.append(fitness)

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


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

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

# Save the winner.
print('Number of evaluations: {0:d}'.format(pop.total_evaluations))
winner = pop.statistics.best_genome()
with open('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")
예제 #3
0
            if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians:
                break

            fitness += 1.0

        fitnesses.append(fitness)

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


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

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

# Save the winner.
print('Number of evaluations: {0:d}'.format(pop.total_evaluations))
winner = pop.statistics.best_genome()
with open('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")