def pareto_front_population(): size_of_list = 6 population = [] for i in range(size_of_list + 1): values = [False] * (size_of_list - i) + [True] * i indv = MultipleValueChromosome(values) indv.genetic_age = i population.append(indv) return population
def test_fitness_is_not_inherited_crossover(): crossover = SinglePointCrossover() parent1 = MultipleValueChromosome( [np.random.choice([True, False]) for _ in range(10)]) parent2 = MultipleValueChromosome( [np.random.choice([True, False]) for _ in range(10)]) child1, child2 = crossover(parent1, parent2) assert not child1.fit_set assert not child2.fit_set
def test_adding_trainers_to_predictor_fitness_function( predictor_fitness_function): for i in range(10): trainer = MultipleValueChromosome([i]) predictor_fitness_function.add_trainer(trainer) assert trainer not in predictor_fitness_function._trainers assert np.any([[i] == trainer.values for trainer in predictor_fitness_function._trainers])
def non_dominated_population(): young_weak = MultipleValueChromosome([False, False]) middle_average = MultipleValueChromosome([False, True]) middle_average.genetic_age = 1 old_fit = MultipleValueChromosome([True, True]) old_fit.genetic_age = 2 return [young_weak, middle_average, old_fit]
def test_predicted_fitness_for_trainer(predictor_fitness_function, sample_population): predictor = MultipleValueChromosome([0, 9]) for i, trainer in enumerate(sample_population): prediction = \ predictor_fitness_function.predict_fitness_for_trainer(predictor, trainer) expected_prediction = i + 4.5 np.testing.assert_almost_equal(prediction, expected_prediction)
def test_mutation_is_single_point(): mutator = SinglePointMutation(mutation_onemax_specific) parent = MultipleValueChromosome( [np.random.choice([True, False]) for _ in range(10)]) child = mutator(parent) discrepancies = 0 for i in range(len(parent.values)): if child.values[i] != parent.values[i]: discrepancies += 1 assert discrepancies <= 1
def selected_indiviudals(pareto_front_population): list_size = len(pareto_front_population[0].values) list_one = [False] * int(list_size / 2) + [True] * int((list_size + 1) / 2) list_two = [False] * int((list_size + 1) / 2) + [True] * int(list_size / 2) selected_indv_one = MultipleValueChromosome(list_one) selected_indv_two = MultipleValueChromosome(list_two) selected_indv_one.genetic_age = list_size selected_indv_two.genetic_age = list_size + 1 return [selected_indv_one, selected_indv_two]
def test_genetic_age_is_oldest_parent(): crossover = SinglePointCrossover() parent1 = MultipleValueChromosome([np.random.choice([True, False]) for _ in range(10)]) parent2 = MultipleValueChromosome([np.random.choice([True, False]) for _ in range(10)]) parent1.genetic_age = 8 parent2.genetic_age = 4 child1, child2 = crossover(parent1, parent2) assert child1.genetic_age == 8 assert child2.genetic_age == 8
def sample_bool_list_chromosome(): chromosome = MultipleValueChromosome( [np.random.choice([True, False]) for _ in range(10)]) return chromosome
def test_fitness_predictor_fitness_function_call(predictor_fitness_function, predictor_values): predictor = MultipleValueChromosome(predictor_values) expected_fitness = abs(4.5 - np.mean(predictor_values)) fitness = predictor_fitness_function(predictor) np.testing.assert_almost_equal(fitness, expected_fitness)
def sample_population(): return [MultipleValueChromosome(list(range(i, i + 10))) for i in range(10)]
def sample_int_list_chromosome(): chromosome = MultipleValueChromosome( [np.random.choice([1, 0]) for _ in range(10)]) return chromosome
def test_fitness_is_not_inherited_mutation(): mutator = SinglePointMutation(mutation_onemax_specific) parent = MultipleValueChromosome( [np.random.choice([True, False]) for _ in range(10)]) child = mutator(parent) assert not child.fit_set