示例#1
0
    def test_add_to_initial_population_successful(self):
        class SpyFitnessFunc:
            def __init__(self):
                self.fitness_func_executions = 0

            def fitness_func(self, bitstring):
                self.fitness_func_executions += 1
                return 69.0

        unused_spy_fitness_function_holder = SpyFitnessFunc()
        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome(
                (5, ), unused_spy_fitness_function_holder.fitness_func))
        annealing_schedule = PassThroughAnnealingSchedule(0, True)
        optimizer_builder = BasicAnnealerOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, annealing_schedule,
            unused_spy_fitness_function_holder.fitness_func)

        spy_fitness_function_holder = SpyFitnessFunc()
        population_genome = Genome(spy_fitness_function_holder.fitness_func,
                                   (0, 0, 0, 0, 0))

        optimizer = optimizer_builder \
            .add_to_initial_population(population_genome) \
            .build()

        self.assertAlmostEqual(optimizer.best_genome.fitness, 69.0)
        self.assertEqual(spy_fitness_function_holder.fitness_func_executions,
                         1)
        self.assertIsInstance(optimizer, BasicAnnealingOptimizer)
示例#2
0
    def test_multiple_population_add(self):
        class SpyFitnessFunc:
            def __init__(self):
                self.fitness_func_executions = 0

            def fitness_func(self, bitstring):
                self.fitness_func_executions += 1
                return 69.0

        unused_spy_fitness_function_holder = SpyFitnessFunc()
        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome(
                (5, ), unused_spy_fitness_function_holder.fitness_func))
        convergence_criterion = PassThroughConvergenceCriterion(True)
        optimizer_builder = BasicOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, convergence_criterion,
            unused_spy_fitness_function_holder.fitness_func)

        spy_fitness_function_holder = SpyFitnessFunc()
        population_genome = Genome(spy_fitness_function_holder.fitness_func,
                                   (0, 0, 0, 0, 0))

        optimizer = optimizer_builder \
            .add_to_initial_population(population_genome) \
            .build()

        self.assertAlmostEqual(optimizer.best_genome.fitness, 69.0)
        self.assertEqual(spy_fitness_function_holder.fitness_func_executions,
                         1)
        self.assertIsInstance(optimizer, BasicOptimizer)

        with self.assertRaises(InitialPopulationUndefinedException):
            optimizer_builder.clear().build()
示例#3
0
    def test_convergence(self):
        def fitness_func_zero(bitstring):
            return 0

        initial_candidate = Genome(fitness_func_zero, (0,))

        convergence_criterion = PassThroughConvergenceCriterion(True)
        genome_builder = PassThroughGenomeFactory(Genome(fitness_func_zero, (1,)))
        optimizer = BasicOptimizer(initial_candidate, genome_builder, convergence_criterion)

        self.assertFalse(optimizer.converged)
        optimizer.next()
        self.assertTrue(optimizer.converged)
示例#4
0
    def test_build_raises_exception_with_no_population(self):
        def fitness_func(bitstring):
            self.fail()
            return 0.0

        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome((5, ), fitness_func))
        annealing_schedule = PassThroughAnnealingSchedule(0, True)
        optimizer_builder = BasicAnnealerOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, annealing_schedule,
            fitness_func)

        with self.assertRaises(InitialPopulationUndefinedException):
            optimizer_builder.build()
示例#5
0
    def test_build_raises_exception_with_no_population(self):
        def fitness_func(bitstring):
            self.fail()
            return 0.0

        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome((5, ), fitness_func))
        convergence_criterion = PassThroughConvergenceCriterion(True)
        optimizer_builder = BasicOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, convergence_criterion,
            fitness_func)

        with self.assertRaises(InitialPopulationUndefinedException):
            optimizer_builder.build()
示例#6
0
    def test_add_to_initial_population_returns_self(self):
        def fitness_func(bitstring):
            self.fail()
            return 0.0

        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome((5, ), fitness_func))
        annealing_schedule = PassThroughAnnealingSchedule(0, True)
        optimizer_builder = BasicAnnealerOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, annealing_schedule,
            fitness_func)

        returned_object = optimizer_builder.add_to_initial_population(
            Genome.new_default_genome((5, ), fitness_func))
        self.assertEqual(optimizer_builder, returned_object)
示例#7
0
    def test_add_to_initial_population_returns_self(self):
        def fitness_func(bitstring):
            self.fail()
            return 0.0

        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome((5, ), fitness_func))
        convergence_criterion = PassThroughConvergenceCriterion(True)
        optimizer_builder = BasicOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, convergence_criterion,
            fitness_func)

        returned_object = optimizer_builder.add_to_initial_population(
            Genome.new_default_genome((5, ), fitness_func))
        self.assertEqual(optimizer_builder, returned_object)
示例#8
0
    def test_next_generation_improvement(self):
        def fitness_func_zero(bitstring):
            return 0

        def fitness_func_one(bitstring):
            return 1

        initial_candidate = Genome(fitness_func_zero, (0,))

        convergence_criterion = PassThroughConvergenceCriterion(True)
        genome_builder = PassThroughGenomeFactory(Genome(fitness_func_one, (1,)))
        optimizer = BasicOptimizer(initial_candidate, genome_builder, convergence_criterion)

        optimizer.next()

        self.assertEqual(1, optimizer.best_genome.fitness)
示例#9
0
    def test_add_to_initial_population_from_factory_successful(self):
        def fitness_func(bitstring):
            return 69.0

        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome((5, ), fitness_func))
        annealing_schedule = PassThroughAnnealingSchedule(0, True)
        optimizer_builder = BasicAnnealerOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, annealing_schedule,
            fitness_func)

        optimizer = optimizer_builder \
            .add_to_initial_population_from_factory(genome_factory, 1) \
            .build()

        self.assertAlmostEqual(optimizer.best_genome.fitness, 69.0)
        self.assertIsInstance(optimizer, BasicAnnealingOptimizer)
示例#10
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)
示例#11
0
    def test_add_to_initial_population_from_factory_successful(self):
        def fitness_func(bitstring):
            return 69.0

        genome_factory = PassThroughGenomeFactory(
            Genome.new_default_genome((5, ), fitness_func))
        convergence_criterion = PassThroughConvergenceCriterion(True)
        optimizer_builder = BasicOptimizerBuilder(
            (5, ), NumpyRNG(), genome_factory, convergence_criterion,
            fitness_func)

        optimizer = optimizer_builder \
            .add_to_initial_population_from_factory(genome_factory, 1) \
            .build()

        self.assertAlmostEqual(optimizer.best_genome.fitness, 69.0)
        self.assertIsInstance(optimizer, BasicOptimizer)
示例#12
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)
示例#13
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)