Esempio n. 1
0
def test_evaluate_explicit():
    print("-----test_evaluate_explicit-----")
    n_lin = int(math.pow(500, 1.0 / 3)) + 1
    x_1 = np.linspace(0, 5, n_lin)
    x_2 = np.linspace(0, 5, n_lin)
    x_3 = np.linspace(0, 5, n_lin)
    x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3)
    x = x[np.random.choice(x.shape[0], 500, replace=False), :]
    # make solution
    y_t = (x[:, 0] * x[:, 0] + 3.5 * x[:, 1])
    y = y_t.reshape(-1, 1)

    py_training_data = ExplicitTrainingData(x, y)
    py_explicit_regressor = StandardRegression()
    c_explicit_regressor = bingocpp.StandardRegression()
    c_training_data = bingocpp.ExplicitTrainingData(x, y)

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    c_1 = c_manip.generate()
    c_1.stack = np.copy(py_1.command_array)
    c_manip.simplify_stack(c_1)

    py_fit = py_explicit_regressor.evaluate_fitness(py_1, py_training_data)
    c_fit = c_explicit_regressor.evaluate_fitness(c_1, c_training_data)

    assert py_fit == pytest.approx(c_fit)
Esempio n. 2
0
def test_dump():
    print("-----test_dump-----")
    constants = np.array([1, 5, 6, 7, 8, 32, 54, 68])

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    py_1.constants = np.copy(constants)
    orig_stack = np.copy(py_1.command_array)
    orig_const = np.copy(py_1.constants)
    orig_age = py_1.genetic_age

    c_1 = c_manip.generate()
    c_1.stack = np.copy(orig_stack)
    c_1.constants = np.copy(orig_const)
    c_manip.simplify_stack(c_1)

    py_stack, py_con, py_age = py_manip.dump(py_1)
    c_pair, c_age = c_manip.dump(c_1)
    c_stack = c_pair[0]
    c_con = c_pair[1]

    assert py_stack.all() == c_stack.all()
    assert py_con.all() == c_con.all()
    assert py_age == c_age
Esempio n. 3
0
def test_count_constants():
    print("-----test_count_constants-----")

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    c_1 = c_manip.generate()
    py_1.command_array[0] = (0, 0, 0)
    py_1.command_array[1] = (1, 0, 0)
    py_1.command_array[1] = (1, 1, 1)
    py_1.command_array[-1] = (2, 2, 1)
    c_1.stack = np.copy(py_1.command_array)
    c_manip.simplify_stack(c_1)

    constants = np.array([1, 5, 6, 7, 8, 32, 54, 68])

    py_1.set_constants(constants)
    c_1.set_constants(constants)

    py_con = py_1.count_constants()
    c_con = c_1.count_constants()

    assert py_con == c_con
Esempio n. 4
0
def test_evaluate_fitness_vector_implicit():
    print("-----test_evaluate_fitness_vector_implicit-----")
    x_t = snake_walk()
    y = (x_t[:, 0] + x_t[:, 1])
    x = np.hstack((x_t, y.reshape([-1, 1])))

    py_training_data = ImplicitTrainingData(x)
    py_implicit_regressor = ImplicitRegression()
    c_training_data = bingocpp.ImplicitTrainingData(x)
    c_implicit_regressor = bingocpp.ImplicitRegression()

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    temp = np.array([[1, 0, 0], [0, 0, 0], [0, 1, 1], [3, 2, 2], [4, 2, 3],
                     [4, 4, 0], [3, 2, 0], [3, 0, 0], [3, 4, 2], [3, 1, 4],
                     [2, 6, 0], [3, 8, 7], [2, 3, 11], [3, 7, 7], [3, 2, 7],
                     [2, 9, 5], [4, 4, 14], [3, 9, 1], [3, 4, 5], [0, 1, 1],
                     [4, 8, 4], [1, -1, -1], [3, 20, 5],
                     [2, 9, 17], [1, -1, -1], [4, 23, 4], [4, 25, 19],
                     [2, 26, 14], [4, 22, 10], [0, 0, 0], [2, 0, 17],
                     [2, 29, 16], [4, 23, 14], [4, 3, 22], [0, 2, 2],
                     [2, 31, 27], [2, 35, 28], [2, 25, 29], [2, 36, 28],
                     [3, 8, 29], [3, 4, 24], [2, 14, 9], [4, 25, 9], [0, 2, 2],
                     [3, 26, 10], [3, 12, 6], [0, 1, 1], [4, 42,
                                                          26], [3, 41, 6],
                     [3, 13, 1], [3, 42, 36], [3, 15, 34], [2, 14, 23],
                     [2, 13, 12], [0, 2, 2], [2, 28, 45], [3, 1, 12],
                     [3, 15, 30], [3, 34, 38], [3, 43, 50], [2, 31, 9],
                     [2, 54, 46], [1, -1, -1], [4, 60, 29]])

    py_1 = py_manip.generate()
    c_1 = c_manip.generate()
    py_1.command_array = np.copy(temp)
    c_1.stack = np.copy(temp)
    c_manip.simplify_stack(c_1)

    assert py_training_data.x.all() == pytest.approx(c_training_data.x.all())
    assert py_training_data.dx_dt.all() == pytest.approx(
        c_training_data.dx_dt.all())

    py_fit = py_implicit_regressor.evaluate_fitness(py_1, py_training_data)
    py_fit = py_implicit_regressor.evaluate_fitness_vector(
        py_1, py_training_data)

    c_fit = c_implicit_regressor.evaluate_fitness(c_1, c_training_data)
    c_fit = c_implicit_regressor.evaluate_fitness_vector(c_1, c_training_data)

    assert py_fit.all() == pytest.approx(c_fit.all())
