def build_parameters(): params = NEAT.Parameters() params.PopulationSize = 150 params.DynamicCompatibility = True params.AllowClones = False params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 100 params.OldAgeTreshold = 35 params.MinSpecies = 3 params.MaxSpecies = 10 params.RouletteWheelSelection = False params.RecurrentProb = 0.2 params.OverallMutationRate = 0.02 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.01 params.MutateAddLinkProb = 0.02 params.MutateRemLinkProb = 0.00 params.CrossoverRate = 0.5 params.MutateWeightsSevereProb = 0.01 params.MutateNeuronTraitsProb = 0 params.MutateLinkTraitsProb = 0 return params
def test_multi_neat(self): params = NEAT.Parameters() params.PopulationSize = 100 genome = NEAT.Genome( 0, # ID 3, # number of inputs. Note: always add one extra input, for bias 0, # number of hidden nodes 2, # number of outputs False, # FS_NEAT; auto-determine an appropriate set of inputs for the evolved networks NEAT.ActivationFunction.UNSIGNED_SIGMOID, # OutputActType NEAT.ActivationFunction.UNSIGNED_SIGMOID, # HiddenActType 0, # SeedType params # Parameters ) seed = 42 pop = NEAT.Population( genome, params, True, # whether the population should be randomized 1.0, # how much the population should be randomized, seed) for generation in range(3): # retrieve a list of all genomes in the population genome_list = NEAT.GetGenomeList(pop) # apply the evaluation function to all genomes for genome in genome_list: fitness = self.evaluate(genome) genome.SetFitness(fitness) # advance to the next generation pop.Epoch()
def get_params(params_file): if params_file is None: return _default_params() else: params = neat.Parameters() params.Load(params_file) return params
def __init__(self, path=None): self.traits_calculated = False # Evolution. self.neat = NEAT.Parameters() self.neat.PopulationSize = 80 self.neat.OldAgeTreshold = 10 self.neat.SpeciesMaxStagnation = 10 self.neat.MinSpecies = 2 self.neat.MaxSpecies = 8 self.neat.OverallMutationRate = 0.6 self.neat.MutateAddNeuronProb = 0.05 self.neat.MutateAddLinkProb = 0.05 self.neat.AllowLoops = False # Coral Growth. self.seed_type = 1 # 0 is flat bottom and 1 is round, use 0 with 'has_ground' self.max_nodes = 15000 self.max_volume = 200.0 self.max_steps = 150 self.max_growth = .25 self.max_defect = 1.5 self.max_face_growth = 1.3 self.C = .3 self.n_signals = 2 self.signal_thresholds = 3 # if n thresholds, n+1 options. self.n_memory = 2 self.n_morphogens = 2 self.morphogen_thresholds = 3 self.morphogen_steps = 200 self.use_gravity = True self.use_polar_direction = True self.has_ground = False # These are coral specific and should get re-factored somewhere... self.light_amount = 0.7 self.gradient_height = 6.0 self.gradient_bottom = 0.2 self.collection_radius = 5 self.addTrait('energy_diffuse_steps', (0, 8), 'int') if path: for line in open(path).readlines(): key, value = line.strip().split('\t') if value == 'True': value = '1' if value == 'False': value = '0' setattr(self, key, float(value) if '.' in value else int(value))
def init_neat(self): params = NEAT.Parameters() params.PopulationSize = self.args.population_size params.AllowClones = self.args.allow_clones params.MaxWeight = self.args.max_weight params.WeightMutationMaxPower = self.args.weight_mutation_max_power params.MutateWeightsSevereProb = self.args.mutate_weights_severe_prob params.WeightMutationRate = self.args.weight_mutation_rate params.InterspeciesCrossoverRate = self.args.interspecies_crossover_rate params.CrossoverRate = self.args.crossover_rate params.OverallMutationRate = self.args.mutation_rate params.RecurrentProb = 0.0 params.RecurrentLoopProb = 0.0 params.Elitism = self.args.elitism params.SurvivalRate = self.args.survival_rate num_inputs = len(self.neural_input_vectors[0]) num_hidden_nodes = 0 num_outputs = self.effect.num_parameters if self.args.neural_mode == 'targets': num_outputs *= self.num_frames params.MutateAddNeuronProb = 0.0 params.MutateAddLinkProb = 0.0 params.MutateRemLinkProb = 0.0 params.MutateRemSimpleNeuronProb = 0.0 else: params.MutateAddNeuronProb = self.args.add_neuron_probability params.MutateAddLinkProb = self.args.add_link_probability params.MutateRemLinkProb = self.args.remove_link_probability params.MutateRemSimpleNeuronProb = self.args.remove_simple_neuron_probability output_activation_function = NEAT.ActivationFunction.UNSIGNED_SIGMOID if self.args.output_activation_function == 'linear': output_activation_function = NEAT.ActivationFunction.LINEAR elif self.args.output_activation_function == 'sine': output_activation_function = NEAT.ActivationFunction.UNSIGNED_SINE genome = NEAT.Genome( 0, # ID num_inputs, num_hidden_nodes, num_outputs, self.args.fs_neat, output_activation_function, # OutputActType NEAT.ActivationFunction.TANH, # HiddenActType 0, # SeedType params # Parameters ) self.population = NEAT.Population( genome, params, True, # whether the population should be randomized 2.0, # how much the population should be randomized, self.seed)
def optimize_neat(config, n_inputs, n_hidden, n_outputs, out_dir): print('Starting Optimization') params = NEAT.Parameters() params.PopulationSize = 60 params.OldAgeTreshold = 10 params.SpeciesMaxStagnation = 20 params.AllowLoops = False for i in range(config['n_morphogens']): addTrait(params, 'K%i' % i, (.03, .08)) addTrait(params, 'F%i' % i, (.01, .06)) addTrait(params, 'diffU%i' % i, (.005, .02)) addTrait(params, 'diffV%i' % i, (.0025, .01)) ######################## Create NEAT objects ############################### fs_neat = False seed_type = 0 out_type = NEAT.ActivationFunction.UNSIGNED_SIGMOID hidden_type = NEAT.ActivationFunction.UNSIGNED_SIGMOID genome_prototye = NEAT.Genome(0, n_inputs, n_hidden, n_outputs, fs_neat, \ out_type, hidden_type, seed_type, params, 0) rand_seed = int(time.time()) pop = NEAT.Population(genome_prototye, params, True, 1.0, rand_seed) ######################## Main evolution loop ############################### top_fitness = 0 # Fitness function is defined in [0, 1] top_grid = None for generation in range(config['generations']): print('Starting generation', generation) genomes = NEAT.GetGenomeList(pop) fitness_list = [simulate_genome(g, config)[0] for g in genomes] NEAT.ZipFitness(genomes, fitness_list) max_fitness = max(fitness_list) print('Generation complete') print('Max fitness', max_fitness) print('Mean fitness', np.mean(fitness_list)) if max_fitness > top_fitness: print('New best fitness') best_genome = genomes[fitness_list.index(max_fitness)] _, best_grid = simulate_genome(best_genome, config) top_fitness = max_fitness top_grid = best_grid np.save(out_dir + '/grid_%i' % generation, best_grid) best_genome.Save(out_dir + '/genome_%i' % generation) pop.Epoch() print()
def create_params(): params = NEAT.Parameters() params.PopulationSize = 150 params.DynamicCompatibility = True params.CompatTreshold = 3.0 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 100 params.OldAgeTreshold = 35 params.MinSpecies = 5 params.MaxSpecies = 10 params.RouletteWheelSelection = False params.MutateRemLinkProb = 0.02 params.RecurrentProb = 0 params.OverallMutationRate = 0.15 params.MutateAddLinkProb = 0.1 params.MutateAddNeuronProb = 0.03 params.MutateWeightsProb = 0.90 params.MaxWeight = 8.0 params.WeightMutationMaxPower = 0.2 params.WeightReplacementMaxPower = 1.0 params.MutateActivationAProb = 0.0 params.ActivationAMutationMaxPower = 0.5 params.MinActivationA = 0.05 params.MaxActivationA = 6.0 params.MutateNeuronActivationTypeProb = 0.3 params.ActivationFunction_SignedGauss_Prob = 1.0 params.ActivationFunction_SignedSigmoid_Prob = 1.0 params.ActivationFunction_SignedSine_Prob = 1.0 params.ActivationFunction_Linear_Prob = 1.0 params.ActivationFunction_Tanh_Prob = 0.0 params.ActivationFunction_SignedStep_Prob = 0.0 params.ActivationFunction_UnsignedSigmoid_Prob = 0.0 params.ActivationFunction_TanhCubic_Prob = 0.0 params.ActivationFunction_UnsignedStep_Prob = 0.0 params.ActivationFunction_UnsignedGauss_Prob = 0.0 params.ActivationFunction_Abs_Prob = 0.0 params.ActivationFunction_UnsignedSine_Prob = 0.0 params.MutateNeuronTraitsProb = 0 params.MutateLinkTraitsProb = 0 params.AllowLoops = False return params
def build_parameters(): params = NEAT.Parameters() params.PopulationSize = 100 params.DynamicCompatibility = True params.NormalizeGenomeSize = True params.WeightDiffCoeff = 0.1 params.CompatTreshold = 2.0 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 15 params.OldAgeTreshold = 35 params.MinSpecies = 2 params.MaxSpecies = 10 params.RouletteWheelSelection = False params.RecurrentProb = 0.0 params.OverallMutationRate = 1.0 params.ArchiveEnforcement = False params.MutateWeightsProb = 0.05 params.WeightMutationMaxPower = 0.5 params.WeightReplacementMaxPower = 8.0 params.MutateWeightsSevereProb = 0.0 params.WeightMutationRate = 0.25 params.WeightReplacementRate = 0.9 params.MaxWeight = 8 params.MutateAddNeuronProb = 0.001 params.MutateAddLinkProb = 0.3 params.MutateRemLinkProb = 0.0 params.MinActivationA = 4.9 params.MaxActivationA = 4.9 params.ActivationFunction_SignedSigmoid_Prob = 0.0 params.ActivationFunction_UnsignedSigmoid_Prob = 1.0 params.ActivationFunction_Tanh_Prob = 0.0 params.ActivationFunction_SignedStep_Prob = 0.0 params.CrossoverRate = 0.0 params.MultipointCrossoverRate = 0.0 params.SurvivalRate = 0.2 params.MutateNeuronTraitsProb = 0 params.MutateLinkTraitsProb = 0 params.AllowLoops = True params.AllowClones = True return params
def create_robot_params(): """ The function to create NEAT hyper-parameters for population of robots """ params = NEAT.Parameters() params.PopulationSize = 250 params.DynamicCompatibility = True params.AllowClones = False params.AllowLoops = True params.CompatTreshold = 2.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 20 params.OldAgeTreshold = 200 params.MinSpecies = 3 params.MaxSpecies = 20 params.RouletteWheelSelection = True params.RecurrentProb = 0.2 params.OverallMutationRate = 0.4 params.LinkTries = 40 params.SpeciesDropoffAge = 200 params.DisjointCoeff = 1.0 params.ExcessCoeff = 1.0 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 0.8 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 30.0 params.MinWeight = -30.0 params.MutateAddNeuronProb = 0.03 params.MutateAddLinkProb = 0.05 params.MutateRemLinkProb = 0.1 params.Elitism = 0.1 params.CrossoverRate = 0.8 params.MultipointCrossoverRate = 0.6 params.InterspeciesCrossoverRate = 0.01 params.MutateNeuronTraitsProb = 0.1 params.MutateLinkTraitsProb = 0.1 return params
def create_dummy_stuff(mult=1.0): params = NEAT.Parameters() params.PopulationSize = 500 params.DynamicCompatibility = True params.WeightDiffCoeff = 4.0 params.CompatTreshold = 2.0 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 15 params.OldAgeTreshold = 35 params.MinSpecies = 5 params.MaxSpecies = 25 params.RouletteWheelSelection = False params.RecurrentProb = 0.0 params.OverallMutationRate = 0.5 * mult params.MutateWeightsProb = 0.8 params.WeightMutationMaxPower = 1.5 *mult params.WeightReplacementMaxPower = 2.0 params.MutateWeightsSevereProb = 0.2 *mult params.WeightMutationRate = 0.2 *mult params.MaxWeight = 8 params.MutateAddNeuronProb = 0.03 params.MutateAddLinkProb = 0.05 params.MutateRemLinkProb = 0.0 params.MinActivationA = 4.9 params.MaxActivationA = 4.9 params.ActivationFunction_SignedSigmoid_Prob = 0.0 params.ActivationFunction_UnsignedSigmoid_Prob = 1.0 params.ActivationFunction_Tanh_Prob = 0.0 params.ActivationFunction_SignedStep_Prob = 0.0 params.CrossoverRate = 0.75 # mutate only 0.25 params.MultipointCrossoverRate = 0.4 params.SurvivalRate = 0.2 g= NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params) seed = 1032 pop = NEAT.Population(g, params, True, 1.0, seed) species = pop.Species[0] RNG=pop.RNG return pop,species,RNG,params
def __init__(self, path=None): self.traits_calculated = False # Evolution. self.neat = NEAT.Parameters() self.neat.PopulationSize = 80 self.neat.OldAgeTreshold = 10 self.neat.SpeciesMaxStagnation = 10 self.neat.MinSpecies = 2 self.neat.MaxSpecies = 8 self.neat.OverallMutationRate = 0.6 self.neat.MutateAddNeuronProb = 0.05 self.neat.MutateAddLinkProb = 0.05 self.neat.AllowLoops = False # Coral Growth. self.max_polyps = 15000 self.max_volume = 50.0 self.max_steps = 150 self.max_growth = .20 self.max_defect = 1.4 self.max_face_growth = 1.3 self.n_morphogens = 2 self.n_signals = 3 self.n_memory = 0 self.light_amount = 0.7 self.gradient_height = 6.0 self.gradient_bottom = 0.2 self.collection_radius = 5 self.C = .4 self.morphogen_thresholds = 2 self.morphogen_steps = 200 self.use_polar_direction = False self.addTrait('energy_diffuse_steps', (0, 8), 'int') if path: for line in open(path).readlines(): key, value = line.strip().split('\t') if value == 'True': value = '1' if value == 'False': value = '0' setattr(self, key, float(value) if '.' in value else int(value))
def standard_initialization(): params = NEAT.Parameters() params.ActivationFunction_SignedSigmoid_Prob = 1.0 params.ActivationFunction_UnsignedSigmoid_Prob = 0.0 params.ActivationFunction_Tanh_Prob = 1.0 params.ActivationFunction_TanhCubic_Prob = 0.0 params.ActivationFunction_SignedStep_Prob = 1.0 params.ActivationFunction_UnsignedStep_Prob = 0.0 params.ActivationFunction_SignedGauss_Prob = 1.0 params.ActivationFunction_UnsignedGauss_Prob = 0.0 params.ActivationFunction_Abs_Prob = 1.0 params.ActivationFunction_SignedSine_Prob = 1.0 params.ActivationFunction_UnsignedSine_Prob = 0.0 params.ActivationFunction_Linear_Prob = 1.0 params.ActivationFunction_Relu_Prob = 1.0 params.ActivationFunction_Softplus_Prob = 0.0 return params
def __init__(self, population_size=100, input_size=9, output_size=1, generations=50): """Initialize the trainer. Keyword arguments: population_size -- number of genomes per generation (default 100) input_size -- size of the input state + 1 (default 9) output_size -- size of the result of the genome neural networks (default 1) generations -- number of generations (default 50) """ self.generations = generations self.params = NEAT.Parameters() self.params.PopulationSize = population_size genome = NEAT.Genome(0, input_size, 0, output_size, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, self.params, 0) self.population = NEAT.Population(genome, self.params, True, 1.0, 0)
def _default_params(): params = neat.Parameters() params.PopulationSize = 150 params.DynamicCompatibility = True params.AllowClones = True params.CompatThreshold = 5.0 params.CompatThresholdModifier = 0.3 params.YoungAgeThreshold = 15 params.SpeciesMaxStagnation = 100 params.OldAgeThreshold = 35 params.MinSpecies = 2 params.MaxSpecies = 10 params.RouletteWheelSelection = True params.RecurrentProb = 0.0 params.OverallMutationRate = 0.25 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.01 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.05 params.MutateAddLinkProb = 0.1 params.MutateRemLinkProb = 0.05 params.MutateRemSimpleNeuronProb = 0.05 params.EliteFraction = 0.15 params.CrossoverRate = 0.5 params.ActivationFunction_UnsignedGauss_Prob = 0.25 params.ActivationFunction_UnsignedSigmoid_Prob = 0.25 params.ActivationFunction_UnsignedSine_Prob = 0.25 params.ActivationFunction_Relu_Prob = 0.25 params.MutateNeuronTraitsProb = 0 params.MutateLinkTraitsProb = 0 return params
def main(): rng = NEAT.RNG() rng.TimeSeed() params = NEAT.Parameters() params.PopulationSize = 128 params.DynamicCompatibility = True params.WeightDiffCoeff = 1.0 params.CompatTreshold = 2.0 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 15 params.OldAgeTreshold = 35 params.MinSpecies = 5 params.MaxSpecies = 10 params.RouletteWheelSelection = False params.Elitism = True params.RecurrentProb = 0.5 params.OverallMutationRate = 0.2 params.MutateWeightsProb = 0.8 # params.MutateNeuronTimeConstantsProb = 0.1 # params.MutateNeuronBiasesProb = 0.1 params.WeightMutationMaxPower = 0.5 params.WeightReplacementMaxPower = 1.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.25 params.TimeConstantMutationMaxPower = 0.1 params.BiasMutationMaxPower = params.WeightMutationMaxPower params.MaxWeight = 8 params.MutateAddNeuronProb = 0.1 params.MutateAddLinkProb = 0.2 params.MutateRemLinkProb = 0.0 params.MutateActivationAProb = 0.0; params.ActivationAMutationMaxPower = 0.5; params.MinActivationA = 1.1 params.MaxActivationA = 6.9 params.MinNeuronTimeConstant = 0.04 params.MaxNeuronTimeConstant = 0.24 params.MinNeuronBias = -params.MaxWeight params.MaxNeuronBias = params.MaxWeight params.MutateNeuronActivationTypeProb = 0.0 params.ActivationFunction_SignedSigmoid_Prob = 0 params.ActivationFunction_UnsignedSigmoid_Prob = 0 params.ActivationFunction_Tanh_Prob = 1 params.ActivationFunction_TanhCubic_Prob = 0 params.ActivationFunction_SignedStep_Prob = 0 params.ActivationFunction_UnsignedStep_Prob = 0 params.ActivationFunction_SignedGauss_Prob = 0 params.ActivationFunction_UnsignedGauss_Prob = 0 params.ActivationFunction_Abs_Prob = 0 params.ActivationFunction_SignedSine_Prob = 0 params.ActivationFunction_UnsignedSine_Prob = 0 params.ActivationFunction_Linear_Prob = 0 params.CrossoverRate = 0.75 # mutate only 0.25 params.MultipointCrossoverRate = 0.4 params.SurvivalRate = 0.2 params.MutateNeuronTraitsProb = 0 params.MutateLinkTraitsProb = 0 trials = 5 generations = 10 g = NEAT.Genome( 0, 24 + 1 + 1, 0, 4, False, NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.TANH, 0, params, 0, 1) pop = NEAT.Population(g, params, True, 1.0, rnd.randint(0, 1000)) hof = [] maxf_ever = 0 env = gym.make('BipedalWalker-v3') try: for generation in range(generations): fitnesses = [] #args = [x for x in NEAT.GetGenomeList(pop)] #dv.block=True #fitnesses = dv.map_sync(evaluate_genome, args) for _, genome in tqdm(enumerate(NEAT.GetGenomeList(pop))): fitness = evaluate_genome(env, genome, trials) fitnesses.append(fitness) for genome, fitness in zip(NEAT.GetGenomeList(pop), fitnesses): genome.SetFitness(fitness) maxf = max([x.GetFitness() for x in NEAT.GetGenomeList(pop)]) print('Generation: {}, max fitness: {}'.format(generation, maxf)) if maxf > maxf_ever: maxf_ever = maxf hof.append(pickle.dumps(pop.GetBestGenome())) pop.Epoch() except KeyboardInterrupt: pass print('Replaying forever..') if hof: while True: net = NEAT.NeuralNetwork() g = pickle.loads(hof[-1]) g.BuildPhenotype(net) do_trial(env, net, True)
def evolutionary_run(**kwargs): """ Conduct an evolutionary run using the snake and muscle model. Args: gens: generations of evolution pop_size: population size mut_prob: mutation probability """ global args, current_network, fitness_function, run_num, output_path, population_size, simulation params = NEAT.Parameters() params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 1000 params.OldAgeTreshold = 35 params.MinSpecies = 1 params.MaxSpecies = 25 params.RouletteWheelSelection = False params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.4 params.MutateAddLinkProb = 0.4 params.MutateRemLinkProb = 0.05 params.PopulationSize = kwargs['pop_size'] # Initialize the population if args.fixed_strength: genome = NEAT.Genome(0, 22, 0, 16, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) # If not including a periodic input. if args.no_periodic: genome = NEAT.Genome(0, 21, 0, 16, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) else: genome = NEAT.Genome(0, 22, 0, 24, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) # If not including a periodic input. if args.no_periodic: genome = NEAT.Genome(0, 21, 0, 24, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) population = NEAT.Population(genome, params, True, 1.0) genome_list = NEAT.GetGenomeList(population) morph_pop = MorphGenomes(kwargs['pop_size']) for ind in genome_list: morph_pop.addIndividual(ind.GetID()) morph_genomes = morph_pop.getGenomes() # Zip the two genome components together for use in the parallel call. zip_args = [(ind, morph_genomes[ind.GetID()]) for ind in genome_list] mnlog.write_population_statistics_headers( output_path + str(run_num) + "_fitnesses.dat", optional_additions="Distance,Efficiency") # Setup multiprocessing cores = mpc.cpu_count() pool = mpc.Pool(processes=cores - 2) for gen in xrange(kwargs['gens']): #fitnesses = [] #for z in zip_args: # fitnesses.append(evaluate_individual(z)) fitnesses = pool.map(evaluate_individual, zip_args) # Validate the fitnesses. fitnesses = check_valid_fitnesses(fitnesses) # Set the fitnesses appropriately. genome_list, fitnesses, dist_fit = set_sliding_window_fitnesses( genome_list, fitnesses, args.window_size, fitness_function) print("Generation " + str(gen) + "\t: " + str(max(zip(*fitnesses)[1]))) # Write the best performing individual to a file. mnlog.write_best_individual( output_path + "best_individuals/Evo_NEAT_run_" + str(run_num) + "_best_gen_" + str(gen) + ".dat", genome_list[dist_fit.index(max(dist_fit))] ) #genome_list[fitnesses.index(max(zip(*fitnesses)[2]))] morph_pop.logGenome( genome_list[fitnesses.index(max(fitnesses))].GetID(), "Evo_NEAT_run_" + str(run_num) + "_best_gen_" + str(gen), output_path) # Write information about the best individual we wrote. with open( output_path + "/" + str(run_num) + "_best_individuals_logging.dat", "a") as f: f.write("Generation: "+str(gen)+" Individual is: "+str(genome_list[dist_fit.index(max(dist_fit))].GetID())+\ " Fitness is: "+str(max(dist_fit))+"\n") # Log the progress of the entire population. mnlog.write_population_statistics_multi_component( output_path + str(run_num) + "_fitnesses.dat", genome_list, fitnesses, gen) # Log the final population for later evaluation. if gen == kwargs['gens'] - 1: population.Save(output_path + "run_" + str(run_num) + "_population_generation_" + str(gen) + ".dat") # Create the next generation population.Epoch() morph_pop.NextGen() genome_list = NEAT.GetGenomeList(population) zip_args = [] for ind in genome_list: pid1 = ind.GetPID1() pid2 = ind.GetPID2() gid = ind.GetID() # Handle Crossover morph_pop.Crossover(gid, pid1, pid2) # Handle Mutation morph_pop.MutatePopulation() # Zip the arguments for calling the evolution function. morph_genomes = morph_pop.getGenomes() zip_args = [(ind, morph_genomes[ind.GetID()]) for ind in genome_list]
def main(): params = NEAT.Parameters() params.PopulationSize = 512 iterations = 100 ticker = 'AAPL' # Parse Arguments # Example: # python3 seed.py --Population 512 --Iterations 20 --Ticker AAPL parser = argparse.ArgumentParser() parser.add_argument("--Population", '-p', help="Set Population Size") parser.add_argument("--Iterations", '-i', help="Set How Many Iterations") parser.add_argument("--Ticker", '-t', help="Set Which Ticker") args = parser.parse_args() if args.Population: params.PopulationSize = int(args.Population) if args.Iterations: iterations = int(args.Iterations) if args.Ticker: ticker = args.Ticker print(params.PopulationSize, iterations) genome = NEAT.Genome(0, 4, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params, 0, 0) pop = NEAT.Population(genome, params, True, 1.0, 0) # the 0 is the RNG seed # Input Parameters pop.Parameters.ActivationFunction_UnsignedSigmoid_Prob = 1 / 3 pop.Parameters.ActivationFunction_Tanh_Prob = 1 / 3 pop.Parameters.ActivationFunction_UnsignedGauss_Prob = 1 / 3 pop.Parameters.MinSpecies = 5 pop.Parameters.MaxSpecies = 40 pop.Parameters.YoungAgeFitnessBoost = 1.1 pop.Parameters.EliteFraction = 0.01 pop.Parameters.MutateActivationAProb = 0.25 pop.Parameters.OverallMutationRate = 0.2 pop.Parameters.MutateAddNeuronProb = 0.03 pop.Parameters.MutateAddLinkProb = 0.3 pop.Parameters.MutateWeightsProb = 0.8 pop.Parameters.CrossoverRate = 0.75 pop.Parameters.InterspeciesCrossoverRate = 0.001 # Load and Split Datasets [test, train] = get_datasets(ticker) split = int(len(test['Close']) * 0.6) signals_test = train[split:] prices_train = test[:split] train = train[:split] test = test[split:] # Keep track of the best Genome and Fitness best_genome = None best_fitness = -100 # Determine how many cores available for parallel processing num_cores = multiprocessing.cpu_count() for generation in range(iterations): # retrieve a list of all genomes in the population genome_list = NEAT.GetGenomeList(pop) # Get a list of fitnesses for all genomes currently in the population results = Parallel(n_jobs=num_cores)( delayed(evaluate)(g, train, prices_train) for g in genome_list) fitnesses = [f[0] for f in results] m = max(fitnesses) if m > best_fitness: best_fitness = m best_genome = results[fitnesses.index(m)][1] # Print the running best fitness print(generation, best_fitness) pop.Epoch() # Write out the best genome to a pickle object. filehandler = open('best_genome_' + ticker + '.obj', 'wb') pickle.dump(best_genome, filehandler) print(get_portfolio(best_genome, signals_test, test))
def main(): pygame.init() screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption("NEAT volleyball") ### Physics stuff space = pm.Space() space.gravity = Vec2d(0.0, -500.0) # walls - the left-top-right walls body = pm.Body() walls= [pm.Segment(body, (50, 50), (50, 1550), 10) ,pm.Segment(body, (50, 1550), (560, 1550), 10) ,pm.Segment(body, (560, 1550), (560, 50), 10) ] floor = pm.Segment(body, (50, 50), (560, 50), 10) floor.friction = 1.0 floor.elasticity = 0.0 floor.collision_type = collision_type_floor for s in walls: s.friction = 0 s.elasticity = 0.99 s.collision_type = collision_type_wall space.add(walls) space.add(floor) params = NEAT.Parameters() params.PopulationSize = 250 params.DynamicCompatibility = True params.AllowClones = True params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 100 params.OldAgeTreshold = 35 params.MinSpecies = 3 params.MaxSpecies = 10 params.RouletteWheelSelection = True params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.01 params.MutateAddLinkProb = 0.05 params.MutateRemLinkProb = 0.00 rng = NEAT.RNG() rng.TimeSeed() #rng.Seed(0) g = NEAT.Genome(0, 6, 0, 2, False, NEAT.ActivationFunction.TANH, NEAT.ActivationFunction.UNSIGNED_SIGMOID, 0, params) pop = NEAT.Population(g, params, True, 1.0) best_genome_ever = None fast_mode = False for generation in range(1000): print "Generation:", generation now = time.time() genome_list = [] for s in pop.Species: for i in s.Individuals: genome_list.append(i) print 'All individuals:', len(genome_list) for i, g in enumerate(genome_list): total_fitness = 0 for trial in range(20): f, fast_mode = evaluate(g, space, screen, fast_mode, rnd.randint(80, 400), rnd.randint(-200, 200), rnd.randint(80, 400)) total_fitness += f g.SetFitness(total_fitness / 20) print best = max([x.GetLeader().GetFitness() for x in pop.Species]) print 'Best fitness:', best, 'Species:', len(pop.Species) # Draw the best genome's phenotype net = NEAT.NeuralNetwork() best_genome_ever = pop.Species[0].GetLeader() best_genome_ever.BuildPhenotype(net) img = np.zeros((250, 250, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 250, 250), net ) cv2.imshow("current best", img) cv2.waitKey(1) if best >= 10000: break # evolution is complete if an individual keeps the ball up for that many timesteps #if pop.GetStagnation() > 500: # break print "Evaluation took", time.time() - now, "seconds." print "Reproducing.." now = time.time() pop.Epoch() print "Reproduction took", time.time() - now, "seconds." # Show the best genome's performance forever pygame.display.set_caption("Best genome ever") while True: evaluate(best_genome_ever, space, screen, False, rnd.randint(80, 400), rnd.randint(-200, 200), rnd.randint(80, 400))
def run_objectrecognition(number_of_generations, save_path, test_size): data_filename_all = 'genration_data_all.csv' data_filename_all = os.path.join(save_path, data_filename_all) data_filename_stats = 'generation_data_stats.csv' data_filename_stats = os.path.join(save_path, data_filename_stats) data_filename_test = 'data_filename_test.csv' data_filename_test = os.path.join(save_path, data_filename_test) average_imagename = 'average_std.png' average_imagename = os.path.join(save_path, average_imagename) individuals_imagename = 'individuals.png' individuals_imagename = os.path.join(save_path, individuals_imagename) filename_winner = 'winner-feedforward.pickle' filename_winner = os.path.join(save_path, filename_winner) network_structure_filename = 'best_network_gen_' network_structure_filename = os.path.join(save_path, network_structure_filename) test_inputs_filename = 'test_inputs' test_inputs_filename = os.path.join(save_path, test_inputs_filename) test_outputs_filename = 'test_outputs' test_outputs_filename = os.path.join(save_path, test_outputs_filename) best_network_filename = 'winner_network_with_save.txt' best_network_filename = os.path.join(save_path, best_network_filename) specie_filename = 'speciation.csv' specie_filename = os.path.join(save_path, specie_filename) parameters = 'parameters.txt' parameters = os.path.join(save_path, parameters) plot_filename = 'best_network_gen_' plot_filename = os.path.join(save_path, plot_filename) start_time_total = time.time() train_inputs, test_inputs, train_outputs, test_outputs = load_images_2classRGB.load_data( test_size) print('trainsize: ', len(train_inputs)) # with open(test_inputs_filename, 'wb') as f: # pickle.dump(test_inputs, f) # with open(test_outputs_filename, 'wb') as f: # pickle.dump(test_outputs, f) params = NEAT.Parameters() params.PopulationSize = 100 params.MinSpecies = 5 params.MaxSpecies = 15 params.CompatTreshold = 0.3 params.OverallMutationRate = 0.75 params.MutateWeightsProb = 0.7 params.WeightMutationRate = 0.4 params.WeightMutationMaxPower = 0.5 params.MutateWeightsSevereProb = 0.05 params.WeightReplacementMaxPower = 4.0 params.WeightReplacementRate = 0.2 params.MutateAddNeuronProb = 0.1 params.MutateAddLinkProb = 0.1 params.MutateRemLinkProb = 0.01 num_inputs = 1110 + 1 # number of inputs. Note: always add one extra input, for bias num_hidden_nodes = 0 num_outputs = 2 output_activation_function = NEAT.ActivationFunction.UNSIGNED_SIGMOID hidden_activation_function = NEAT.ActivationFunction.UNSIGNED_SIGMOID params.Save(parameters) genome = NEAT.Genome( 0, num_inputs, num_hidden_nodes, num_outputs, False, # FS_NEAT; auto-determine an appropriate set of inputs for the evolved networks output_activation_function, hidden_activation_function, 0, params, 0) pop = NEAT.Population( genome, params, True, # whether the population should be randomized 3.0, # how much the population should be randomized, 30 #20 #90 # the 42 is the RNG seed #70 1:20 (2:80) ) def softmax2(array): return np.exp(array) / np.sum(np.exp(array), axis=0) def evaluate(genome, trainx, trainy): # this creates a neural network (phenotype) from the genome net = NEAT.NeuralNetwork() genome.BuildPhenotype(net) evaluate_time = time.time() empty = [0, 0] genome_fitness = 0 for _im, _label in zip(trainx, trainy): net.Input(_im) net.Activate() output = net.Output() # print('-----------------------------------------------------------------') # print('real:') # print(_label) # print('out:') # print(output_list) output = softmax2(output) # print(output) # print('dot:') # dotproduct = np.dot(np.array(output),np.array(_label)) # print(dotproduct) # # print('original label:') # lab = get_label(_label) # # print(lab) # out, guess = get_max(output) # # print('guess label:') # # print(guess) # if lab == guess: # empty[lab] += 1 dotproduct = np.dot(np.array(output), np.array(_label)) # print(dotproduct) if dotproduct > 0.5: # print('out:') # print(output) bonus = abs(np.array(output)[0] - np.array(output)[1]) # print('bonus:') # print(bonus) genome_fitness += dotproduct + bonus else: # print('out:') # print(output) bonus = abs(np.array(output)[0] - np.array(output)[1]) # print('bonus:') # print(-1*bonus) genome_fitness -= ((1 - dotproduct) + bonus) # if dotproduct > 0.5: # # print('out:') # # print(output) # bonus = abs(np.array(output)[0]-np.array(output)[1]) # # print('bonus:') # # print(bonus) # genome_fitness += dotproduct + bonus # else: # # print('out:') # # print(output) # bonus = abs(np.array(output)[0]-np.array(output)[1]) # # print('bonus:') # # print(-1*bonus) # genome_fitness -= ((1-dotproduct) + bonus) # print(empty, np.sum(empty), len(trainy)) evaluate_time_end = (time.time() - evaluate_time) / float(len(trainy)) # print('mean genome', np.mean(empty)) # print('accur genome', np.sum(empty)/float(len(trainy))) # print('std genome', np.std(empty)) # print('1/1+std genome', 1.0/(1+np.std(empty))) fitness_images = genome_fitness / (2.0 * float(len(trainy))) # print(evaluate_time_end) # try 0.01, 0.1, 0.05 # print(fitness_images) fitness = fitness_images # the output can be used as any other Python iterable. For the purposes of the tutorial, # we will consider the fitness of the individual to be the neural network that outputs constantly # 0.0 from the first output (the second output is ignored) return fitness all_fitness_per_generation = [] std_fitness_per_generation = [] avg_fitness_per_generation = [] best_fitness_per_generation = [] intermediate_test_fitness_list = [] specie_list = [] gens = [] test_gens = [] best_genome_ever = None best_genome_ever_fitness = 0 # m=400 for generation in range(number_of_generations): x_sub, y_sub = zip( *random.sample(list(zip(train_inputs, train_outputs)), 500)) # x_sub, y_sub = train_inputs, train_outputs print(len(x_sub)) # if generation == 1500: # params.MutateWeightsProb = 0.8 # params.WeightMutationMaxPower = 2.0 # params.MutateAddNeuronProb = 0.1 # params.MutateAddLinkProb = 0.1 # if generation == 2500: # params.WeightMutationMaxPower = 0.4 # params.MutateWeightsProb = 0.9 start_time = time.time() # run for 100 generations gens.append(generation) print( '---------------------------------------------------------------------' ) print('---- Generation: {!r}'.format(generation)) print( '---------------------------------------------------------------------' ) # retrieve a list of all genomes in the population genome_list = NEAT.GetGenomeList(pop) print('individuals:') print(len(genome_list)) fitnesslist = [] total_fitness = 0 best = 0 best_genome = None # apply the evaluation function to all genomes for genome in genome_list: fitness = evaluate(genome, x_sub, y_sub) fitnesslist.append(fitness) total_fitness += fitness genome.SetFitness(fitness) if fitness > best: best_genome = genome if fitness > best_genome_ever_fitness: best_genome_ever = genome else: best_genome = best_genome if generation % 10 == 0: best_genome_from_gen = pop.GetBestGenome() net = NEAT.NeuralNetwork() best_genome_from_gen.BuildPhenotype(net) intermediate_test_fitness = 0 pos_test_set_good = 0 neg_test_set_good = 0 test_gens.append(generation) for _im, _label in zip(test_inputs, test_outputs): net.Input(_im) net.Activate() output = net.Output() output_list = [output[0]] + [output[1]] output = softmax2(output) dotproduct = np.dot(np.array(output), np.array(_label)) if dotproduct > 0.5: intermediate_test_fitness += 1 if _label[0] == 1: pos_test_set_good += 1 elif _label[1] == 1: neg_test_set_good += 1 else: intermediate_test_fitness += 0 intermediate_test_fitness = intermediate_test_fitness / float( len(test_outputs)) intermediate_test_fitness_list.append(intermediate_test_fitness) best_genome_from_gen.Save('best_from_gen_' + str(generation) + '_fit_' + str(intermediate_test_fitness)) if generation % 400 == 0: net = NEAT.NeuralNetwork() best_genome_from_gen.BuildPhenotype(net) viz.Draw(best_genome_from_gen) fig = plt.gcf() plt.axis([-100, 4100, -100, 4100]) plt.axis('off') fig.savefig(str(best_genome_from_gen) + 'generation_' + str(generation) + '.png', dpi=150, transparent=True) print( '---------------------------------------------------------------------' ) print('Number of positive samples correctly classified: ', pos_test_set_good) print('Number of negative samples correctly classified: ', neg_test_set_good) print( '---------------------------------------------------------------------' ) print( '--- Intermediate test fitness for generation {!r}: {!r}--------------' .format(generation, intermediate_test_fitness)) print( '---------------------------------------------------------------------' ) print("--- Evolving time: {!r} seconds ---".format( (time.time() - start_time))) count_list = [] for s in pop.Species: # print('specie:') # print(s) count = 0 for i in s.Individuals: count += 1 # print('individuals:') # print(count) row = [generation, s.ID(), s.GetLeader().GetFitness(), count] print(generation, s.ID(), s.GetLeader().GetFitness(), count) specie_list.append(row) all_fitness_per_generation.append(fitnesslist) avg_fitness = total_fitness / float(len(genome_list)) print('--- Number of species: {!r}--- '.format( len(pop.Species))) avg_fitness_per_generation.append(avg_fitness) print('--- Average fitness: {!r}--- '.format(avg_fitness)) std_fitness = np.std(fitnesslist) std_fitness_per_generation.append(std_fitness) print('--- Standard deviation fitness: {!r}--- '.format(std_fitness)) best_fitness = max(fitnesslist) best_fitness_per_generation.append(best_fitness) print('--- Best fitness: {!r}--- '.format(best_fitness)) # at this point we may output some information regarding the progress of evolution, best fitness, etc. # it's also the place to put any code that tracks the progress and saves the best genome or the entire # population. We skip all of this in the tutorial. # advance to the next generation print("--- Generation time: {!r} seconds ---".format( (time.time() - start_time))) pop.Epoch() best_genome_from_all = pop.GetBestGenome() print(best_genome_from_all) print(best_genome_ever) net = NEAT.NeuralNetwork() best_genome_from_all.BuildPhenotype(net) test_fitness = 0 viz.Draw(best_genome_from_all) fig = plt.gcf() plt.axis([-100, 4100, -100, 4100]) plt.axis('off') fig.savefig(plot_filename + str(best_genome_from_all) + '.png', dpi=150, transparent=True) best_genome_from_all.Save(best_network_filename) with open(filename_winner, 'wb') as f: pickle.dump(best_genome_from_all, f) pos_test_set_good = 0 neg_test_set_good = 0 empty_final = [0, 0, 0, 0] for _im, _label in zip(test_inputs, test_outputs): net.Input(_im) net.Activate() output = net.Output() output_list = [output[0]] + [output[1]] output = softmax2(output) dotproduct = np.dot(np.array(output), np.array(_label)) if dotproduct > 0.5: test_fitness += 1 if _label[0] == 1: pos_test_set_good += 1 elif _label[1] == 1: neg_test_set_good += 1 # else: # _im = np.reshape(_im[:(len(_im)-1)], (8, 37)) # plt.imshow(_im) # plt.title(str(_label)) # plt.show() # test_fitness += 0 test_fitness_total = test_fitness / (float(len(test_outputs))) print('test fitness:{!s}'.format(test_fitness_total)) print('pos_tes_sample_correct:{!s}'.format(pos_test_set_good)) print('neg_tes_sample_correct:{!s}'.format(neg_test_set_good)) print('total test smaples:{!s}'.format(len(test_outputs))) # add: size of network, save network,, species with open(data_filename_all, 'w') as f: w = csv.writer(f, delimiter=',') for s in all_fitness_per_generation: w.writerow(s) with open(specie_filename, 'w') as f: w = csv.writer(f, delimiter=',') for s in specie_list: w.writerow(s) with open(data_filename_test, 'w') as f: w = csv.writer(f, delimiter=',') for s in intermediate_test_fitness_list: w.writerow([s]) with open(data_filename_stats, 'w') as f: w = csv.writer(f, delimiter=',') for avg, std, best in zip(avg_fitness_per_generation, std_fitness_per_generation, best_fitness_per_generation): w.writerow([avg, std, best]) print( '_____________________________________________________________________' ) print( '---------------------------------------------------------------------' ) print("--- Total time for {!r} generations: {!r} minutes ---".format( len(gens), (time.time() - start_time_total) / 60.0)) plt.figure(figsize=(7.195, 3.841), dpi=150) plt.scatter(gens, avg_fitness_per_generation, s=0.1, c='green') plt.scatter(gens, np.array(avg_fitness_per_generation) + np.array(std_fitness_per_generation), s=0.1, c='red') plt.scatter(gens, np.array(avg_fitness_per_generation) - np.array(std_fitness_per_generation), s=0.1, c='red') plt.scatter(gens, best_fitness_per_generation, s=0.1, c='blue') plt.plot(test_gens, intermediate_test_fitness_list, linewidth=1.0, c='blue') plt.ylabel("Average fitness") plt.xlabel("Generations") plt.savefig(average_imagename) # plt.show() plt.figure(figsize=(7.195, 3.841), dpi=150) for xe, ye in zip(gens, all_fitness_per_generation): plt.scatter([xe] * len(ye), ye, s=0.01, c='green') plt.ylabel("Fitness per individual") plt.xlabel("Generations") # plt.axes().set_xticklabels(generation) plt.savefig(individuals_imagename) # plt.show()
def main(use_futures, redirect_out=True): comm = MPI.COMM_WORLD rank = comm.Get_rank() # mode = MPI.MODE_CREATE | MPI.MODE_WRONLY if redirect_out: sys.stdout = open('out.txt', 'w') sys.stderr = open('err.txt', 'w') print("Prepare traits and genomes") neat_params = neat.Parameters() system( "grep -v '//' < input/neat_parameters.txt | grep . > input/neat_parameters.filtered.txt" ) neat_params.Load('input/neat_parameters.filtered.txt') system("rm input/neat_parameters.filtered.txt") # system("grep -v '//' < input/global_parameters.json | grep . > input/global_parameters.filtered.json") # network_parameters = json.load(open('input/global_parameters.filtered.json', 'r')) # system("rm input/global_parameters.filtered.json") # mode = MPI.MODE_RDONLY network_parameters = json.load(open('input/global_parameters.json', 'r')) # network_parameters = json.load(open(comm, 'input/global_parameters.json', mode)) for trait_name, trait_value in traits.network_traits.items(): neat_params.SetGenomeTraitParameters(trait_name, trait_value) for trait_name, trait_value in traits.neuron_traits.items(): # change to SetNeuronTraitParameters to let the neuron parameters mutate individually for each neuron neat_params.SetGenomeTraitParameters(trait_name, trait_value) for trait_name, trait_value in traits.synapse_traits.items(): # change to SetLinkTraitParameters to let the synapse parameters mutate individually for each synapse neat_params.SetGenomeTraitParameters(trait_name, trait_value) genome = neat.Genome( 0, # Some genome ID, I don't know what it means. network_parameters['inputs_number'], 2, # ignored for seed_type == 0, specifies number of hidden units if seed_type == 1 network_parameters['outputs_number'], False, #fs_neat. If == 1, a minimalistic perceptron is created: each output is connected to a random input and the bias. neat.ActivationFunction. UNSIGNED_SIGMOID, # output neurons activation function neat.ActivationFunction. UNSIGNED_SIGMOID, # hidden neurons activation function 0, # seedtype neat_params, # global parameters object returned by neat.Parameters() 0 # number of hidden layers ) population = neat.Population( genome, neat_params, True, # whether to randomize the population 0.5, # how much to randomize 0 # the RNG seed ) # fh = MPI.File.Open(comm, "datafile", mode) # line1 = str(comm.rank)*(comm.rank+1) + '\n' # line2 = chr(ord('a')+comm.rank)*(comm.rank+1) + '\n' # fh.Write_ordered(line1) # fh.Write_ordered(line2) # fh.Close() print("Start solving generations") outfile = open('output/fitness.txt', 'w') # mode = MPI.MODE_CREATE | MPI.MODE_WRONLY # outfile = MPI.File.Open(comm, 'output/fitness.txt', mode) # with open('output/fitness.txt', 'w') as outfile: for generation_number in range(network_parameters['generations']): print("Generation " + str(generation_number) + " started") genome_list = neat.GetGenomeList(population) fitnesses_list = [] # map(prepare_genomes, genome_list) for genome in genome_list: prepare_genomes(genome) if use_futures: executor = fut.MPIPoolExecutor() # fitnesses_list = executor.map(evaluate_futures, genome_list) for fitness in executor.map(evaluate_futures, genome_list): fitnesses_list.append(fitness) else: # fitnesses_list = map(evaluate, genome_list) for genome in genome_list: fitnesses_list.append(evaluate(genome)) neat.ZipFitness(genome_list, fitnesses_list) population.GetBestGenome().Save('output/best_genome.txt') # mode = MPI.MODE_APPEND # genomefile = MPI.File.Open(comm, 'output/best_genome.txt', mode) # genomefile.Write_ordered('\n' + str(population.GetBestGenome().GetNeuronTraits()) + # '\n' + str(population.GetBestGenome().GetGenomeTraits())) # genomefile.Close() genomefile = open('output/best_genome.txt', 'a') genomefile.write('\n' + str(population.GetBestGenome().GetNeuronTraits()) + '\n' + str(population.GetBestGenome().GetGenomeTraits())) genomefile.close() # copytree('genome' + str(population.GetBestGenome().GetID()), # 'output/generation' + str(generation_number) + '_best_genome') try: copytree( 'genome' + str(population.GetBestGenome().GetID()), 'output/generation' + str(generation_number) + '_best_genome') except FileExistsError: print('folder generation' + str(generation_number) + '_best_genome exists') # outfile.Write_ordered(str(generation_number) + '\t' + str(max(fitnesses_list)) + '\n') outfile.write( str(generation_number) + '\t' + str(max(fitnesses_list)) + '\n') outfile.flush() # sys.stderr.write( # '\rGeneration ' + str(generation_number) # + ': fitness = ' + str(population.GetBestGenome().GetFitness()) # ) # advance to the next generation print("Generation " + str(generation_number) + \ ": fitness = " + str(population.GetBestGenome().GetFitness())) print("Generation " + str(generation_number) + " finished") population.Epoch() # outfile.Close() outfile.close()
def create_params(): params = NEAT.Parameters() params.PopulationSize = 300 params.DynamicCompatibility = True params.CompatTreshold = 3.0 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 100 params.OldAgeTreshold = 35 params.MinSpecies = 5 params.MaxSpecies = 15 params.RouletteWheelSelection = False params.MutateRemLinkProb = 0.02 params.RecurrentProb = 0 params.OverallMutationRate = 0.15 params.MutateAddLinkProb = 0.03 params.MutateAddNeuronProb = 0.03 params.MutateWeightsProb = 0.90 params.MaxWeight = 8.0 params.WeightMutationMaxPower = 0.2 params.WeightReplacementMaxPower = 1.0 params.MutateActivationAProb = 0.0 params.ActivationAMutationMaxPower = 0.5 params.MinActivationA = 0.05 params.MaxActivationA = 6.0 params.MutateNeuronActivationTypeProb = 0.3 params.ActivationFunction_SignedGauss_Prob = 1.0 params.ActivationFunction_SignedStep_Prob = 1.0 params.ActivationFunction_Linear_Prob = 1.0 params.ActivationFunction_SignedSine_Prob = 1.0 params.ActivationFunction_SignedSigmoid_Prob = 1.0 params.ActivationFunction_Tanh_Prob = 0.0 params.ActivationFunction_UnsignedSigmoid_Prob = 0.0 params.ActivationFunction_TanhCubic_Prob = 0.0 params.ActivationFunction_UnsignedStep_Prob = 0.0 params.ActivationFunction_UnsignedGauss_Prob = 0.0 params.ActivationFunction_Abs_Prob = 0.0 params.ActivationFunction_UnsignedSine_Prob = 0.0 params.MutateNeuronTraitsProb = 0 params.MutateLinkTraitsProb = 0 params.DivisionThreshold = 0.5 params.VarianceThreshold = 0.03 params.BandThreshold = 0.3 params.InitialDepth = 2 params.MaxDepth = 3 params.IterationLevel = 1 params.Leo = False params.GeometrySeed = False params.LeoSeed = False params.LeoThreshold = 0.3 params.CPPN_Bias = 0.33 #-1.0# params.Qtree_X = 0.0 params.Qtree_Y = 0.0 params.Width = 1.0 params.Height = 1.0 params.Elitism = 0.1 return params
def __init__(self): self.population_size = 100 self.grid_row = 20 self.grid_col = 20 self.total_steps = 20 # Total roaming steps without goal before termination self.num_predator = 3 self.num_prey = 6 self.predator_random = 0 self.prey_random = 1 self.total_generations = 20000 self.angle_res = 45 self.observing_prob = 1 self.coupling = 2 # Number of predators required to simultaneously observe a prey self.is_memoried_predator = 0 arch_type_pred = 0 # 0-Quasi GRU; #1-Quasi NTM #Determine the neural architecture self.is_memoried_prey = 1 arch_type_prey = 1 # 0-Quasi GRU; #1-Quasi NTM #Determine the neural architecture self.prey_speed_boost = 1 self.periodic_poi_mode = 0 self.period = 0 self.arch_type = 1 # 0-Quasi GRU; #1-Quasi NTM #Determine the neural architecture if self.arch_type == 0: self.arch_type = 'quasi_gru' elif self.arch_type == 1: self.arch_type = 'quasi_ntm' else: sys.exit('Invalid choice of neural architecture') #Tertiary Variables (Closed for cleanliness) Open to modify if True: # GIVEN # DEAP/SSNE stuff self.use_ssne = 1 self.use_deap = 0 if self.use_deap or self.use_ssne: self.deap_param_predator = Deap_param( self.angle_res, self.is_memoried_predator, arch_type_pred) self.deap_param_prey = Deap_param(self.angle_res, self.is_memoried_prey, arch_type_prey) self.D_reward = 0 # D reward scheme self.prey_global = 0 #Prey's reward scheme self.wheel_action = 0 #TERTIARY self.domain_setup = 0 self.aamas_domain = 0 self.use_neat = 0 # Use NEAT VS. Keras based Evolution module self.obs_dist = 2 # Observe distance (Radius of prey that predators have to be in for successful observation) self.coupling = 1 # Number of predators required to simultaneously observe a prey self.use_py_neat = 0 # Use Python implementation of NEAT self.sensor_avg = True # Average distance as state input vs (min distance by default) self.state_representation = 1 # 1 --> Angled brackets, 2--> List of predator/preys self.num_evals_ccea = 3 #EV0-NET self.use_hall_of_fame = 0 self.hof_weight = 0.99 self.leniency = 0 # Fitness calculation based on leniency vs averaging if self.use_neat: #Neat if self.use_py_neat: #Python NEAT self.py_neat_config = Py_neat_params() else: #Multi-NEAT parameters import MultiNEAT as NEAT self.params = NEAT.Parameters() self.params.PopulationSize = self.population_size self.params.fs_neat = 1 self.params.evo_hidden = 10 self.params.MinSpecies = 5 self.params.MaxSpecies = 15 self.params.EliteFraction = 0.05 self.params.RecurrentProb = 0.2 self.params.RecurrentLoopProb = 0.2 self.params.MaxWeight = 8 self.params.MutateAddNeuronProb = 0.01 self.params.MutateAddLinkProb = 0.05 self.params.MutateRemLinkProb = 0.01 self.params.MutateRemSimpleNeuronProb = 0.005 self.params.MutateNeuronActivationTypeProb = 0.005 self.params.ActivationFunction_SignedSigmoid_Prob = 0.01 self.params.ActivationFunction_UnsignedSigmoid_Prob = 0.5 self.params.ActivationFunction_Tanh_Prob = 0.1 self.params.ActivationFunction_SignedStep_Prob = 0.1 else: #Use keras self.keras_evonet_hnodes = 25 # Keras based Evo-net's hidden nodes
def evolutionary_run(**kwargs): """ Conduct an evolutionary run using the worm. Args: gens: generations of evolution pop_size: population size mut_prob: mutation probability """ global args, current_network, run_num, output_path, population_size, simulation params = NEAT.Parameters() params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 1000 params.OldAgeTreshold = 35 params.MinSpecies = 1 params.MaxSpecies = 25 params.RouletteWheelSelection = False params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.4 params.MutateAddLinkProb = 0.4 params.MutateRemLinkProb = 0.05 params.PopulationSize = kwargs['pop_size'] # Calculate the number of inputs (without bias and periodic signal) and outputs based on the number of joints. num_outputs = kwargs['num_joints'] * 2 num_inputs = kwargs['num_joints'] * 2 + kwargs['num_joints'] + 1 # Initialize the populations genome = NEAT.Genome(0, num_inputs + 2, 0, num_outputs, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) # If not including a periodic input. if args.no_periodic: genome = NEAT.Genome(0, num_inputs + 1, 0, num_outputs, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) population = NEAT.Population(genome, params, True, 1.0) mnlog.write_population_statistics_headers(output_path + str(run_num) + "_fitnesses.dat") # Setup multiprocessing cores = mpc.cpu_count() pool = mpc.Pool(processes=cores - 2) for gen in xrange(kwargs['gens']): genome_list = NEAT.GetGenomeList(population) fitnesses = pool.map(evaluate_individual, genome_list) #fitnesses = [] #for g_l in genome_list: # fitnesses.append(evaluate_individual(g_l)) for g, f in zip(genome_list, fitnesses): g.SetFitness(f) print("Generation " + str(gen) + "\t: " + str(max(fitnesses))) # Write the best performing individual to a file. mnlog.write_best_individual( output_path + "best_individuals/Evo_NEAT_run_" + str(run_num) + "_best_gen_" + str(gen) + ".dat", genome_list[fitnesses.index(max(fitnesses))]) # Log the progress of the entire population. mnlog.write_population_statistics( output_path + str(run_num) + "_fitnesses.dat", genome_list, fitnesses, gen) # Log the final population for later evaluation. if gen == kwargs['gens'] - 1: population.Save(output_path + "run_" + str(run_num) + "_population_generation_" + str(gen) + ".dat") # Create the next generation population.Epoch()
def evolutionary_run(**kwargs): """ Conduct an evolutionary run. Args: gens: generations of evolution pop_size: population size mut_prob: mutation probability """ global args, current_network, run_num, output_path, population_size, simulation params = NEAT.Parameters() params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 50 params.OldAgeTreshold = 35 params.MinSpecies = 1 params.MaxSpecies = 25 params.RouletteWheelSelection = False params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.04 params.MutateAddLinkProb = 0.1 params.MutateRemSimpleNeuronProb = 0.04 params.MutateRemLinkProb = 0.1 # Phased Searching # params.PhasedSearching = True; # params.SimplifyingPhaseMPCTreshold = 20; # params.SimplifyingPhaseStagnationTreshold = 20; # params.ComplexityFloorGenerations = 20; params.PopulationSize = kwargs['pop_size'] params.Save(output_path + str(run_num) + "_NEAT_params.cfg") # Initialize the populations genome = NEAT.Genome(0, 22, 0, 16, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) # If not including a periodic input. if args.no_periodic: genome = NEAT.Genome(0, 21, 0, 16, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) population = NEAT.Population(genome, params, True, 1.0) genome_list = NEAT.GetGenomeList(population) mnlog.write_population_statistics_headers( output_path + str(run_num) + "_fitnesses.dat", optional_additions="Num_Neurons,Num_Connections") # Setup multiprocessing cores = mpc.cpu_count() pool = mpc.Pool(processes=cores - 2) for gen in xrange(kwargs['gens']): ind_descriptor = pool.map( evaluate_individual, genome_list) # 0 - fit, 1 - # neurons, 2 - # connections fitnesses = [] num_conns = [] num_neurons = [] for g, ind in zip(genome_list, ind_descriptor): g.SetFitness(ind[0]) fitnesses.append(ind[0]) num_neurons.append(ind[1]) num_conns.append(ind[2]) print("Generation " + str(gen) + "\t: " + str(max(fitnesses))) # Write the best performing individual to a file. mnlog.write_best_individual( output_path + "best_individuals/Evo_NEAT_Mus_run_" + str(run_num) + "_best_gen_" + str(gen) + ".dat", genome_list[fitnesses.index(max(fitnesses))]) # Log the progress of the entire population. mnlog.write_population_statistics( output_path + str(run_num) + "_fitnesses.dat", genome_list, fitnesses, gen, optional_additions=zip(num_neurons, num_conns)) # Log the final population for later evaluation. if gen == kwargs['gens'] - 1: population.Save(output_path + "run_" + str(run_num) + "_population_generation_" + str(gen) + ".dat") # Create the next generation population.Epoch() # Get the genomes genome_list = NEAT.GetGenomeList(population)
def evolutionary_run(**kwargs): """ Conduct an evolutionary run using the snake and muscle model. Args: gens: generations of evolution pop_size: population size mut_prob: mutation probability """ global args, current_network, run_num, output_path, population_size, simulation params = NEAT.Parameters() params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 50 params.OldAgeTreshold = 35 params.MinSpecies = 1 params.MaxSpecies = 25 params.RouletteWheelSelection = False params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.04 params.MutateAddLinkProb = 0.1 params.MutateRemSimpleNeuronProb = 0.04 params.MutateRemLinkProb = 0.1 # Phased Searching # params.PhasedSearching = True; # params.SimplifyingPhaseMPCTreshold = 20; # params.SimplifyingPhaseStagnationTreshold = 20; # params.ComplexityFloorGenerations = 20; params.PopulationSize = kwargs['pop_size'] params.Save(output_path+str(run_num)+"_NEAT_params.cfg") # Initialize the populations genome = NEAT.Genome(0, 22, 0, 8, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) # If not including a periodic input. if args.no_periodic: genome = NEAT.Genome(0, 21, 0, 8, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) population = NEAT.Population(genome, params, True, 1.0) genome_list = NEAT.GetGenomeList(population) # Initialize the muscle groups either symmetrically or not symmetrically. if args.sym_mus_groups: mus_networks = {ind.GetID(): MuscleNetwork(gid=ind.GetID(),num_groups=4,num_nodes=[4,4,4,4]) for ind in genome_list} else: mus_networks = {ind.GetID(): MuscleNetwork(gid=ind.GetID(),num_groups=8,num_nodes=[4,4,4,4,4,4,4,4]) for ind in genome_list} mnlog.write_population_statistics_headers(output_path+str(run_num)+"_fitnesses.dat",optional_additions="Num_Neurons,Num_Connections") # Setup multiprocessing cores = mpc.cpu_count() pool = mpc.Pool(processes=cores-2) # Zip the arguments for the evaluate wrapper function. zip_args = [(ind,mus_networks[ind.GetID()]) for ind in genome_list] for gen in xrange(kwargs['gens']): ind_descriptor = pool.map(evaluate_individual,zip_args) # fitnesses = pool.map( # Simulation(log_frames=args.log_frames, run_num=args.run_num, eval_time=args.eval_time, dt=.02, n=4), # zip_args # ) fitnesses = [] num_conns = [] num_neurons = [] for g,ind in zip(genome_list,ind_descriptor): g.SetFitness(ind[0]) fitnesses.append(ind[0]) num_neurons.append(ind[1]) num_conns.append(ind[2]) print("Generation "+str(gen)+"\t: "+str(max(fitnesses))) # Write the best performing individual to a file. mnlog.write_best_individual(output_path+"best_individuals/Evo_NEAT_Mus_run_"+str(run_num)+"_best_gen_"+str(gen)+".dat", genome_list[fitnesses.index(max(fitnesses))]) muslog.write_network(output_path+"best_individuals/Evo_NEAT_Mus_run_"+str(run_num)+"_best_mn_gen_"+str(gen)+".dat", mus_networks[genome_list[fitnesses.index(max(fitnesses))].GetID()]) # Log the progress of the entire population. mnlog.write_population_statistics(output_path+str(run_num)+"_fitnesses.dat", genome_list, fitnesses, gen, optional_additions=zip(num_neurons,num_conns)) # Log the final population for later evaluation. if gen == kwargs['gens'] - 1: population.Save(output_path+"run_"+str(run_num)+"_population_generation_"+str(gen)+".dat") muslog.write_networks(output_path+"run_"+str(run_num)+"_population_generation_mus_nets_"+str(gen)+".dat", [mn for k, mn in mus_networks.iteritems()]) # Create the next generation population.Epoch() new_mus_nets = {} zip_args = [] # Perform evolutionary development of the Muscle Networks. genome_list = NEAT.GetGenomeList(population) for ind in genome_list: pid1 = ind.GetPID1() pid2 = ind.GetPID2() gid = ind.GetID() # Handle Crossover if pid2 >= 0: new_mus_nets[gid] = mus_networks[pid1].crossover(mus_networks[pid2]) else: new_mus_nets[gid] = mus_networks[pid1].copy() # Handle Mutation new_mus_nets[gid].mutate(kwargs['mut_prob']) # Set the Genome ID in the new muscle node. new_mus_nets[gid].gid = gid zip_args.append((ind,new_mus_nets[gid])) mus_networks = new_mus_nets
def __init__(self): self.population_size = 500 self.D_reward = 1 # D reward scheme self.grid_row = 15 self.grid_col = 15 self.total_steps = 25 # Total roaming steps without goal before termination self.num_agents = 3 self.num_poi = 10 self.agent_random = 0 self.poi_random = 1 self.wheel_action = 0 self.angle_res = 90 self.total_generations = 10000 #DEAP stuff self.use_deap = 1 self.is_memoried = 0 if self.use_deap: self.deap_param = Deap_param(self.angle_res, self.is_memoried) #POI periodicity self.period = 3 self.test_observation = True #Tertiary Variables (Closed for cleanli #DEAP stuff self.is_memoried = 0 if True: # GIVEN #TERTIARY self.use_keras = 0 self.domain_setup = 0 self.aamas_domain = 0 self.use_neat = 0 # Use NEAT VS. Keras based Evolution module self.obs_dist = 1 # Observe distance (Radius of POI that agents have to be in for successful observation) self.coupling = 1 # Number of agents required to simultaneously observe a POI self.use_py_neat = 0 # Use Python implementation of NEAT self.sensor_avg = True # Average distance as state input vs (min distance by default) self.state_representation = 1 # 1 --> Angled brackets, 2--> List of agent/POIs #EV0-NET self.use_hall_of_fame = 0 self.hof_weight = 0.99 self.leniency = 1 # Fitness calculation based on leniency vs averaging if self.use_neat: #Neat if self.use_py_neat: #Python NEAT self.py_neat_config = Py_neat_params() else: #Multi-NEAT parameters import MultiNEAT as NEAT self.params = NEAT.Parameters() self.params.PopulationSize = self.population_size self.params.fs_neat = 1 self.params.evo_hidden = 10 self.params.MinSpecies = 5 self.params.MaxSpecies = 15 self.params.EliteFraction = 0.05 self.params.RecurrentProb = 0.2 self.params.RecurrentLoopProb = 0.2 self.params.MaxWeight = 8 self.params.MutateAddNeuronProb = 0.01 self.params.MutateAddLinkProb = 0.05 self.params.MutateRemLinkProb = 0.01 self.params.MutateRemSimpleNeuronProb = 0.005 self.params.MutateNeuronActivationTypeProb = 0.005 self.params.ActivationFunction_SignedSigmoid_Prob = 0.01 self.params.ActivationFunction_UnsignedSigmoid_Prob = 0.5 self.params.ActivationFunction_Tanh_Prob = 0.1 self.params.ActivationFunction_SignedStep_Prob = 0.1 elif self.use_keras: #Use keras self.keras_evonet_hnodes = 100 # Keras based Evo-net's hidden nodes
cwd = os.getcwd() # Saving and Loading genome_suffix = '.mng' # default = '.mng' (MultiNeatGenome) # Debug verbose = True # EVO NET CONFIG client_connection_timeout = 1000 client_comm_cycle_time = 10 # default: 10 evo_client_map = {} # MULTINEAT CONFIG para_file = cwd + '/params.mnp' params = MN.Parameters() # params.PopulationSize is set by init_pop random_seed = 0 # MY EVO CONFIG n_generations = 100000 timeout_one_gen = 300 # seconds # VREP COMM MODES mode1 = sim.simx_opmode_oneshot mode2 = sim.simx_opmode_oneshot_wait mode3 = sim.simx_opmode_buffer mode4 = sim.simx_opmode_streaming + 50 mode5 = sim.simx_opmode_discontinue mode6 = sim.simx_opmode_blocking
for _ in range(2): net.Activate() o = net.Output() error += abs(o[0]) net.Flush() net.Input([0, 0, 1]) for _ in range(2): net.Activate() o = net.Output() error += abs(o[0]) return (4 - error) ** 2 params = NEAT.Parameters() params.PopulationSize = 150 params.DynamicCompatibility = True params.WeightDiffCoeff = 4.0 params.CompatTreshold = 2.0 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 15 params.OldAgeTreshold = 35 params.MinSpecies = 5 params.MaxSpecies = 10 params.RouletteWheelSelection = False params.RecurrentProb = 0.0 params.OverallMutationRate = 0.8 params.MutateWeightsProb = 0.90
def evolutionary_run(gens, pop_size, output_path, run_num, numofjoints): """ Conduct an evolutionary run using the snake and muscle model. """ params = NEAT.Parameters() params.CompatTreshold = 5.0 params.CompatTresholdModifier = 0.3 params.YoungAgeTreshold = 15 params.SpeciesMaxStagnation = 1000 params.OldAgeTreshold = 35 params.MinSpecies = 1 params.MaxSpecies = 25 params.RouletteWheelSelection = False params.RecurrentProb = 0.25 params.OverallMutationRate = 0.33 params.MutateWeightsProb = 0.90 params.WeightMutationMaxPower = 1.0 params.WeightReplacementMaxPower = 5.0 params.MutateWeightsSevereProb = 0.5 params.WeightMutationRate = 0.75 params.MaxWeight = 20 params.MutateAddNeuronProb = 0.4 params.MutateAddLinkProb = 0.4 params.MutateRemLinkProb = 0.05 params.CrossoverRate = 0.4 assert pop_size >= 0, "wrong population size argument! pop_size: %d" % pop_size params.PopulationSize = pop_size # worm has only one dof (turning around y-axis) per joint num_outputs = numofjoints # the inputs for the ANN are the 7 current joint positions and the amplitude of a sine wave as well as a bias num_inputs = numofjoints + 1 + 1 # Initialize the population # Genome(ID, NumInputs, NumHidden, NumOutputs, ActivationFunction?, Output activation function, Hidden layer acitvation function, seed, params) genome = NEAT.Genome(0, num_inputs, 0, num_outputs, False, NEAT.ActivationFunction.SIGNED_SIGMOID, NEAT.ActivationFunction.SIGNED_SIGMOID, 0, params) # Population(Genome, params, randomizedweights?, randomrange) population = NEAT.Population(genome, params, True, 1.0) genome_list = NEAT.GetGenomeList(population) morph_pop = MorphGenomes(pop_size) for ind in genome_list: morph_pop.addIndividual(ind.GetID()) morph_genomes = morph_pop.getGenomes() # ANN genome and morphology genome are zipped together # Zip the two genome components together for use in the parallel call. zip_args = [(ind, morph_genomes[ind.GetID()]) for ind in genome_list] mnlog.write_population_statistics_headers(output_path + str(run_num) + "_fitnesses.dat") # Setup multiprocessing cores = mpc.cpu_count() #cores = 1 pool = mpc.Pool(initializer=initProcess, initargs=( GlobalVarWorkaround.args, GlobalVarWorkaround.man, GlobalVarWorkaround.worm, ), processes=cores) assert gens >= 0, "wrong number of generations as argument! gens: %d" % gens for gen in xrange(gens): print gen #fitnesses = map(evaluate_individual,zip_args) # serial execution fitnesses = pool.map(evaluate_individual, zip_args) replace_nanfitnesses(fitnesses) for g, f in zip(genome_list, fitnesses): g.SetFitness(f) print("Generation " + str(gen) + "\t: " + str(max(fitnesses))) # Write the best performing individual to a file. mnlog.write_best_individual( output_path + "best_individuals/Evo_NEAT_run_" + str(run_num) + "_best_gen_" + str(gen) + ".dat", genome_list[fitnesses.index(max(fitnesses))]) morph_pop.logGenome( genome_list[fitnesses.index(max(fitnesses))].GetID(), "Evo_NEAT_run_" + str(run_num) + "_best_gen_" + str(gen), output_path) # Write information about the best individual we wrote. with open( output_path + "/" + str(run_num) + "_best_individuals_logging.dat", "a") as f: f.write("Generation: "+str(gen)+" Individual is: "+str(genome_list[fitnesses.index(max(fitnesses))].GetID())+\ " Fitness is: "+str(max(fitnesses))+"\n") # Log the progress of the entire population. mnlog.write_population_statistics( output_path + str(run_num) + "_fitnesses.dat", genome_list, fitnesses, gen) # Log the final population for later evaluation. if gen == gens - 1: population.Save(output_path + "run_" + str(run_num) + "_population_generation_" + str(gen) + ".dat") # Create the next generation population.Epoch() genome_list = NEAT.GetGenomeList(population) morph_pop.NextGen() zip_args = [] for ind in genome_list: # PID .. parent ID pid1 = ind.GetPID1() # if pid2 is negative it means that no crossover happend! pid2 = ind.GetPID2() gid = ind.GetID() # Handle Crossover morph_pop.Crossover(gid, pid1, pid2) # Handle Mutation morph_pop.MutatePopulation() # Zip the arguments for calling the evolution function. morph_genomes = morph_pop.getGenomes() zip_args = [(ind, morph_genomes[ind.GetID()]) for ind in genome_list]
def random_initialization(seed=None): import random if seed != None: random.seed(seed) else: from datetime import datetime random.seed(datetime.now()) # Algorithm Parameters params = NEAT.Parameters() params.PopulationSize = random.randint(5, 1000) # Min Max number of species params.MinSpecies = min(random.randint(1, 10), params.PopulationSize) params.MaxSpecies = min(max(random.randint(1, 20), params.MinSpecies), params.PopulationSize) # GA Parameters # Percent of best individuals that are allowed to reproduce. 1.0 = 100% params.SurvivalRate = round(random.random() / 2, 2) # Probability for a baby to result from sexual reproduction (crossover/mating). 1.0 = 100% params.CrossoverRate = round(random.random(), 2) # mutate only 0.25 # If a baby results from sexual reproduction, this probability determines if mutation will # be performed after crossover. 1.0 = 100% (always mutate after crossover) params.OverallMutationRate = round(random.random(), 2) # Probability for a baby to result from Multipoint Crossover when mating. 1.0 = 100% # The default if the Average mating. params.MultipointCrossoverRate = round(random.random(), 2) # Mutation parameters # Probability for a baby to be mutated with the Add-Neuron mutation. params.MutateAddNeuronProb = round(random.random(), 2) # Probability for a baby to be mutated with the Add-Link mutation params.MutateAddLinkProb = round(random.random(), 2) # Probability that a link mutation will be made recurrent params.RecurrentProb = round(random.random(), 2) # Probability for a baby's weights to be mutated params.MutateWeightsProb = round(random.random(), 2) # Probability for a particular gene to be mutated. 1.0 = 100% params.WeightMutationRate = round(random.random(), 2) # Probabilities for a particular activation function appearance params.ActivationFunction_SignedSigmoid_Prob = 1.0 params.ActivationFunction_UnsignedSigmoid_Prob = 0.0 params.ActivationFunction_Tanh_Prob = 1.0 params.ActivationFunction_TanhCubic_Prob = 0.0 params.ActivationFunction_SignedStep_Prob = 1.0 params.ActivationFunction_UnsignedStep_Prob = 0.0 params.ActivationFunction_SignedGauss_Prob = 1.0 params.ActivationFunction_UnsignedGauss_Prob = 0.0 params.ActivationFunction_Abs_Prob = 1.0 params.ActivationFunction_SignedSine_Prob = 1.0 params.ActivationFunction_UnsignedSine_Prob = 0.0 params.ActivationFunction_Linear_Prob = 1.0 params.ActivationFunction_Relu_Prob = 1.0 params.ActivationFunction_Softplus_Prob = 0.0 return params