Exemple #1
0
def evaluate_population(genomes, create_func, force_func):
    for g in genomes:
        net = create_func(g)

        fitness = 0

        for runs in range(runs_per_net):
            sim = CartPole()
            fitness += run_simulation(sim, net, force_func)

        # The genome's fitness is its average performance across all runs.
        g.fitness = fitness / float(runs_per_net)
print(c)
print()
print('Python implementation of network:')
print(nn.create_feed_forward_function(c))

local_dir = os.path.dirname(__file__)
config = Config(os.path.join(local_dir, 'nn_config'))
net = nn.create_feed_forward_phenotype(c)

sim = CartPole()

print()
print("Initial conditions:")
print("        x = %.4f" % sim.x)
print("    x_dot = %.4f" % sim.dx)
print("    theta = %.4f" % sim.theta)
print("theta_dot = %.4f" % sim.dtheta)
print()
n = run_simulation(sim, net, discrete_actuator_force)
print('Pole balanced for %d of %d time steps' % (n, num_steps))

print()
print("Final conditions:")
print("        x = %.4f" % sim.x)
print("    x_dot = %.4f" % sim.dx)
print("    theta = %.4f" % sim.theta)
print("theta_dot = %.4f" % sim.dtheta)
print()
print("Making movie...")
make_movie(net, discrete_actuator_force, 15.0, "nn_movie.mp4")