test_minimizers.py 2.16 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.

Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
19
import unittest
20
import numpy as np
Martin Reinecke's avatar
changes  
Martin Reinecke committed
21
from numpy.testing import assert_allclose
Martin Reinecke's avatar
Martin Reinecke committed
22
import nifty4 as ift
Martin Reinecke's avatar
changes  
Martin Reinecke committed
23
from itertools import product
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
24 25
from test.common import expand

Martin Reinecke's avatar
changes  
Martin Reinecke committed
26 27
spaces = [ift.RGSpace([1024], distances=0.123), ift.HPSpace(32)]
minimizers = [ift.SteepestDescent, ift.RelaxedNewton, ift.VL_BFGS,
28
              ift.ConjugateGradient, ift.NonlinearCG]
29 30


Martin Reinecke's avatar
changes  
Martin Reinecke committed
31
class Test_Minimizers(unittest.TestCase):
32

Martin Reinecke's avatar
changes  
Martin Reinecke committed
33
    @expand(product(minimizers, spaces))
34
    def test_quadratic_minimization(self, minimizer_class, space):
35
        np.random.seed(42)
Martin Reinecke's avatar
changes  
Martin Reinecke committed
36 37 38
        starting_point = ift.Field.from_random('normal', domain=space)*10
        covariance_diagonal = ift.Field.from_random(
                                  'uniform', domain=space) + 0.5
39
        covariance = ift.DiagonalOperator(covariance_diagonal)
40
        required_result = ift.Field.ones(space, dtype=np.float64)
41

42
        IC = ift.GradientNormController(tol_abs_gradnorm=1e-5)
Martin Reinecke's avatar
changes  
Martin Reinecke committed
43 44 45
        minimizer = minimizer_class(controller=IC)
        energy = ift.QuadraticEnergy(A=covariance, b=required_result,
                                     position=starting_point)
46

Martin Reinecke's avatar
Martin Reinecke committed
47
        (energy, convergence) = minimizer(energy)
Martin Reinecke's avatar
changes  
Martin Reinecke committed
48
        assert convergence == IC.CONVERGED
Martin Reinecke's avatar
Martin Reinecke committed
49 50
        assert_allclose(ift.dobj.to_global_data(energy.position.val),
                        1./ift.dobj.to_global_data(covariance_diagonal.val),
Martin Reinecke's avatar
changes  
Martin Reinecke committed
51
                        rtol=1e-3, atol=1e-3)