Exemple #1
0
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])
Exemple #2
0
 def test_add_evaluated(self, partially_evaluated_pop,
                        simple_program_signature):
     i = Individual(Genome([]), simple_program_signature)
     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
Exemple #3
0
    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:
            if self._is_solved():
                verbose_solution("Unsimplified solution found.")
            else:
                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:
            if self._is_solved():
                verbose_solution("Simplified solution found.")
            else:
                verbose_solution("No simplified solution found.")

        return simplified_best
Exemple #4
0
    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
Exemple #5
0
    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 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])
Exemple #7
0
    def run(self) -> Individual:
        """Run the algorithm until termination."""
        while self._full_step():
            if self.generation >= self.config.max_generations:
                break

        # Simplify the best individual for a better generalization and interpretation.
        simplifier = GenomeSimplifier(self.config.evaluator,
                                      self.config.signature)
        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, self.config.signature)
        simplified_best.error_vector = simp_error_vector
        self.best_seen = simplified_best
        return self.best_seen
Exemple #8
0
    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)
Exemple #9
0
 def test_best(self, partially_evaluated_pop, simple_program_signature):
     i = Individual(Genome([]), simple_program_signature)
     i.error_vector = np.array([0, 1, 0])
     partially_evaluated_pop.add(i)
     assert partially_evaluated_pop.best().total_error == 0.0
Exemple #10
0
 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
Exemple #11
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
Exemple #12
0
def _eval_indiv(indiv: Individual, evalr: Evaluator, ):
    indiv.error_vector = evalr.evaluate(indiv.program)
    return indiv
Exemple #13
0
 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
Exemple #14
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