示例#1
0
    def eval_fitness(genomes, config):

        for idx, g in genomes:
            cppn = neat.nn.FeedForwardNetwork.create(g, config)
            net = create_phenotype_network(cppn, substrate, activation)

            fitnesses = []

            for i in xrange(trials):
                ob = env.reset()
                net.reset()

                total_reward = 0

                for j in xrange(max_steps):
                    for k in range(activations):
                        o = net.activate(ob)
                    action = np.argmax(o)
                    ob, reward, done, info = env.step(action)
                    total_reward += reward
                    if done:
                        break
                fitnesses.append(total_reward)

            g.fitness = np.array(fitnesses).mean()
示例#2
0
    def make_nets(self, genome):

        cppn = neat.nn.FeedForwardNetwork.create(genome, self)
        return (cppn,
                create_phenotype_network(cppn,
                                         self.substrate,
                                         self.actfun))
示例#3
0
def eval_single(genome_tuple, config):
    cppn = neat.nn.FeedForwardNetwork.create(genome_tuple, config)
    net = create_phenotype_network(cppn, sub, "sigmoid")

    blockPrint()
    env = gym.make('ppaquette/meta-SuperMarioBros-Tiles-v0')
    env.reset()
    enablePrint()

    action = [0] * 6

    obs, reward, is_finished, info = env.step(action)

    distance = info['distance']
    ever_moved = False
    stuck_counter = 0

    nn_input = np.reshape(obs, (1, -1))[0]

    while True:
        distance_now = info['distance']
        output = net.activate(nn_input)
        output = [round(o) for o in output]

        action = output

        obs, reward, is_finished, info = env.step(action)
        nn_input = np.reshape(obs, (1, -1))[0]
        distance_later = info['distance']

        if is_finished:
            break

        delta_distance = info['distance'] - distance
        distance = info['distance']
        printed = False
        if delta_distance == 0:
            stuck_counter += 1
            if ever_moved:
                if stuck_counter >= 70:
                    sys.stdout.write('@')
                    printed = True
                    break
            else:
                if stuck_counter >= 10:
                    sys.stdout.write('#')
                    printed = True
                    break
        else:
            ever_moved = True
            stuck_counter = 0

        if distance_later - distance_now < -0.8:
            break

    if not printed:
        sys.stdout.write('*')
    sys.stdout.flush()
    env.close()
    return distance
示例#4
0
    def work(self):

        self.env = retro.make('Airstriker-Genesis')

        self.env.reset()

        ob, _, _, _ = self.env.step(self.env.action_space.sample())

        inx = int(ob.shape[0] / 6)
        iny = int(ob.shape[1] / 6)
        done = False
        #print(len(self.sub.input_coordinates))

        cpnn = neat.nn.FeedForwardNetwork.create(self.genome, self.config)
        net = create_phenotype_network(cpnn, self.sub, "sigmoid")
        #print(len(net.input_nodes))

        fitness = 0
        xpos_max = 0
        counter = 0
        last_lives = 0
        while not done:
            self.env.render()
            ob = cv2.resize(ob, (inx, iny))
            #ob = cv2.cvtColor(ob, cv2.COLOR_BGR2GRAY)
            #ob = np.reshape(ob, (inx, iny))
            imgarray = np.ndarray.flatten(ob)
            imgarray = np.interp(imgarray, (0, 254), (-1, +1))
            #for k in range(self.activations):
            for k in range(self.activations):
                actions = net.activate(imgarray)
            actions_padded = np.zeros(12)
            actions_padded[0] = actions[0]
            actions_padded[6] = actions[1]
            actions_padded[7] = actions[2]
            ob, rew, done, info = self.env.step(actions_padded)

            xpos = info['score']

            if xpos > xpos_max:
                xpos_max = xpos
                #counter = 0
                print(xpos)
                fitness += xpos + 0.05
            else:
                counter += 1
                fitness += 0.05

            #fitness += xpos
            if info['lives'] < last_lives:
                done = True
                last_lives = info['lives']

        return fitness
