def init_island(): np.random.seed(10) x = init_x_vals(START, STOP, NUM_POINTS) y = equation_eval(x) training_data = ExplicitTrainingData(x, y) component_generator = ComponentGenerator(x.shape[1]) component_generator.add_operator(2) component_generator.add_operator(3) component_generator.add_operator(4) crossover = AGraphCrossover() mutation = AGraphMutation(component_generator) agraph_generator = AGraphGenerator(STACK_SIZE, component_generator) fitness = ExplicitRegression(training_data=training_data) local_opt_fitness = ContinuousLocalOptimization(fitness, algorithm='lm') evaluator = Evaluation(local_opt_fitness) ea = AgeFitnessEA(evaluator, agraph_generator, crossover, mutation, MUTATION_PROBABILITY, CROSSOVER_PROBABILITY, POP_SIZE) island = Island(ea, agraph_generator, POP_SIZE) return island
def test_evaluation_evaluates_all_list_values_per_individual( population, fitness_function): evaluation = Evaluation(fitness_function) evaluation(population) assert evaluation.eval_count == 25 for indv in population: assert indv.fit_set assert indv.fitness is not None
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_evaluation_evaluates_all_individuals(single_value_population_of_4, fitness_function): evaluation = Evaluation(fitness_function) evaluation(single_value_population_of_4) assert evaluation.eval_count == 4 for indv in single_value_population_of_4: assert indv.fit_set assert indv.fitness is not None
def test_evaluation_skips_already_calculated_fitnesses(population, fitness_function): evaluation = Evaluation(fitness_function) population[0].fitness = 1.0 evaluation(population) assert evaluation.eval_count == 24 for indv in population: assert indv.fit_set assert indv.fitness is not None
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_evaluation_skips_already_calculated_fitnesses( single_value_population_of_4, fitness_function): evaluation = Evaluation(fitness_function) single_value_population_of_4[0].fitness = 1.0 evaluation(single_value_population_of_4) assert evaluation.eval_count == 3 for indv in single_value_population_of_4: assert indv.fit_set assert indv.fitness is not None
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_fitness_equals_true_value_count(fitness_function, population): evaluation = Evaluation(fitness_function) evaluation(population) for indv in population: fitness = 0 for val in indv.values: if val == False: fitness += 1 assert indv.fitness == fitness_function(indv) assert fitness == indv.fitness
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) 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 onemax_evaluator(onemax_fitness): return Evaluation(onemax_fitness)
def test_setting_eval_count(single_value_population_of_4, fitness_function): evaluation = Evaluation(fitness_function) evaluation.eval_count = -4 assert evaluation.eval_count == -4 evaluation(single_value_population_of_4) assert evaluation.eval_count == 0