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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4938 - (hide annotations)
Wed May 14 01:13:23 2014 UTC (5 years, 4 months ago) by jfenwick
File MIME type: text/x-python
File size: 6110 byte(s)
Modify unit tests to read their classes from
esys.escriptcore.utestselect

Change the line in that file to switch between unittest and unittest2


1 caltinay 3950
2 jfenwick 3981 ##############################################################################
3 caltinay 3950 #
4 jfenwick 4657 # Copyright (c) 2012-2014 by University of Queensland
5 jfenwick 3981 # http://www.uq.edu.au
6 caltinay 3950 #
7     # Primary Business: Queensland, Australia
8     # Licensed under the Open Software License version 3.0
9     # http://www.opensource.org/licenses/osl-3.0.php
10     #
11 jfenwick 3981 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 jfenwick 4657 # Development 2012-2013 by School of Earth Sciences
13     # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 jfenwick 3981 #
15     ##############################################################################
16 caltinay 3950
17 jfenwick 4657 __copyright__="""Copyright (c) 2012-2014 by University of Queensland
18 jfenwick 3981 http://www.uq.edu.au
19 caltinay 3950 Primary Business: Queensland, Australia"""
20     __license__="""Licensed under the Open Software License version 3.0
21     http://www.opensource.org/licenses/osl-3.0.php"""
22     __url__="https://launchpad.net/escript-finley"
23    
24     import logging
25     import numpy as np
26 jfenwick 4938 import esys.escriptcore.utestselect as unittest
27 caltinay 3950 import sys
28     from esys.downunder.minimizers import *
29     from esys.downunder.costfunctions import CostFunction
30    
31     # number of dimensions for the test function
32     N=10
33    
34     # this is mainly to avoid warning messages
35 caltinay 4213 logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
36 caltinay 3950
37     # Rosenbrock test function to be minimized. The minimum is 0 and lies at
38     # [1,1,...,1].
39 caltinay 4105 class RosenFunc(CostFunction):
40 gross 4074 def __init__(self):
41 caltinay 4105 super(RosenFunc, self).__init__()
42 gross 4074 def getDualProduct(self, f0, f1):
43 caltinay 3950 return np.dot(f0, f1)
44 caltinay 4105 def getNorm(self,x):
45     return (abs(x.max()))
46 gross 4074 def getGradient(self, x, *args):
47 caltinay 3950 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 caltinay 4105 def getValue(self, x, *args):
56     return np.sum(100.0*(x[1:]-x[:-1]**2.)**2. + (1-x[:-1])**2.)
57 caltinay 3950
58     class TestMinimizerLBFGS(unittest.TestCase):
59     def setUp(self):
60     self.f=RosenFunc()
61     self.minimizer=MinimizerLBFGS(self.f)
62     self.x0=np.array([2.]*N)
63     self.xstar=np.array([1.]*N)
64    
65     def test_max_iterations(self):
66     self.minimizer.setTolerance(1e-10)
67     self.minimizer.setMaxIterations(1)
68 gross 4121 self.assertRaises(MinimizerMaxIterReached, self.minimizer.run,self.x0)
69 caltinay 3950
70     def test_solution(self):
71 caltinay 4105 self.minimizer.setTolerance(1e-8)
72 caltinay 3950 self.minimizer.setMaxIterations(100)
73 caltinay 4273 x=self.minimizer.run(self.x0)
74     xx=self.minimizer.getResult()
75     self.assertEqual(np.amax(abs(x-xx)), 0.)
76 caltinay 3950 # We should be able to get a solution in under 100 iterations
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 gross 4121 self.minimizer.setTolerance(1e-8)
85 caltinay 4128 self.minimizer.setMaxIterations(10)
86     try:
87 caltinay 4273 x=self.minimizer.run(self.x0)
88 caltinay 4128 except MinimizerMaxIterReached:
89     pass
90 caltinay 3950 # callback should be called once for each iteration (including 0th)
91 caltinay 4128 self.assertEqual(n[0], 11)
92 caltinay 3950
93     class TestMinimizerBFGS(unittest.TestCase):
94     def setUp(self):
95     self.f=RosenFunc()
96     self.minimizer=MinimizerBFGS(self.f)
97     self.x0=np.array([2.]*N)
98     self.xstar=np.array([1.]*N)
99    
100     def test_max_iterations(self):
101     self.minimizer.setTolerance(1e-10)
102     self.minimizer.setMaxIterations(1)
103 caltinay 4273 self.assertRaises(MinimizerMaxIterReached, self.minimizer.run, self.x0)
104 caltinay 3950
105     def test_solution(self):
106     self.minimizer.setTolerance(1e-6)
107     self.minimizer.setMaxIterations(100)
108     self.minimizer.setOptions(initialHessian=1e-3)
109 caltinay 4273 x=self.minimizer.run(self.x0)
110     xx=self.minimizer.getResult()
111     self.assertEqual(np.amax(abs(x-xx)), 0.)
112 caltinay 3950 # We should be able to get a solution in under 100 iterations
113     self.assertAlmostEqual(np.amax(abs(x-self.xstar)), 0.)
114    
115     def test_callback(self):
116     n=[0]
117     def callback(k, x, fg, gf):
118     n[0]=n[0]+1
119     self.minimizer.setCallback(callback)
120     self.minimizer.setTolerance(1e-10)
121     self.minimizer.setMaxIterations(10)
122 caltinay 4273 try:
123     x=self.minimizer.run(self.x0)
124     except MinimizerMaxIterReached:
125     pass
126 caltinay 3950 # callback should be called once for each iteration (including 0th)
127     self.assertEqual(n[0], 11)
128    
129     class TestMinimizerNLCG(unittest.TestCase):
130     def setUp(self):
131     self.f=RosenFunc()
132     self.minimizer=MinimizerNLCG(self.f)
133     self.x0=np.array([2.]*N)
134     self.xstar=np.array([1.]*N)
135    
136     def test_max_iterations(self):
137     self.minimizer.setTolerance(1e-10)
138     self.minimizer.setMaxIterations(1)
139 caltinay 4273 self.assertRaises(MinimizerMaxIterReached, self.minimizer.run, self.x0)
140 caltinay 3950
141     def test_solution(self):
142 caltinay 3952 self.minimizer.setTolerance(1e-4)
143     self.minimizer.setMaxIterations(400)
144 caltinay 4273 x=self.minimizer.run(self.x0)
145     xx=self.minimizer.getResult()
146     self.assertEqual(np.amax(abs(x-xx)), 0.)
147 caltinay 3952 # We should be able to get a solution to set tolerance in #iterations
148 caltinay 3950 self.assertAlmostEqual(np.amax(abs(x-self.xstar)), 0., places=3)
149    
150     def test_callback(self):
151     n=[0]
152     def callback(k, x, fg, gf):
153     n[0]=n[0]+1
154     self.minimizer.setCallback(callback)
155     self.minimizer.setTolerance(1e-10)
156     self.minimizer.setMaxIterations(10)
157 caltinay 4273 try:
158     x=self.minimizer.run(self.x0)
159     except MinimizerMaxIterReached:
160     pass
161 caltinay 3950 # callback should be called once for each iteration (including 0th)
162     self.assertEqual(n[0], 11)
163    
164    
165     if __name__ == "__main__":
166     suite = unittest.TestSuite()
167     suite.addTest(unittest.makeSuite(TestMinimizerLBFGS))
168     suite.addTest(unittest.makeSuite(TestMinimizerBFGS))
169     suite.addTest(unittest.makeSuite(TestMinimizerNLCG))
170     s=unittest.TextTestRunner(verbosity=2).run(suite)
171     if not s.wasSuccessful(): sys.exit(1)
172    

  ViewVC Help
Powered by ViewVC 1.1.26