def init_population(self): """Initialize the population.""" self.population = Population() for i in range(self.config.population_size): spawner = self.config.spawner genome = spawner.spawn_genome(self.config.initial_genome_size) self.population.add(Individual(genome))
def step(self): """Perform one generation, or step, of the Simulated Annealing. The step method assumes an evaluated Population one Individual and produces a single candidate Individual. If the candidate individual passes the acceptance function, it becomes the Individual in the Population. """ super().step() if self._get_temp() <= 0: return candidate = Individual( self.config.get_variation_op().produce( [self.population.best().genome], self.config.spawner ), self.config.signature ) candidate.error_vector = self.config.evaluator.evaluate(candidate.program) acceptance_probability = self._acceptance(candidate.total_error) if np.random.random() < acceptance_probability: self.population = Population().add(candidate)
def step(self): """Perform one generation (step) of the genetic algorithm. The step method assumes an evaluated Population and performs parent selection and variation (producing children). """ self.population = Population( [self._make_child() for _ in range(self.config.population_size)])
def pop(atoms): i1 = Individual(Genome()) i1.error_vector = np.array([0, 20, 0]) # 20 i2 = Individual(Genome()) i2.error_vector = np.array([3, 3, 3]) # 9 i3 = Individual(Genome()) i3.error_vector = np.array([1, 2, 3]) # 6 i4 = Individual(Genome()) i4.error_vector = np.array([4, 3, 5]) # 12 return Population([i1, i2, i3, i4])
def init_population(self): """Initialize the population.""" spawner = self.config.spawner init_gn_size = self.config.initial_genome_size pop_size = self.config.population_size signature = self.config.signature self.population = Population() if self._p_context is not None: gen_func = partial(_spawn_individual, self._p_context.ns.spawner, init_gn_size, signature) for indiv in self._p_context.pool.imap_unordered(gen_func, range(pop_size)): self.population.add(indiv) else: for i in range(pop_size): self.population.add(_spawn_individual(spawner, init_gn_size, signature))
def population(atoms, push_config): gn = Genome() sig = ProgramSignature(arity=0, output_stacks=[], push_config=push_config) i1 = Individual(gn, sig) i1.error_vector = np.array([0, 20, 0]) # 20 i2 = Individual(gn, sig) i2.error_vector = np.array([3, 3, 3]) # 9 i3 = Individual(gn, sig) i3.error_vector = np.array([1, 2, 3]) # 6 i4 = Individual(gn, sig) i4.error_vector = np.array([4, 3, 5]) # 12 return Population([i1, i2, i3, i4])
def test_empty_population_len(self): assert len(Population()) == 0
def evaluated_pop(simple_individuals): for i in simple_individuals: i.error_vector = np.arange(3) return Population(simple_individuals)
def partially_evaluated_pop(simple_individuals): simple_individuals[0].error_vector = np.array([1, 2, 3]) simple_individuals[2].error_vector = np.array([0, 0, 0]) return Population(simple_individuals)
def unevaluated_pop(simple_individuals): return Population(simple_individuals)
def test_genome_diversity_empty_pop(self): with pytest.raises(ZeroDivisionError): Population().genome_diversity()
def test_median_error_empty_pop(self): assert np.isnan(Population().median_error()).all()
def test_error_diversity_empty_pop(self): with pytest.raises(ValueError): Population().error_diversity()