def run(self): """Run the algorithm until termination.""" while self._full_step(): if self.generation >= self.config.max_generations: break verbose_solution = self.config.verbosity_config.solution_found if verbose_solution >= self.config.verbosity_config.log_level: if self._is_solved(): log(verbose_solution, "Unsimplified solution found.") else: log(verbose_solution, "No unsimplified solution found.") # Simplify the best individual for a better generalization and interpreteation. simplifier = GenomeSimplifier(self.config.evaluator, self.config.verbosity_config) simp_genome, simp_error_vector = simplifier.simplify( self.best_seen.genome, self.best_seen.error_vector, self.config.simplification_steps ) simplified_best = Individual(simp_genome) simplified_best.error_vector = simp_error_vector if verbose_solution >= self.config.verbosity_config.log_level: if self._is_solved(): log(verbose_solution, "Simplified solution found.") else: log(verbose_solution, "No simplified solution found.") return simplified_best
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. """ if self._get_temp() <= 0: return candidate = Individual( self.config.get_variation_op().produce( [self.population.best().genome], self.config.spawner ) ) 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 test_best(self, partially_evaluated_pop): i = Individual(Genome([])) i.error_vector = np.array([0, 1, 0]) partially_evaluated_pop.add(i) assert partially_evaluated_pop.best().total_error == 0.0
def test_add_evaluated(self, partially_evaluated_pop): i = Individual(Genome([])) i.error_vector = np.array([0, 1, 0]) partially_evaluated_pop.add(i) assert len(partially_evaluated_pop) == 5 assert len(partially_evaluated_pop.evaluated) == 3
def test_add_unevaluated(self, partially_evaluated_pop, simple_program_signature): partially_evaluated_pop.add(Individual(Genome([]), simple_program_signature)) assert len(partially_evaluated_pop) == 5 assert len(partially_evaluated_pop.unevaluated) == 3
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])