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 evol_alg(): crossover = SinglePointCrossover() mutation = SinglePointMutation(mutation_function) selection = Tournament(SELECTION_SIZE) fitness = MultipleValueFitnessFunction() evaluator = Evaluation(fitness) return MuPlusLambda(evaluator, selection, crossover, mutation, 0.2, 0.4, OFFSPRING_SIZE)
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 ev_alg(): crossover = SinglePointCrossover() mutation = SinglePointMutation(np.random.random) selection = Tournament(2) training_data = np.linspace(0.1, 1, FULL_TRAINING_DATA_SIZE) fitness = DistanceToAverage(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 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 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 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) best_indv_values = [] best_indv_values.append(island.best_individual().values) for i in range(500): island.execute_generational_step() best_indv_values.append(island.best_individual().values) bingo.animation.animate_data(best_indv_values)
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) for i in range(25): island.execute_generational_step() print("\nGeneration #", i) print("-" * 80, "\n") report_max_min_mean_fitness(island.population) print("\npopulation: \n") for indv in island.population: print(["{0:.2f}".format(val) for val in indv.values])
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)