def comma_algo(add_e_evaluation, add_v_variation, add_s_selection): crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) evo_alg = MuCommaLambda(add_e_evaluation, add_s_selection, crossover, mutation, 0.2, 0.4, 20) evo_alg.variation = add_v_variation return evo_alg
def test_just_replication(population): crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) var_or_instance = VarOr(crossover, mutation, 0.0, 0.0) _ = var_or_instance(population, 25) for cross, mut in zip(var_or_instance.crossover_offspring, var_or_instance.mutation_offspring): assert not (cross or mut)
def evol_alg(): crossover = SinglePointCrossover() mutation = SinglePointMutation(NumberGenerator(-1)) selection = Tournament(SELECTION_SIZE) fitness = MultipleValueFitnessFunction() evaluator = Evaluation(fitness) return MuPlusLambda(evaluator, selection, crossover, mutation, 0.2, 0.4, OFFSPRING_SIZE)
def ev_alg(full_training_data): crossover = SinglePointCrossover() mutation = SinglePointMutation(np.random.random) selection = Tournament(2) fitness = DistanceToAverage(full_training_data) evaluator = Evaluation(fitness) return MuPlusLambda(evaluator, selection, crossover, mutation, 0., 1.0, MAIN_POPULATION_SIZE)
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 island(): crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) selection = Tournament(10) fitness = MultipleValueFitnessFunction() evaluator = Evaluation(fitness) ev_alg = MuPlusLambda(evaluator, selection, crossover, mutation, 0.2, 0.4, 20) generator = MultipleValueChromosomeGenerator(mutation_function, 10) return Island(ev_alg, generator, 25)
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 test_crossover(population): crossover = SinglePointCrossover() child_1, child_2 = crossover(population[0], population[1]) cross_pt = crossover._crossover_point assert child_1.values[:cross_pt] == \ population[0].values[:cross_pt] assert child_2.values[:cross_pt] == \ population[1].values[:cross_pt] assert child_1.values[cross_pt:] == \ population[1].values[cross_pt:] assert child_2.values[cross_pt:] == \ population[0].values[cross_pt:]
def create_evolutionary_algorithm(): crossover = SinglePointCrossover() mutation = SinglePointMutation(generate_0_or_1) variation_phase = VarOr(crossover, mutation, crossover_probability=0.4, mutation_probability=0.4) fitness = OneMaxFitnessFunction() evaluation_phase = Evaluation(fitness) selection_phase = Tournament(tournament_size=2) return EvolutionaryAlgorithm(variation_phase, evaluation_phase, selection_phase)
def main(): crossover = SinglePointCrossover() mutation = SinglePointMutation(get_random_float) selection = Tournament(10) fitness = ZeroMinFitnessFunction() local_opt_fitness = ContinuousLocalOptimization(fitness) evaluator = Evaluation(local_opt_fitness) ea = MuPlusLambda(evaluator, selection, crossover, mutation, 0.4, 0.4, 20) generator = MultipleFloatChromosomeGenerator(get_random_float, 8) island = Island(ea, generator, 25) island.evolve(1) report_max_min_mean_fitness(island) island.evolve(500) report_max_min_mean_fitness(island)
def test_island_hof(mocker): hof = mocker.Mock() crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) selection = Tournament(10) fitness = MultipleValueFitnessFunction() evaluator = Evaluation(fitness) ev_alg = MuPlusLambda(evaluator, selection, crossover, mutation, 0.2, 0.4, 20) generator = MultipleValueChromosomeGenerator(mutation_function, 10) island = Island(ev_alg, generator, 25, hall_of_fame=hof) island.evolve(10) hof.update.assert_called_once() hof_update_pop = hof.update.call_args[0][0] for h, i in zip(hof_update_pop, island.population): assert h == i
def test_invalid_probabilities(): crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) with pytest.raises(ValueError): _ = VarOr(crossover, mutation, 0.6, 0.41)
def var_or(): crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) var_or_instance = VarOr(crossover, mutation, 0.2, 0.4) return var_or_instance
def dc_ea(onemax_evaluator): crossover = SinglePointCrossover() mutation = SinglePointMutation(return_true) return DeterministicCrowdingEA(onemax_evaluator, crossover, mutation, 0.2, 0.2)