Package minfx :: Package hessian_mods :: Module cholesky_mod
[hide private]
[frames] | no frames]

Source Code for Module minfx.hessian_mods.cholesky_mod

 1  ############################################################################### 
 2  #                                                                             # 
 3  # Copyright (C) 2003-2013 Edward d'Auvergne                                   # 
 4  #                                                                             # 
 5  # This file is part of the minfx optimisation library,                        # 
 6  # https://sourceforge.net/projects/minfx                                      # 
 7  #                                                                             # 
 8  # This program is free software: you can redistribute it and/or modify        # 
 9  # it under the terms of the GNU General Public License as published by        # 
10  # the Free Software Foundation, either version 3 of the License, or           # 
11  # (at your option) any later version.                                         # 
12  #                                                                             # 
13  # This program is distributed in the hope that it will be useful,             # 
14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
16  # GNU General Public License for more details.                                # 
17  #                                                                             # 
18  # You should have received a copy of the GNU General Public License           # 
19  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
20  #                                                                             # 
21  ############################################################################### 
22   
23  # Module docstring. 
24  """Cholesky Hessian modification with added multiple of the identity. 
25   
26  This file is part of the U{minfx optimisation library<https://sourceforge.net/projects/minfx>}. 
27  """ 
28   
29  # Python module imports. 
30  from numpy import dot, sqrt, trace, transpose 
31  from numpy.linalg import LinAlgError, cholesky, solve 
32   
33   
34 -def cholesky_mod(dfk, d2fk, I, n, print_prefix, print_flag, return_matrix=0):
35 """Cholesky with added multiple of the identity. 36 37 Algorithm 6.3 from page 145 of 'Numerical Optimization' by Jorge Nocedal and Stephen J. Wright, 1999, 2nd ed. 38 39 Returns the modified Newton step. 40 """ 41 42 # Find the minimum diagonal value of the Hessian. 43 min_aii = 1e99 44 for i in range(n): 45 min_aii = min(d2fk[i, i], min_aii) 46 47 # Calculate the Frobenius norm of the Hessian. 48 norm = sqrt(trace(dot(d2fk, d2fk))) 49 half_norm = norm / 2.0 50 51 # Choose the initial tk value. 52 if min_aii > 0.0: 53 tk = 0.0 54 else: 55 tk = half_norm 56 57 # Debugging. 58 if print_flag >= 3: 59 print(print_prefix + "Frobenius norm: " + repr(norm)) 60 print(print_prefix + "min aii: " + repr(min_aii)) 61 print(print_prefix + "tk: " + repr(tk)) 62 63 # Loop until the matrix is positive definite. 64 while True: 65 if print_flag >= 3: 66 print(print_prefix + "Iteration") 67 68 # Calculate the matrix A + tk.I 69 matrix = d2fk + tk * I 70 71 # Attempt the Cholesky decomposition. 72 try: 73 L = cholesky(matrix) 74 if print_flag >= 3: 75 print(print_prefix + "\tCholesky matrix L:") 76 for i in range(n): 77 print(print_prefix + "\t\t" + repr(L[i])) 78 except LinAlgError: 79 if print_flag >= 3: 80 print(print_prefix + "\tLinearAlgebraError, matrix is not positive definite.") 81 82 # Update of tk. 83 tk = max(2.0*tk, half_norm) 84 else: 85 # Success, therefore break out of the while loop. 86 break 87 88 # Calculate the Newton direction. 89 y = solve(L, dfk) 90 if return_matrix: 91 return -solve(transpose(L), y), matrix 92 else: 93 return -solve(transpose(L), y)
94