~ubuntu-branches/ubuntu/raring/python-scipy/raring-proposed

« back to all changes in this revision

Viewing changes to Lib/optimize/tests/test_optimize.orig

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-07 14:12:12 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070107141212-mm0ebkh5b37hcpzn
* Remove build dependency on python-numpy-dev.
* python-scipy: Depend on python-numpy instead of python-numpy-dev.
* Package builds on other archs than i386. Closes: #402783.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
""" Unit tests for optimization routines
 
2
Author: Ed Schofield
 
3
Nov 2005
 
4
"""
 
5
 
 
6
from numpy.testing import *
 
7
 
 
8
set_package_path()
 
9
from scipy import optimize
 
10
from numpy import array, zeros, float64, dot, log, exp
 
11
restore_path()
 
12
 
 
13
 
 
14
class test_optimize(ScipyTestCase):
 
15
    """ Test case for a simple constrained entropy maximization problem
 
16
    (the machine translation example of Berger et al in
 
17
    Computational Linguistics, vol 22, num 1, pp 39--72, 1996.)
 
18
    """
 
19
    def setUp(self):
 
20
        self.F = array([[1,1,1],[1,1,0],[1,0,1],[1,0,0],[1,0,0]])
 
21
        self.K = array([1., 0.3, 0.5])
 
22
        self.startparams = zeros(3, float64)
 
23
        self.solution = array([0., -0.524869316, 0.487525860])
 
24
        self.maxiter = 1000
 
25
        self.funccalls = 0
 
26
 
 
27
 
 
28
    def func(self, x):
 
29
        self.funccalls += 1
 
30
        if self.funccalls > 6000:
 
31
            raise RuntimeError, "too many iterations in optimization routine"
 
32
        log_pdot = dot(self.F, x)
 
33
        logZ = log(sum(exp(log_pdot)))
 
34
        f = logZ - dot(self.K, x)
 
35
        return f
 
36
 
 
37
 
 
38
    def grad(self, x):
 
39
        log_pdot = dot(self.F, x)
 
40
        logZ = log(sum(exp(log_pdot)))
 
41
        p = exp(log_pdot - logZ)
 
42
        return dot(self.F.transpose(), p) - self.K
 
43
 
 
44
 
 
45
    def check_cg(self):
 
46
        """ conjugate gradient optimization routine
 
47
        """
 
48
        retval = optimize.fmin_cg(self.func, self.startparams, self.grad, (), \
 
49
                                  maxiter=self.maxiter, \
 
50
                                  full_output=True, disp=False, retall=False)
 
51
 
 
52
        (params, fopt, func_calls, grad_calls, warnflag) = retval
 
53
 
 
54
        err = abs(self.func(params) - self.func(self.solution))
 
55
        #print "CG: Difference is: " + str(err)
 
56
        assert err < 1e-6
 
57
 
 
58
 
 
59
    def check_bfgs(self):
 
60
        """ Broyden-Fletcher-Goldfarb-Shanno optimization routine
 
61
        """
 
62
        retval = optimize.fmin_bfgs(self.func, self.startparams, self.grad, \
 
63
                                    args=(), maxiter=self.maxiter, \
 
64
                                    full_output=True, disp=False, retall=False)
 
65
 
 
66
        (params, fopt, gopt, Hopt, func_calls, grad_calls, warnflag) = retval
 
67
 
 
68
        err = abs(self.func(params) - self.func(self.solution))
 
69
        #print "BFGS: Difference is: " + str(err)
 
70
        assert err < 1e-6
 
71
 
 
72
 
 
73
    def check_powell(self):
 
74
        """ Powell (direction set) optimization routine
 
75
        """
 
76
        retval = optimize.fmin_powell(self.func, self.startparams, \
 
77
                                    args=(), maxiter=self.maxiter, \
 
78
                                    full_output=True, disp=False, retall=False)
 
79
 
 
80
        (params, fopt, direc, numiter, func_calls, warnflag) = retval
 
81
 
 
82
        err = abs(self.func(params) - self.func(self.solution))
 
83
        #print "Powell: Difference is: " + str(err)
 
84
        assert err < 1e-6
 
85
 
 
86
    def check_neldermead(self):
 
87
        """ Nelder-Mead simplex algorithm
 
88
        """
 
89
        retval = optimize.fmin(self.func, self.startparams, \
 
90
                                    args=(), maxiter=self.maxiter, \
 
91
                                    full_output=True, disp=False, retall=False)
 
92
 
 
93
        (params, fopt, numiter, func_calls, warnflag) = retval
 
94
 
 
95
        err = abs(self.func(params) - self.func(self.solution))
 
96
        #print "Nelder-Mead: Difference is: " + str(err)
 
97
        assert err < 1e-6
 
98
 
 
99
    def check_ncg(self):
 
100
        """ line-search Newton conjugate gradient optimization routine
 
101
        """
 
102
        retval = optimize.fmin_ncg(self.func, self.startparams, self.grad,\
 
103
                                    args=(), maxiter=self.maxiter, \
 
104
                                    full_output=False, disp=False, retall=False)
 
105
 
 
106
        params = retval
 
107
 
 
108
        err = abs(self.func(params) - self.func(self.solution))
 
109
        #print "NCG: Difference is: " + str(err)
 
110
        assert err < 1e-6
 
111
 
 
112
 
 
113
    def check_l_bfgs_b(self):
 
114
        """ limited-memory bound-constrained BFGS algorithm
 
115
        """
 
116
        retval = optimize.fmin_l_bfgs_b(self.func, self.startparams, self.grad,\
 
117
                                    args=(), maxfun=self.maxiter)
 
118
 
 
119
        (params, fopt, d) = retval
 
120
 
 
121
        err = abs(self.func(params) - self.func(self.solution))
 
122
        #print "LBFGSB: Difference is: " + str(err)
 
123
        assert err < 1e-6
 
124
 
 
125
 
 
126
if __name__ == "__main__":
 
127
    ScipyTest().run()