def init_island(): np.random.seed(15) 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(component_generator) 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_algorithm = AgeFitnessEA(evaluator, agraph_generator, crossover, mutation, MUTATION_PROBABILITY, CROSSOVER_PROBABILITY, POP_SIZE) island = Island(ea_algorithm, agraph_generator, POP_SIZE) return island
def execute_generational_steps(): x = init_x_vals(-10, 10, 100) 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(component_generator) 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, 0.4, 0.4, POP_SIZE) island = Island(ea, agraph_generator, POP_SIZE) archipelago = SerialArchipelago(island) opt_result = archipelago.evolve_until_convergence(max_generations=500, fitness_threshold=1.0e-4) if opt_result.success: print(archipelago.get_best_individual().get_latex_string()) else: print("Failed.")
def training_function(training_data, ea_choice): component_generator = \ ComponentGenerator(input_x_dimension=training_data.x.shape[1]) component_generator.add_operator("+") component_generator.add_operator("-") component_generator.add_operator("*") agraph_generator = AGraphGenerator(agraph_size=32, component_generator=component_generator) crossover = AGraphCrossover(component_generator) mutation = AGraphMutation(component_generator) fitness = ExplicitRegression(training_data=training_data) local_opt_fitness = ContinuousLocalOptimization(fitness, algorithm='lm') evaluator = Evaluation(local_opt_fitness) POPULATION_SIZE = 64 MUTATION_PROBABILITY = 0.1 CROSSOVER_PROBABILITY = 0.7 if ea_choice == "age_fitness": ea = AgeFitnessEA(evaluator, agraph_generator, crossover, mutation, MUTATION_PROBABILITY, CROSSOVER_PROBABILITY, POPULATION_SIZE) else: ea = DeterministicCrowdingEA(evaluator, crossover, mutation, MUTATION_PROBABILITY, CROSSOVER_PROBABILITY) island = Island(ea, agraph_generator, POPULATION_SIZE) opt_result = island.evolve_until_convergence( max_generations=MAX_GENERATIONS, fitness_threshold=1e-6) return island.get_best_individual(), opt_result
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], automatic_constant_optimization=False, numerical_constant_range=10) component_generator.add_operator("+") component_generator.add_operator("-") component_generator.add_operator("*") crossover = AGraphCrossover(component_generator) mutation = AGraphMutation(component_generator) agraph_generator = AGraphGenerator(STACK_SIZE, component_generator) fitness = ExplicitRegression(training_data=training_data) evaluator = Evaluation(fitness) ea = AgeFitnessEA(evaluator, agraph_generator, crossover, mutation, MUTATION_PROBABILITY, CROSSOVER_PROBABILITY, POP_SIZE) island = Island(ea, agraph_generator, POP_SIZE) return island
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_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( 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 execute_generational_steps(): communicator = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() data = pd.read_csv('./data/fp_2var_test9_py.csv').as_matrix() x = data[:, 0:2] y = data[:2] if rank == 0: x = init_x_vals(-10, 10, 100) y = equation_eval(x) x = MPI.COMM_WORLD.bcast(x, root=0) y = MPI.COMM_WORLD.bcast(y, root=0) 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) component_generator.add_operator(5) component_generator.add_operator(6) component_generator.add_operator(7) component_generator.add_operator(10) crossover = AGraphCrossover(component_generator) mutation = AGraphMutation(component_generator) agraph_generator = AGraphGenerator(STACK_SIZE, component_generator) fitness = ExplicitRegression(training_data=training_data, metric='root mean squared error') local_opt_fitness = ContinuousLocalOptimization(fitness, algorithm='L-BFGS-B') evaluator = Evaluation(local_opt_fitness) #ea = AgeFitnessEA(evaluator, agraph_generator, crossover, # mutation, 0.4, 0.4, POP_SIZE) ea = DeterministicCrowdingEA(evaluator, crossover, mutation, 0.4, 0.4) island = Island(ea, agraph_generator, POP_SIZE) archipelago = ParallelArchipelago(island) opt_result = archipelago.evolve_until_convergence( MAX_GENERATIONS, fitness_threshold=FITNESS_THRESHOLD, min_generations=MIN_GENERATIONS, stagnation_generations=STAGNATION_LIMIT) if opt_result.success: if rank == 0: print("best: ", archipelago.get_best_individual())
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 explicit_regression_benchmark(): np.random.seed(15) communicator = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() x = None y = None if rank == 0: x = init_x_vals(-10, 10, 100) y = equation_eval(x) x = MPI.COMM_WORLD.bcast(x, root=0) y = MPI.COMM_WORLD.bcast(y, root=0) 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(component_generator) 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, 0.4, 0.4, POP_SIZE) island = Island(ea, agraph_generator, POP_SIZE) archipelago = ParallelArchipelago(island) opt_result = archipelago.evolve_until_convergence(max_generations=500, fitness_threshold=1.0e-4) if rank == 0: if opt_result.success: print("print the best indv", archipelago.get_best_individual()) else: print("Failed.")
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 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
def execute_generational_steps(): communicator = MPI.COMM_WORLD rank = MPI.COMM_WORLD.Get_rank() x = None y = None if rank == 0: df = pd.read_csv('data/combined_clean_data.csv') df = df.dropna() train, test = train_test_split(df, test_size=0.2, random_state=42) columns = df.columns x = train.loc[:, ~columns.str.contains('Damage')] x = x.loc[:, x.columns != 'Time'] x = x.loc[:, x.columns != 'Machine'].values y = train.loc[:, columns.str.contains('Damage')] y = y.iloc[:, 0].values.reshape((-1, 1)) x = MPI.COMM_WORLD.bcast(x, root=0) y = MPI.COMM_WORLD.bcast(y, root=0) 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) # * component_generator.add_operator(5) # / # component_generator.add_operator(6) # sin # component_generator.add_operator(7) # cos # component_generator.add_operator(8) # exponential # component_generator.add_operator(10) # power # component_generator.add_operator(12) # sqrt crossover = AGraphCrossover(component_generator) mutation = AGraphMutation(component_generator) agraph_generator = AGraphGenerator(STACK_SIZE, component_generator) fitness = ExplicitRegression(training_data=training_data, metric='mean squared error') local_opt_fitness = ContinuousLocalOptimization(fitness, algorithm='lm') evaluator = Evaluation(local_opt_fitness) ea = DeterministicCrowdingEA(evaluator, crossover, mutation, CROSSOVER_PROBABILITY, MUTATION_PROBABILITY) island = FitnessPredictorIsland(ea, agraph_generator, POP_SIZE, predictor_size_ratio=0.2) pareto_front = ParetoFront(secondary_key=lambda ag: ag.get_complexity(), similarity_function=agraph_similarity) archipelago = ParallelArchipelago(island, hall_of_fame=pareto_front) optim_result = archipelago.evolve_until_convergence( MAX_GENERATIONS, FITNESS_THRESHOLD, convergence_check_frequency=CHECK_FREQUENCY, min_generations=MIN_GENERATIONS, checkpoint_base_name='checkpoint', num_checkpoints=2) if optim_result.success: if rank == 0: print("best: ", archipelago.get_best_individual()) if rank == 0: print(optim_result) print("Generation: ", archipelago.generational_age) print_pareto_front(pareto_front) plot_pareto_front(pareto_front)