def test_can_set_initial_population( default_evolutionary_algorithm: EvolutionaryAlgorithm, ): ind1 = default_evolutionary_algorithm.individual_structures.build() ind2 = default_evolutionary_algorithm.individual_structures.build() default_evolutionary_algorithm.set_initial_population([ind1, ind2]) assert ind1 in default_evolutionary_algorithm._population assert ind2 in default_evolutionary_algorithm._population
def test_initial_population_is_preserved( default_evolutionary_algorithm: EvolutionaryAlgorithm, ): ind1 = default_evolutionary_algorithm.individual_structures.build() ind2 = default_evolutionary_algorithm.individual_structures.build() default_evolutionary_algorithm.set_initial_population([ind1, ind2]) assert ind1 in default_evolutionary_algorithm._population assert ind2 in default_evolutionary_algorithm._population default_evolutionary_algorithm.ensure_population_initialized() assert ind1 in default_evolutionary_algorithm._population assert ind2 in default_evolutionary_algorithm._population assert (len(default_evolutionary_algorithm._population) == default_evolutionary_algorithm.population_size)
def test_iteration_regenerates_right_sized_population(): eva = EvolutionaryAlgorithm( ranker=Ranker(lambda x: 1.0), selector=Random(selection_size=1), population_size=5, generations=1, individual_structure=UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)), multiple_individual_operators=[], single_individual_operators=[], ) eva.run() assert len(eva._population) == 5
def test_iteration_calls_selector(): selector = Random(selection_size=1) eva = EvolutionaryAlgorithm( ranker=Ranker(lambda x: 1.0), selector=selector, population_size=1, generations=1, individual_structure=UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)), multiple_individual_operators=[], single_individual_operators=[], ) expect(selector).__call__.once().and_return([(1, )]) eva.run() assert eva._population[0] == (1, )
def test_iteration_calls_ranker(): ranker = Ranker(lambda x: 1.0) assert not ranker.ranked_population eva = EvolutionaryAlgorithm( ranker=ranker, selector=Random(selection_size=1), population_size=1, generations=1, individual_structure=UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)), multiple_individual_operators=[], single_individual_operators=[], ) eva.run() assert len(eva._population) == len(ranker.ranked_population) assert set(eva._population) == set(r[0] for r in ranker.ranked_population)
def test_iteration_calls_multiple_individual_operator(): individual_structure = UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)) crossover_operator = OnePointCrossoverOperator(individual_structure) eva = EvolutionaryAlgorithm( ranker=Ranker(lambda x: 1.0), selector=Random(selection_size=1), population_size=1, generations=1, individual_structure=individual_structure, multiple_individual_operators=[(crossover_operator, 1)], single_individual_operators=[], ) expect(crossover_operator).__call__.once().and_return((1, )) eva.run() assert eva._population[0] == (1, )
def test_iterations_calls_callback(): counter = 0 def callback(_: Evolution): nonlocal counter counter += 1 eva = EvolutionaryAlgorithm( ranker=Ranker(lambda x: 1.0), selector=Random(selection_size=1), population_size=1, generations=1, individual_structure=UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)), multiple_individual_operators=[], single_individual_operators=[], iteration_callbacks=[callback], ) eva.run() assert counter == 1
def test_iteration_calls_multiple_instances_of_single_individual_operator(): individual_structure = UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)) mutation_operator = MutationOperator(individual_structure) mutation_operator_2 = MutationOperator(individual_structure) eva = EvolutionaryAlgorithm( ranker=Ranker(lambda x: 1.0), selector=Random(selection_size=1), population_size=1, generations=1, individual_structure=individual_structure, multiple_individual_operators=[], single_individual_operators=[(mutation_operator, 1), (mutation_operator_2, 1)], ) expect(mutation_operator).__call__.once().and_return((1, )) expect(mutation_operator_2).__call__.once().with_args((1, )).and_return( (0, )) eva.run() assert eva._population[0] == (0, )
def default_evolutionary_algorithm( default_score_function) -> EvolutionaryAlgorithm: return EvolutionaryAlgorithm( population_size=10, generations=10, selector=Tournament(selection_size=2, tournament_size=10), ranker=Ranker(default_score_function), individual_structure=UniformIndividualStructure( tuple(IntGene(lower_bound=0, upper_bound=10) for _ in range(10))), multiple_individual_operators=[], single_individual_operators=[], )
def initialize(self, score_function: Callable[[IndividualType], Q]): if self._selector: return EvolutionaryAlgorithm( population_size=self.population_size, generations=self.generations, individual_structure=self._individual_structure, ranker=self._ranker(score_function, executor=self._executor), selector=self._selector, multiple_individual_operators=self._multiple_individual_operators, single_individual_operators=self._single_individual_operators, elite_size=self.elite_size, iteration_callbacks=self._callbacks, ) assert False, "Must specify selector!"
def test_iteration_preserves_elites(): ranker = Ranker(lambda x: 1.0) expect(ranker).rank.once() ranker.ranked_population = [((0, ), 3)] + [((1, ), 0)] * 4 selector = Random(selection_size=1) expect(selector).__call__.once().and_return([(1, )]) eva = EvolutionaryAlgorithm( ranker=ranker, selector=selector, population_size=5, generations=1, individual_structure=UniformIndividualStructure( IntGene(lower_bound=0, upper_bound=1)), multiple_individual_operators=[], single_individual_operators=[], elite_size=1, ) # invert order, to make sure that we're indeed selecting the elites. eva.set_initial_population(ranker.ranked_population[-1:]) eva.run() assert eva._population[0] == (0, ) assert len(eva._population) == 5
def test_ensure_run_initialises_population( default_evolutionary_algorithm: EvolutionaryAlgorithm, ): default_evolutionary_algorithm.run() assert (len(default_evolutionary_algorithm._population) == default_evolutionary_algorithm.population_size)
def test_ensure_initial_population_generates_right_number_of_individuals( default_evolutionary_algorithm: EvolutionaryAlgorithm, ): default_evolutionary_algorithm.ensure_population_initialized() assert (len(default_evolutionary_algorithm._population) == default_evolutionary_algorithm.population_size)