Example #1
0
    def test_SetBoundsErrorCases(self):
        # Array sizes don't match
        some_bounds = libUnfitPython.Bounds(3)
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        lower[:] = [0.0, 0.0]
        upper[:] = [1.0, 1.0, 1.0]
        self.assertFalse(some_bounds.SetBounds(lower, upper))

        # Negative infinite bound
        lower.append(-float('inf'))
        self.assertFalse(some_bounds.SetBounds(lower, upper))

        # Positive infinite bound
        lower[2] = 0.0
        upper[1] = float('inf')
        self.assertFalse(some_bounds.SetBounds(lower, upper))

        # Lower bound > upper bound
        upper[1] = -1.0
        self.assertFalse(some_bounds.SetBounds(lower, upper))

        # Now check that the original bounds are intact
        some_bounds.GetBounds(lower, upper)
        for bnd in lower:
            self.assertAlmostEqual(max_negative_num, bnd, test_tol)
        for bnd in upper:
            self.assertAlmostEqual(max_positive_num, bnd, test_tol)
Example #2
0
class QuadraticCostFunction(libUnfitPython.GenericCostFunction):
    t = libUnfitPython.std_vector_double()
    y = libUnfitPython.std_vector_double()

    def __init__(self, t_data=None, y_data=None):
        libUnfitPython.GenericCostFunction.__init__(self)

        if t_data != None and y_data != None:
            self.t[:] = t_data
            self.y[:] = y_data
        else:
            self.t[:] = []
            self.y[:] = []

    def FileInit(self, filename):
        i = 0

        with open(filename) as f:
            for line in f:
                if (i == 0):
                    self.t[:] = [float(x) for x in line.split()]
                    i = 1
                else:
                    self.y[:] = [float(x) for x in line.split()]

    def __call__(self, x):
        r = libUnfitPython.std_vector_double()
        r[:] = self.y[:]
        for i, ti in enumerate(self.t):
            model = x[0] + x[1] * ti + x[2] * ti * ti
            r[i] -= model
        return r
Example #3
0
    def test_ClampPointWithinBounds(self):
        some_bounds = libUnfitPython.Bounds()
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        lower[:] = [0.0, 0.0, 0.0]
        upper[:] = [1.0, 1.0, 1.0]
        some_bounds.SetBounds(lower, upper)

        # Already within bounds, should do nothing
        point = libUnfitPython.std_vector_double()
        point[:] = [0.5, 0.5, 0.5]
        some_bounds.ClampWithinBounds(point)
        self.assertAlmostEqual(0.5, point[0], test_tol)
        self.assertAlmostEqual(0.5, point[1], test_tol)
        self.assertAlmostEqual(0.5, point[2], test_tol)

        # Clamp to lower bound
        point[1] = -1.0
        some_bounds.ClampWithinBounds(point)
        self.assertAlmostEqual(0.5, point[0], test_tol)
        self.assertAlmostEqual(0.0, point[1], test_tol)
        self.assertAlmostEqual(0.5, point[2], test_tol)

        # Clamp to upper bound
        point[1] = 2.0
        some_bounds.ClampWithinBounds(point)
        self.assertAlmostEqual(0.5, point[0], test_tol)
        self.assertAlmostEqual(1.0, point[1], test_tol)
        self.assertAlmostEqual(0.5, point[2], test_tol)
Example #4
0
    def test_SetOneBoundErrorCases(self):
        # Array sizes don't match
        some_bounds = libUnfitPython.Bounds(3)

        # Negative inifinite bound
        self.assertFalse(some_bounds.SetBounds(2, -float('inf'), 1.0))

        # Positive inifnite bounds
        self.assertFalse(some_bounds.SetBounds(2, 0.0, float('inf')))

        # Lower bound > upper bound
        self.assertFalse(some_bounds.SetBounds(2, 1.0, 0.0))

        # Check adding an index greater than the vector size (should scale)
        self.assertTrue(
            some_bounds.SetBounds(5, max_negative_num, max_positive_num))
        self.assertEqual(6, some_bounds.GetNumberOfBounds())

        # Now check that the original and extended bounds are intact
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        some_bounds.GetBounds(lower, upper)
        for bnd in lower:
            self.assertAlmostEqual(max_negative_num, bnd, test_tol)
        for bnd in upper:
            self.assertAlmostEqual(max_positive_num, bnd, test_tol)
Example #5
0
    def setUp(self):
        '''
            Processes done at the start of every test in this class.
        '''
        lower = libUnfitPython.std_vector_double()
        lower[:] = [-10, -10, -10]
        upper = libUnfitPython.std_vector_double()
        upper[:] = [10, 10, 10]

        self.de = libUnfitPython.DifferentialEvolution()
        self.de.bounds.SetBounds(lower, upper)
    def setUp(self):
        '''
            Processes done at the start of every test in this class.
        '''
        lower = libUnfitPython.std_vector_double()
        lower[:] = [-10, -10, -10]
        upper = libUnfitPython.std_vector_double()
        upper[:] = [10, 10, 10]

        self.sa = libUnfitPython.SimulatedAnnealing()
        self.sa.bounds.SetBounds(lower, upper)
