예제 #1
0
def eval_genome(genome, config):
    net = neat.nn.FeedForwardNetwork.create(genome, config)
    #net_fpga =  neat.nn.FeedForwardNetworkFPGA.create(genome, config)
    #net.my_create_net_layer(genome, config)
    fitnesses = []

    for runs in range(runs_per_net):
        sim = cart_pole.CartPole()

        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        while sim.t < simulation_seconds:
            inputs = sim.get_scaled_state()
            #action = net_fpga.activate_cpu(inputs)
            action = net.activate(inputs)
            #action = net.my_activate(inputs)
            # Apply action to the simulated cart-pole
            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            if abs(sim.x) >= sim.position_limit or abs(
                    sim.theta) >= sim.angle_limit_radians:
                break

            fitness = sim.t

        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return min(fitnesses)
예제 #2
0
def evaluate_genome(g):
    net = nn.create_feed_forward_phenotype(g)

    fitnesses = []

    for runs in range(runs_per_net):
        sim = cart_pole.CartPole()

        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        for s in range(num_steps):
            inputs = sim.get_scaled_state()
            action = net.serial_activate(inputs)

            # Apply action to the simulated cart-pole
            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            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)
예제 #3
0
def eval_genome(genome, config):
    net = neat.ctrnn.CTRNN.create(genome, config, time_const)

    fitnesses = []
    for runs in range(runs_per_net):
        sim = cart_pole.CartPole()
        net.reset()

        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        while sim.t < simulation_seconds:
            inputs = sim.get_scaled_state()
            action = net.advance(inputs, time_const, time_const)

            # Apply action to the simulated cart-pole
            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians:
                break

            fitness = sim.t

        fitnesses.append(fitness)

        #print("{0} fitness {1}".format(net, fitness))


    # The genome's fitness is its worst performance across all runs.
    return min(fitnesses)
예제 #4
0
def evaluate_genome(g):
    net = ctrnn.create_phenotype(g)

    fitnesses = []

    for runs in range(runs_per_net):
        sim = cart_pole.CartPole()

        # Run the given simulation for up to num_steps time steps.
        fitness = 0.0
        for s in range(num_steps):
            inputs = sim.get_scaled_state()
            action = net.parallel_activate(inputs)

            # Apply action to the simulated cart-pole
            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            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)
def eval_genome(genome, config):
    net = neat.nn.FeedForwardNetwork.create(genome, config)

    fitnesses = []

    eps = 0.00000001

    for runs in range(runs_per_net):
        sim = cart_pole.CartPole()

        steps = 0
        fitness = 0.0

        while steps < simulation_steps:
            inputs = sim.get_scaled_state()
            action = net.activate(inputs)

            # Apply action to the simulated cart-pole
            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            # Stop if the network fails to keep the cart within the position or angle limits.
            # The per-run fitness is the number of time steps the network can balance the pole
            # without exceeding these limits.
            if abs(sim.x) >= sim.position_limit or abs(
                    sim.theta) >= sim.angle_limit_radians:
                break

            # why add the 1? it's so that solutions that manage to keep the pole stable get non-trivial credit
            # otherwise the credit based on their state could be too small to let them survive
            fitness += 1.0 + 1.0 / (abs(sim.x) + abs(sim.dx) + abs(sim.theta) +
                                    abs(sim.dtheta) + eps)
            steps += 1

        fitnesses.append(fitness)

    # The genome's fitness is its worst performance across all runs.
    return min(fitnesses)
예제 #6
0
print()
print("Initial conditions:")
print("        x = {0:.4f}".format(sim.x))
print("    x_dot = {0:.4f}".format(sim.dx))
print("    theta = {0:.4f}".format(sim.theta))
print("theta_dot = {0:.4f}".format(sim.dtheta))
print()

# Run the given simulation for up to 100k time steps.
num_balanced = 0
for s in range(10 ** 5):
    inputs = sim.get_scaled_state()
    action = net.serial_activate(inputs)

    # Apply action to the simulated cart-pole
    force = discrete_actuator_force(action)
    sim.step(force)

    # Stop if the network fails to keep the cart within the position or angle limits.
    # The per-run fitness is the number of time steps the network can balance the pole
    # without exceeding these limits.
    if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians:
        break

    num_balanced += 1


print('Pole balanced for {0:d} of 100k time steps'.format(num_balanced))