示例#5
0
def eval_fitness(genomes, config):

    for idx, g in genomes:

        cppn = neat.nn.FeedForwardNetwork.create(g, config)
        net = create_phenotype_network(cppn, sub)

        sum_square_error = 0.0
        for inputs, expected in zip(xor_inputs, xor_outputs):

            new_input = inputs + (1.0, )
            net.reset()
            for i in range(activations):

                output = net.activate(new_input)

            sum_square_error += ((output[0] - expected[0])**2.0) / 4.0

        g.fitness = 1 - sum_square_error
示例#6
0
def eval_fitness(genomes, config):
    """
    Fitness function.
    For each genome evaluate its fitness, in this case, as the mean squared error.
    """
    for _, genome in genomes:
        cppn = neat.nn.FeedForwardNetwork.create(genome, config)
        net = create_phenotype_network(cppn, SUBSTRATE)

        sum_square_error = 0.0

        for xor_inputs, xor_expected in zip(XOR_INPUTS, XOR_OUTPUTS):
            new_xor_input = xor_inputs + (1.0,)
            net.reset()

            for _ in range(ACTIVATIONS):
                xor_output = net.activate(new_xor_input)

            sum_square_error += ((xor_output[0] - xor_expected[0])**2.0)/4.0

        genome.fitness = 1 - sum_square_error
示例#7
0
def eval_genome(genome_id, genome, config):
    genome.fitness = 0.0
    cppn = neat.nn.FeedForwardNetwork.create(genome, config)
    net = create_phenotype_network(cppn, sub)

    #for i in range(0, 5):
    game.restart_game()
    GUI.set_game(game)
    game_over = False
    board = game.Board()
    consecutive_not_moved = 0
    successful_moves = 0

    while not game_over:
        in_neurons = (np.array(game.Board())).flatten()
        output = net.activate(in_neurons)
        # Use the 'most activated' output neuron as the intended direction
        # Generate list of tuples which are (direction, output weight)
        output_moves = [(map_neuron_to_move(i), output[i])
                        for i in range(len(output))]
        output_moves = sorted(output_moves, key=lambda x: x[1])

        # Try move the board starting with the highest weighted output direction
        for (direction, weight) in output_moves:
            moved = game.try_move(direction)
            if moved:
                break

        if moved:
            GUI.repaint_board()
            successful_moves = successful_moves + 1
        else:
            consecutive_not_moved = consecutive_not_moved + 1

        if game.State() == State.WIN or game.State() == State.LOSS:
            game_over = True
        elif consecutive_not_moved == NOT_MOVED_RESTART_THRESHOLD:
            game_over = True
    genome.fitness = game.Score()
