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...")
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()
# 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) x, values, runtimes, gd_xs = alg.newton(hw1_func.weird_func, 0.0, eps, maximum_iterations, alg.backtracking_line_search) print(
# 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) print( 'Optimum of the weird function found by BFGS with backtracking line search', x) # Run algorithms on the quadratic function eps = 1e-10 initial_x = np.matrix('4.0; 0.3')