Package minimise :: Module newton
[hide private]
[frames] | no frames]

Source Code for Module minimise.newton

 1  from LinearAlgebra import inverse 
 2  from Numeric import copy, dot, matrixmultiply 
 3   
 4  from generic_line_search import generic_line_search 
 5  from generic_minimise import generic_minimise 
 6   
 7   
8 -class newton(generic_line_search, generic_minimise):
9 - def __init__(self, func, dfunc=None, d2func=None, args=(), x0=None, line_search_algor=None, func_tol=1e-5, maxiter=1000, full_output=0, print_flag=0, a0=1.0, mu=0.0001, eta=0.9):
10 "Class for Newton minimisation specific functions." 11 12 self.func = func 13 self.dfunc = dfunc 14 self.d2func = d2func 15 self.args = args 16 self.xk = x0 17 self.func_tol = func_tol 18 self.maxiter = maxiter 19 self.full_output = full_output 20 self.print_flag = print_flag 21 22 if not line_search_algor: 23 raise NameError, "No line search algorithm has been supplied." 24 else: 25 self.line_search_algor = line_search_algor 26 27 # Set a0. 28 self.a0 = a0 29 30 # Line search constants for the Wolfe conditions. 31 self.mu = mu 32 self.eta = eta 33 34 # Initialise the function, gradient, and hessian evaluation counters. 35 self.f_count = 0 36 self.g_count = 0 37 self.h_count = 0 38 39 # Initialise the warning string. 40 self.warning = None 41 42 # The initial Newton function value, gradient vector, and hessian matrix. 43 self.fk, self.f_count = apply(self.func, (self.xk,)+self.args), self.f_count + 1 44 self.dfk, self.g_count = apply(self.dfunc, (self.xk,)+self.args), self.g_count + 1 45 self.d2fk, self.h_count = apply(self.d2func, (self.xk,)+self.args), self.h_count + 1 46 47 # Minimisation. 48 self.minimise = self.generic_minimise
49 50
51 - def backup_current_data(self):
52 "Function to backup the current data into fk_last." 53 54 self.fk_last = self.fk
55 56
57 - def dir(self):
58 "Calculate the Newton direction." 59 60 self.pk = -matrixmultiply(inverse(self.d2fk), self.dfk) 61 if dot(self.dfk, self.pk) >= 0.0: 62 self.pk = -self.dfk
63 64
65 - def update_data(self):
66 "Function to update the function value, gradient vector, and hessian matrix" 67 68 self.xk = copy.deepcopy(self.xk_new) 69 self.fk, self.f_count = apply(self.func, (self.xk,)+self.args), self.f_count + 1 70 self.dfk, self.g_count = apply(self.dfunc, (self.xk,)+self.args), self.g_count + 1 71 self.d2fk, self.h_count = apply(self.d2func, (self.xk,)+self.args), self.h_count + 1
72