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

Source Code for Module minimise.polak_ribiere_plus_cg

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2003 Edward d'Auvergne                                        # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23   
 24  from Numeric import dot 
 25   
 26  from base_classes import Conjugate_gradient, Line_search, Min 
 27   
 28   
29 -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=""):
30 """Polak-Ribiere + conjugate gradient algorithm. 31 32 Page 122 from 'Numerical Optimization' by Jorge Nocedal and Stephen J. Wright, 1999, 2nd ed. 33 34 The algorithm is: 35 36 Given x0 37 Evaluate f0 = f(x0), g0 = g(x0) 38 Set p0 = -g0, k = 0 39 while g0 != 0: 40 Compute ak and set xk+1 = xk + ak.pk 41 Evaluate gk+1 42 bk+1 = max(dot(gk+1, (gk+1 - gk)) / dot(gk, gk), 0) 43 pk+1 = -gk+1 + bk+1.pk 44 k = k + 1 45 """ 46 47 if print_flag: 48 if print_flag >= 2: 49 print print_prefix 50 print print_prefix 51 print print_prefix + "Polak-Ribiere + conjugate gradient minimisation" 52 print print_prefix + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" 53 min = Polak_ribiere_plus(func, dfunc, args, x0, min_options, func_tol, grad_tol, maxiter, a0, mu, eta, full_output, print_flag, print_prefix) 54 if min.init_failure: 55 print print_prefix + "Initialisation of minimisation has failed." 56 return None 57 results = min.minimise() 58 return results
59 60
61 -class Polak_ribiere_plus(Conjugate_gradient, Line_search, Min):
62 - def __init__(self, func, dfunc, args, x0, min_options, func_tol, grad_tol, maxiter, a0, mu, eta, full_output, print_flag, print_prefix):
63 """Class for Polak-Ribiere + conjugate gradient minimisation specific functions. 64 65 Unless you know what you are doing, you should call the function 'polak_ribiere_plus' rather 66 than using this class. 67 """ 68 69 # Function arguments. 70 self.func = func 71 self.dfunc = dfunc 72 self.args = args 73 self.xk = x0 74 self.func_tol = func_tol 75 self.grad_tol = grad_tol 76 self.maxiter = maxiter 77 self.full_output = full_output 78 self.print_flag = print_flag 79 self.print_prefix = print_prefix 80 81 # Set a0. 82 self.a0 = a0 83 84 # Line search constants for the Wolfe conditions. 85 self.mu = mu 86 self.eta = eta 87 88 # Initialisation failure flag. 89 self.init_failure = 0 90 91 # Setup the line search options and algorithm. 92 self.line_search_options(min_options) 93 self.setup_line_search() 94 95 # Initialise the function, gradient, and Hessian evaluation counters. 96 self.f_count = 0 97 self.g_count = 0 98 self.h_count = 0 99 100 # Initialise the warning string. 101 self.warning = None 102 103 # Set the convergence test function. 104 self.setup_conv_tests() 105 106 # Calculate the initial function value and gradient vector. 107 self.fk, self.f_count = apply(self.func, (self.xk,)+self.args), self.f_count + 1 108 self.dfk, self.g_count = apply(self.dfunc, (self.xk,)+self.args), self.g_count + 1 109 self.pk = -self.dfk 110 self.dot_dfk = dot(self.dfk, self.dfk)
111 112
113 - def calc_bk(self):
114 """Function to calcaluate the Polak-Ribiere + beta value.""" 115 116 # Calculate beta at k+1. 117 bk_new = dot(self.dfk_new, self.dfk_new - self.dfk) / self.dot_dfk 118 return max(bk_new, 0.0)
119