def test_get_pareto_front(pareto_front_population, selected_indiviudals, onemax_evaluator): selection = AgeFitness() population = pareto_front_population + selected_indiviudals onemax_evaluator(population) new_population = selection.select_pareto_front(population) assert collections.Counter(new_population) == \ collections.Counter(pareto_front_population)
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
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)
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] assert age_fitness_selection._selection_attempts == 1
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] assert age_fitness_selection._selection_attempts == 2
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 assert age_fitness_selection._selection_attempts == 1 count = 1 for indv in new_population: if not any(indv.values): count *= 2 elif all(indv.values): count *= 3 assert count == 6
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) age_fitness_selection = AgeFitness(selection_size=len(population)) new_population = age_fitness_selection(population, TARGET_POP_SIZE) 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
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)