/[escript]/trunk/downunder/test/python/run_minimizers.py
ViewVC logotype

Contents of /trunk/downunder/test/python/run_minimizers.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3950 - (show annotations)
Fri Aug 24 06:10:02 2012 UTC (6 years, 10 months ago) by caltinay
File MIME type: text/x-python
File size: 5903 byte(s)
More tests and corrections in the alternative minimizers.

1
2 ########################################################
3 #
4 # Copyright (c) 2012 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2012 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="https://launchpad.net/escript-finley"
21
22 import logging
23 import numpy as np
24 import unittest
25 import sys
26 from esys.downunder.minimizers import *
27 from esys.downunder.costfunctions import CostFunction
28
29 # number of dimensions for the test function
30 N=10
31
32 # this is mainly to avoid warning messages
33 logger=logging.getLogger('inv')
34 logger.setLevel(logging.INFO)
35 handler=logging.StreamHandler()
36 handler.setLevel(logging.INFO)
37 logger.addHandler(handler)
38
39 # Rosenbrock test function to be minimized. The minimum is 0 and lies at
40 # [1,1,...,1].
41 class RosenFunc(CostFunction):
42 def _getInner(self, f0, f1):
43 return np.dot(f0, f1)
44 def _getValue(self, x, *args):
45 return np.sum(100.0*(x[1:]-x[:-1]**2.)**2. + (1-x[:-1])**2.)
46 def _getGradient(self, x, *args):
47 xm = x[1:-1]
48 xm_m1 = x[:-2]
49 xm_p1 = x[2:]
50 der = np.zeros_like(x)
51 der[1:-1] = 200*(xm-xm_m1**2) - 400*(xm_p1 - xm**2)*xm - 2*(1-xm)
52 der[0] = -400*x[0]*(x[1]-x[0]**2) - 2*(1-x[0])
53 der[-1] = 200*(x[-1]-x[-2]**2)
54 return der
55
56 class TestMinimizerLBFGS(unittest.TestCase):
57 def setUp(self):
58 self.f=RosenFunc()
59 self.minimizer=MinimizerLBFGS(self.f)
60 self.x0=np.array([2.]*N)
61 self.xstar=np.array([1.]*N)
62
63 def test_max_iterations(self):
64 self.minimizer.setTolerance(1e-10)
65 self.minimizer.setMaxIterations(1)
66 reason=self.minimizer.run(self.x0)
67 self.assertEqual(reason, MinimizerLBFGS.MAX_ITERATIONS_REACHED)
68
69 def test_solution(self):
70 self.minimizer.setTolerance(1e-6)
71 self.minimizer.setMaxIterations(100)
72 self.minimizer.setOptions(initialHessian=1e-3)
73 reason=self.minimizer.run(self.x0)
74 x=self.minimizer.getResult()
75 # We should be able to get a solution in under 100 iterations
76 self.assertEqual(reason, MinimizerLBFGS.TOLERANCE_REACHED)
77 self.assertAlmostEqual(np.amax(abs(x-self.xstar)), 0.)
78
79 def test_callback(self):
80 n=[0]
81 def callback(k, x, fg, gf):
82 n[0]=n[0]+1
83 self.minimizer.setCallback(callback)
84 self.minimizer.setTolerance(1e-10)
85 self.minimizer.setMaxIterations(10)
86 reason=self.minimizer.run(self.x0)
87 # callback should be called once for each iteration (including 0th)
88 self.assertEqual(n[0], 11)
89
90 class TestMinimizerBFGS(unittest.TestCase):
91 def setUp(self):
92 self.f=RosenFunc()
93 self.minimizer=MinimizerBFGS(self.f)
94 self.x0=np.array([2.]*N)
95 self.xstar=np.array([1.]*N)
96
97 def test_max_iterations(self):
98 self.minimizer.setTolerance(1e-10)
99 self.minimizer.setMaxIterations(1)
100 reason=self.minimizer.run(self.x0)
101 self.assertEqual(reason, MinimizerBFGS.MAX_ITERATIONS_REACHED)
102
103 def test_solution(self):
104 self.minimizer.setTolerance(1e-6)
105 self.minimizer.setMaxIterations(100)
106 self.minimizer.setOptions(initialHessian=1e-3)
107 reason=self.minimizer.run(self.x0)
108 x=self.minimizer.getResult()
109 # We should be able to get a solution in under 100 iterations
110 self.assertEqual(reason, MinimizerBFGS.TOLERANCE_REACHED)
111 self.assertAlmostEqual(np.amax(abs(x-self.xstar)), 0.)
112
113 def test_callback(self):
114 n=[0]
115 def callback(k, x, fg, gf):
116 n[0]=n[0]+1
117 self.minimizer.setCallback(callback)
118 self.minimizer.setTolerance(1e-10)
119 self.minimizer.setMaxIterations(10)
120 reason=self.minimizer.run(self.x0)
121 # callback should be called once for each iteration (including 0th)
122 self.assertEqual(n[0], 11)
123
124 class TestMinimizerNLCG(unittest.TestCase):
125 def setUp(self):
126 self.f=RosenFunc()
127 self.minimizer=MinimizerNLCG(self.f)
128 self.x0=np.array([2.]*N)
129 self.xstar=np.array([1.]*N)
130
131 def test_max_iterations(self):
132 self.minimizer.setTolerance(1e-10)
133 self.minimizer.setMaxIterations(1)
134 reason=self.minimizer.run(self.x0)
135 self.assertEqual(reason, MinimizerNLCG.MAX_ITERATIONS_REACHED)
136
137 def test_solution(self):
138 self.minimizer.setTolerance(1e-3)
139 self.minimizer.setMaxIterations(300)
140 reason=self.minimizer.run(self.x0)
141 x=self.minimizer.getResult()
142 # We should be able to get a solution in under 300 iterations
143 self.assertEqual(reason, MinimizerNLCG.TOLERANCE_REACHED)
144 self.assertAlmostEqual(np.amax(abs(x-self.xstar)), 0., places=3)
145
146 def test_callback(self):
147 n=[0]
148 def callback(k, x, fg, gf):
149 n[0]=n[0]+1
150 self.minimizer.setCallback(callback)
151 self.minimizer.setTolerance(1e-10)
152 self.minimizer.setMaxIterations(10)
153 reason=self.minimizer.run(self.x0)
154 # callback should be called once for each iteration (including 0th)
155 self.assertEqual(n[0], 11)
156
157
158 if __name__ == "__main__":
159 suite = unittest.TestSuite()
160 suite.addTest(unittest.makeSuite(TestMinimizerLBFGS))
161 suite.addTest(unittest.makeSuite(TestMinimizerBFGS))
162 suite.addTest(unittest.makeSuite(TestMinimizerNLCG))
163 s=unittest.TextTestRunner(verbosity=2).run(suite)
164 if not s.wasSuccessful(): sys.exit(1)
165

  ViewVC Help
Powered by ViewVC 1.1.26