Esempio n. 5
0
def test_agcpp_evaluate_deriv():
    print("-----test_agcpp_evaluate_deriv-----")
    n_lin = int(math.pow(500, 1.0 / 3)) + 1
    x_1 = np.linspace(0, 5, n_lin)
    x_2 = np.linspace(0, 5, n_lin)
    x_3 = np.linspace(0, 5, n_lin)
    x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3)
    x = x[np.random.choice(x.shape[0], 500, replace=False), :]

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    c_1 = c_manip.generate()
    py_1.command_array[0] = (0, 0, 0)
    py_1.command_array[1] = (0, 1, 1)
    py_1.command_array[2] = (1, 0, 0)
    py_1.command_array[3] = (1, 1, 1)
    py_1.command_array[4] = (2, 3, 1)
    py_1.command_array[-1] = (2, 4, 2)

    c_1.stack = np.copy(py_1.command_array)
    c_manip.simplify_stack(c_1)

    constants = np.array([1, 5, 6, 7, 8, 32, 54, 68])

    py_1.set_constants(constants)
    c_1.set_constants(constants)

    py_fit = py_1.evaluate_deriv(x)
    c_fit = c_1.evaluate_deriv(x)
    py_fit_const = py_1.evaluate_with_const_deriv(x)
    c_fit_const = c_1.evaluate_with_const_deriv(x)

    assert py_fit[0].all() == pytest.approx(c_fit[0].all())
    assert py_fit[1].all() == pytest.approx(c_fit[1].all())
    assert py_fit_const[0].all() == pytest.approx(c_fit_const[0].all())
    assert py_fit_const[1].all() == pytest.approx(c_fit_const[1].all())
Esempio n. 6
0
def test_complexity():
    print("-----test_complexity-----")
    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    c_1 = c_manip.generate()
    c_1.stack = np.copy(py_1.command_array)
    c_manip.simplify_stack(c_1)

    py_complexity = py_1.complexity()
    c_complexity = c_1.complexity()

    assert py_complexity == c_complexity
Esempio n. 7
0
def test_load():
    print("-----test_load-----")
    constants = np.array([1, 5, 6, 7, 8, 32, 54, 68])

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    orig_stack = np.copy(py_1.command_array)

    py_1 = py_manip.load([orig_stack, constants, 0])
    c_1 = c_manip.load([[orig_stack, constants], 0])

    assert py_1.command_array.all() == c_1.stack.all()
    assert py_1.constants.all() == c_1.constants.all()
    assert py_1.genetic_age == c_1.genetic_age
Esempio n. 8
0
def test_needs_optimization():
    print("-----test_needs_optimization-----")

    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    c_1 = c_manip.generate()
    py_1.command_array[0] = (0, 0, 0)
    py_1.command_array[1] = (1, -1, -1)
    py_1.command_array[-1] = (2, 1, 0)
    c_1.stack = np.copy(py_1.command_array)
    c_manip.simplify_stack(c_1)

    py_opt = py_1.needs_optimization()
    c_opt = c_1.needs_optimization()

    assert py_opt == c_opt
