Beispiel #1
0
def compare(solver, test_f, test_grad_f, test_hessian_f, gamma):
    solver_gd = gradient_descent_solver.GradDescSolver(solver.x0,
                                                       solver.maxIts,
                                                       solver.tol)
    solver_gd.solve(test_f, test_grad_f, Time=False)
    print(solver_gd)

    solver_nm = newton_solver.NewtonSolver(solver.x0, solver.maxIts,
                                           solver.tol)
    solver_nm.solve(test_grad_f, test_hessian_f, Time=False)
    print(solver_nm)

    solver_bfgs = bfgs_solver.BFGSSolver(solver.x0, solver.maxIts, solver.tol)
    solver_bfgs.solve(test_f, test_grad_f, Time=False)
    print(solver_bfgs)

ns = []
times = []

n = 5

while (n <= 505):

    x0 = np.repeat(1.5, n)

    tol = 1e-4
    max_its = 10000

    start = time.time()
    solver_bfgs = bfgs_solver.BFGSSolver(x0, max_its, tol)
    solver_bfgs.solve(rosenbrock_f, rosenbrock_grad_f, Time=False)
    time_taken = time.time() - start

    if (n - 5) % 20 == 0:
        output = "Finished n = {}\n".format(n)
        print(output)
    ns.append(n)
    times.append(time_taken)
    n += 10

f = plt.figure(1)
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.xlabel(r'Dimension $n$', fontsize=16)
plt.ylabel(r'Runtime ($s$)', fontsize=16)
tol = 1e-6
max_its = 100000

solver = solver.Solver(x0, max_its, tol)

solver_gd = gradient_descent_solver.GradDescSolver(solver.x0, solver.maxIts,
                                                   solver.tol)
solver_gd.solve(rosenbrock_f, rosenbrock_grad_f, Time=False)
print(solver_gd)

solver_nm = newton_solver.NewtonSolver(solver.x0, solver.maxIts, solver.tol)
solver_nm.solve(rosenbrock_grad_f, rosenbrock_hess_f, Time=False)
print(solver_nm)

solver_bfgs = bfgs_solver.BFGSSolver(solver.x0, solver.maxIts, solver.tol)
solver_bfgs.solve(rosenbrock_f, rosenbrock_grad_f, Time=False)
print(solver_bfgs)

root = np.repeat(1.0, n)

plot_region = [0, 3, 0, 3]

x = np.linspace(-5, 5, 500)
y = np.linspace(-5, 9.6, 500)
x, y = np.meshgrid(x, y)

z = rosen(np.array([x, y]))

grad_pts = solver_gd.trace
newt_pts = solver_nm.trace