Example #7
0
    def setUp(self):
        '''
            Processes done at the start of every test in this class.
        '''
        lower = libUnfitPython.std_vector_double()
        lower[:] = [-10, -10, -10]
        upper = libUnfitPython.std_vector_double()
        upper[:] = [10, 10, 10]

        self.lm = libUnfitPython.LevenbergMarquardt()
        self.lm.bounds.SetBounds(lower, upper)
    def setUp(self):
        '''
            Processes done at the start of every test in this class.
        '''
        lower = libUnfitPython.std_vector_double()
        lower[:] = [-10, -10, -10]
        upper = libUnfitPython.std_vector_double()
        upper[:] = [10, 10, 10]

        self.nm = libUnfitPython.NelderMead()
        self.nm.bounds.SetBounds(lower, upper)
 def setUp(self):
     '''
         Processes done at the start of every test in this class.
     '''
     lower = libUnfitPython.std_vector_double()
     lower[:] = [-10, -10, -10]
     upper = libUnfitPython.std_vector_double()
     upper[:] = [10, 10, 10]
     
     self.ga = libUnfitPython.GeneticAlgorithm()
     self.ga.bounds.SetBounds(lower, upper)
Example #10
0
    def test_OnePointWithinBounds(self):
        some_bounds = libUnfitPython.Bounds()
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        lower[:] = [0.0, 0.0, 0.0]
        upper[:] = [1.0, 1.0, 1.0]
        some_bounds.SetBounds(lower, upper)

        self.assertTrue(some_bounds.IsWithinBounds(1, 0.5))
        self.assertFalse(some_bounds.IsWithinBounds(1, -1.0))
        self.assertFalse(some_bounds.IsWithinBounds(1, 2.0))
        self.assertFalse(some_bounds.IsWithinBounds(5, 0.5))
Example #11
0
    def test_SetUpperAndLower(self):
        some_bounds = libUnfitPython.Bounds(5)
        self.assertEqual(5, some_bounds.GetNumberOfBounds())

        # Set bounds and check they are set
        some_bounds.SetLowerBound(2, 0.0)
        some_bounds.SetUpperBound(3, 1.0)
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        some_bounds.GetBounds(lower, upper)
        self.assertAlmostEqual(0.0, lower[2], test_tol)
        self.assertAlmostEqual(max_positive_num, upper[2], test_tol)
        self.assertAlmostEqual(max_negative_num, lower[3], test_tol)
        self.assertAlmostEqual(1.0, upper[3], test_tol)
    def test_AppendExtend(self):
        vector1 = libUnfitPython.std_vector_double()
        vector1[:] = [1, 2, 3, 4, 5]
        vector2 = libUnfitPython.std_vector_double()
        vector2[:] = [1, 2, 3]

        vector2.append(4)
        test = [1, 2, 3, 4]
        for i, vi in enumerate(vector2):
            self.assertEqual(vi, test[i])
        vector2.extend(vector1[3:5])
        test = [1, 2, 3, 4, 4, 5]
        for i, vi in enumerate(vector2):
            self.assertEqual(vi, test[i])
Example #13
0
    def test_PointsWithinBounds(self):
        some_bounds = libUnfitPython.Bounds()
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        lower[:] = [0.0, 0.0, 0.0]
        upper[:] = [1.0, 1.0, 1.0]
        some_bounds.SetBounds(lower, upper)

        point = libUnfitPython.std_vector_double()
        point[:] = [0.5, 0.5, 0.5]
        self.assertTrue(some_bounds.IsWithinBounds(point))
        point[1] = -1.0
        self.assertFalse(some_bounds.IsWithinBounds(point))
        point[1] = 2.0
        self.assertFalse(some_bounds.IsWithinBounds(point))
Example #14
0
 def __call__(self, x):
     r = libUnfitPython.std_vector_double()
     r[:] = self.y[:]
     for i, ti in enumerate(self.t):
         model = x[0] + x[1] * ti + x[2] * ti * ti
         r[i] -= model
     return r
Example #15
0
 def test_PointsWithinBoundsEdgeCases(self):
     some_bounds = libUnfitPython.Bounds(3)
     point = libUnfitPython.std_vector_double()
     point[:] = [max_positive_num, max_negative_num, max_positive_num]
     self.assertTrue(some_bounds.IsWithinBounds(point))
     point[0] = point[0] * 10.0
     self.assertFalse(some_bounds.IsWithinBounds(point))
     point[0] = max_positive_num
     point[1] = point[1] * 10
     self.assertFalse(some_bounds.IsWithinBounds(point))
