1
2
3
4
5 """
6 Line Search with the quadratic interpolation method with the computation of the gradient of the function
7 """
8
9 import math
10 import numpy
11
13 """
14 Line Search with the quadratic interpolation when the gradient of the function is provided
15 """
16 - def __init__(self, min_alpha_step, alpha_step = 1., **kwargs):
17 """
18 Needs to have :
19 - a minimum step size (min_alpha_step)
20 Can have :
21 - a step modifier, a factor to modulate the step (alpha_step = 1.)
22 """
23 self.minStepSize = min_alpha_step
24 self.stepSize = alpha_step
25
26 - def __call__(self, origin, function, state, **kwargs):
27 """
28 Returns a good candidate
29 Parameters :
30 - origin is the origin of the search
31 - function is the function to minimize
32 - state is the state of the optimizer
33 """
34 direction = state['direction']
35 ak = 0.
36 if 'initial_alpha_step' in state:
37 bk = state['initial_alpha_step']
38 else:
39 bk = self.stepSize
40 v_bk = function(origin + bk * direction)
41
42 while abs(bk - ak) > self.minStepSize:
43 v_ak = function(origin + ak * direction)
44 g_ak = numpy.dot(function.gradient(origin + ak * direction), direction)
45 ck = ak - .5 * (ak - bk) * g_ak / (g_ak - (v_ak - v_bk) / (ak - bk))
46 v_ck = function(origin + ck * direction)
47
48 bk = ak
49 ak = ck
50 v_bk = v_ak
51 v_ak = v_ck
52
53 state['alpha_step'] = ck
54 return origin + ck * direction
55