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)
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()
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)
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()
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()
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)
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)
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)
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)
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)
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)
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)
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)