Example #1
0
def main():
    str_acq = 'pi'
    num_iter = 10
    X_train = np.array([
        [-5],
        [-1],
        [1],
        [2],
    ])
    num_init = X_train.shape[0]
    model_bo = bo.BO(np.array([[-6., 6.]]), str_acq=str_acq)
    X_test = np.linspace(-6, 6, 400)
    X_test = np.reshape(X_test, (400, 1))
    for ind_ in range(1, num_iter + 1):
        Y_train = fun_target(X_train)
        next_x, dict_info = model_bo.optimize(X_train,
                                              fun_target(X_train),
                                              str_initial_method_ao='uniform')
        cov_X_X = dict_info['cov_X_X']
        inv_cov_X_X = dict_info['inv_cov_X_X']
        hyps = dict_info['hyps']

        mu_test, sigma_test, Sigma_test = gp.predict_test_(
            X_train, Y_train, X_test, cov_X_X, inv_cov_X_X, hyps)
        acq_test = acquisition.pi(mu_test.flatten(), sigma_test.flatten(),
                                  Y_train)
        acq_test = np.expand_dims(acq_test, axis=1)
        X_train = np.vstack((X_train, next_x))
        Y_train = fun_target(X_train)
        utils_plotting.plot_bo_step(X_train,
                                    Y_train,
                                    X_test,
                                    fun_target(X_test),
                                    mu_test,
                                    sigma_test,
                                    path_save=PATH_SAVE,
                                    str_postfix='bo_{}_'.format(str_acq) +
                                    str(ind_),
                                    int_init=num_init)
        utils_plotting.plot_bo_step_acq(X_train,
                                        Y_train,
                                        X_test,
                                        fun_target(X_test),
                                        mu_test,
                                        sigma_test,
                                        acq_test,
                                        path_save=PATH_SAVE,
                                        str_postfix='bo_{}_'.format(str_acq) +
                                        str(ind_),
                                        int_init=num_init)
Example #2
0
def test_pi():
    with pytest.raises(AssertionError) as error:
        package_target.pi('abc', np.ones(10), np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones(10), 'abc', np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones(10), np.ones(10), 'abc')
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones(10), np.ones(10), np.zeros((5, 1)), 1)
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones(5), np.ones(10), np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones(10), np.ones(10), np.zeros(5))
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones(10), np.ones((10, 1)), np.zeros((5, 1)))
    with pytest.raises(AssertionError) as error:
        package_target.pi(np.ones((10, 1)), np.ones(10), np.zeros((5, 1)))

    val_acq = package_target.pi(np.arange(0, 10), np.ones(10), np.zeros((5, 1)))
    truth_val_acq = np.array([5.00000000e-01, 1.58657674e-01, 2.27512118e-02, 1.35003099e-03, 3.16765954e-05, 2.86725916e-07, 9.86952260e-10, 1.28045212e-12, 6.22500364e-16, 1.12951395e-19])
    print(val_acq)

    assert (np.abs(val_acq - truth_val_acq) < TEST_EPSILON).all()