line_search.py 2.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import abc

import numpy as np

from keepers import Loggable


def bare_dot(a, b):
    try:
        return a.dot(b, bare=True)
    except(AttributeError, TypeError):
        pass

    try:
        return a.vdot(b)
    except(AttributeError):
        pass

    return np.vdot(a, b)


class LineSearch(object, Loggable):
    """
    Class for finding a step size.◙
    """
    __metaclass__ = abc.ABCMeta

    def __init__(self):

        """
        Parameters
        ----------

        f : callable f(x, *args)
            Objective function.

        fprime : callable f'(x, *args)
            Objective functions gradient.

        f_args : tuple (optional)
            Additional arguments passed to objective function and its
            derivation.
        """

        self.xk = None
        self.pk = None

        self.f_k = None
        self.fprime_k = None

    def set_functions(self, f, fprime, f_args=()):
        assert(callable(f))
        assert(callable(fprime))

        self.f = f
        self.fprime = fprime
        self.f_args = f_args

    def set_coordinates(self, xk, pk, f_k=None, fprime_k=None):
        """
        Set the coordinates for a new line search.

        Parameters
        ----------
        xk : ndarray, d2o, field
            Starting point.

        pk : ndarray, d2o, field
            Unit vector in search direction.

        f_k : float (optional)
            Function value f(x_k).

        fprime_k : ndarray, d2o, field (optional)
            Function value fprime(xk).

        """

        self.xk = xk
        self.pk = pk

        if f_k is None:
            self.f_k = self.f(xk)
        else:
            self.f_k = f_k

        if fprime_k is None:
            self.fprime_k = self.fprime(xk)
        else:
            self.fprime_k = fprime_k

    def _phi(self, alpha):
        if alpha == 0:
            value = self.f_k
        else:
            value = self.f(self.xk + self.pk*alpha, *self.f_args)
        return value

    def _phiprime(self, alpha):
        if alpha == 0:
            gradient = self.fprime_k
        else:
            gradient = self.fprime(self.xk + self.pk*alpha, *self.f_args)

        return bare_dot(gradient, self.pk)

    @abc.abstractmethod
    def perform_line_search(self):
        raise NotImplementedError