Exemplo n.º 1
0
def test_keep_pareto_front_miss_target_pop_size(pareto_front_population,
                                                onemax_evaluator,
                                                selected_indiviudals):
    selected_indv_one = selected_indiviudals[0]
    selected_indv_two = selected_indiviudals[1]
    population = pareto_front_population + selected_indiviudals
    onemax_evaluator(population)
    print("starting population")
    for i in population:
        print(i.fitness, i.genetic_age)

    age_fitness_selection = AgeFitness(selection_size=len(population))
    new_population = age_fitness_selection(population, TARGET_POP_SIZE)
    print("new population")
    for i in new_population:
        print(i.fitness, i.genetic_age)

    assert len(new_population) == len(pareto_front_population)

    selected_indvs_removed = True
    for indv in new_population:
        if (indv.genetic_age == selected_indv_one and
                indv.values == selected_indv_one.values) or \
               (indv.genetic_age == selected_indv_two and
                indv.values == selected_indv_two.values):
            selected_indvs_removed = False
            break
    assert selected_indvs_removed
Exemplo n.º 2
0
def test_all_but_one_removed(all_dominated_population, onemax_evaluator):
    age_fitness_selection = AgeFitness()
    onemax_evaluator(all_dominated_population)

    target_pop_size = 1
    new_population = age_fitness_selection(all_dominated_population,
                                           target_pop_size)
    assert len(new_population) == target_pop_size
Exemplo n.º 3
0
def test_none_selected_for_removal(non_dominated_population, onemax_evaluator):
    age_fitness_selection = AgeFitness()
    onemax_evaluator(non_dominated_population)

    target_pop_size = 1
    new_population = age_fitness_selection(non_dominated_population,
                                           target_pop_size)
    assert len(new_population) == len(non_dominated_population)
Exemplo n.º 4
0
def test_all_removed_in_one_iteration(weak_individual, fit_individual,
                                      onemax_evaluator):
    population = [weak_individual for _ in range(10)] + [fit_individual]
    onemax_evaluator(population)

    age_fitness_selection = AgeFitness(selection_size=len(population))

    target_pop_size = 1
    new_population = age_fitness_selection(population, target_pop_size)

    assert len(new_population) == target_pop_size
    assert new_population[0].values == [True]
Exemplo n.º 5
0
def test_all_but_one_removed_large_selection_size(strong_population,
                                                  weak_individual,
                                                  onemax_evaluator):
    population = strong_population + [weak_individual]
    onemax_evaluator(population)

    age_fitness_selection = AgeFitness(selection_size=len(strong_population))

    target_pop_size = 1
    new_population = age_fitness_selection(population, target_pop_size)

    assert len(new_population) == target_pop_size
    assert new_population[0].values == [True]
Exemplo n.º 6
0
def test_selection_size_larger_than_population(weak_population, fit_individual,
                                               onemax_evaluator):
    population = weak_population + [fit_individual]
    onemax_evaluator(population)

    age_fitness_selection = AgeFitness(selection_size=(len(population) + 100))

    target_pop_size = 2
    new_population = age_fitness_selection(population, target_pop_size)

    assert len(new_population) == target_pop_size
    count = 1
    for indv in new_population:
        if not any(indv.values):
            count *= 2
        elif all(indv.values):
            count *= 3

    assert count == 6
Exemplo n.º 7
0
def test_target_population_size_is_valid(strong_population):
    age_fitness_selection = AgeFitness()
    with pytest.raises(ValueError):
        age_fitness_selection(strong_population, len(strong_population) + 1)