diff --git a/bayeso/bo.py b/bayeso/bo.py index 85c77d9..0f0d05f 100644 --- a/bayeso/bo.py +++ b/bayeso/bo.py @@ -137,7 +137,7 @@ def _optimize(self, fun_negative_acquisition, str_initial_method, int_samples): return next_point.flatten(), next_points def optimize(self, X_train, Y_train, - str_initial_method=constants.STR_OPTIMIZER_INITIALIZATION, + str_initial_method=constants.STR_AO_INITIALIZATION, int_samples=constants.NUM_ACQ_SAMPLES, is_normalized=True, ): diff --git a/bayeso/constants.py b/bayeso/constants.py index 3eeaf72..296c387 100644 --- a/bayeso/constants.py +++ b/bayeso/constants.py @@ -12,8 +12,8 @@ STR_OPTIMIZER_METHOD_GP = 'BFGS' STR_GP_COV = 'se' STR_BO_ACQ = 'ei' -STR_BO_INITIALIZATION = 'sobol' -STR_OPTIMIZER_INITIALIZATION = 'sobol' +STR_BO_INITIALIZATION = 'uniform' +STR_AO_INITIALIZATION = 'uniform' NUM_BO_GRID = 50 NUM_BO_RANDOM = 1000 diff --git a/bayeso/utils/utils_bo.py b/bayeso/utils/utils_bo.py index 89ec89b..96f0d84 100644 --- a/bayeso/utils/utils_bo.py +++ b/bayeso/utils/utils_bo.py @@ -12,8 +12,9 @@ def get_grid(arr_ranges, int_grid): assert isinstance(arr_ranges, np.ndarray) assert isinstance(int_grid, int) - assert len(arr_ranges.shape) + assert len(arr_ranges.shape) == 2 assert arr_ranges.shape[1] == 2 + assert (arr_ranges[:, 0] <= arr_ranges[:, 1]).all() list_grid = [] for range_ in arr_ranges: @@ -67,7 +68,7 @@ def get_next_best_acquisition(arr_points, arr_acquisitions, cur_points): return next_point def optimize_many_(model_bo, fun_target, X_train, Y_train, int_iter, - str_initial_method_ao=constants.STR_OPTIMIZER_INITIALIZATION, + str_initial_method_ao=constants.STR_AO_INITIALIZATION, int_samples_ao=constants.NUM_ACQ_SAMPLES, ): assert isinstance(model_bo, bo.BO) @@ -113,7 +114,7 @@ def optimize_many_(model_bo, fun_target, X_train, Y_train, int_iter, return X_final, Y_final def optimize_many(model_bo, fun_target, X_train, int_iter, - str_initial_method_ao=constants.STR_OPTIMIZER_INITIALIZATION, + str_initial_method_ao=constants.STR_AO_INITIALIZATION, int_samples_ao=constants.NUM_ACQ_SAMPLES, ): assert isinstance(model_bo, bo.BO) @@ -142,7 +143,7 @@ def optimize_many(model_bo, fun_target, X_train, int_iter, def optimize_many_with_random_init(model_bo, fun_target, int_init, int_iter, str_initial_method_bo=constants.STR_BO_INITIALIZATION, - str_initial_method_ao=constants.STR_OPTIMIZER_INITIALIZATION, + str_initial_method_ao=constants.STR_AO_INITIALIZATION, int_samples_ao=constants.NUM_ACQ_SAMPLES, int_seed=None, ): diff --git a/docs/index.rst b/docs/index.rst index 7fedb6d..a424617 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -10,6 +10,8 @@ Welcome to bayeso's documentation! The code can be found in `our GitHub repository `_. +It is still under construction. We will update soon. + .. toctree:: :maxdepth: 2 :caption: Contents: diff --git a/tests/test_benchmarks.py b/tests/test_benchmarks.py index 35158e0..51cdf5a 100644 --- a/tests/test_benchmarks.py +++ b/tests/test_benchmarks.py @@ -37,4 +37,3 @@ def test_branin(): val_fun = benchmarks.branin(X) assert len(val_fun.shape) == 1 assert val_fun.shape[0] == X.shape[0] - diff --git a/tests/test_bo.py b/tests/test_bo.py index 4d3170a..6c8bac2 100644 --- a/tests/test_bo.py +++ b/tests/test_bo.py @@ -124,11 +124,19 @@ def test_optimize(): model_bo.optimize(X, Y, str_initial_method='abc') with pytest.raises(AssertionError) as error: model_bo.optimize(X, Y, int_samples='abc') + with pytest.raises(AssertionError) as error: + model_bo.optimize(X, Y, is_normalized='abc') next_point, next_points, acquisitions, cov_X_X, inv_cov_X_X, hyps = model_bo.optimize(X, Y) assert isinstance(next_point, np.ndarray) + assert isinstance(next_points, np.ndarray) + assert isinstance(acquisitions, np.ndarray) assert isinstance(cov_X_X, np.ndarray) assert isinstance(inv_cov_X_X, np.ndarray) assert isinstance(hyps, dict) assert len(next_point.shape) == 1 + assert len(next_points.shape) == 2 + assert len(acquisitions.shape) == 1 assert next_point.shape[0] == dim_X + assert next_points.shape[1] == dim_X + assert next_points.shape[0] == acquisitions.shape[0] diff --git a/tests/test_gp.py b/tests/test_gp.py index db38168..f3eb9a3 100644 --- a/tests/test_gp.py +++ b/tests/test_gp.py @@ -45,6 +45,8 @@ def test_get_kernels(): gp.get_kernels(X, hyps, 1) with pytest.raises(ValueError) as error: gp.get_kernels(X, hyps, 'abc') + with pytest.raises(AssertionError) as error: + gp.get_kernels(X, hyps, 'se', debug=1) cov_X_X, inv_cov_X_X = gp.get_kernels(X, hyps, 'se') print(cov_X_X) @@ -89,6 +91,10 @@ def test_log_ml(): gp.log_ml(X, np.expand_dims(np.arange(0, 4), axis=1), arr_hyps, str_cov, prior_mu_X) with pytest.raises(AssertionError) as error: gp.log_ml(X, Y, arr_hyps, str_cov, np.expand_dims(np.arange(0, 4), axis=1)) + with pytest.raises(AssertionError) as error: + gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, is_fixed_noise=1) + with pytest.raises(AssertionError) as error: + gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X, debug=1) log_ml = gp.log_ml(X, Y, arr_hyps, str_cov, prior_mu_X) print(log_ml) @@ -123,6 +129,10 @@ def test_get_optimized_kernel(): gp.get_optimized_kernel(X, Y, prior_mu, 'abc') with pytest.raises(AssertionError) as error: gp.get_optimized_kernel(X, Y, prior_mu, 'se', str_optimizer_method=1) + with pytest.raises(AssertionError) as error: + gp.get_optimized_kernel(X, Y, prior_mu, 'se', is_fixed_noise=1) + with pytest.raises(AssertionError) as error: + gp.get_optimized_kernel(X, Y, prior_mu, 'se', debug=1) def test_predict_test_(): np.random.seed(42) @@ -222,3 +232,7 @@ def test_predict_optimized(): gp.predict_optimized(np.random.randn(10, dim_X), Y, X_test, str_cov='se', prior_mu=prior_mu) with pytest.raises(AssertionError) as error: gp.predict_optimized(X, np.random.randn(10, 1), X_test, str_cov='se', prior_mu=prior_mu) + with pytest.raises(AssertionError) as error: + gp.predict_optimized(X, Y, X_test, is_fixed_noise=1) + with pytest.raises(AssertionError) as error: + gp.predict_optimized(X, Y, X_test, debug=1) diff --git a/tests/test_utils_bo.py b/tests/test_utils_bo.py index 4ed3954..1dfef1f 100644 --- a/tests/test_utils_bo.py +++ b/tests/test_utils_bo.py @@ -83,6 +83,17 @@ def test_get_grid(): [10., 2., 5.], ]) + with pytest.raises(AssertionError) as error: + utils_bo.get_grid('abc', 3) + with pytest.raises(AssertionError) as error: + utils_bo.get_grid(arr_range_1, 'abc') + with pytest.raises(AssertionError) as error: + utils_bo.get_grid(np.arange(0, 10), 3) + with pytest.raises(AssertionError) as error: + utils_bo.get_grid(np.ones((3, 3)), 3) + with pytest.raises(AssertionError) as error: + utils_bo.get_grid(np.array([[0.0, -2.0], [10.0, 20.0]]), 3) + arr_grid_1 = utils_bo.get_grid(arr_range_1, 3) arr_grid_2 = utils_bo.get_grid(arr_range_2, 3) @@ -106,6 +117,40 @@ def test_get_best_acquisition(): assert best_initial.shape[1] == arr_initials.shape[1] assert best_initial == np.array([[1]]) +def test_get_next_best_acquisition(): + arr_points = np.array([ + [0.0, 1.0], + [1.0, -3.0], + [-2.0, -4.0], + [1.0, 3.0], + ]) + arr_acquisitions = np.array([1.1, 0.2, 0.5, 0.6]) + cur_points = np.array([ + [-10.0, 1.0], + [1.0, -3.0], + [11.0, 2.0], + ]) + + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(1, arr_acquisitions, cur_points) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(np.arange(0, 4), arr_acquisitions, cur_points) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(arr_points, 1, cur_points) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(arr_points, np.ones((4, 2)), cur_points) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, 1) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, np.arange(0, 3)) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(arr_points, np.arange(0, 10), cur_points) + with pytest.raises(AssertionError) as error: + utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, np.ones((3, 5))) + + next_point = utils_bo.get_next_best_acquisition(arr_points, arr_acquisitions, cur_points) + assert (next_point == np.array([-2.0, -4.0])).all() + def test_optimize_many_(): np.random.seed(42) arr_range = np.array([ diff --git a/tests/test_utils_covariance.py b/tests/test_utils_covariance.py index 686c79d..53716da 100644 --- a/tests/test_utils_covariance.py +++ b/tests/test_utils_covariance.py @@ -34,6 +34,8 @@ def test_convert_hyps(): utils_covariance.convert_hyps('se', 2.1) with pytest.raises(AssertionError) as error: utils_covariance.convert_hyps('abc', 2.1) + with pytest.raises(AssertionError) as error: + utils_covariance.convert_hyps('se', dict(), is_fixed_noise=1) cur_hyps = {'noise': 0.1, 'signal': 1.0, 'lengthscales': np.array([1.0, 1.0])} converted_hyps = utils_covariance.convert_hyps('se', cur_hyps) @@ -52,9 +54,18 @@ def test_restore_hyps(): utils_covariance.restore_hyps('abc', 2.1) with pytest.raises(AssertionError) as error: utils_covariance.restore_hyps('se', np.array([[1.0, 1.0], [1.0, 1.0]])) + with pytest.raises(AssertionError) as error: + utils_covariance.restore_hyps('se', np.array([1.0, 1.0, 1.0]), is_fixed_noise=1) + with pytest.raises(AssertionError) as error: + utils_covariance.restore_hyps('se', np.array([1.0, 1.0, 1.0]), fixed_noise='abc') cur_hyps = np.array([0.1, 1.0, 1.0, 1.0, 1.0]) restored_hyps = utils_covariance.restore_hyps('se', cur_hyps) assert restored_hyps['noise'] == cur_hyps[0] assert restored_hyps['signal'] == cur_hyps[1] assert (restored_hyps['lengthscales'] == cur_hyps[2:]).all() + + restored_hyps = utils_covariance.restore_hyps('se', cur_hyps, is_fixed_noise=True) + assert restored_hyps['noise'] == constants.GP_NOISE + assert restored_hyps['signal'] == cur_hyps[0] + assert (restored_hyps['lengthscales'] == cur_hyps[1:]).all()