test_vl_bfgs.py 2.64 KB
Newer Older
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
1 2
import unittest

Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
3
from numpy.testing import assert_equal, assert_almost_equal
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
4 5 6 7 8 9 10

from nifty import *

from itertools import product
from test.common import expand
from test.common import generate_spaces

Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
11
np.random.seed(42)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
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


class QuadraticPot(Energy):
    def __init__(self, position, N):
        super(QuadraticPot, self).__init__(position)
        self.N = N
        
    def at(self, position):
        return self.__class__(position, N = self.N)


    @property
    def value(self):
        H = 0.5 *self.position.dot(self.N.inverse_times(self.position))
        return H.real

    @property
    def gradient(self):
        g = self.N.inverse_times(self.position)
        return_g = g.copy_empty(dtype=np.float)
        return_g.val = g.val.real
        return return_g
            
    @property
    def curvature(self):
        return self.N



class VL_BFGS_Tests(unittest.TestCase):
    spaces = generate_spaces()


    @expand(product(spaces, [10,  100, 1000], [1E-3, 1E-4, 1E-5], [2, 3, 4] ))
    def test_property(self, space, iteration_limit, convergence_tolerance, 
                      convergence_level):
        
        x = Field.from_random('normal', domain=space)
        N = DiagonalOperator(space, diagonal = 1.)
        energy = QuadraticPot(position=x , N=N)
        
        minimizer = VL_BFGS(iteration_limit=iteration_limit,
                                    convergence_tolerance=convergence_tolerance, 
                                    convergence_level=convergence_level)
                                    
        (energy, convergence) = minimizer(energy)
        
        if energy.position.domain[0] != space:
            raise TypeError
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
61
        if type(convergence) != int:
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
            raise TypeError

    @expand(product(spaces, [10,  100, 1000], [1E-3, 1E-4, 1E-5], [2, 3, 4] ))
    def test_property(self, space, iteration_limit, convergence_tolerance, 
                      convergence_level):
        
        x = Field.from_random('normal', domain=space)
        test_x = Field(space, val = 0.)
        N = DiagonalOperator(space, diagonal = 1.)
        energy = QuadraticPot(position=x , N=N)
        
        minimizer = VL_BFGS(iteration_limit=iteration_limit,
                                    convergence_tolerance=convergence_tolerance, 
                                    convergence_level=convergence_level)
                                    
        (energy, convergence) = minimizer(energy)
        
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
79
        assert_almost_equal(energy.value, 0, decimal=3)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
80
        assert_almost_equal(energy.position.val.get_full_data(), 
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
81
                            test_x.val.get_full_data(), decimal=3)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
82
        assert_equal(convergence, convergence_level+2)
Matevz, Sraml (sraml)'s avatar
Matevz, Sraml (sraml) committed
83