<s> '''<STR_LIT>''' <EOL> import numpy as np <EOL> import random <EOL> from deap import algorithms , base , creator , tools <EOL> import multiprocessing <EOL> from policy import CNNRNNPolicy <EOL> from nn_utilities import update_model_weights <EOL> from deap import creator <EOL> import scoop <EOL> from scoop import futures <EOL> import code , traceback , signal <EOL> from torcs_controller import pyclient <EOL> PRETRAINED_WEIGHTS_CNN = '<STR_LIT>' <EOL> def ga_fitness ( individual ) : <EOL> sessionid = multiprocessing . current_process ( ) . _identity [ <NUM_LIT:0> ] <EOL> pretrained_weights_cnn = np . loadtxt ( '<STR_LIT:{}>' . format ( PRETRAINED_WEIGHTS_CNN ) ) <EOL> POLICY = CNNRNNPolicy ( cnn_weights = pretrained_weights_cnn ) <EOL> TORCS = pyclient . TORCS ( episode_length = <NUM_LIT> , <EOL> policy = POLICY , <EOL> sessionid = sessionid ) <EOL> update_model_weights ( POLICY . rnn , np . asarray ( individual ) ) <EOL> POLICY . rnn . reset_states ( ) <EOL> fitness = <NUM_LIT:0> <EOL> fitness1 = TORCS . run ( track_id = <NUM_LIT:1> ) <EOL> fitness2 = TORCS . run ( track_id = <NUM_LIT:2> ) <EOL> fitness = min ( fitness1 , fitness2 ) <EOL> return fitness , <EOL> def run ( num_gen , <EOL> n , <EOL> mutpb , <EOL> cxpb ) : <EOL> """<STR_LIT>""" <EOL> history = tools . History ( ) <EOL> toolbox . decorate ( "<STR_LIT>" , history . decorator ) <EOL> toolbox . decorate ( "<STR_LIT>" , history . decorator ) <EOL> pool = multiprocessing . Pool ( processes = <NUM_LIT:12> ) <EOL> toolbox . register ( "<STR_LIT>" , pool . map ) <EOL> pop = toolbox . population ( n = n ) <EOL> history . update ( pop ) <EOL> hof = tools . HallOfFame ( <NUM_LIT:1> ) <EOL> stats = tools . Statistics ( lambda ind : ind . fitness . values ) <EOL> stats . register ( "<STR_LIT>" , np . mean ) <EOL> stats . register ( "<STR_LIT>" , np . std ) <EOL> stats . register ( "<STR_LIT>" , np . min ) <EOL> stats . register ( "<STR_LIT>" , np . max ) <EOL> pop , log = algorithms . eaSimple ( pop , <EOL> toolbox , <EOL> cxpb = cxpb , <EOL> mutpb = mutpb , <EOL> ngen = num_gen , <EOL> stats = stats , <EOL> halloffame = hof , <EOL> verbose = True ) <EOL> return pop , log , hof , history <EOL> creator . create ( "<STR_LIT>" , base . Fitness , weights = ( <NUM_LIT:1.0> , ) ) <EOL> creator . create ( "<STR_LIT>" , list , fitness = creator . FitnessMax ) <EOL> INDIVIDUAL_SIZE = <NUM_LIT> <EOL> toolbox = base . Toolbox ( ) <EOL> toolbox . register ( "<STR_LIT>" , random . uniform , - <NUM_LIT> , <NUM_LIT> ) <EOL> toolbox . register ( "<STR_LIT>" , <EOL> tools . initRepeat , <EOL> creator . Individual , <EOL> toolbox . attr_float , <EOL> n = INDIVIDUAL_SIZE ) <EOL> toolbox . register ( "<STR_LIT>" , tools . initRepeat , list , toolbox . individual ) <EOL> toolbox . register ( "<STR_LIT>" , ga_fitness ) <EOL> toolbox . register ( "<STR_LIT>" , tools . cxTwoPoint ) <EOL> toolbox . register ( "<STR_LIT>" , tools . mutGaussian , mu = <NUM_LIT:0> , sigma = <NUM_LIT> , indpb = <NUM_LIT> ) <EOL> toolbox . register ( "<STR_LIT>" , tools . selTournament , tournsize = <NUM_LIT:2> ) <EOL> def main ( ) : <EOL> try : <EOL> NUM_GENERATIONS = <NUM_LIT:100> <EOL> POPULATION_SIZE = <NUM_LIT> <EOL> MUTATION_PROB = <NUM_LIT> <EOL> CROSSOVER_PROB = <NUM_LIT:0.5> <EOL> pop , log , hof , history = run ( num_gen = NUM_GENERATIONS , <EOL> n = POPULATION_SIZE , <EOL> cxpb = CROSSOVER_PROB , <EOL> mutpb = MUTATION_PROB ) <EOL> best = np . asarray ( hof ) <EOL> gen = log . select ( "<STR_LIT>" ) <EOL> fitness_maxs = log . select ( "<STR_LIT>" ) <EOL> fitness_avgs = log . select ( "<STR_LIT>" ) <EOL> from matplotlib import pyplot as plt <EOL> plt . plot ( fitness_maxs ) <EOL> plt . plot ( fitness_avgs ) <EOL> plt . legend ( [ '<STR_LIT>' , '<STR_LIT>' ] , loc = <NUM_LIT:4> ) <EOL> plt . xlabel ( '<STR_LIT>' ) <EOL> plt . ylabel ( '<STR_LIT>' ) <EOL> np . savetxt ( '<STR_LIT>' , best ) <EOL> np . savetxt ( '<STR_LIT>' , fitness_avgs ) <EOL> np . savetxt ( '<STR_LIT>' , fitness_maxs ) <EOL> individuals = [ ] <EOL> for i in history . genealogy_history . items ( ) : <EOL> individuals . append ( i [ <NUM_LIT:1> ] ) <EOL> inp = np . array ( individuals ) <EOL> np . savetxt ( '<STR_LIT>' , inp ) <EOL> plt . savefig ( '<STR_LIT>' ) <EOL> plt . show ( ) <EOL> import IPython <EOL> IPython . embed ( ) <EOL> finally : <EOL> pass <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
