def main_quadratic():
    np.seterr(divide='ignore')
    # the hessian matrix of the quadratic function
    Q = np.matrix('5 2; 2 2')

    # the vector of linear coefficient of the quadratic function
    v = np.matrix('21; 13')

    A = np.matrix('2 1; 2 -5;-1 1')

    b = np.matrix('8; 10; 3')

    t = len( b )

    func = lambda x, order: quadratic_log_barrier( Q, v, A, b, x, t, order )


    # Start at (0,0)
    initial_x = np.matrix('0.0; 0.0')
    # Find the (1e-8)-suboptimal solution
    eps = 1e-8
    maximum_iterations = 65536

    # Run the algorithms
    x, values, runtimes, gd_xs = alg.gradient_descent( func, initial_x, eps, maximum_iterations, alg.backtracking_line_search )

    x, values, runtimes, newton_xs = alg.newton( func, initial_x, eps, maximum_iterations, alg.backtracking_line_search )

    # Draw contour plots
    draw_contour( func, gd_xs, newton_xs, levels=np.arange(-300, 300, 10), x=np.arange(-4, 2.1, 0.1), y=np.arange(-4, 2.1, 0.1) )
    input("Press Enter to continue...")
Пример #2
0
def main_quadratic2():
    np.seterr(divide='ignore')

    # the hessian matrix of the quadratic function
    Q = np.matrix('5 2; 2 2')

    # the vector of linear coefficient of the quadratic function
    v = np.matrix('21; 13')

    A = np.matrix('2 1; 2 -5;-1 1')

    b = np.matrix('8; 10; 3')

    mu = np.sqrt(2)

    lower_t = 0.1249
    upper_t = 2
    #upper_t = 100

    # Start at (0,0)
    initial_x = np.matrix('0.0; 0.0')
    # Find the (1e-8)-suboptimal solution
    eps = 1e-8
    maximum_iterations = 65536

    ts = []
    gd_iterations = []
    newton_iterations = []

    t = lower_t
    while t <= upper_t:
        func = lambda x, order: quadratic_log_barrier(Q, v, A, b, x, t, order)
        ts.append(t)

        # Run the algorithms
        x, values, runtimes, gd_xs = alg.gradient_descent(
            func, initial_x, eps, maximum_iterations,
            alg.backtracking_line_search)
        gd_iterations.append(len(values) - 1)
        x, values, runtimes, newton_xs = alg.newton(
            func, initial_x, eps, maximum_iterations,
            alg.backtracking_line_search)
        newton_iterations.append(len(values) - 1)

        t *= mu

    plt.plot(ts, gd_iterations, linewidth=2, color='r', label='GD', marker='o')
    plt.plot(ts,
             newton_iterations,
             linewidth=2,
             color='m',
             label='Newton',
             marker='o')
    plt.xlabel('t')
    plt.ylabel('iterations')
    plt.legend()
    plt.show()
Пример #3
0
# Choose the quadratic objective. This notation defines a "closure", returning
# an oracle function which takes x (and order) as its only parameter, and calls
# obj.quadratic with parameters H and b defined above, and the parameters of
# the closure (x and order)
func = lambda x, order: hw1_func.quadratic(H, b, x, order)

# Find the (1e-4)-suboptimal solution
eps = 1e-4
maximum_iterations = 65536

# Run the algorithms on the weird function
x = alg.bisection(hw1_func.weird_func, -100, 100, eps, maximum_iterations)
print('Optimum of the weird function found by bisection', x)

x, values, runtimes, gd_xs = alg.gradient_descent(hw1_func.weird_func, 0.0,
                                                  eps, maximum_iterations,
                                                  alg.exact_line_search)
print('Optimum of the weird function found by GD with exact line search', x)

x, values, runtimes, gd_xs = alg.newton(hw1_func.weird_func, 0.0, eps,
                                        maximum_iterations,
                                        alg.exact_line_search)
print('Optimum of the weird function found by Newton with exact line search',
      x)

x, values, runtimes, gd_xs = alg.gradient_descent(hw1_func.weird_func, 0.0,
                                                  eps, maximum_iterations,
                                                  alg.backtracking_line_search)
print(
    'Optimum of the weird function found by GD with backtracking line search',
    x)
Пример #4
0
# Choose the quadratic objective. This notation defines a "closure", returning
# an oracle function which takes x (and order) as its only parameter, and calls
# obj.quadratic with parameters H and b defined above, and the parameters of
# the closure (x and order)
func = lambda x, order: hw_func.quadratic(H, b, x, order)

# Run algorithms on the weird function
eps = 1e-4
maximum_iterations = 65536

x = alg.bisection(hw_func.weird_func, -100, 100, eps, maximum_iterations)
print('Optimum of the weird function found by bisection', x)

x, values, runtimes, gd_xs = alg.gradient_descent(hw_func.weird_func, 0.0, eps,
                                                  maximum_iterations,
                                                  alg.backtracking_line_search)
print(
    'Optimum of the weird function found by GD with backtracking line search',
    x)

x, values, runtimes, gd_xs = alg.newton(hw_func.weird_func, 0.0, eps,
                                        maximum_iterations,
                                        alg.backtracking_line_search)
print(
    'Optimum of the weird function found by Newton with backtracking line search',
    x)

x, values, runtimes, gd_xs = alg.bfgs(hw_func.weird_func, 0.0, 1.0, eps,
                                      maximum_iterations,
                                      alg.backtracking_line_search)
Пример #5
0
#     [1, 5, 205],
#     [2, 4, 304],
#     [3, 3, 403],
#     [4, 2, 502],
#     [5, 1, 601],
#     [6, -10, 690],
#     [7, -10, 790],
#     [8, -1, 899],
# ]

training_set = [
    [0, 1, 1, 1, 1],
    [1, 1, 0, 1, 0],
    [0, 1, 1, 0, 1],
    [0, 0, 0, 0, 0],
    [1, 1, 1, 1, 0],
    [1, 0, 0, 0, 1],
    [1, 0, 1, 1, 0],
    [1, 1, 1, 0, 0],
    [0, 0, 1, 0, 1],
    [0, 1, 0, 1, 0],
    [1, 0, 1, 1, 1],
    [0, 1, 1, 0, 0],
    [1, 0, 0, 1, 1],
]

hypo = LogisticHypothesis()
gradient_descent(hypothesis=hypo,
                 training_set=training_set,
                 convergent_criteria=0.001)
Пример #6
0
#     [1, 5, 205],
#     [2, 4, 304],
#     [3, 3, 403],
#     [4, 2, 502],
#     [5, 1, 601],
#     [6, -10, 690],
#     [7, -10, 790],
#     [8, -1, 899],
# ]

training_set = [
    [0,1,1,1,1],
    [1,1,0,1,0],
    [0,1,1,0,1],
    [0,0,0,0,0],
    [1,1,1,1,0],
    [1,0,0,0,1],
    [1,0,1,1,0],
    [1,1,1,0,0],
    [0,0,1,0,1],
    [0,1,0,1,0],
    [1,0,1,1,1],
    [0,1,1,0,0],
    [1,0,0,1,1],
]

hypo = LogisticHypothesis();
gradient_descent(hypothesis=hypo,
                 training_set=training_set,
                 convergent_criteria=0.001)