Пример #1
0
def test_barrier_one_step():
    # Simple 2-dimensional problem with identity matrix.
    A = np.eye(2)
    b = np.array([1.0, 0.0])
    reg_coef = 1.0

    x_0 = np.array([0.0, 1.0])
    u_0 = np.array([2.0, 2.0])

    (x_star, u_star), message, history = optimization.barrier_method_lasso(
        A, b, reg_coef, x_0, u_0,
        lasso_duality_gap=oracles.lasso_duality_gap,
        trace=True)
    eq_(message, "success")
    ok_(np.allclose(
        x_star,
        np.array([0.00315977, 0.0])))
    ok_(isinstance(x_star, np.ndarray))
    ok_(np.allclose(
        u_star,
        np.array([3.16979051e-03, 2.00000000e-05])))
    ok_(isinstance(u_star, np.ndarray))
    check_equal_histories(
        history,
        {'time': [None] * 7,
         'duality_gap': [
             2.0,
             0.47457244767635376,
             0.083152839863428252,
             0.0094880329978710432,
             0.0009840651705023129,
             9.9498755346316692e-05,
             9.9841761475039092e-06]})
Пример #2
0
def test_barrier_univariate():
    # Minimize 1/2 * x^2 + 3.0 * u,
    #     s.t. -u <= x <= u

    A = np.array([[1.0]])
    b = np.array([0.0])
    reg_coef = 3.0

    x_0 = np.array([1.0])
    u_0 = np.array([2.0])

    print("Univariate")
    (x_star, u_star), message, history = optimization.barrier_method_lasso(
        A, b, reg_coef, x_0, u_0,
        lasso_duality_gap=oracles.lasso_duality_gap,
        trace=True)
    eq_(message, "success")
    ok_(np.allclose(
        x_star,
        np.array([-7.89796404e-07])))
    ok_(isinstance(x_star, np.ndarray))
    ok_(np.allclose(
        u_star,
        np.array([0.66666568])))
    ok_(isinstance(u_star, np.ndarray))
    check_equal_histories(
        history,
        {'time': [None] * 2,
         'duality_gap': [4.0, 2.3693898355252885e-06]})
def experiment_2(parameter='n', seed=31415):

    # n, m and lambda
    n, m, reg_coef = 500, 500, 1.0

    np.random.seed(seed)
    grids = dict()
    grids['n'] = [10, 100, 500, 1000, 2000]
    grids['m'] = [10, 100, 500, 1000, 2000]
    grids['reg_coef'] = [0.01, 0.1, 1.0, 10.0, 100.0]

    fig1, ax1 = plt.subplots()
    fig2, ax2 = plt.subplots()

    ax1.set_xlabel('Номер итерации')
    ax1.set_ylabel('Гарантированная точность по зазору двойственности')
    ax1.grid()
    ax1.set_yscale('log')

    ax2.set_xlabel('Время от начала эксперимента')
    ax2.set_ylabel('Гарантированная точность по зазору двойственности')
    ax2.grid()
    ax2.set_yscale('log')
    os.makedirs("report/pics/2", exist_ok=True)

    experiment_parameters = {'n': n, 'm': m, 'reg_coef': 1.0}

    for value in grids[parameter]:
        experiment_parameters[parameter] = value
        A = np.random.randn(experiment_parameters['m'],
                            experiment_parameters['n'])
        b = np.random.randn(experiment_parameters['m'])
        # x_0 = np.zeros(experiment_parameters['n'])
        x_0 = 0.1 * np.ones(experiment_parameters['n'])
        u_0 = 2 * x_0

        reg_coef = experiment_parameters['reg_coef']
        (x_opt, u_opt), message, history = barrier_method_lasso(
            A,
            b,
            reg_coef,
            x_0=x_0,
            u_0=u_0,
            lasso_duality_gap=lasso_duality_gap,
            trace=True)
        ax1.plot(history['duality_gap'], label=f'{parameter}={value}')
        ax2.plot(history['time'],
                 history['duality_gap'],
                 label=f'{parameter}={value}')

    ax1.legend()
    ax2.legend()

    os.makedirs("report/pics/2", exist_ok=True)
    fig1.savefig(f"report/pics/2/barrier_lasso_gap_vs_iter_{parameter}.pdf",
                 bbox_inches='tight')
    fig2.savefig(f"report/pics/2/barrier_lasso_gap_vs_time_{parameter}.pdf",
                 bbox_inches='tight')
def experiment_1(parameter='gamma'):

    # gamma and eps_inner

    np.random.seed(31415)
    data_path = "data"
    datasets = ["abalone", "cpusmall", "housing", "triazines"]

    gamma_grid = [2, 10, 100, 1000]
    inner_eps_grid = [1e-8, 1e-6, 1e-4, 1e-2, 1e-1, 5e-1]

    for dataset in datasets:
        fig1, ax1 = plt.subplots()
        fig2, ax2 = plt.subplots()

        ax1.set_xlabel('Номер итерации')
        ax1.set_ylabel('Гарантированная точность по зазору двойственности')
        ax1.grid()
        ax1.set_yscale('log')

        ax2.set_xlabel('Время от начала эксперимента')
        ax2.set_ylabel('Гарантированная точность по зазору двойственности')
        ax2.grid()
        ax2.set_yscale('log')

        print("___________________________")
        logging.info(f"{dataset} is in process...")

        A, b = load_svmlight_file(os.path.join(data_path, dataset))
        print(A.shape, 1 - A.size / (A.shape[0] * A.shape[1]),
              np.linalg.matrix_rank(A.toarray()))

        os.makedirs("report/pics/1", exist_ok=True)

        n = A.shape[1]
        x0 = np.zeros(n)
        u0 = 0.1 * np.ones(n)
        # x0 = np.random.randn(n)
        # u0 = np.abs(x0) * 2
        if parameter == 'gamma':
            for gamma in gamma_grid:
                (x_opt, u_opt), message, history = barrier_method_lasso(
                    A,
                    b,
                    1.0,
                    x0,
                    u0,
                    gamma=gamma,
                    lasso_duality_gap=lasso_duality_gap,
                    trace=True)
                ax1.plot(history['duality_gap'], label=f'gamma={gamma}')
                ax2.plot(history['time'],
                         history['duality_gap'],
                         label=f'gamma={gamma}')
        else:
            for inner_eps in inner_eps_grid:
                (x_opt, u_opt), message, history = barrier_method_lasso(
                    A,
                    b,
                    1.0,
                    np.zeros(n),
                    np.ones(n),
                    tolerance_inner=inner_eps,
                    lasso_duality_gap=lasso_duality_gap,
                    trace=True)
                ax1.plot(history['duality_gap'],
                         label=f'eps_inner={inner_eps}')
                ax2.plot(history['time'],
                         history['duality_gap'],
                         label=f'eps_inner={inner_eps}')

        ax1.legend()
        ax2.legend()

        os.makedirs("report/pics/1", exist_ok=True)
        fig1.savefig(
            f"report/pics/1/barrier_lasso_gap_vs_iter_{parameter}_{dataset}.pdf",
            bbox_inches='tight')
        fig2.savefig(
            f"report/pics/1/barrier_lasso_gap_vs_time_{parameter}_{dataset}.pdf",
            bbox_inches='tight')