File: test_optimisers.py

package info (click to toggle)
python-cogent 1.4.1-1.2
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze
  • size: 13,260 kB
  • ctags: 20,087
  • sloc: python: 116,163; ansic: 732; makefile: 74; sh: 9
file content (87 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python

import unittest, time, sys, os, numpy
from cogent.maths.optimisers import SimulatedAnnealing, Powell

__author__ = "Peter Maxwell and Gavin Huttley"
__copyright__ = "Copyright 2007-2009, The Cogent Project"
__credits__ = ["Peter Maxwell", "Gavin Huttley"]
__license__ = "GPL"
__version__ = "1.4.1"
__maintainer__ = "Gavin Huttley"
__email__ = "gavin.huttley@anu.edu.au"
__status__ = "Production"

class NullFile(object):
    def write(self, x):
        pass
        
def quiet(f, *args, **kw):
    orig = sys.stdout
    try:
        sys.stdout = NullFile()
        result = f(*args, **kw)
    finally:
        sys.stdout = orig
    return result
    
def easilyOptimised(direction):
    def testoptparvector(vector):
        if 0.0 <= vector[0] <= 10.0:
            return -direction * (vector[0]-5.0)**2 + 3
        else:
            return -direction * numpy.inf
    bounds = ([0.0], [10.0])
    return (testoptparvector, [1.0], bounds)
    
class OptimiserTestCase(unittest.TestCase):
    def _test_optimisation(self, klass, kw, kw2, accuracy):
        direction = +1
        (f,x,b) = easilyOptimised(direction)
        o = klass(f,x,b, seed=1, direction=direction, **kw)
        t0 = time.time()
        (f, x) = quiet(o.run, **kw2)
        t1 = time.time()
        
        if 'max_evaluations' in kw:
            evals = o.getEvaluationCount()
            #print (evals, kw)
            #self.assertEqual(kw['max_evaluations'], evals)
        #print evals, 'in', t1-t0, '=', evals/(t1-t0), '/sec'
        
        self.assert_(abs(f-3) < .1, (klass.__name__, kw, x, f))
        self.assert_(abs(x[0]-5) < accuracy, (klass.__name__, kw, x, f))
        
    def test_simanneal(self):
        self._test_optimisation(SimulatedAnnealing, {}, {}, 0.0001)
        
    def test_simanneal_limited(self):
        self._test_optimisation(SimulatedAnnealing,
                {'max_evaluations': 50}, {'show_progress':True}, 0.5)

    def test_powell(self):
        self._test_optimisation(Powell, {}, {}, 0.0001)

    def test_powell_limited(self):
        self._test_optimisation(Powell,
                {'max_evaluations': 10}, {'show_progress':True}, 0.1)
                
    def test_checkpointing(self):
        (f,x,b) = easilyOptimised(1)
        filename = 'checkpoint.tmp.pickle'
        if os.path.exists(filename):
            os.remove(filename)
        for proc in [0,1]:
            o = SimulatedAnnealing(f,x,b, seed=1, direction=1)
            o.setCheckpointing(filename)
            quiet(o.run)
        o = SimulatedAnnealing(f,x,b, seed=1, direction=1, init_temp=3.21)
        o.setCheckpointing(filename)
        self.assertRaises(Exception, quiet, o.run)
        if os.path.exists(filename):
            os.remove(filename)
        
        
        
if __name__ == '__main__':
        unittest.main()