def test_incorrect_dimensions_passed_in(self):
        def fitness_func(bitstring):
            return 0.0

        left = Genome(fitness_func, (0, 0, 0, 0, 0))
        right = Genome(fitness_func, (1, 1, 1, 1, 1))
        dimensions = (4,)

        comparer = IncrementalComparer()
        rng = NumpyOnesRandom()
        genome_builder = UniformCrossoverGenomeFactory(dimensions, rng, 0.0, comparer, fitness_func)

        with self.assertRaises(DimensionsMismatchException):
            genome_builder.build([left, right])
    def test_right_inheritance(self):
        def fitness_func(bitstring):
            return 0.0

        left = Genome(fitness_func, 2 * np.ones((5, 5)))
        right = Genome(fitness_func, 3 * np.ones((5, 5)))
        dimensions = (5, 5)

        comparer = IncrementalComparer()
        rng = NumpyPassThroughRandom(0.8)
        genome_builder = UniformCrossoverGenomeFactory(dimensions, rng, 0.0, comparer, fitness_func)

        new_genome = genome_builder.build([left, right])

        self.assertTrue(np.array_equal(right.bitstring, new_genome.bitstring))
    def test_mutation(self):
        def fitness_func(bitstring):
            return 0.0

        left = Genome(fitness_func, (0, 0, 0, 0, 0))
        right = Genome(fitness_func, (0, 0, 0, 0, 0))
        dimensions = (5,)

        comparer = IncrementalComparer()
        rng = NumpyOnesRandom()
        genome_builder = UniformCrossoverGenomeFactory(dimensions, rng, 1.0, comparer, fitness_func)

        new_genome = genome_builder.build([left, right])

        self.assertEqual([1, 1, 1, 1, 1], list(new_genome.bitstring))
Exemplo n.º 4
0
    def build(self, prior_genomes: List[Genome]) -> Genome:
        """ Builds a new Genome from a list of priors

        :param prior_genomes: list of prior Genomes
        :return: a new Genome object
        """
        def bit_flip(bit_value):
            if bit_value == 0:
                return 1
            else:
                return 0

        def recursive_bit_flip(bitstring, dimensions):
            random_index = self._rng.random_int(0, dimensions[0])
            if len(dimensions) == 1:
                bitstring[random_index] = bit_flip(bitstring[random_index])
            else:
                recursive_bit_flip(bitstring[random_index], dimensions[1:])

        selected_genome = prior_genomes[self._rng.random_int(
            0, len(prior_genomes))]
        candidate_genome_bitstring = deepcopy(selected_genome.bitstring)

        for _ in range(self._n):
            recursive_bit_flip(candidate_genome_bitstring, self._dimensions)

        return Genome(self._fitness_func, candidate_genome_bitstring,
                      *self._args, **self._kwargs)
Exemplo n.º 5
0
    def test_convergence(self):
        def fitness_func_zero(bitstring):
            return 0

        initial_population = [Genome(fitness_func_zero, (0, ))]
        for genome in initial_population:
            genome.run()

        convergence_criterion = PassThroughConvergenceCriterion(True)
        genome_builder = PassThroughGenomeFactory(
            Genome(fitness_func_zero, (1, )))
        optimizer = BasicGeneticOptimizer(initial_population, genome_builder,
                                          convergence_criterion)

        self.assertFalse(optimizer.converged)
        optimizer.next()
        self.assertTrue(optimizer.converged)
Exemplo n.º 6
0
    def test_convergence(self):
        def fitness_func_zero(bitstring):
            return 0

        initial_candidate = Genome(fitness_func_zero, (0, ))
        initial_candidate.run()

        annealing_schedule = PassThroughAnnealingSchedule(0.9, True)
        genome_builder = PassThroughGenomeFactory(
            Genome(fitness_func_zero, (1, )))
        rng = NumpyCustomRandom(0.0, 1)
        optimizer = BasicAnnealingOptimizer(initial_candidate, genome_builder,
                                            annealing_schedule, rng)

        self.assertFalse(optimizer.converged)
        self.assertAlmostEqual(0.9, optimizer.temperature)
        optimizer.next()
        self.assertTrue(optimizer.converged)
        self.assertAlmostEqual(0.9, optimizer.temperature)