示例#8
0
    Returns the winning genome and the statistics of the run.
    """
    winner, stats = run_hyper(gens,
                              env,
                              200,
                              CONFIG,
                              SUBSTRATE,
                              ACTIVATIONS,
                              max_trials=0)
    print("hyperneat_mountain_car done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    LOGGER = logging.getLogger()
    LOGGER.setLevel(logging.INFO)
    ENVIRONMENT = gym.make("MountainCar-v0")

    # Run! Only relevant to look at the winner.
    WINNER = run(200, ENVIRONMENT)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    cppn = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    NET = create_phenotype_network(cppn, SUBSTRATE)
    draw_net(cppn, filename="hyperneat_mountain_car_cppn")
    draw_net(NET, filename="hyperneat_mountain_car_winner")
    with open('hyperneat_mountain_car_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
示例#9
0
    pop.add_reporter(stats)
    pop.add_reporter(neat.reporting.StdOutReporter(True))

    winner = pop.run(eval_fitness, gens)
    print("hyperneat_xor done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    winner = run(300)[0]
    print('\nBest genome:\n{!s}'.format(winner))

    # Verify network output against training data.
    print('\nOutput:')
    cppn = neat.nn.FeedForwardNetwork.create(winner, config)
    winner_net = create_phenotype_network(cppn, sub)
    for inputs, expected in zip(xor_inputs, xor_outputs):
        new_input = inputs + (1.0, )
        winner_net.reset()
        for i in range(activations):
            output = winner_net.activate(new_input)
        print("  input {!r}, expected output {!r}, got {!r}".format(
            inputs, expected, output))

    # Save CPPN if wished reused and draw it to file along with the winner.
    with open('hyperneat_xor_cppn.pkl', 'wb') as output:
        pickle.dump(cppn, output, pickle.HIGHEST_PROTOCOL)
    draw_net(cppn, filename="hyperneat_xor_cppn")
    draw_net(winner_net, filename="hyperneat_xor_winner")
示例#10
0
def run(gens, env):
    """
    Run the pole balancing task using the Gym environment
    Returns the winning genome and the statistics of the run.
    """
    winner, stats = run_hyper(gens, env, 500, CONFIG, SUBSTRATE, ACTIVATIONS)
    print("hyperneat_polebalancing done")
    return winner, stats


# If run as script.
if __name__ == '__main__':
    # Setup logger and environment.
    LOGGER = logging.getLogger()
    LOGGER.setLevel(logging.INFO)
    ENVIRONMENT = gym.make("CartPole-v1")

    # Run! Only relevant to look at the winner.
    WINNER = run(100, ENVIRONMENT)[0]

    # Save CPPN if wished reused and draw it + winner to file.
    CPPN = neat.nn.FeedForwardNetwork.create(WINNER, CONFIG)
    WINNER_NET = create_phenotype_network(CPPN, SUBSTRATE)
    draw_net(
        CPPN, filename="pureples/experiments/pole_balancing/hyperneat_pole_balancing_cppn")
    with open('pureples/experiments/pole_balancing/hyperneat_pole_balancing_cppn.pkl', 'wb') as output:
        pickle.dump(CPPN, output, pickle.HIGHEST_PROTOCOL)
    draw_net(
        WINNER_NET, filename="pureples/experiments/pole_balancing/hyperneat_pole_balancing_winner")
示例#11
0
def eval_fitness(genomes, config):
    activation = "sigmoid"
    i = 0
    for genome_id, genome in genomes:
        i += 1
        print('Testing population member ' + str(i))

        cppn = neat.nn.FeedForwardNetwork.create(genome, config)
        net = create_phenotype_network(cppn, sub, activation)

        env = gym.make('ppaquette/meta-SuperMarioBros-Tiles-v0')
        env.reset()

        action = [0] * 6

        obs, reward, is_finished, info = env.step(action)

        distance = info['distance']
        ever_moved = False
        stuck_counter = 0

        nn_input = np.reshape(obs, (1, -1))[0]

        while True:
            distance_now = info['distance']
            output = net.activate(nn_input)
            output = [round(o) for o in output]

            action = output

            obs, reward, is_finished, info = env.step(action)
            nn_input = np.reshape(obs, (1, -1))[0]
            distance_later = info['distance']

            if is_finished:
                break

            delta_distance = info['distance'] - distance
            distance = info['distance']
            if delta_distance == 0:
                stuck_counter += 1
                if ever_moved:
                    if stuck_counter >= 70:
                        print('Stopped because he moved but then did not.')
                        break
                else:
                    if stuck_counter >= 10:
                        print('Stopped because he never moved')
                        break
            else:
                ever_moved = True
                stuck_counter = 0

            if distance_later - distance_now < -0.8:
                break

        genome.fitness = distance
        print(genome.fitness)
        print(genome)

        env.close()

    visualize.plot_stats(stats, ylog=False, view=False)
    stats.save()
示例#12
0
    def _make_nets(genome, config):

        cppn = neat.nn.FeedForwardNetwork.create(genome, config)
        return cppn, create_phenotype_network(cppn, config.substrate,
                                              config.actfun)