scipy_minimizer.py 4.44 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# 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/>.
#
Martin Reinecke's avatar
Martin Reinecke committed
14
# Copyright(C) 2013-2018 Max-Planck-Society
Martin Reinecke's avatar
Martin Reinecke committed
15
16
17
18
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
19
from __future__ import division
Martin Reinecke's avatar
Martin Reinecke committed
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
from .minimizer import Minimizer
from ..field import Field
from .. import dobj


class ScipyMinimizer(Minimizer):
    """Scipy-based minimizer

    Parameters
    ----------
    controller : IterationController
        Object that decides when to terminate the minimization.
    method     : str
        The selected Scipy minimization method.
    options    : dictionary
        A set of custom options for the selected minimizer.
    """

    def __init__(self, controller, method, options, need_hessp):
        super(ScipyMinimizer, self).__init__()
        if not dobj.is_numpy():
            raise NotImplementedError
        self._controller = controller
        self._method = method
        self._options = options
        self._need_hessp = need_hessp

    def __call__(self, energy):
Martin Reinecke's avatar
fix    
Martin Reinecke committed
48
        class _MinimizationDone(BaseException):
Martin Reinecke's avatar
Martin Reinecke committed
49
50
51
52
53
54
55
56
57
58
59
            pass

        class _MinHelper(object):
            def __init__(self, controller, energy):
                self._controller = controller
                self._energy = energy
                self._domain = energy.position.domain

            def _update(self, x):
                pos = Field(self._domain, x.reshape(self._domain.shape))
                if (pos.val != self._energy.position.val).any():
Martin Reinecke's avatar
Martin Reinecke committed
60
                    self._energy = self._energy.at(pos.locked_copy())
Martin Reinecke's avatar
Martin Reinecke committed
61
62
63
64
65
66
67
68
69
70
                    status = self._controller.check(self._energy)
                    if status != self._controller.CONTINUE:
                        raise _MinimizationDone

            def fun(self, x):
                self._update(x)
                return self._energy.value

            def jac(self, x):
                self._update(x)
Martin Reinecke's avatar
Martin Reinecke committed
71
                return self._energy.gradient.val.flatten()
Martin Reinecke's avatar
Martin Reinecke committed
72
73
74
75
76

            def hessp(self, x, p):
                self._update(x)
                vec = Field(self._domain, p.reshape(self._domain.shape))
                res = self._energy.curvature(vec)
Martin Reinecke's avatar
Martin Reinecke committed
77
                return res.val.flatten()
Martin Reinecke's avatar
Martin Reinecke committed
78
79
80
81
82
83
84

        import scipy.optimize as opt
        hlp = _MinHelper(self._controller, energy)
        energy = None
        status = self._controller.start(hlp._energy)
        if status != self._controller.CONTINUE:
            return hlp._energy, status
Martin Reinecke's avatar
Martin Reinecke committed
85
        x = hlp._energy.position.val.flatten()
Martin Reinecke's avatar
Martin Reinecke committed
86
87
        try:
            if self._need_hessp:
Martin Reinecke's avatar
Martin Reinecke committed
88
89
90
91
                r = opt.minimize(hlp.fun, x,
                                 method=self._method, jac=hlp.jac,
                                 hessp=hlp.hessp,
                                 options=self._options)
Martin Reinecke's avatar
Martin Reinecke committed
92
            else:
Martin Reinecke's avatar
Martin Reinecke committed
93
94
95
                r = opt.minimize(hlp.fun, x,
                                 method=self._method, jac=hlp.jac,
                                 options=self._options)
Martin Reinecke's avatar
Martin Reinecke committed
96
97
98
        except _MinimizationDone:
            status = self._controller.check(hlp._energy)
            return hlp._energy, self._controller.check(hlp._energy)
Martin Reinecke's avatar
Martin Reinecke committed
99
        if not r.success:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
100
            dobj.mprint("Problem in Scipy minimization:", r.message)
Martin Reinecke's avatar
Martin Reinecke committed
101
        else:
Martin Reinecke's avatar
stage 1    
Martin Reinecke committed
102
            dobj.mprint("Problem in Scipy minimization")
Martin Reinecke's avatar
Martin Reinecke committed
103
104
105
106
        return hlp._energy, self._controller.ERROR


def NewtonCG(controller):
Martin Reinecke's avatar
Martin Reinecke committed
107
108
109
110
111
112
    """Returns a ScipyMinimizer object carrying out the Newton-CG algorithm.

    See Also
    --------
    ScipyMinimizer
    """
Martin Reinecke's avatar
Martin Reinecke committed
113
114
115
116
117
    return ScipyMinimizer(controller, "Newton-CG",
                          {"xtol": 1e-20, "maxiter": None}, True)


def L_BFGS_B(controller, maxcor=10):
Martin Reinecke's avatar
Martin Reinecke committed
118
119
120
121
122
123
    """Returns a ScipyMinimizer object carrying out the L-BFGS-B algorithm.

    See Also
    --------
    ScipyMinimizer
    """
Martin Reinecke's avatar
Martin Reinecke committed
124
125
126
    return ScipyMinimizer(controller, "L-BFGS-B",
                          {"ftol": 1e-20, "gtol": 1e-20, "maxcor": maxcor},
                          False)