Package minimise :: Package line_search :: Module backtrack
[hide private]
[frames] | no frames]

Source Code for Module minimise.line_search.backtrack

 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 -def backtrack(func, args, x, f, g, p, a_init=1.0, rho=0.5, c=1e-4, max_iter=500):
27 """Backtracking line search. 28 29 Procedure 3.1, page 41, from 'Numerical Optimization' by Jorge Nocedal and Stephen J. Wright, 30 1999, 2nd ed. 31 32 Requires the gradient vector at point xk. 33 34 35 Function options 36 ~~~~~~~~~~~~~~~~ 37 38 func - The function to minimise. 39 args - The tuple of arguments to supply to the functions func. 40 x - The parameter vector. 41 f - The function value at the point x. 42 g - The gradient vector at the point x. 43 p - The descent direction. 44 a_init - Initial step length. 45 rho - The step length scaling factor (should be between 0 and 1). 46 c - Constant between 0 and 1 determining the slope for the sufficient decrease condition. 47 48 49 Returned objects 50 ~~~~~~~~~~~~~~~~ 51 52 The parameter vector, minimised along the direction xk + ak.pk, to be used at k+1. 53 54 55 Internal variables 56 ~~~~~~~~~~~~~~~~~~ 57 58 ai - The step length at line search iteration i. 59 xai - The parameter vector at step length ai. 60 fai - The function value at step length ai. 61 """ 62 63 # Initialise values. 64 a = a_init 65 f_count = 0 66 i = 0 67 68 while i <= max_iter: 69 fk = apply(func, (x + a*p,)+args) 70 f_count = f_count + 1 71 72 # Check if the sufficient decrease condition is met. If not, scale the step length by rho. 73 if fk <= f + c*a*dot(g, p): 74 return a, f_count 75 else: 76 a = rho*a 77 78 # Increment the counter. 79 i = i + 1
80