mutation_upper = 1.2 # J.Madge 05.11.2017 Upper limit of the mutation operator. p = Population() current_gen = p.gen() next_gen = current_gen + 1 # J.Madge 06.11.2017 Select the best Resistance and best Spy to represent this generation. best_res = p.top(1, current_gen, ResGenotype.Type)[0] best_res.set_index(current_gen) p.insert(best_res, 'Best') best_spy = p.top(1, current_gen, SpyGenotype.Type)[0] best_spy.set_index(current_gen) p.insert(best_spy, 'Best') # J.Madge 05.11.2017 Create database for the next generation. p.create(next_gen) # 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)
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)