def _aux(aux_params): number_of_variables = aux_params['number_of_variables'] previous_generations_top = [] for i in range(aux_params['iterations']): seed = aux_params['pool'] test_params = aux_params debug._format_output(['{a} Seed'.format(a=i)] + seed, number_of_variables) solved, pool = _is_solved(test_params, previous_generations_top) previous_generations_top.append(pool) if len(previous_generations_top) > 13: previous_generations_top.pop(0) if solved: return pool test_params['pool'] = reproduction.reproduce(test_params) debug._format_output(['reproduction'] + test_params['pool'], number_of_variables) test_params['pool'] = crossover.crossover(test_params) debug._format_output(['crossover'] + test_params['pool'], number_of_variables) test_params['pool'] = mutation.mutate_pool( test_params['pool'], test_params['mutation_probability']) debug._format_output(['mutated'] + test_params['pool'], number_of_variables) aux_params['pool'] = test_params['pool'] return previous_generations_top[-1]
def reproduction(self, simulation_step, probability_female_reproduce=1, offspring_size=[2], offspring_parameter='constant', zero_offspring_possible=False, sex_ratio=0.5): ''' Function reproduction This function produces new individuals and their characteristics, given the reproductive individuals that are alive and the reproductive parameters. Input: Output: ''' if self.include_reproduction: self.new_inds = reproduce( individual_id=self.individual_id, individual_sex=self.sex, individual_reproductive_status=self.reproductive_status, simulation_step=simulation_step, group=self.group, individual_group=self.individual_group, individual_positions=self.positions, set_genetics=self.include_genetics, individual_genetics=self.genetics, probability_female_reproduce=probability_female_reproduce, offspring_size=offspring_size, offspring_parameter=offspring_parameter, zero_offspring_possible=zero_offspring_possible, sex_ratio=sex_ratio)
def reproduction(self, simulation_step, probability_female_reproduce = 1, offspring_size = [2], offspring_parameter = 'constant', zero_offspring_possible = False, sex_ratio = 0.5): ''' Function reproduction This function produces new individuals and their characteristics, given the reproductive individuals that are alive and the reproductive parameters. Input: Output: ''' if self.include_reproduction: self.new_inds = reproduce(individual_id = self.individual_id, individual_sex = self.sex, individual_reproductive_status = self.reproductive_status, simulation_step = simulation_step, group = self.group, individual_group = self.individual_group, individual_positions = self.positions, set_genetics = self.include_genetics, individual_genetics = self.genetics, probability_female_reproduce = probability_female_reproduce, offspring_size = offspring_size, offspring_parameter = offspring_parameter, zero_offspring_possible = zero_offspring_possible, sex_ratio = sex_ratio)
def evolution_step(population, **args): for individual in population: variate.mutate(individual.RNN) evaluation.evaluate_population(population, **args) population = reproduction.reproduce(population, **args) return population
def threaded_reproduce(self): ''' Reproduces the creatures ''' self.builder.get_object('progress')['value'] = 0 self.builder.get_object('reproduce')['state'] = 'disabled' creatures = copy(self.creatures) total_creatures = len(creatures) self.creatures = [] k = 0 for i, creature in enumerate(creatures): for _ in range(OFFSPRINGS_PER_SELECTION_SIZE): self.creatures.append(creature) creature.frame.grid(row=k // COL_COUNT, column=k % COL_COUNT) k += 1 offspring = reproduce(creature, self.serializable_creatures) self.create_creature(offspring, k) k += 1 progress = i * 100 // total_creatures self.builder.get_object('progress')['value'] = progress for i in range(k, k + RANDOM_NEW_POPULATION_SIZE): creature = Creature(n=random.randint(MIN_VERTICES_COUNT, MAX_VERTICES_COUNT), view_port=self.scroll_frame.view_port, size=MAX_SIZE) self.create_creature(creature, i) self.builder.get_object( 'details')['text'] = f'Generation #{self.get_generation()}' self.builder.get_object('progress')['value'] = 0 self.builder.get_object('train')['state'] = 'active' self.builder.get_object('find_fitness')['state'] = 'active' self.builder.get_object('find_fitness_no_gui')['state'] = 'active'
def handle_collisions(self, state): for character in state.sprites: collided_chars = pygame.sprite.spritecollide( character, state.sprites, False) if (collided_chars): (repro_partner, hadchild) = character.collide(collided_chars, state.day) if (hadchild): character.eval_fitness(state.day) repro_partner.eval_fitness(state.day) new_child = pygame_helpers.spawn_critter( state, character.x, character.y) (genome, network) = reproduction.reproduce( state.NEAT_CONFIG, state.GID_INDEX, character.genome, repro_partner.genome) new_child.setbrainandgenome(state.GID_INDEX, genome, network) state.GID_INDEX += 1 collided_plants = pygame.sprite.spritecollide( character, state.plants, False) if (collided_plants): character.collideplants(collided_plants, state.day)
def threaded_reproduce(self): ''' Reproduces the creatures ''' print('Reproducing the creatures') creatures = copy(self.creatures) self.creatures = [] for creature in creatures: self.creatures.append(creature) for _ in range(OFFSPRINGS_PER_SELECTION_SIZE): offspring = reproduce(creature, self.serializable_creatures) self.creatures.append(offspring) for _ in range(RANDOM_NEW_POPULATION_SIZE): creature = Creature( n=random.randint(MIN_VERTICES_COUNT, MAX_VERTICES_COUNT), size=MAX_SIZE) self.creatures.append(creature)
def update(self, state): state.FONT = pygame.font.SysFont("comicsansms", 24) state.FOOD_TEXTURE = pygame_helpers.load_image('food.png') state.CHAR_TEXTURES = [ (False, pygame_helpers.load_image('F_01.png')), (True, pygame_helpers.load_image('M_01.png')) ] state.GID_INDEX = 1 state.NEAT_CONFIG = neat.Config( neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, "config", ) # load default genome pickle_in = open("default_genome.p", "rb") default_genome = pickle.load(pickle_in) state.screen = pygame.display.get_surface() state.clock = pygame.time.Clock() state.new_day = False state.day = 1 state.width, state.height = state.screen.get_size() # split work for this into processes later state.world = Island(state.width, state.height) state.sprites, state.plants = pygame.sprite.Group( ), pygame.sprite.Group() for _ in range(self.population): x, y = pygame_helpers.random_position(state) new_critter = pygame_helpers.spawn_critter(state, x, y) state.sprites.add(new_critter) # give each critter a brain based on the input one (genome, network) = reproduction.reproduce( state.NEAT_CONFIG, state.GID_INDEX, default_genome, default_genome ) new_critter.setbrainandgenome(state.GID_INDEX, genome, network) state.GID_INDEX += 1 for _ in range(self.bush_pop): x, y = pygame_helpers.random_position(state) state.plants.add(Bush(state.FOOD_TEXTURE, state.day, x, y)) pygame.time.set_timer(pygame.USEREVENT + 1, 5000) print('finished loading') return Play()