Example #16
0
    def test_SetAndReset(self):
        some_bounds = libUnfitPython.Bounds(5)
        self.assertEqual(5, some_bounds.GetNumberOfBounds())

        # Set bounds and check they are set
        some_bounds.SetBounds(2, 0.0, 1.0)
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        some_bounds.GetBounds(lower, upper)
        self.assertAlmostEqual(0.0, lower[2], test_tol)
        self.assertAlmostEqual(1.0, upper[2], test_tol)

        # Reset bounds and check they have been reset
        some_bounds.ResetBounds()
        some_bounds.GetBounds(lower, upper)
        for bnd in lower:
            self.assertAlmostEqual(max_negative_num, bnd, test_tol)
        for bnd in upper:
            self.assertAlmostEqual(max_positive_num, bnd, test_tol)
 def __call__(self, x):
     '''
         Method called by FindMin function. Modify the model if needed.
     '''
     r = libUnfitPython.std_vector_double()
     r[:] = self.y[:]
     for i, ti in enumerate(self.t):
         model = x[0] * math.exp(-x[1] * ti)
         r[i] -= model
     return r
    def test_Indexing(self):
        vector = libUnfitPython.std_vector_double()
        vector[:] = [1, 2, 3]

        #tests normal indexing
        self.assertEqual(1, vector[0])
        self.assertEqual(2, vector[1])
        self.assertEqual(3, vector[2])

        #tests python negative indexing
        self.assertEqual(3, vector[-1])
        self.assertEqual(2, vector[-2])
        self.assertEqual(1, vector[-3])
Example #19
0
    def test_BoundsConstructors(self):
        # Start with no bounds
        no_bounds = libUnfitPython.Bounds()
        self.assertEqual(0, no_bounds.GetNumberOfBounds())

        # Now set the number of bounds
        no_bounds.SetNumberOfBounds(5)
        lower = libUnfitPython.std_vector_double()
        upper = libUnfitPython.std_vector_double()
        no_bounds.GetBounds(lower, upper)
        for bnd in lower:
            self.assertAlmostEqual(max_negative_num, bnd, test_tol)
        for bnd in upper:
            self.assertAlmostEqual(max_positive_num, bnd, test_tol)

        # Start with some bounds
        some_bounds = libUnfitPython.Bounds(5)
        self.assertEqual(5, some_bounds.GetNumberOfBounds())
        some_bounds.GetBounds(lower, upper)
        for bnd in lower:
            self.assertAlmostEqual(max_negative_num, bnd, test_tol)
        for bnd in upper:
            self.assertAlmostEqual(max_positive_num, bnd, test_tol)
    def test_Slicing(self):
        vector1 = libUnfitPython.std_vector_double()
        vector1[:] = [1, 2, 3, 4, 5]

        vector2 = libUnfitPython.std_vector_double()
        vector2[:] = vector1[:3]
        test = [1, 2, 3]
        for i, vi in enumerate(vector2):
            self.assertEqual(vi, test[i])

        vector2[:] = [1, 2, 3, 4, 5, 6]
        vector2[1:5] = [3, 4, 5, 6]
        test = [1, 3, 4, 5, 6, 6]
        for i, vi in enumerate(vector2):
            self.assertEqual(vi, test[i])
        vector2[1:3] = [7, 8, 9, 10]
        test = [1, 7, 8, 9, 10, 5, 6, 6]
        for i, vi in enumerate(vector2):
            self.assertEqual(vi, test[i])
        vector2[2:6] = [11, 11]
        test = [1, 7, 11, 11, 6, 6]
        for i, vi in enumerate(vector2):
            self.assertEqual(vi, test[i])
class MyCostFunction(libUnfitPython.GenericCostFunction):
    t = libUnfitPython.std_vector_double()
    y = libUnfitPython.std_vector_double()

    def __init__(self, t_data=None, y_data=None):
        libUnfitPython.GenericCostFunction.__init__(self)

        if t_data != None and y_data != None:
            self.t[:] = t_data
            self.y[:] = y_data
        else:
            self.t[:] = []
            self.y[:] = []

    def __call__(self, x):
        '''
            Method called by FindMin function. Modify the model if needed.
        '''
        r = libUnfitPython.std_vector_double()
        r[:] = self.y[:]
        for i, ti in enumerate(self.t):
            model = x[0] * math.exp(-x[1] * ti)
            r[i] -= model
        return r
