1
2
3
4
5 """
6 Restarts a conjugate gradient search by deleting the step key in the state dictionary
7 """
8
9 import random
10 import numpy
11 import numpy.linalg
12
14 """
15 A step decorator that periodically deletes the direction key in the dictionary so that the CG search can be restarted
16 """
17 - def __init__(self, step, iteration_period = 10):
18 """
19 Constructs the decorator
20 - step is the step that will be decorated
21 - iteration_period is the number of iteration between the deletion
22 """
23 self.step = step
24 self.iteration_period = iteration_period
25
26 - def __call__(self, function, point, state):
27 """
28 Computes a step based on a function and a point
29 """
30 if (state['iteration'] % self.iteration_period == 0) & ('direction' in state):
31 del state['direction']
32 step = self.step(function, point, state)
33 return step
34
36 """
37 A step decorator that deletes the direction key in the dictionary if the last gradients are not orthogonal enough so that the CG search can be restarted
38 """
39 - def __init__(self, step, orthogonal_coeff = 0.1):
40 """
41 Constructs the decorator
42 - step is the step that will be decorated
43 - orthogonal_coeff is the orthogonal limit
44 """
45 self.step = step
46 self.orthogonal_coeff = orthogonal_coeff
47
48 - def __call__(self, function, point, state):
49 """
50 Computes a step based on a function and a point
51 """
52 newGradient = function.gradient(point)
53 if 'gradient' in state:
54 if abs(numpy.dot(state['gradient'], newGradient) / numpy.linalg.norm(newGradient)) > self.orthogonal_coeff:
55 del state['direction']
56 step = self.step(function, point, state)
57 return step
58