iteration_controller.py 3.03 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Martin Reinecke's avatar
Martin Reinecke committed
19
from builtins import range
Martin Reinecke's avatar
Martin Reinecke committed
20
import abc
Martin Reinecke's avatar
Martin Reinecke committed
21
from ..nifty_meta import NiftyMeta
Martin Reinecke's avatar
Martin Reinecke committed
22
23

from keepers import Loggable
Martin Reinecke's avatar
Martin Reinecke committed
24
from future.utils import with_metaclass
Martin Reinecke's avatar
Martin Reinecke committed
25

Theo Steininger's avatar
Theo Steininger committed
26
27
28

class IterationController(
        with_metaclass(NiftyMeta, type('NewBase', (Loggable, object), {}))):
Martin Reinecke's avatar
Martin Reinecke committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    """The abstract base class for all iteration controllers.
    An iteration controller is an object that monitors the progress of a
    minimization iteration. At the begin of the minimization, its start()
    method is called with the energy object at the initial position.
    Afterwards, its check() method is called during every iteration step with
    the energy object describing the current position.
    Based on that information, the iteration controller has to decide whether
    iteration needs to progress further (in this case it returns CONTINUE), or
    if sufficient convergence has been reached (in this case it returns
    CONVERGED), or if some error has been detected (then it returns ERROR).

    The concrete convergence criteria can be chosen by inheriting from this
    class; the implementer has full flexibility to use whichever criteria are
    appropriate for a particular problem - as ong as they can be computed from
    the information passed to the controller during the iteration process.
    """
Martin Reinecke's avatar
Martin Reinecke committed
45

Martin Reinecke's avatar
Martin Reinecke committed
46
    CONVERGED, CONTINUE, ERROR = list(range(3))
Martin Reinecke's avatar
Martin Reinecke committed
47

Theo Steininger's avatar
Theo Steininger committed
48
49
50
51
52
53
54
55
56
57
58
59
    def __init__(self):
        self._iteration_count = -1
        self._convergence_count = 0

    @property
    def iteration_count(self):
        return self._iteration_count

    @property
    def convergence_count(self):
        return self._convergence_count

Martin Reinecke's avatar
Martin Reinecke committed
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
    @abc.abstractmethod
    def start(self, energy):
        """
        Parameters
        ----------
        energy : Energy object
           Energy object at the start of the iteration

        Returns
        -------
        status : integer status, can be CONVERGED, CONTINUE or ERROR
        """

        raise NotImplementedError

    @abc.abstractmethod
    def check(self, energy):
        """
        Parameters
        ----------
        energy : Energy object
           Energy object at the start of the iteration

        Returns
        -------
        status : integer status, can be CONVERGED, CONTINUE or ERROR
        """

        raise NotImplementedError