Package minfx :: Module cauchy_point
[hide private]
[frames] | no frames]

Source Code for Module minfx.cauchy_point

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003-2013 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the minfx optimisation library,                        # 
  6  # https://gna.org/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  """Cauchy Point trust region nonlinear optimization. 
 25   
 26  This file is part of the U{minfx optimisation library<https://gna.org/projects/minfx>}. 
 27  """ 
 28   
 29  # Python module imports. 
 30  from numpy import dot, sqrt 
 31   
 32  # Minfx module imports. 
 33  from minfx.base_classes import Min, Trust_region 
 34   
 35   
36 -def cauchy_point(func=None, dfunc=None, d2func=None, args=(), x0=None, func_tol=1e-25, grad_tol=None, maxiter=1e6, delta_max=1e5, delta0=1.0, eta=0.2, full_output=0, print_flag=0, print_prefix=""):
37 """Cauchy Point trust region algorithm. 38 39 Page 69 from 'Numerical Optimization' by Jorge Nocedal and Stephen J. Wright, 1999, 2nd ed. The Cauchy point is defined by:: 40 41 delta 42 pCk = - tau_k ------- dfk 43 ||dfk|| 44 45 where: 46 47 - delta_k is the trust region radius, 48 - dfk is the gradient vector, 49 50 and:: 51 52 / 1 if dfk . Bk . dfk <= 0 53 tau_k = < 54 \ min(||dfk||**2/(delta . dfk . Bk . dfk), 1) otherwise. 55 """ 56 57 if print_flag: 58 if print_flag >= 2: 59 print(print_prefix) 60 print(print_prefix) 61 print(print_prefix + "Cauchy point minimisation") 62 print(print_prefix + "~~~~~~~~~~~~~~~~~~~~~~~~~") 63 min = Cauchy_point(func, dfunc, d2func, args, x0, func_tol, grad_tol, maxiter, delta_max, delta0, eta, full_output, print_flag, print_prefix) 64 results = min.minimise() 65 return results
66 67
68 -class Cauchy_point(Trust_region, Min):
69 - def __init__(self, func, dfunc, d2func, args, x0, func_tol, grad_tol, maxiter, delta_max, delta0, eta, full_output, print_flag, print_prefix):
70 """Class for Cauchy Point trust region minimisation specific functions. 71 72 Unless you know what you are doing, you should call the function 'cauchy_point' rather than using this class. 73 """ 74 75 # Function arguments. 76 self.func = func 77 self.dfunc = dfunc 78 self.d2func = d2func 79 self.args = args 80 self.xk = x0 81 self.func_tol = func_tol 82 self.grad_tol = grad_tol 83 self.maxiter = maxiter 84 self.full_output = full_output 85 self.print_flag = print_flag 86 self.print_prefix = print_prefix 87 self.delta_max = delta_max 88 self.delta = delta0 89 self.eta = eta 90 91 # Initialise the function, gradient, and Hessian evaluation counters. 92 self.f_count = 0 93 self.g_count = 0 94 self.h_count = 0 95 96 # Initialise the warning string. 97 self.warning = None 98 99 # Set the convergence test function. 100 self.setup_conv_tests() 101 102 # Initial values before the first iteration. 103 self.fk, self.f_count = self.func(*(self.xk,)+self.args), self.f_count + 1 104 self.dfk, self.g_count = self.dfunc(*(self.xk,)+self.args), self.g_count + 1 105 self.d2fk, self.h_count = self.d2func(*(self.xk,)+self.args), self.h_count + 1
106 107
108 - def new_param_func(self):
109 """Find the Cauchy point.""" 110 111 # Calculate the curvature and norm. 112 curv = dot(self.dfk, dot(self.d2fk, self.dfk)) 113 norm_dfk = sqrt(dot(self.dfk, self.dfk)) 114 115 # tau_k. 116 if curv <= 0.0: 117 self.tau_k = 1.0 118 else: 119 self.tau_k = min(norm_dfk ** 3 / (self.delta * curv), 1.0) 120 121 if self.print_flag >= 2: 122 print(self.print_prefix + "dfk . Bk . dfk: " + repr(curv)) 123 print(self.print_prefix + "tau_k: " + repr(self.tau_k)) 124 125 # Cauchy point. 126 self.pk = - self.tau_k * self.delta * self.dfk / norm_dfk 127 128 # Find the new parameter vector and function value at that point. 129 self.xk_new = self.xk + self.pk 130 self.fk_new, self.f_count = self.func(*(self.xk_new,)+self.args), self.f_count + 1 131 self.dfk_new, self.g_count = self.dfunc(*(self.xk_new,)+self.args), self.g_count + 1
132 133
134 - def update(self):
135 """Update function. 136 137 Function to update the function value, gradient vector, and Hessian matrix. 138 """ 139 140 self.xk = self.xk_new * 1.0 141 self.fk = self.fk_new 142 self.dfk = self.dfk_new * 1.0 143 self.d2fk, self.h_count = self.d2func(*(self.xk,)+self.args), self.h_count + 1
144