scipy_minimizer.py 6.16 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.

19
from __future__ import absolute_import, division, print_function
20

21
import numpy as np
22
from .. import dobj
23
from ..compat import *
Martin Reinecke's avatar
Martin Reinecke committed
24
from ..field import Field
25
26
from ..multi_field import MultiField
from ..domain_tuple import DomainTuple
Martin Reinecke's avatar
Martin Reinecke committed
27
from ..logger import logger
28
from .iteration_controller import IterationController
29
from .minimizer import Minimizer
30
31


32
33
34
35
36
37
38
39
40
41
42
43
44
45
def _multiToArray(fld):
    szall = 0
    for val in fld.values():
        if val.dtype != np.float64:
            raise TypeError("need float64 fields")
        szall += val.size
    res = np.empty(szall, dtype=np.float64)
    ofs = 0
    for val in fld.values():
        res[ofs:ofs+val.size] = val.local_data.reshape(-1)
        ofs += val.size
    return res


Martin Reinecke's avatar
Martin Reinecke committed
46
def _toArray(fld):
47
48
49
    if isinstance(fld, Field):
        return fld.local_data.reshape(-1)
    return _multiToArray(fld)
Martin Reinecke's avatar
Martin Reinecke committed
50
51


Martin Reinecke's avatar
Martin Reinecke committed
52
def _toArray_rw(fld):
53
54
55
    if isinstance(fld, Field):
        return fld.local_data.copy().reshape(-1)
    return _multiToArray(fld)
Martin Reinecke's avatar
Martin Reinecke committed
56
57
58


def _toField(arr, dom):
59
60
61
62
63
64
65
66
67
    if isinstance(dom, DomainTuple):
        return Field.from_local_data(dom, arr.reshape(dom.shape).copy())
    ofs = 0
    res = []
    for d in dom.domains():
        res.append(Field.from_local_data(
            d, arr[ofs:ofs+d.size].copy().reshape(d.shape)))
        ofs += d.size
    return MultiField(dom, tuple(res))
Martin Reinecke's avatar
Martin Reinecke committed
68
69


70
71
72
73
74
75
class _MinHelper(object):
    def __init__(self, energy):
        self._energy = energy
        self._domain = energy.position.domain

    def _update(self, x):
Martin Reinecke's avatar
Martin Reinecke committed
76
77
        pos = _toField(x, self._domain)
        if (pos != self._energy.position).any():
78
            self._energy = self._energy.at(pos)
79
80
81
82
83
84
85

    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
86
        return _toArray_rw(self._energy.gradient)
87
88
89

    def hessp(self, x, p):
        self._update(x)
Martin Reinecke's avatar
Martin Reinecke committed
90
        res = self._energy.metric(_toField(p, self._domain))
Martin Reinecke's avatar
Martin Reinecke committed
91
        return _toArray_rw(res)
Martin Reinecke's avatar
Martin Reinecke committed
92
93
94
95
96
97
98
99
100
101
102
103
104


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

    Parameters
    ----------
    method     : str
        The selected Scipy minimization method.
    options    : dictionary
        A set of custom options for the selected minimizer.
    """

105
    def __init__(self, method, options, need_hessp, bounds):
Martin Reinecke's avatar
Martin Reinecke committed
106
107
108
109
110
        if not dobj.is_numpy():
            raise NotImplementedError
        self._method = method
        self._options = options
        self._need_hessp = need_hessp
111
        self._bounds = bounds
Martin Reinecke's avatar
Martin Reinecke committed
112
113
114

    def __call__(self, energy):
        import scipy.optimize as opt
115
116
117
118
119
120
121
122
        hlp = _MinHelper(energy)
        energy = None  # drop handle, since we don't need it any more
        bounds = None
        if self._bounds is not None:
            if len(self._bounds) == 2:
                lo = self._bounds[0]
                hi = self._bounds[1]
                bounds = [(lo, hi)]*hlp._energy.position.size
Martin Reinecke's avatar
Martin Reinecke committed
123
            else:
124
125
                raise ValueError("unrecognized bounds")

Martin Reinecke's avatar
Martin Reinecke committed
126
        x = _toArray_rw(hlp._energy.position)
127
128
129
        hessp = hlp.hessp if self._need_hessp else None
        r = opt.minimize(hlp.fun, x, method=self._method, jac=hlp.jac,
                         hessp=hessp, options=self._options, bounds=bounds)
Martin Reinecke's avatar
Martin Reinecke committed
130
        if not r.success:
131
            logger.error("Problem in Scipy minimization: {}".format(r.message))
132
133
            return hlp._energy, IterationController.ERROR
        return hlp._energy, IterationController.CONVERGED
Martin Reinecke's avatar
Martin Reinecke committed
134
135


136
def NewtonCG(xtol, maxiter, disp=False):
Martin Reinecke's avatar
Martin Reinecke committed
137
138
139
140
141
142
    """Returns a ScipyMinimizer object carrying out the Newton-CG algorithm.

    See Also
    --------
    ScipyMinimizer
    """
Martin Reinecke's avatar
fix    
Martin Reinecke committed
143
    options = {"xtol": xtol, "maxiter": maxiter, "disp": disp}
144
    return ScipyMinimizer("Newton-CG", options, True, None)
Martin Reinecke's avatar
Martin Reinecke committed
145
146


147
def L_BFGS_B(ftol, gtol, maxiter, maxcor=10, disp=False, bounds=None):
Martin Reinecke's avatar
Martin Reinecke committed
148
149
150
151
152
153
    """Returns a ScipyMinimizer object carrying out the L-BFGS-B algorithm.

    See Also
    --------
    ScipyMinimizer
    """
154
    options = {"ftol": ftol, "gtol": gtol, "maxiter": maxiter,
Martin Reinecke's avatar
fix    
Martin Reinecke committed
155
               "maxcor": maxcor, "disp": disp}
156
    return ScipyMinimizer("L-BFGS-B", options, False, bounds)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176


class ScipyCG(Minimizer):
    def __init__(self, tol, maxiter):
        if not dobj.is_numpy():
            raise NotImplementedError
        self._tol = tol
        self._maxiter = maxiter

    def __call__(self, energy, preconditioner=None):
        from scipy.sparse.linalg import LinearOperator as scipy_linop, cg
        from .quadratic_energy import QuadraticEnergy
        if not isinstance(energy, QuadraticEnergy):
            raise ValueError("need a quadratic energy for CG")

        class mymatvec(object):
            def __init__(self, op):
                self._op = op

            def __call__(self, inp):
Martin Reinecke's avatar
Martin Reinecke committed
177
                return _toArray(self._op(_toField(inp, self._op.domain)))
178
179

        op = energy._A
Martin Reinecke's avatar
Martin Reinecke committed
180
181
        b = _toArray(energy._b)
        sx = _toArray(energy.position)
182
183
184
185
186
187
188
189
190
191
        sci_op = scipy_linop(shape=(op.domain.size, op.target.size),
                             matvec=mymatvec(op))
        prec_op = None
        if preconditioner is not None:
            prec_op = scipy_linop(shape=(op.domain.size, op.target.size),
                                  matvec=mymatvec(preconditioner))
        res, stat = cg(sci_op, b, x0=sx, tol=self._tol, M=prec_op,
                       maxiter=self._maxiter)
        stat = (IterationController.CONVERGED if stat >= 0 else
                IterationController.ERROR)
Martin Reinecke's avatar
Martin Reinecke committed
192
        return energy.at(_toField(res, op.domain)), stat