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

Source Code for Module minfx.polak_ribiere_plus_cg

  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  """Polak-Ribiere + conjugate gradient optimization. 
 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 
 31   
 32  # Minfx module imports. 
 33  from minfx.base_classes import Conjugate_gradient, Line_search, Min 
 34   
 35   
36 -def polak_ribiere_plus(func=None, dfunc=None, args=(), x0=None, min_options=None, func_tol=1e-25, grad_tol=None, maxiter=1e6, a0=1.0, mu=0.0001, eta=0.1, full_output=0, print_flag=0, print_prefix=""):
37 """Polak-Ribiere + conjugate gradient algorithm. 38 39 Page 122 from 'Numerical Optimization' by Jorge Nocedal and Stephen J. Wright, 1999, 2nd ed. The algorithm is: 40 41 - Given x0 42 - Evaluate f0 = f(x0), g0 = g(x0) 43 - Set p0 = -g0, k = 0 44 - while g0 != 0: 45 - Compute ak and set xk+1 = xk + ak.pk 46 - Evaluate gk+1 47 - bk+1 = max(dot(gk+1, (gk+1 - gk)) / dot(gk, gk), 0) 48 - pk+1 = -gk+1 + bk+1.pk 49 - k = k + 1 50 """ 51 52 if print_flag: 53 if print_flag >= 2: 54 print(print_prefix) 55 print(print_prefix) 56 print(print_prefix + "Polak-Ribiere + conjugate gradient minimisation") 57 print(print_prefix + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~") 58 min = Polak_ribiere_plus(func, dfunc, args, x0, min_options, func_tol, grad_tol, maxiter, a0, mu, eta, full_output, print_flag, print_prefix) 59 if min.init_failure: 60 print(print_prefix + "Initialisation of minimisation has failed.") 61 return None 62 results = min.minimise() 63 return results
64 65
66 -class Polak_ribiere_plus(Conjugate_gradient, Line_search, Min):
67 - def __init__(self, func, dfunc, args, x0, min_options, func_tol, grad_tol, maxiter, a0, mu, eta, full_output, print_flag, print_prefix):
68 """Class for Polak-Ribiere + conjugate gradient minimisation specific functions. 69 70 Unless you know what you are doing, you should call the function 'polak_ribiere_plus' rather than using this class. 71 """ 72 73 # Function arguments. 74 self.func = func 75 self.dfunc = dfunc 76 self.args = args 77 self.xk = x0 78 self.func_tol = func_tol 79 self.grad_tol = grad_tol 80 self.maxiter = maxiter 81 self.full_output = full_output 82 self.print_flag = print_flag 83 self.print_prefix = print_prefix 84 85 # Set a0. 86 self.a0 = a0 87 88 # Line search constants for the Wolfe conditions. 89 self.mu = mu 90 self.eta = eta 91 92 # Initialisation failure flag. 93 self.init_failure = 0 94 95 # Setup the line search options and algorithm. 96 self.line_search_options(min_options) 97 self.setup_line_search() 98 99 # Initialise the function, gradient, and Hessian evaluation counters. 100 self.f_count = 0 101 self.g_count = 0 102 self.h_count = 0 103 104 # Initialise the warning string. 105 self.warning = None 106 107 # Set the convergence test function. 108 self.setup_conv_tests() 109 110 # Calculate the initial function value and gradient vector. 111 self.fk, self.f_count = self.func(*(self.xk,)+self.args), self.f_count + 1 112 self.dfk, self.g_count = self.dfunc(*(self.xk,)+self.args), self.g_count + 1 113 self.pk = -self.dfk 114 self.dot_dfk = dot(self.dfk, self.dfk)
115 116
117 - def calc_bk(self):
118 """Function to calculate the Polak-Ribiere + beta value.""" 119 120 # Calculate beta at k+1. 121 bk_new = dot(self.dfk_new, self.dfk_new - self.dfk) / self.dot_dfk 122 return max(bk_new, 0.0)
123