Example #1
0
def main():
    # (max_depth, n_estimators)
    num_init = 1

    model_bo = bo.BO(np.array([[1, 10], [100, 500]]), debug=True)
    list_Y = []
    list_time = []
    for _ in range(0, 5):
        X_final, Y_final, time_final, _, _ = wrappers_bo_function.run_single_round(
            model_bo,
            fun_target,
            num_init,
            10,
            str_initial_method_bo='sobol',
            str_sampling_method_ao='sobol',
            num_samples_ao=100)
        list_Y.append(Y_final)
        list_time.append(time_final)
    arr_Y = np.array(list_Y)
    arr_Y = np.expand_dims(np.squeeze(arr_Y), axis=0)
    arr_time = np.array(list_time)
    arr_time = np.expand_dims(arr_time, axis=0)

    utils_plotting.plot_minimum_vs_iter(arr_Y, ['xgboost'],
                                        num_init,
                                        True,
                                        path_save=PATH_SAVE,
                                        str_postfix='xgboost')
    utils_plotting.plot_minimum_vs_time(arr_time,
                                        arr_Y, ['xgboost'],
                                        num_init,
                                        True,
                                        path_save=PATH_SAVE,
                                        str_postfix='xgboost')
Example #2
0
def main():
    num_bo = 5
    num_iter = 10
    num_init = 5

    bounds = obj_fun.get_bounds()
    model_bo = bo.BO(bounds, debug=True)
    list_Y = []
    list_time = []
    for ind_bo in range(0, num_bo):
        print('BO Round', ind_bo + 1)
        X_final, Y_final, time_final, _, _ = wrappers_bo_function.run_single_round(model_bo, fun_target, num_init, num_iter, str_initial_method_bo='sobol', str_sampling_method_ao='sobol', num_samples_ao=100)
        print(X_final)
        print(Y_final)
        print(time_final)
        list_Y.append(Y_final)
        list_time.append(time_final)

        bx_best, y_best = utils_bo.get_best_acquisition_by_history(X_final, Y_final)
        print(bx_best, y_best)

    arr_Y = np.array(list_Y)
    arr_Y = np.expand_dims(np.squeeze(arr_Y), axis=0)
    arr_time = np.array(list_time)
    arr_time = np.expand_dims(arr_time, axis=0)
    utils_plotting.plot_minimum_vs_iter(arr_Y, [STR_FUN_TARGET], num_init, True, path_save=PATH_SAVE, str_postfix=STR_FUN_TARGET)
    utils_plotting.plot_minimum_vs_time(arr_time, arr_Y, [STR_FUN_TARGET], num_init, True, path_save=PATH_SAVE, str_postfix=STR_FUN_TARGET)
def main():
    num_bo = 5
    num_iter = 10
    num_init = 5

    bounds = obj_fun.get_bounds()

    model_bo = wrappers.BayesianOptimization(
        bounds,
        fun_target,
        num_iter,
        str_surrogate='gp',
        str_cov='matern52',
        str_acq='ei',
        str_initial_method_bo='sobol',
        str_sampling_method_ao='sobol',
        str_optimizer_method_gp='BFGS',
        str_optimizer_method_bo='L-BFGS-B',
        num_samples_ao=100,
        debug=True,
    )

    list_Y = []
    list_time = []
    for ind_bo in range(0, num_bo):
        print('BO Round', ind_bo + 1)
        X, Y, time_all, _, _ = model_bo.optimize(num_init, seed=42 * ind_bo)
        list_Y.append(Y)
        list_time.append(time_all)

        bx_best, y_best = utils_bo.get_best_acquisition_by_history(X, Y)
        print(bx_best, y_best)

    Ys = np.array(list_Y)
    Ys = np.expand_dims(np.squeeze(Ys), axis=0)
    times = np.array(list_time)
    times = np.expand_dims(times, axis=0)

    utils_plotting.plot_minimum_vs_iter(Ys, [STR_FUN_TARGET],
                                        num_init,
                                        True,
                                        path_save=PATH_SAVE,
                                        str_postfix=STR_FUN_TARGET)
    utils_plotting.plot_minimum_vs_time(times,
                                        Ys, [STR_FUN_TARGET],
                                        num_init,
                                        True,
                                        path_save=PATH_SAVE,
                                        str_postfix=STR_FUN_TARGET)
Example #4
0
def test_plot_minimum_vs_time():
    num_model = 2
    num_bo = 3
    num_iter = 10
    arr_times = np.ones((num_model, num_bo, num_iter))
    arr_minima = np.ones((num_model, num_bo, num_iter))
    list_str_label = ['abc', 'def']
    num_init = 3
    draw_std = True

    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, arr_minima,
                                            list_str_label, num_init, 1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, arr_minima,
                                            list_str_label, 'abc', draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, arr_minima, 1, num_init,
                                            draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, 1, list_str_label,
                                            num_init, draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(1, arr_minima, list_str_label,
                                            num_init, draw_std)

    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(np.ones(
            (4, num_bo, num_iter)), arr_minima, list_str_label, num_init,
                                            draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(np.ones(
            (num_model, 4, num_iter)), arr_minima, list_str_label, num_init,
                                            draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(np.ones(
            (num_model, num_bo, 25)), arr_minima, list_str_label, num_init,
                                            draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(np.ones(
            (num_bo, num_iter)), arr_minima, list_str_label, num_init,
                                            draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(np.ones(num_iter), arr_minima,
                                            list_str_label, num_init, draw_std)

    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, np.ones((10, 2)),
                                            list_str_label, num_init, draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, np.ones(2),
                                            list_str_label, num_init, draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, arr_minima,
                                            ['abc', 'def', 'ghi'], num_init,
                                            draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times, arr_minima,
                                            list_str_label, 12, draw_std)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            include_marker=1)

    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            include_legend=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            use_tex=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            path_save=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            str_postfix=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            str_x_axis=1)

    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            str_y_axis=1)
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            pause_figure='abc')
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            time_pause='abc')
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            range_shade='abc')
    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            markers='abc')

    with pytest.raises(AssertionError) as error:
        package_target.plot_minimum_vs_time(arr_times,
                                            arr_minima,
                                            list_str_label,
                                            num_init,
                                            draw_std,
                                            colors='abc')