|
| 1 | +from gradv import GradientMethod |
| 2 | +import numpy as np |
| 3 | +import time |
| 4 | +from newtonm import NewtonMethod |
| 5 | +from numpy.linalg import inv |
| 6 | +from cg import CGMethod |
| 7 | +from bfgs import BFGS |
| 8 | +import armijo |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +class Rosenb: |
| 13 | + |
| 14 | + """ |
| 15 | + Select Solver (Gradient-Method, Newton-Method, CG-Method, BFGS-Method) |
| 16 | + """ |
| 17 | + |
| 18 | + |
| 19 | + def algo(self, fo, alsolver): |
| 20 | + x0 = fo.x0 |
| 21 | + f = fo.f |
| 22 | + fd = fo.fd |
| 23 | + H = fo.H |
| 24 | + x = fo.x0 |
| 25 | + eps = 0.0001 |
| 26 | + it = None |
| 27 | + while(np.linalg.norm(fd(x)) > eps): |
| 28 | + solver = None |
| 29 | + if(alsolver == 'gradv'): |
| 30 | + #Nutze Gradientenverfahren Loesung |
| 31 | + solver = GradientMethod(f, fd, H, x, eps) |
| 32 | + elif(alsolver == 'newtonm'): |
| 33 | + solver = NewtonMethod(f, fd, H, x, eps) |
| 34 | + elif(alsolver == 'cg'): |
| 35 | + solver = CGMethod(f, fd, H, x, eps) |
| 36 | + elif(alsolver == 'bfgs'): |
| 37 | + solver = BFGS(f, fd, H, x, eps) |
| 38 | + else: |
| 39 | + print("ERROR, NO SOLVER SELECTED. EXITING") |
| 40 | + exit() |
| 41 | + |
| 42 | + x, it = solver.work() |
| 43 | + |
| 44 | + return x, it |
| 45 | + |
| 46 | +""" |
| 47 | +Create Function that should be minimized |
| 48 | +""" |
| 49 | +class Function(object): |
| 50 | + x0 = None |
| 51 | + f = None |
| 52 | + fd = None |
| 53 | + H = None |
| 54 | + |
| 55 | + def __init__(self, x0, f, fd, H): |
| 56 | + self.x0 = x0 |
| 57 | + self.f = f |
| 58 | + self.fd = fd |
| 59 | + self.H = H |
| 60 | + """ |
| 61 | + Rosenbrock function |
| 62 | + """ |
| 63 | + def testFunctionRosenbrock(): |
| 64 | + f = lambda xy: (10*(xy[0] - xy[1]**2))**2 + (1-xy[0])**2 |
| 65 | + fd = lambda xy: np.array([202.*xy[0] - 200*xy[1]**2 - 2, -400*xy[1]*(xy[0] - xy[1]**2)]) |
| 66 | + H = lambda xy: np.array([ [202., -400.*xy[1] ], |
| 67 | + [-400.*xy[1], 800.*xy[1]**2 - 400.*(xy[0] - xy[1]**2) ] |
| 68 | + ]) |
| 69 | + x0 = np.array([-2.,2.]) |
| 70 | + return Function(x0,f,fd,H) |
| 71 | + |
| 72 | + """ |
| 73 | + Simple quadratic function ((x,y) -> (x + 3)^2 + y^2) |
| 74 | + """ |
| 75 | + def testFunction2(): |
| 76 | + f = lambda xy: (xy[0] + 3)**2 + xy[1]**2 |
| 77 | + fd = lambda xy: np.array([2*xy[0] + 6, 2*xy[1]]) |
| 78 | + H = lambda xy: np.array([[2., 0.],[0., 2.]]) |
| 79 | + x0 = np.array([3.,0.]) |
| 80 | + return Function(x0,f,fd,H) |
| 81 | + |
| 82 | + |
| 83 | +print("STARTING TESTS") |
| 84 | +a = Rosenb() |
| 85 | +""" |
| 86 | +print("-----Testing quadratic(2) function-----") |
| 87 | +f = Function.testFunction2() |
| 88 | +x_gv, it_gv = a.algo(f,'gradv') |
| 89 | +x_nm, it_nm = a.algo(f, 'newtonm') |
| 90 | +x_cg, it_cg = a.algo(f, 'cg') |
| 91 | +#x_bfgs, it_bfgs = a.algo(f, 'bfgs') |
| 92 | +print("Result (Grad): ",x_gv, " steps: ", it_gv) |
| 93 | +print("Result (Newton): ",x_nm, " steps: ", it_nm) |
| 94 | +print("Result (CG): ",x_cg, "steps: ", it_cg) |
| 95 | +#print("Result (BFGS): ",x_bfgs, "steps: ", it_bfgs) |
| 96 | +assert np.allclose(x_gv, np.array([-3.,0.]), rtol=1e-04, atol=1e-05) |
| 97 | +assert np.allclose(x_nm, np.array([-3.,0.]), rtol=1e-04, atol=1e-05) |
| 98 | +assert np.allclose(x_cg, np.array([-3.,0.]), rtol=1e-04, atol=1e-05) |
| 99 | +#assert np.allclose(x_bfgs, np.array([-3.,0.]), rtol=1e-04, atol=1e-05) |
| 100 | +print(" + Pass") |
| 101 | +""" |
| 102 | + |
| 103 | +print("-----Testing Rosenbrock function-----") |
| 104 | +f = Function.testFunctionRosenbrock() |
| 105 | +x_gv, it_gv = a.algo(f,'gradv') |
| 106 | +x_nm, it_nm = a.algo(f, 'newtonm') |
| 107 | +x_cg, it_cg = a.algo(f, 'cg') |
| 108 | +x_bfgs, it_bfgs = a.algo(f, 'bfgs') |
| 109 | +print("Result (Grad): ",x_gv, " steps: ", it_gv) |
| 110 | +print("Result (Newton): ",x_nm, " steps: ", it_nm) |
| 111 | +print("Result (CG): ",x_cg, "steps: ", it_cg) |
| 112 | +print("Result (BFGS): ",x_bfgs, "steps: ", it_bfgs) |
| 113 | +assert np.allclose(x_gv, np.array([1.,1.]), rtol=1e-04, atol=1e-05) |
| 114 | +assert np.allclose(x_nm, np.array([1.,1.]), rtol=1e-04, atol=1e-05) |
| 115 | +assert np.allclose(x_cg, np.array([1.,1.]), rtol=1e-04, atol=1e-05) |
| 116 | +assert np.allclose(x_bfgs, np.array([1.,1.]), rtol=1e-04, atol=1e-05) |
| 117 | +print(" + Pass") |
0 commit comments