-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcg.py
45 lines (42 loc) · 1.08 KB
/
cg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
from numpy.linalg import inv
from scipy import optimize as opt
import math
import armijo
class CGMethod(object):
"""
Constructor CG-Method
"""
def __init__ (self, f, fd, H, xk, eps):
self.fd = fd
self.f = f
self.H = H
self.xk = xk
self.eps = eps
return
"""
CG-Method
"""
def work (self):
f = self.f
fd = self.fd
H = self.H
xk = self.xk
eps = self.eps
t = 1
it = 0
#maxit = 10000
dprev = - fd(xk)
xprev = self.xk
dk = None
while(np.linalg.norm(fd(xk)) > eps): #and (it < maxit):
t = armijo.schrittweite(f, fd, xprev, dprev, sigma = 0.02, rho = 0.5, gamma = 2)
xk = xprev + t * dprev
normprev = (np.linalg.norm(fd(xprev)))**2
xprev = xk
normk = (np.linalg.norm(fd(xk)))**2
dk = - fd(xk) + (normk / normprev)*(dprev)
dprev = dk
print("Log-Values(CG): ", math.log10(f(xk)))
it += 1
return xk, it