コード例 #1
0
def test_ranker_ranks_with_executor():
    ranker = Ranker(score_function, executor=ThreadPoolExecutor(max_workers=1))
    pop = [(0, 0, 0), (1, 0, 1), (1, 1, 1)]
    ranker.rank(pop)
    assert ranker.ranked_population[0] == ((1, 1, 1), 1)
    assert ranker.ranked_population[1] == ((1, 0, 1), float(2) / 3)
    assert ranker.ranked_population[2] == ((0, 0, 0), 0)
コード例 #2
0
def test_ranker_ranks_without_executor():
    ranker = Ranker(score_function)
    pop = [(0, 0, 0), (1, 0, 1), (1, 1, 1)]
    ranker.rank(pop)
    assert ranker.ranked_population[0] == ((1, 1, 1), 1)
    assert ranker.ranked_population[1] == ((1, 0, 1), float(2) / 3)
    assert ranker.ranked_population[2] == ((0, 0, 0), 0)
コード例 #3
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=[],
    )
コード例 #4
0
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
コード例 #5
0
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
コード例 #6
0
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, )
コード例 #7
0
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)
コード例 #8
0
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, )
コード例 #9
0
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
コード例 #10
0
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, )