def add_member(self, genotype: Genotype, diffs: int = None) -> None: if genotype in self.members: self.members.remove(genotype) genotype.species_diff = self.calculate_compatibility( genotype)[1] if diffs is None else diffs genotype.species_id = self.id self.members.append(genotype)
def _mutate_topology(self, genotype: Genotype, species: Species = None): species_template = ( {}, {}) if species is None else species.get_mutate_template() add_node = np.random.uniform() < self.prop_top_add_node if add_node: genotype.mutate_add_node(self.config, self.innovation_map, species_template) if np.random.uniform() < self.prop_top_add_edge and not add_node: genotype.mutate_add_edge(self.config, self.innovation_map, species_template)
def restart_to_best(self, config: Config, best_genotype: Genotype, population: List[Genotype], innovation_map: InnovationMap): best_template = best_genotype.get_template() best_genotype.origin_generation = config.generation best_genotype.ancestor = None for genotype in population: if genotype in self.members: continue old_w = genotype.fitness / best_genotype.fitness if best_genotype.fitness != 0 else 1.0 genotype.mutate_to_template(config, best_template, old_w, innovation_map) self.add_member(genotype)
def get_by_compatibility(self, representative: Genotype, count: int) -> List[Genotype]: if count <= 0: return [] compatible = [] uncompatible = [] for g in self.archive: if representative.is_compatible( g, self.config.compatibility_max_diffs)[0]: compatible.append(g) else: uncompatible.append(g) compatible_indices = np.argsort([g.score for g in compatible])[::-1] candidates = [ compatible[compatible_indices[i]] for i in range(min(len(compatible_indices), count)) ] for c in candidates: self.archive.remove(c) self.removes += len(candidates) return candidates
def crossover_impl(self, parents: List[Genotype]) -> Tuple[Genotype, float, float, PopulationStrategies]: cr = self.generate_cr() f = np.random.uniform() if np.random.uniform() < 0.2 else self._generate_mutation_factor_cauchy() self.cr.append(cr) self.f.append(f) return Genotype.crossover(self.config.generation, f, cr, parents[0], parents[1], parents[2], parents[3]), f, cr, self.strategy
def crossover_impl(self, parents: List[Genotype]) -> Tuple[Genotype, float, float, PopulationStrategies]: cr = np.random.normal(0.9, 0.1) cr = 1.0 if cr > 1.0 else (0.1 if cr < 0.1 else cr) f = self.generate_mutation_factor_norm() self.cr.append(cr) self.f.append(f) return Genotype.crossover_triple(self.config.generation, f, cr, parents[0], parents[1], parents[2], parents[3]), f, cr, self.strategy
def __init__(self, config: Config): self._species_id_counter = -1 self.reached_max_species_generation = 0 self.reached_max_species = False self.config = config self.innovation_map = InnovationMap() self.archive = Archive(self.config) self.compatibility = [] # type: List[float] self.species = [] # type: List[Species] self.population_offsprings = [ ] # type: List[Tuple[Genotype, Species, PopulationStrategies]] self.succ_offsprings = [] # type: List[Genotype] # self.test_seed = [i for i in range(self.config.seed_test)] # self.config.dataset.get_random_seed(self.config.seed_test) self.test_seed = self.config.dataset.get_random_seed( self.config.seed_test) # self.seed = random.sample(self.test_seed, self.config.seed_max) self.seed = self.config.dataset.get_random_seed(self.config.seed_max) # self.seed = [5320921] # self.seed = [42] self.last_merge = 0 self.crossover_manager = CrossoverManager(config) self.mutation_manager = MutationManager(config, self.innovation_map) self.population = Genotype.init_population( self.config, self.innovation_map) # type: List[Genotype] self.evaluate_ancestors() self.comp_before = 1 self.comp_after = 1 self.specs_before = 0 self.specs_after = 0 self.stagnating = 0 self.avg_score = [] self._next_growth = 10
def get_offspring(self, ancestor: Genotype, population: List[Genotype], elite: List[Genotype], species: List[Genotype], species_elite: List[Genotype], config: Config) -> Tuple[Genotype, float, float, PopulationStrategies]: if np.random.uniform() < 0.01: # or config.generation - ancestor.origin_generation >= 5: return ancestor.mutation_copy(config.generation), 0.0, 0.0, PopulationStrategies.SKIP strategy = self.get_strategy() if strategy == self.curr_to_best_spec: best = self.roulette_select(species_elite, 1, [ancestor])[0] parents = self.roulette_select(species, 2, [best, ancestor]) return self.curr_to_best_spec.crossover([ancestor, best, parents[0], parents[1]]) elif strategy == self.curr_to_best_interspec: best = self.roulette_select(elite, 1, [ancestor])[0] parents = self.roulette_select(population, 2, [ancestor, best]) return self.curr_to_best_interspec.crossover([ancestor, best, parents[0], parents[1]]) elif strategy == self.curr_to_rand_spec: parents = self.roulette_select(species, 3, [ancestor]) return self.curr_to_rand_spec.crossover([ancestor, parents[0], parents[1], parents[2]]) elif strategy == self.curr_to_rand_interspec: parents = self.roulette_select(population, 3, [ancestor]) return self.curr_to_rand_interspec.crossover([ancestor, parents[0], parents[1], parents[2]]) elif strategy == self.rand_to_rand_spec: parents = self.roulette_select(species, 3) return self.rand_to_rand_spec.crossover([ancestor, parents[0], parents[1], parents[2]]) elif strategy == self.rand_to_rand_interspec: parents = self.roulette_select(population, 3) return self.rand_to_rand_interspec.crossover([ancestor, parents[0], parents[1], parents[2]]) elif strategy == self.rand_to_best_spec: best = self.roulette_select(species_elite, 1)[0] parents = self.roulette_select(species, 2, [best]) return self.rand_to_best_spec.crossover([ancestor, best, parents[0], parents[1]]) elif strategy == self.rand_to_best_interspec: best = self.roulette_select(elite, 1)[0] parents = self.roulette_select(population, 2, [best]) return self.rand_to_best_interspec.crossover([ancestor, best, parents[0], parents[1]])