Esempio n. 9
0
def test_distance():
    print("-----test_distance-----")
    py_manip = AGraphCpp.AGraphCppManipulator(3, 64, nloads=2)
    py_manip.add_node_type(2)
    py_manip.add_node_type(3)
    py_manip.add_node_type(4)
    c_manip = bingocpp.AcyclicGraphManipulator(3, 64, nloads=2)
    c_manip.add_node_type(2)
    c_manip.add_node_type(3)
    c_manip.add_node_type(4)

    py_1 = py_manip.generate()
    py_2 = py_manip.generate()
    c_1 = c_manip.generate()
    c_2 = c_manip.generate()
    c_1.stack = np.copy(py_1.command_array)
    c_2.stack = np.copy(py_2.command_array)
    c_manip.simplify_stack(c_1)
    c_manip.simplify_stack(c_2)

    py_dist = py_manip.distance(py_1, py_2)
    c_dist = c_manip.distance(c_1, c_2)

    assert py_dist == c_dist
Esempio n. 10
0
def compare_cpp_agcpp_implicit(X, Y, operator, params):
    """does the comparison"""
    X = np.hstack((X, Y.reshape([-1, 1])))
    Y = None
    # make solution manipulator
    sol_manip = bingocpp.AcyclicGraphManipulator(X.shape[1], 16, nloads=2)
    sol_manip.add_node_type(2)
    sol_manip.add_node_type(3)
    sol_manip.add_node_type(4)
    sol_manip.add_node_type(5)
    sol_manip.add_node_type(6)
    sol_manip.add_node_type(7)
    sol_manip.add_node_type(8)
    sol_manip.add_node_type(9)
    sol_manip.add_node_type(10)
    sol_manip.add_node_type(11)
    sol_manip.add_node_type(12)

    # make true equation
    equ = sol_manip.generate()
    stack = np.copy(equ.stack)
    stack[0] = (0, 0, 0)
    stack[1] = (0, 1, 1)
    stack[2] = (0, 2, 2)
    stack[3] = (operator, params[0], params[1])
    stack[-1] = (3, 3, 2)
    equ.stack = np.copy(stack)
    sol_manip.simplify_stack(equ)

    print(stack)
    print("equstack\n", equ.stack)

    # make predictor manipulator
    pred_manip = fpm(32, X.shape[0])

    # make training data
    training_data = bingocpp.ImplicitTrainingData(X)

    # make fitness_metric
    explicit_regressor = bingocpp.ImplicitRegression()

    # make and run island manager
    islmngr = SerialIslandManager(N_ISLANDS,
                                  solution_training_data=training_data,
                                  solution_manipulator=sol_manip,
                                  predictor_manipulator=pred_manip,
                                  fitness_metric=explicit_regressor)
    epsilon = 1.05 * islmngr.isles[0].solution_fitness_true(equ) + 1.0e-10
    print("EPSILON IS - ", epsilon, equ.latexstring())
    converged = islmngr.run_islands(MAX_STEPS,
                                    epsilon,
                                    step_increment=N_STEPS,
                                    make_plots=False)

    if not converged:
        # try to run again if it fails
        islmngr = SerialIslandManager(N_ISLANDS,
                                      solution_training_data=training_data,
                                      solution_manipulator=sol_manip,
                                      predictor_manipulator=pred_manip,
                                      fitness_metric=explicit_regressor)
        epsilon = 1.05 * islmngr.isles[0].solution_fitness_true(equ) + 1.0e-10
        print("EPSILON IS - ", epsilon, equ.latexstring())
        converged = islmngr.run_islands(MAX_STEPS,
                                        epsilon,
                                        step_increment=N_STEPS,
                                        make_plots=False)
