コード例 #1
0
def qf_test_2(eps):
    param = [1, 1, 1, 0, 0, 0]
    f = qf(param)
    a = 1
    size_1, size_2 = random.uniform(0.5 * a, a), random.uniform(0.5 * a, a)
    x_1, y_1 = 1, 1
    f.solution[0], f.solution[1] = x_1, y_1
    f.min = 5
    Q = [1, 2, 1, 2]
    f.lipschitz_function(Q), f.lipschitz_gradient(Q)
    solver = main_solver(f, Q, eps)
    solver.init_help_function()
    res = solver.halving_square()[2]
    norm_gradient = lambda x: f.calculate_function(x[0], x[1]) - f.min
    plt.semilogy([i for i in range(len(res))], [norm_gradient(i) for i in res])
    plt.semilogy([i for i in range(len(res))],
                 [(i[0] - x_1)**2 + (i[1] - y_1)**2 for i in res])
    plt.grid()
    n = 13
    plt.xlabel("Iterations Number", fontsize=13)
    plt.ylabel("Value of Error", fontsize=13)
    plt.legend([r"$f(x)-f^*$", r"$\|x-x^*\|$"], fontsize=13)
    plt.xticks(fontsize=13)
    plt.yticks(fontsize=13)
    q_2 = 1. / 2 * np.log(f.M * a**2 / (4 * eps)) / np.log(2)
    q_1 = np.log(f.L * a / (np.sqrt(2) * eps)) / np.log(2)
    print('Theoretical Iteration Number through function constant',
          np.ceil(q_1))
    print('Theoretical Iteration Number through gradient constant',
          np.ceil(q_2))
    return res
コード例 #2
0
def get_tests_estimates(epsilon, mean_input=True, num_compare=False):
    N = 1000
    name = ['True gradient', 'Constant estimate', 'Current gradient']
    full_results = []
    dict_results = dict()
    for eps in epsilon:
        results = []
        for j in range(N):
            param = np.random.uniform(-10, 10, 6)
            param[2] = abs(param[2])
            f = qf(param)
            size_1, size_2 = random.uniform(0.5, 1), random.uniform(0.5, 1)
            x_1, y_1 = f.solution[0], f.solution[1]
            Q = [
                x_1 - (1 - size_1), x_1 + (1 + size_1), y_1 - (1 - size_2),
                y_1 + (1 + size_2)
            ]
            R = 2
            L = f.lipschitz_function(Q)
            M = f.lipschitz_gradient(Q)
            solver = main_solver(f, Q, eps)
            m1 = time()
            solver.init_help_function(stop_func='true_grad')
            res_1 = solver.halving_square()
            m2 = time()
            solver.init_help_function(stop_func='const_est')
            res_2 = solver.halving_square()
            m3 = time()
            solver.init_help_function(stop_func='cur_grad')
            res_3 = solver.halving_square()
            m4 = time()
            results.append(
                (eps, res_1[1], m2 - m1, res_2[1], m3 - m2, res_3[1], m4 - m3))
        if mean_input:
            print('eps = ', "{:.1e}".format(eps))
            _ = dict()
            for j in range(3):
                list = [i[2 + j * 2] for i in results if i[1 + j * 2] >= 0]
                _[name[j]] = np.mean(list) * 1000
                print('Mean time (%s) = %.2fms' %
                      (name[j], 1000 * np.mean(list)))
                list = [i[2 + j * 2] for i in results if i[1 + j * 2] < 0]
                q = len(list)
                if q > 0:
                    print("%s: Number of failed tests equals %d" %
                          (name[j], q))
            dict_results[eps] = _
        if num_compare:
            list_n = [(i, j, k) for i in range(3) for j in range(3)
                      for k in range(3) if i != j and j != k and i != k]
            for n in list_n:
                ind = len([
                    i for i in results
                    if i[2 + n[0] * 2] <= i[2 + n[1] * 2] <= i[2 + n[2] * 2]
                ])
                print("'%s' <= '%s' <= '%s': %d" %
                      (name[n[0]], name[n[1]], name[n[2]], ind))
        full_results += results
    return dict_results
コード例 #3
0
def comparison(f, Q, eps):
    n = 0
    m1 = time()
    res_1 = gradient_descent(f.calculate_function, Q, f.gradient,
                             f.lipschitz_gradient(Q), eps, f.min)
    m2 = time()
    solver = main_solver(f, Q, eps)
    solver.init_help_function()
    res_2 = solver.halving_square()
    m3 = time()
    res_3 = ellipsoid(f, Q, eps=eps)
    m4 = time()
    return res_1[2], m2 - m1, res_2[2], m3 - m2, res_3[2], m4 - m3
コード例 #4
0
def comparison_LogSumExp(N=2, time_max=100, a=None):
    if a is None:
        a = np.random.uniform(-100, 100, size=(N, ))
    f = LogSumExp(a)
    Q = f.get_square()
    L, M = f.lipschitz_function(Q), f.lipschitz_gradient(Q)
    solver = main_solver(f, Q, eps=None)
    res = dict()
    fdict = dict()

    print('Ellipsoids')
    res['Ellipsoids'] = ellipsoid(f,
                                  Q,
                                  time_max=time_max,
                                  time=True,
                                  cur_eps=eps)
    fdict = {**fdict, **f.values}
    f.values = dict()

    print('CurGrad')
    solver.init_help_function()
    res['HalvingSquare-CurGrad'] = solver.halving_square(time_max=time_max,
                                                         time=True)
    fdict = {**fdict, **f.values}
    f.values = dict()

    print('Const_est')
    solver.init_help_function('const_est')
    res['HalvingSquare-Const'] = solver.halving_square(eps=1e-2)
    fdict = {**fdict, **f.values}
    f.values = dict()

    print('GD')
    res['GD'] = gradient_descent(f.calculate_function,
                                 Q,
                                 f.gradient,
                                 M,
                                 time=True,
                                 time_max=time_max)
    fdict = {**fdict, **f.values}
    f.values = fdict

    return res, f