def test_potential_hof_members():
    island_a = Mock(hall_of_fame=[COMM_RANK, COMM_RANK])
    archipelago = ParallelArchipelago(num_island(1))
    archipelago._island = island_a
    actual_members = archipelago._get_potential_hof_members()
    expected_memebers = [i for i in range(COMM_SIZE) for _ in range(2)]
    return mpi_assert_equal(actual_members, expected_memebers)
def test_best_fitness_returned(one_island):
    generator = MultipleValueChromosomeGenerator(generate_zero,
                                                 VALUE_LIST_SIZE)
    best_indv = generator()
    one_island.load_population([best_indv], replace=False)
    archipelago = ParallelArchipelago(one_island)
    assert archipelago.get_best_fitness() == 0
def test_blocking_fitness_eval_count():
    steps = 1
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island, non_blocking=False)
    archipelago.evolve(steps)
    expected_evaluations = COMM_SIZE * (POP_SIZE + steps * OFFSPRING_SIZE)
    actual_evaluations = archipelago.get_fitness_evaluation_count()
    return mpi_assert_equal(actual_evaluations, expected_evaluations)
def test_island_migration_doesnt_chane_pop_size():
    island = num_island(COMM_RANK, pop_size=COMM_RANK + 2)
    archipelago = ParallelArchipelago(island)
    archipelago._coordinate_migration_between_islands()
    expected_pop = (COMM_SIZE * (COMM_SIZE - 1)) // 2 + (2 * COMM_SIZE)
    total_pop_after = COMM.allreduce(len(archipelago._island.population),
                                     MPI.SUM)
    return mpi_assert_equal(total_pop_after, expected_pop)
def test_non_blocking_evolution():
    steps = 200
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island,
                                      sync_frequency=10,
                                      non_blocking=True)
    archipelago.evolve(steps)
    island_age = archipelago._island.generational_age
    archipelago_age = archipelago.generational_age
    return mpi_assert_mean_near(island_age, archipelago_age, rel=0.1)
def test_convergence():
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island,
                                      sync_frequency=10,
                                      non_blocking=True)
    result = archipelago.evolve_until_convergence(
        max_generations=100,
        fitness_threshold=0,
        convergence_check_frequency=25)
    return mpi_assert_true(result.success)
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 test_dump_then_load_equal_procs():
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island,
                                      sync_frequency=10,
                                      non_blocking=True)
    file_name = "testing_pa_dump_and_load_eq.pkl"
    archipelago.dump_to_file(file_name)
    archipelago = \
        load_parallel_archipelago_from_file(file_name)
    if COMM_RANK == 0:
        os.remove(file_name)

    origin_proc = archipelago._island.population[0].values[0]
    return mpi_assert_equal(origin_proc, COMM_RANK)
def test_dump_then_load(one_island):
    archipelago = ParallelArchipelago(one_island)
    archipelago.evolve(1)
    file_name = "testing_pa_dump_and_load.pkl"
    archipelago.dump_to_file(file_name)
    archipelago.evolve(1)
    archipelago = \
        load_parallel_archipelago_from_file(file_name)

    assert 1 == archipelago.generational_age
    archipelago.evolve(1)
    assert 2 == archipelago.generational_age

    os.remove(file_name)
def test_island_migration():
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island)
    archipelago._coordinate_migration_between_islands()

    native_individual_values = [COMM_RANK] * VALUE_LIST_SIZE
    non_native_indv_found = False
    for individual in archipelago._island.population:
        if individual.values != native_individual_values:
            non_native_indv_found = True
            break
    has_unpaired_island = COMM_SIZE % 2 == 1
    if has_unpaired_island:
        return mpi_assert_exactly_n_false(non_native_indv_found, 1)
    return mpi_assert_true(non_native_indv_found)
Exemple #11
0
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_stale_checkpoint_removal():
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island, non_blocking=False)
    archipelago.evolve_until_convergence(3,
                                         -1.0,
                                         num_checkpoints=1,
                                         checkpoint_base_name="stale_check")
    COMM.barrier()
    correct_files = [
        not os.path.isfile("stale_check_0.pkl"),
        not os.path.isfile("stale_check_1.pkl"),
        not os.path.isfile("stale_check_2.pkl"),
        os.path.isfile("stale_check_3.pkl")
    ]
    COMM.barrier()
    if COMM_RANK == 0:
        os.remove("stale_check_3.pkl")
    return mpi_assert_true(all(correct_files))
def test_dump_then_load_more_procs():
    island = num_island(COMM_RANK)
    archipelago = ParallelArchipelago(island,
                                      sync_frequency=10,
                                      non_blocking=True)
    file_name = "testing_pa_dump_and_load_gt.pkl"
    archipelago.dump_to_file(file_name)
    _remove_proc_from_pickle(file_name)
    archipelago = \
        load_parallel_archipelago_from_file(file_name)
    if COMM_RANK == 0:
        os.remove(file_name)

    origin_proc = archipelago._island.population[0].values[0]
    expected_origin = COMM_RANK + 1
    if COMM_RANK == COMM_SIZE - 1:
        expected_origin = 1
    return mpi_assert_equal(origin_proc, expected_origin)
def test_fitness_eval_count(one_island, sync_freq, non_blocking):
    num_islands = 1
    archipelago = ParallelArchipelago(one_island, sync_frequency=sync_freq,
                                       non_blocking=non_blocking)
    assert archipelago.get_fitness_evaluation_count() == 0
    archipelago.evolve(1)
    if non_blocking:
        expected_evals = num_islands * (POP_SIZE +
                                              sync_freq * OFFSPRING_SIZE)
    else:
        expected_evals = num_islands * (POP_SIZE + OFFSPRING_SIZE)
    assert archipelago.get_fitness_evaluation_count() == expected_evals
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)
def test_potential_hof_members(mocker, one_island):
    island_a = mocker.Mock(hall_of_fame=['a'])
    archipelago = ParallelArchipelago(one_island)
    archipelago._island = island_a
    assert archipelago._get_potential_hof_members() == ['a']
def test_best_fitness_returned():
    island = num_island(COMM_RANK + 1)
    if COMM_RANK == 0:
        island.load_population([perfect_individual()], replace=False)
    archipelago = ParallelArchipelago(island)
    return mpi_assert_equal(archipelago.get_best_fitness(), 0)