Esempio n. 11
0
def main(max_steps, epsilon, data_size):
    """main function which runs regression"""
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()

    # load data on rank 0
    if rank == 0:
        # make data
        # n_lin = int(math.pow(data_size, 1.0/3)) + 1
        # x_1 = np.linspace(0, 5, n_lin)
        # x_2 = np.linspace(0, 5, n_lin)
        # x_3 = np.linspace(0, 5, n_lin)
        # x = np.array(np.meshgrid(x_1, x_2, x_3)).T.reshape(-1, 3)
        # x = x[np.random.choice(x.shape[0], data_size, replace=False), :]

        # make solution
        # y = (x[:,0]*x[:,0]+3.5*x[:,1])
        # x_true = x
        # y_true = y

        x = snake_walk()
        y = (x[:, 0] + x[:, 1])
        x_true = np.hstack((x, y.reshape([-1, 1])))
        y_true = None
    else:
        x_true = None
        y_true = None
    # then broadcast to all ranks
    x_true = MPI.COMM_WORLD.bcast(x_true, root=0)
    y_true = MPI.COMM_WORLD.bcast(y_true, root=0)

    # make solution manipulator
    # sol_manip = agm(x_true.shape[1], 64, nloads=2)
    # sol_manip.add_node_type(AGNodes.Add)
    # sol_manip.add_node_type(AGNodes.Subtract)
    # sol_manip.add_node_type(AGNodes.Multiply)
    # sol_manip.add_node_type(AGNodes.Divide)
    # sol_manip.add_node_type(AGNodes.Exp)
    # sol_manip.add_node_type(AGNodes.Log)
    # sol_manip.add_node_type(AGNodes.Sin)
    # sol_manip.add_node_type(AGNodes.Cos)
    # sol_manip.add_node_type(AGNodes.Abs)
    # sol_manip.add_node_type(AGNodes.Sqrt)


    # make solution manipulator
    # y_true = y_true.reshape(-1, 1)
    # sol_manip2 = AGraphCpp.AGraphCppManipulator(x_true.shape[1], 64, nloads=2)
    sol_manip2 = bingocpp.AcyclicGraphManipulator(x_true.shape[1], 64, nloads=2)
    # sol_manip2 = bingocpp.AcyclicGraphManipulator(x_true.shape[1], 64, nloads=2, opt_rate=0)

    # sol_manip.add_node_type(2)  # +
    # sol_manip.add_node_type(3)  # -
    # sol_manip.add_node_type(4)  # *
    # sol_manip.add_node_type(5)  # /
    # sol_manip.add_node_type(6)  # sin
    # sol_manip.add_node_type(7)  # cos
    # sol_manip.add_node_type(8)  # exp
    # sol_manip.add_node_type(9)  # log
    # # sol_manip.add_node_type(10)  # pow
    # sol_manip.add_node_type(11)  # abs
    # sol_manip.add_node_type(12)  # sqrt


    sol_manip2.add_node_type(2)  # +
    sol_manip2.add_node_type(3)  # -
    sol_manip2.add_node_type(4)  # *
    sol_manip2.add_node_type(5)  # /
    sol_manip2.add_node_type(6)  # sin
    sol_manip2.add_node_type(7)  # cos
    sol_manip2.add_node_type(8)  # exp
    sol_manip2.add_node_type(9)  # log
    # sol_manip2.add_node_type(10)  # pow
    sol_manip2.add_node_type(11)  # abs
    sol_manip2.add_node_type(12)  # sqrt

    # make predictor manipulator
    pred_manip = fpm(128, data_size)

    # make training data
    # training_data = ImplicitTrainingData(x_true)
    training_data = bingocpp.ImplicitTrainingData(x_true)
    # training_data = ExplicitTrainingData(x_true, y_true)
    # training_data2 = bingocpp.ExplicitTrainingData(x_true, y_true)

    # make fitness metric
    # implicit_regressor = ImplicitRegression()
    implicit_regressor = bingocpp.ImplicitRegression()
    # explicit_regressor = StandardRegression(const_deriv=True)
    # explicit_regressor2 = bingocpp.StandardRegression()


    # make and run island manager
    islmngr = ParallelIslandManager(#restart_file='test.p',
        solution_training_data=training_data,
        solution_manipulator=sol_manip2,
        predictor_manipulator=pred_manip,
        solution_pop_size=64,
        fitness_metric=implicit_regressor)
        # fitness_metric=explicit_regressor)

    # islmngr2 = ParallelIslandManager(#restart_file='test.p',
    #     solution_training_data=training_data,
    #     solution_manipulator=sol_manip,
    #     predictor_manipulator=pred_manip,
    #     solution_pop_size=64,
    #     fitness_metric=explicit_regressor)


    # islmngr = ParallelIslandManager(#restart_file='test.p',
    #     data_x=x_true, data_y=y_true,
    #     solution_manipulator=sol_manip,
    #     predictor_manipulator=pred_manip,
    #     solution_pop_size=64,
    #     fitness_metric=StandardRegression)

    # islmngr2 = ParallelIslandManager(#restart_file='test.p',
    #     data_x=x_true, data_y=y_true,
    #     solution_manipulator=sol_manip,
    #     predictor_manipulator=pred_manip,
    #     solution_pop_size=64,
    #     fitness_metric=StandardRegression)
    non_one = time.time()
    islmngr.run_islands(max_steps, epsilon, min_steps=500,
                        step_increment=500, when_update=50)
    non_two = time.time()
    non_time = non_two - non_one
    
    timesN.append(non_time)
    agesN.append(islmngr.age)