Package boxmin :: Package tests :: Module test_amoeba
[hide private]

Source Code for Module boxmin.tests.test_amoeba

 1  #!/usr/bin/env python 
 2   
 3  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 4  #  test_amoeba.py 
 5  # 
 6  #  This program is in the public domain. 
 7  # 
 8  # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 9   
10  """Unit tests for amoeba fit""" 
11   
12  import unittest 
13  import boxmin 
14  from   boxmin.amoeba import amoeba 
15  import array 
16  import boxmin.tests.cfitfn_example  as cfitfn_example 
17   
18 -def quadratic(vec):
19 """Quadratic volume objective function""" 20 return vec[0]**2+vec[1]**2+vec[2]**2
21
22 -class TestUnbounded(unittest.TestCase):
23 """Unbounded Amoeba Fit"""
24 - def setUp(self):
25 self.problem = amoeba() 26 self.problem.setup(quadratic,[1,1,1])
27
28 - def test1(self):
29 """amoeba init unbounded""" 30 P = self.problem.work[0:4] 31 self.assertAlmostEqual(P[0],1,15) 32 self.assertAlmostEqual(P[1],1,15) 33 self.assertAlmostEqual(P[2],1,15)
34 35
36 - def test2(self):
37 """amoeba fit unbounded""" 38 [P,v, iters, fcalls] = self.problem.fit(1e-7); 39 self.assertAlmostEqual(P[0],0,7) 40 self.assertAlmostEqual(P[1],0,7) 41 self.assertAlmostEqual(P[2],0,7)
42 43
44 -class TestBounded(unittest.TestCase):
45 """Bounded Amoeba Fit"""
46 - def setUp(self):
47 self.problem = amoeba() 48 self.problem.setup(quadratic,[2,2,2],[-1,-1,-1,3,3,3])
49
50 - def test1(self):
51 """amoeba init bounded""" 52 P = self.problem.work[0:3] 53 self.assertAlmostEqual(P[0],2,15) 54 self.assertAlmostEqual(P[1],2,15) 55 self.assertAlmostEqual(P[2],2,15)
56
57 - def test2(self):
58 """amoeba fit bounded""" 59 [P,v, iters, fcalls] = self.problem.fit(1e-8) 60 self.assertAlmostEqual(P[0],0,7) 61 self.assertAlmostEqual(P[1],0,7) 62 self.assertAlmostEqual(P[2],0,7)
63 64 65
66 -class TestFitstep(unittest.TestCase):
67 """Bounded Amoeba Step-by-step fit"""
68 - def setUp(self):
69 self.problem = amoeba() 70 self.problem.setup(quadratic,[2,2,2],[1,1,1,3,3,3])
71
72 - def test1(self):
73 """amoeba fit bounded step-by-step""" 74 for it in range(1000): 75 [P,v,dv] = self.problem.step() 76 if dv < 1e-7: break 77 self.assertAlmostEqual(P[0],2,7) 78 self.assertAlmostEqual(P[1],2,7) 79 self.assertAlmostEqual(P[2],2,7)
80 81 82 83 84 85 86 87 if __name__ == "__main__": 88 unittest.main() 89