/[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 4938 - (show 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
2 ##############################################################################
3 #
4 # Copyright (c) 2012-2014 by University of Queensland
5 # http://www.uq.edu.au
6 #
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 # Development until 2012 by Earth Systems Science Computational Center (ESSCC)
12 # Development 2012-2013 by School of Earth Sciences
13 # Development from 2014 by Centre for Geoscience Computing (GeoComp)
14 #
15 ##############################################################################
16
17 __copyright__="""Copyright (c) 2012-2014 by University of Queensland
18 http://www.uq.edu.au
19 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 import esys.escriptcore.utestselect as unittest
27 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 logging.basicConfig(format='%(name)s: %(message)s', level=logging.INFO)
36
37 # Rosenbrock test function to be minimized. The minimum is 0 and lies at
38 # [1,1,...,1].
39 class RosenFunc(CostFunction):
40 def __init__(self):
41 super(RosenFunc, self).__init__()
42 def getDualProduct(self, f0, f1):
43 return np.dot(f0, f1)
44 def getNorm(self,x):
45 return (abs(x.max()))
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 def getValue(self, x, *args):
56 return np.sum(100.0*(x[1:]-x[:-1]**2.)**2. + (1-x[:-1])**2.)
57
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 self.assertRaises(MinimizerMaxIterReached, self.minimizer.run,self.x0)
69
70 def test_solution(self):
71 self.minimizer.setTolerance(1e-8)
72 self.minimizer.setMaxIterations(100)
73 x=self.minimizer.run(self.x0)
74 xx=self.minimizer.getResult()
75 self.assertEqual(np.amax(abs(x-xx)), 0.)
76 # 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 self.minimizer.setTolerance(1e-8)
85 self.minimizer.setMaxIterations(10)
86 try:
87 x=self.minimizer.run(self.x0)
88 except MinimizerMaxIterReached:
89 pass
90 # callback should be called once for each iteration (including 0th)
91 self.assertEqual(n[0], 11)
92
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 self.assertRaises(MinimizerMaxIterReached, self.minimizer.run, self.x0)
104
105 def test_solution(self):
106 self.minimizer.setTolerance(1e-6)
107 self.minimizer.setMaxIterations(100)
108 self.minimizer.setOptions(initialHessian=1e-3)
109 x=self.minimizer.run(self.x0)
110 xx=self.minimizer.getResult()
111 self.assertEqual(np.amax(abs(x-xx)), 0.)
112 # 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 try:
123 x=self.minimizer.run(self.x0)
124 except MinimizerMaxIterReached:
125 pass
126 # 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 self.assertRaises(MinimizerMaxIterReached, self.minimizer.run, self.x0)
140
141 def test_solution(self):
142 self.minimizer.setTolerance(1e-4)
143 self.minimizer.setMaxIterations(400)
144 x=self.minimizer.run(self.x0)
145 xx=self.minimizer.getResult()
146 self.assertEqual(np.amax(abs(x-xx)), 0.)
147 # We should be able to get a solution to set tolerance in #iterations
148 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 try:
158 x=self.minimizer.run(self.x0)
159 except MinimizerMaxIterReached:
160 pass
161 # 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