Exemplo 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)
Exemplo n.º 2
0
def test_getting_subset_of_training_data(python):
    data_input = np.arange(5).reshape((-1, 1))
    training_data = ExplicitTrainingData(data_input, data_input) \
                    if python \
                    else bingocpp.ExplicitTrainingData(data_input, data_input)
    subset_training_data = training_data[[0, 2, 3]]

    expected_subset = np.array([[0], [2], [3]])
    np.testing.assert_array_equal(subset_training_data.x, expected_subset)
    np.testing.assert_array_equal(subset_training_data.y, expected_subset)
Exemplo n.º 3
0
def test_explicit_get_item():
    print("-----test_explicit_get_item-----")
    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)
    c_training_data = bingocpp.ExplicitTrainingData(x, y)

    items = [5, 10, 15, 18, 64, 92, 129, 186, 201, 215, 293, 355, 389]

    py_result = py_training_data.__getitem__(items)
    c_result = c_training_data.__getitem__(items)

    assert py_result.x.all() == c_result.x.all()
    assert py_result.y.all() == c_result.y.all()
Exemplo n.º 4
0
def explicit_data_nan(request, dummy_sum_equation, dummy_sum_equation_cpp):
    x, y = init_x_and_y()
    x[0, 0] = np.nan
    if request.param == "python":
        return (SampleTrainingData(x, y), dummy_sum_equation)
    return (bingocpp.ExplicitTrainingData(x, y), dummy_sum_equation_cpp)
Exemplo n.º 5
0
def dummy_training_data_cpp():
    if bingocpp is None:
        return None
    x, y = init_x_and_y()
    return bingocpp.ExplicitTrainingData(x, y)
Exemplo n.º 6
0
def compare_cpp_agcpp_explicit(X, Y, operator, params):
    """does the comparison"""
    Y = Y.reshape([-1, 1])
    # 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[-1] = (operator, params[0], params[1])
    equ.stack = np.copy(stack)

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

    # make training data
    training_data = bingocpp.ExplicitTrainingData(X, Y)

    # make fitness_metric
    explicit_regressor = bingocpp.StandardRegression()

    # 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: ", epsilon)
    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: ", epsilon)
        converged = islmngr.run_islands(MAX_STEPS,
                                        epsilon,
                                        step_increment=N_STEPS,
                                        make_plots=False)

    assert converged
Exemplo n.º 7
0
def explicit_regression_cpp():
    training_data = cppBackend.ExplicitTrainingData(TEST_X_PARTIALS, TEST_Y_ZEROS)
    return cppBackend.ExplicitRegression(training_data)