print()
print("Final conditions:")
예제 #7
0
def eval_genomes(genomes, config):
    global generation
    generation += 1
    best_genome = None
    best_fitness = 0

    for genome_id, genome in genomes:
        genome.fitness = eval_genome(genome, config)

        if genome.fitness > best_fitness:
            best_genome = genome
            best_fitness = genome.fitness

    # visualization for best genome
    if visualize:
        net = neat.nn.FeedForwardNetwork.create(best_genome, config)
        sim = cart_pole.CartPole(**initial_values)

        while sim.t < simulation_seconds:
            inputs = sim.get_scaled_state()
            action = net.activate(inputs)

            force = cart_pole.discrete_actuator_force(action)
            sim.step(force)

            if abs(sim.x) >= sim.position_limit or abs(
                    sim.theta) >= sim.angle_limit_radians:
                break

            cart = gz.rectangle(lx=25 * scale,
                                ly=12.5 * scale,
                                xy=(150 * scale, 80 * scale),
                                fill=(0, 1, 0))

            force_direction = 1 if force > 0 else -1

            force_rect = gz.rectangle(lx=5,
                                      ly=12.5 * scale,
                                      xy=(150 * scale - force_direction *
                                          (25 * scale) / 2, 80 * scale),
                                      fill=(1, 0, 0))

            cart_group = gz.Group([cart, force_rect])

            star = gz.star(radius=10 * scale,
                           fill=(1, 1, 0),
                           xy=(150 * scale, 25 * scale),
                           angle=-math.pi / 2)

            pole = gz.rectangle(lx=2.5 * scale,
                                ly=50 * scale,
                                xy=(150 * scale, 55 * scale),
                                fill=(1, 1, 0))

            pole_group = gz.Group([pole, star])

            # convert position to display units
            visX = sim.x * 50 * scale

            # draw background
            surface = gz.Surface(w, h, bg_color=(0, 0, 0))

            # draw cart, pole and text
            group = gz.Group([
                cart_group.translate((visX, 0)),
                pole_group.translate(
                    (visX, 0)).rotate(sim.theta,
                                      center=(150 * scale + visX, 80 * scale)),
                gz.text('Gen %d Time %.2f (Fitness %.2f)' %
                        (generation, sim.t, best_genome.fitness),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 25),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('x: %.2f' % (sim.x, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 50),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('dx: %.2f' % (sim.dx, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 75),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('theta: %d' % (sim.theta * 180 / math.pi, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 100),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('dtheta: %d' % (sim.dtheta * 180 / math.pi, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 1, 1),
                        xy=(10, 125),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
                gz.text('force: %d' % (force, ),
                        fontfamily='NanumGothic',
                        fontsize=20,
                        fill=(1, 0, 0),
                        xy=(10, 150),
                        fontweight='bold',
                        v_align='top',
                        h_align='left'),
            ])
            group.draw(surface)

            img = cv2.UMat(surface.get_npimage())
            img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

            cv2.imshow('result', img)
            if cv2.waitKey(1) == ord('q'):
                exit()
예제 #8
0
print()
print("Initial conditions:")
print("        x = {0:.4f}".format(sim.x))
print("    x_dot = {0:.4f}".format(sim.dx))
print("    theta = {0:.4f}".format(sim.theta))
print("theta_dot = {0:.4f}".format(sim.dtheta))
print()

# Run the given simulation for up to 120 seconds.
balance_time = 0.0
while sim.t < 120.0:
    inputs = sim.get_scaled_state()
    action = net.advance(inputs, sim.time_step, sim.time_step)

    # Apply action to the simulated cart-pole
    force = discrete_actuator_force(action)
    sim.step(force)

    # Stop if the network fails to keep the cart within the position or angle limits.
    # The per-run fitness is the number of time steps the network can balance the pole
    # without exceeding these limits.
    if abs(sim.x) >= sim.position_limit or abs(sim.theta) >= sim.angle_limit_radians:
        break

    balance_time = sim.t


print('Pole balanced for {0:.1f} of 120.0 seconds'.format(balance_time))

print()
print("Final conditions:")
예제 #9
0
config = neat.Config(
    neat.DefaultGenome,
    neat.DefaultReproduction,
    neat.DefaultSpeciesSet,
    neat.DefaultStagnation,
    'config-feedforward'
)

net = neat.nn.FeedForwardNetwork.create(genome, config)
sim = cart_pole.CartPole(**initial_values)

while sim.t < simulation_seconds:
    inputs = sim.get_scaled_state()
    action = net.activate(inputs)

    force = cart_pole.discrete_actuator_force(action)
    sim.step(force)

    if abs(sim.x) > 3.3:
        sim.x *= -1

    cart = gz.rectangle(
        lx=25 * scale,
        ly=12.5 * scale,
        xy=(150 * scale, 80 * scale),
        fill=(0, 1, 0)
    )

    force_direction = 1 if force > 0 else -1

    force_rect = gz.rectangle(