Пример #1
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())
Пример #2
0
def implicit_regression_cpp():
    training_data = cppBackend.ImplicitTrainingData(TEST_X_PARTIALS, TEST_DX_DT)
    return cppBackend.ImplicitRegression(training_data)
Пример #3
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)
Пример #4
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)