Example #22
0
    def test_ExponentialFileInput(self):
        cf = Exponential.ExponentialCostFunction()
        cf.FileInit('Values/TestExponential.txt')

        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0]

        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 1.58190784, cost_tol)

        self.lm.FindMin(cf, guess)

        self.assertAlmostEqual(guess[0], 0.99999682538, guess_tol)
        self.assertAlmostEqual(guess[1], 0.500017605642, guess_tol)
 def test_ExponentialDirectInput(self):
     cf = Exponential.ExponentialCostFunction([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                              [1.0, 0.6065, 0.3679, 0.2231, 0.1353, 0.0821, 0.0498, 0.0302, 0.0183, 0.0111, 0.0067])
     
     guess = libUnfitPython.std_vector_double()
     guess[:] = [0, 0]
               
     cost = 0
     for r in cf(guess):
         cost = cost + r * r
                           
     self.assertAlmostEqual(cost, 1.58190784, cost_tol)
                       
     self.ga.FindMin(cf, guess)
                           
     self.assertAlmostEqual(guess[0], 1.02684400144, guess_tol)
     self.assertAlmostEqual(guess[1], 0.517748487134, guess_tol)
    def test_ExponentialFileInput(self):
        cf = Exponential.ExponentialCostFunction()
        cf.FileInit('Values/TestExponential.txt')
    
        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0]
        
        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 1.58190784, cost_tol)
        
        self.ga.FindMin(cf, guess)
        
        self.assertAlmostEqual(guess[0], 1.02684400144, guess_tol)
        self.assertAlmostEqual(guess[1], 0.517748487134, guess_tol)
Example #25
0
    def test_LinearFileInput(self):
        cf = Linear.LinearCostFunction()
        cf.FileInit('Values/TestLinear.txt')

        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0]

        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 5365, cost_tol)

        self.lm.FindMin(cf, guess)

        self.assertAlmostEqual(guess[0], 4.99999998785, guess_tol)
        self.assertAlmostEqual(guess[1], 3.00000000175, guess_tol)
Example #26
0
    def test_LinearDirectInput(self):
        cf = Linear.LinearCostFunction([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                       [8, 11, 14, 17, 20, 23, 26, 29, 32, 35])

        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0]

        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 5365, cost_tol)

        self.lm.FindMin(cf, guess)

        self.assertAlmostEqual(guess[0], 4.99999998785, guess_tol)
        self.assertAlmostEqual(guess[1], 3.00000000175, guess_tol)
    def test_QuadraticFileInput(self):
        cf = Quadratic.QuadraticCostFunction()
        cf.FileInit('Values/TestQuadratic.txt')
    
        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0, 0]
    
        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 5188, cost_tol)
        
        self.ga.FindMin(cf, guess)
        
        self.assertAlmostEqual(guess[0], -4.06385903347, guess_tol)
        self.assertAlmostEqual(guess[1], -2.17175521045, guess_tol)
        self.assertAlmostEqual(guess[2], 0.754356468106, guess_tol)
    def test_QuadraticFileInput(self):
        cf = Quadratic.QuadraticCostFunction()
        cf.FileInit('Values/TestQuadratic.txt')

        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0, 0]

        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 5188, cost_tol)

        self.nm.FindMin(cf, guess)

        self.assertAlmostEqual(guess[0], 2.00000093872, guess_tol)
        self.assertAlmostEqual(guess[1], -5.00000040316, guess_tol)
        self.assertAlmostEqual(guess[2], 1.00000003577, guess_tol)
Example #29
0
    def test_QuadraticFileInput(self):
        cf = Quadratic.QuadraticCostFunction()
        cf.FileInit('Values/TestQuadratic.txt')

        guess = libUnfitPython.std_vector_double()
        guess[:] = [0, 0, 0]

        cost = 0
        for r in cf(guess):
            cost = cost + r * r

        self.assertAlmostEqual(cost, 5188, cost_tol)

        self.lm.FindMin(cf, guess)

        self.assertAlmostEqual(guess[0], 1.99999958222, guess_tol)
        self.assertAlmostEqual(guess[1], -4.99999983709, guess_tol)
        self.assertAlmostEqual(guess[2], 0.999999986896, guess_tol)
 def test_QuadraticDirectInput(self):
     cf = Quadratic.QuadraticCostFunction([1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                                          [-2, -4, -4, -2, 2, 8, 16, 26, 38, 52])
     
     guess = libUnfitPython.std_vector_double()
     guess[:] = [0, 0, 0]
               
     cost = 0
     for r in cf(guess):
         cost = cost + r * r
                           
     self.assertAlmostEqual(cost, 5188, cost_tol)
                           
     self.ga.FindMin(cf, guess)
                           
     self.assertAlmostEqual(guess[0], -4.06385903347, guess_tol)
     self.assertAlmostEqual(guess[1], -2.17175521045, guess_tol)
     self.assertAlmostEqual(guess[2], 0.754356468106, guess_tol)