# J.Madge 05.11.2017 RESISTANCE: Create the next generation. res_next_gen_genes = [] res_next_gen_genes += p.top(promote, current_gen, ResGenotype.Type) for i in xrange(tournament_num): parent1 = p.tournament(tournament_size, current_gen, ResGenotype.Type) parent2 = p.tournament(tournament_size, current_gen, ResGenotype.Type) child = parent1.crossover(parent2, res_cross_pos, ResGenotype.Type) child.mutate(res_mutations, mutation_lower, mutation_upper) res_next_gen_genes.append(child) for i, g in enumerate(res_next_gen_genes): g.reset_data(i) p.insert(g, next_gen) # # J.Madge 05.11.2017 SPY: Create the next generation. spy_next_gen_genes = [] spy_next_gen_genes += p.top(promote, current_gen, SpyGenotype.Type) for i in xrange(tournament_num): parent1 = p.tournament(tournament_size, current_gen, SpyGenotype.Type) parent2 = p.tournament(tournament_size, current_gen, SpyGenotype.Type) child = parent1.crossover(parent2, spy_cross_pos, SpyGenotype.Type) child.mutate(spy_mutations, mutation_lower, mutation_upper) spy_next_gen_genes.append(child) for i, g in enumerate(spy_next_gen_genes): g.reset_data(i) p.insert(g, next_gen) p.set_gen(next_gen)
from genetic_algorithm import Population initial_generation = 0 # J.Madge 05.11.2017 Number of the initial generation. population_size = 20 # J.Madge 05.11.2017 Number of genotypes in the population. initialise_lower_bound = 0.4 # J.Madge 05.11.2017 Lowest initial values a gene can be initialized to. initialise_upper_bound = 0.6 # J.Madge 05.11.2017 Highest initial values a gene can be initialized to. p = Population() # J.Madge 05.11.2017 Set the population state (size of the population, number of the initial generation). p.create_state() p.set_size(population_size) p.set_gen(initial_generation) # J.Madge 05.11.2017 Create the initial generation and initialize the contained genotypes to the lower and upper bounds. p.create(initial_generation) p.create('Best') p.initialise(initial_generation, initialise_lower_bound, initialise_upper_bound)