1
2
3
4
5 import numpy
6
8 """
9 The backtracking algorithm for enforcing Armijo rule
10 """
11 - def __init__(self, rho = 0.1, alpha_step = 1., alpha_factor = 0.5, **kwargs):
12 """
13 Can have :
14 - a coefficient for the Armijo rule (rho = 0.1)
15 - an alpha factor to modulate the step (alpha_step = 1.)
16 - an alpha factor < 1 that will decrease the step size until the rule is valid (alpha_factor = 0.5)
17 """
18 self.rho = rho
19 self.stepSize = alpha_step
20 self.stepFactor = alpha_factor
21
22 - def __call__(self, origin, function, state, **kwargs):
23 """
24 Tries to find an acceptable candidate
25 """
26 direction = state['direction']
27 if 'initial_alpha_step' in state:
28 alpha = state['initial_alpha_step']
29 else:
30 alpha = self.stepSize
31
32 f1temp = function(origin)
33 gradient = state['gradient']
34 while(True):
35 ftemp = function(origin + alpha * direction)
36
37 if ftemp <= f1temp + self.rho * alpha * numpy.dot(gradient, direction):
38 state['alpha_step'] = alpha
39 return origin + alpha * direction
40 alpha = alpha * self.stepFactor
41