Exemplo n.º 7
0
    def test_next_generation_improvement(self):
        def fitness_func_zero(bitstring):
            return 0

        def fitness_func_one(bitstring):
            return 1

        initial_population = [Genome(fitness_func_zero, (0, ))]
        for genome in initial_population:
            genome.run()

        convergence_criterion = PassThroughConvergenceCriterion(True)
        genome_builder = PassThroughGenomeFactory(
            Genome(fitness_func_one, (1, )))
        optimizer = BasicGeneticOptimizer(initial_population, genome_builder,
                                          convergence_criterion)

        optimizer.next()

        self.assertEqual(1, optimizer.best_genome.fitness)
    def test_inheritance(self):
        def fitness_func(bitstring):
            return 0.0

        left = Genome(fitness_func, 2 * np.ones((2, 6)))
        right = Genome(fitness_func, 3 * np.ones((2, 6)))
        dimensions = (2, 6)

        comparer = IncrementalComparer()
        rng = NumpyPassThroughRandom(3)
        genome_builder = SinglePointCrossoverGenomeFactory(
            dimensions, rng, 0.0, comparer, fitness_func)

        expected = np.ones((2, 6))
        expected[:, 3:] = 3
        expected[:, :3] = 2

        new_genome = genome_builder.build([left, right])

        self.assertTrue(np.array_equal(expected, new_genome.bitstring))
Exemplo n.º 9
0
    def test_worse_solution_acceptance(self):
        def fitness_func_zero(bitstring):
            return 0

        def fitness_func_one(bitstring):
            return 1

        initial_candidate = Genome(fitness_func_one, (0, ))
        initial_candidate.run()

        annealing_schedule = PassThroughAnnealingSchedule(0.9, True)
        genome_builder = PassThroughGenomeFactory(
            Genome(fitness_func_zero, (1, )))
        rng = NumpyCustomRandom(0.0, 1)
        optimizer = BasicAnnealingOptimizer(initial_candidate, genome_builder,
                                            annealing_schedule, rng)

        optimizer.next()

        self.assertEqual(0, optimizer.best_genome.fitness)
        self.assertAlmostEqual(0.9, optimizer.temperature)
Exemplo n.º 10
0
    def build(self, prior_genomes: List[Genome]) -> Genome:
        """ Builds a new Genome from a list of priors

        :param prior_genomes: list of prior Genomes
        :return: a new Genome object
        """
        def recursive_gene_selection(sub_dimensions, sub_new_bitstring,
                                     sub_left_bitstring, sub_right_bitstring):
            if not (sub_dimensions[0] == len(sub_right_bitstring)
                    and sub_dimensions[0] == len(sub_left_bitstring)):
                raise DimensionsMismatchException

            if len(sub_dimensions) == 1:
                sub_new_bitstring[:
                                  crossover_index] = sub_left_bitstring[:
                                                                        crossover_index]
                sub_new_bitstring[crossover_index:] = sub_right_bitstring[
                    crossover_index:]

                for index in range(sub_dimensions[0]):
                    if self._rng.random() < self._mutation_rate:
                        sub_new_bitstring[index] = self._rng.random_int(0, 2)
            else:
                for index in range(sub_dimensions[0]):
                    recursive_gene_selection(sub_dimensions[1:],
                                             sub_new_bitstring[index],
                                             sub_left_bitstring[index],
                                             sub_right_bitstring[index])

        new_bitstring = self._rng.random_int_array(0, 2, self._dimensions)
        left_prior = self._comparer.compare(prior_genomes).bitstring
        right_prior = self._comparer.compare(prior_genomes).bitstring
        crossover_index = self._rng.random_int(1, self._dimensions[-1])

        recursive_gene_selection(self._dimensions, new_bitstring, left_prior,
                                 right_prior)

        return Genome(self._fitness_func, new_bitstring, *self._args,
                      **self._kwargs)