File: test_function_optimisation.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (77 lines) | stat: -rw-r--r-- 2,900 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
#!/usr/bin/env python
"""Tests for optimisation functions"""
from function_optimisation import great_deluge, ga_evolve, _simple_breed,\
    _simple_score, _simple_init, _simple_select
from cogent.util.unit_test import TestCase, main
from numpy import array

__author__ = "Daniel McDonald"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__contributors__ = ["Daniel McDonald"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Daniel McDonald"
__email__ = "mcdonadt@colorado.edu"
__status__ = "Production"

class OptimisationFunctionsTestCase(TestCase):
    """Tests for great_delue and ga_evolve"""
    def test_great_deluge(self):
        """great_deluge should return expected values from foo() obj"""
        class foo:
            def __init__(self, x): self.x = x
            def cost(self): return self.x
            def perturb(self): return self.__class__(self.x - 1)
        
        observed = [i for i in great_deluge(foo(5), max_total_iters=6)]

        self.assertEqual(observed[0][1].x, 4)
        self.assertEqual(observed[1][1].x, 3)
        self.assertEqual(observed[2][1].x, 2)
        self.assertEqual(observed[3][1].x, 1)
        self.assertEqual(observed[4][1].x, 0)
        self.assertEqual(observed[5][1].x, -1)

    def test_ga_evolve(self):
        """ga_evolve should return expected values when using overloaded funcs"""
        init_f = lambda x,y: [1,1,1]
        score_f = lambda x,y: 5
        breed_f = lambda w,x,y,z: [1,1,1]
        select_f = lambda x,y: 2

        expected = [(0, 2), (1, 2), (2, 2)]
        observed = [i for i in ga_evolve(1, 2, 3, 0.5, score_f, breed_f, \
                    select_f, init_f, None, 3)]
        self.assertEqual(observed, expected)        

class PrivateFunctionsTestCase(TestCase):
    """Tests of the private support functions for ga_evolve"""
    def test_simple_breed(self):
        """simple_breed should return expected values when with modded parent"""
        f = lambda: 0.5
        obj = lambda: 0
        obj.mutate = lambda: 1
        expected = [1,1,1,1,1]
        observed = _simple_breed([0, obj], 5, 1.0, f)
        self.assertEqual(observed, expected)
       
    def test_simple_score(self):
        """simple_score should return choosen value with overloaded obj"""
        bar = lambda: 5
        bar.score = lambda x: x
        self.assertEqual(_simple_score(bar,6), 6)

    def test_simple_init(self):
        """simple_init should return a simple list"""
        expected = [array([0]), array([0]), array([0])]
        self.assertEqual(_simple_init(array([0]), 3), expected)

    def test_simple_select(self):
        """simple_select should return our hand picked selection"""
        pop = ['a','b','c','d','e']
        scores = [5,3,8,6,1]
        best_expected = (1, 'e')
        self.assertEqual(_simple_select(pop, scores), best_expected)

if __name__ == '__main__':
    main()