Exemplo n.º 1
0
def run_experiments(output_dir):
    POPULATION_SIZE = 100
    NUM_GENERATIONS = 100
    CROSSOVER_PROB = 0.5

    MUTATION_PROBS = [0.05, 0.10, 0.20, 0.30, 0.40, 0.50]

    for mutation_prob in MUTATION_PROBS:
        pop, log, hof, history, best_per_gen = run(num_gen=NUM_GENERATIONS,
                                               n=POPULATION_SIZE,
                                               cxpb=CROSSOVER_PROB,
                                               mutpb=mutation_prob)
        best = np.asarray(hof)
        gen = log.select("gen")
        fitness_maxs = log.select("max")
        fitness_avgs = log.select("avg")
        plot_results(filename='{}train_cnn_ga_mutpb_{}.png'.
                     format(output_dir,
                            str(mutation_prob).replace('.', '_')),
                     gen=gen,
                     fitness_maxs=fitness_maxs,
                     fitness_avgs=fitness_avgs)

        np.savetxt('{}train_cnn_ga_mutpb_{}.out'.
                   format(output_dir,
                          str(mutation_prob).replace('.', '_')), best)

        # Plot the feature vectors produced by the best individual from each
        # generation
        for gen in range(len(best_per_gen)):
            update_model_weights(model, np.asarray(best_per_gen[gen]))
            feature_vectors = calculate_cnn_output(model, images)
            plot_feature_vectors(feature_vectors, filename='{}feature_vectors_{}__{}.png'.\
                format(output_dir, str(mutation_prob).replace('.', '_'), gen))
def ga_fitness(individual):
    sessionid  = multiprocessing.current_process()._identity[0]

    # Instantiate the control policy
    # Load the ConvNet weights
    pretrained_weights_cnn = \
         np.loadtxt('{}'.format(PRETRAINED_WEIGHTS_CNN))

    POLICY = CNNRNNPolicy(cnn_weights=pretrained_weights_cnn)

    # Instantiate the simulator environment
    TORCS = pyclient.TORCS(episode_length=500,
                           policy=POLICY,
                           sessionid=sessionid)

    # The GA will update the RNN parameters to evaluate candidate solutions
    update_model_weights(POLICY.rnn, np.asarray(individual))
    POLICY.rnn.reset_states()

    # Run an episode on the track and its mirror image and measure the average
    # fitness
    fitness = 0

    fitness1 = TORCS.run(track_id=1)
    fitness2 = TORCS.run(track_id=2)

    fitness = min(fitness1, fitness2)

    return fitness,
Exemplo n.º 3
0
 def __init__(self, cnn_weights=None):
     # Create the ConvNet and load the pretrained weights if provided
     self.cnn = create_cnn()
     if cnn_weights is not None:
         update_model_weights(self.cnn, cnn_weights)
     # Create the RNN
     self.rnn = create_rnn()
def ga_fitness(individual):
    sessionid = multiprocessing.current_process()._identity[0]

    # Instantiate the control policy
    # Load the ConvNet weights
    pretrained_weights_cnn = \
         np.loadtxt('{}'.format(PRETRAINED_WEIGHTS_CNN))

    POLICY = CNNRNNPolicy(cnn_weights=pretrained_weights_cnn)

    # Instantiate the simulator environment
    TORCS = pyclient.TORCS(episode_length=500,
                           policy=POLICY,
                           sessionid=sessionid)

    # The GA will update the RNN parameters to evaluate candidate solutions
    update_model_weights(POLICY.rnn, np.asarray(individual))
    POLICY.rnn.reset_states()

    # Run an episode on the track and its mirror image and measure the average
    # fitness
    fitness = 0

    fitness1 = TORCS.run(track_id=1)
    fitness2 = TORCS.run(track_id=2)

    fitness = min(fitness1, fitness2)

    return fitness,
Exemplo n.º 5
0
def ga_fitness(individual):
    # Update the ConvNet parameters
    update_model_weights(model, np.asarray(individual))

    # Calculate the output feature vectors
    feature_vectors = calculate_cnn_output(model, images)

    # Check their fitness
    fitness = calculate_fitness(feature_vectors)

    return fitness,
import numpy as np

weights_index = -3

# Specify the path to the pretrained convolutional neural network weights here:
pretrained_weights_cnn = \
    np.loadtxt('experiments/train_cnn_ga_11/train_cnn_ga_mutpb_0_2.out')

saved_weights_rnn_multiple = np.loadtxt('history.out')
saved_weights_rnn_best = np.loadtxt('weights.out')

POLICY = CNNRNNPolicy(cnn_weights=pretrained_weights_cnn)

weights = saved_weights_rnn_multiple[weights_index]

update_model_weights(POLICY.rnn, weights)

# Instantiate the simulator environment
TORCS = pyclient.TORCS(episode_length=500, policy=POLICY, sessionid=1)

# Run an episode on the track and its mirror image and measure the average
# fitness
fitness1 = TORCS.run(track_id=1)
fitness2 = TORCS.run(track_id=2)

fitness = min(fitness1, fitness2)

print('fitness 1: {}, fitness 2: {}, min fitness: {}'.format(
    fitness1, fitness2, fitness))

import IPython

weights_index = -3

# Specify the path to the pretrained convolutional neural network weights here:
pretrained_weights_cnn = \
    np.loadtxt('experiments/train_cnn_ga_11/train_cnn_ga_mutpb_0_2.out')

saved_weights_rnn_multiple = np.loadtxt('history.out')
saved_weights_rnn_best = np.loadtxt('weights.out')

POLICY = CNNRNNPolicy(cnn_weights=pretrained_weights_cnn)

weights = saved_weights_rnn_multiple[weights_index]

update_model_weights(POLICY.rnn, weights)

# Instantiate the simulator environment
TORCS = pyclient.TORCS(episode_length=500,
                       policy=POLICY,
                       sessionid=1)

# Run an episode on the track and its mirror image and measure the average
# fitness
fitness1 = TORCS.run(track_id=1)
fitness2 = TORCS.run(track_id=2)

fitness = min(fitness1, fitness2)

print('fitness 1: {}, fitness 2: {}, min fitness: {}'.format(
    fitness1, fitness2, fitness))