1 from Numeric import copy
2
3
6 """Generic code for iterative minimisers.
7
8
9 Function options
10 ~~~~~~~~~~~~~~~~
11
12 func - The function to minimise.
13 dfunc - The function which returns the gradient vector.
14 d2func - The function which returns the hessian matrix or approximation.
15
16 f_args - The tuple of arguments to supply to the function func.
17 df_args - The tuple of arguments to supply to the function dfunc.
18 d2f_args - The tuple of arguments to supply to the function d2func.
19
20 xk - The parameter vector which on input is the initial values, x0.
21 fk - The function value which on input corresponds to x0.
22 dfk - The gradient vector which on input corresponds to x0.
23 d2fk - The hessian matrix or approximation which on input corresponds to x0.
24
25 xk_new - The parameter vector for the next iteration which on input can be anything.
26 fk_new - The function value for the next iteration which on input can be anything.
27 dfk_new - The gradient vector for the next iteration which on input can be anything.
28 d2fk_new - The hessian matrix for the next iteration which on input can be anything.
29
30 func_tol - The cutoff value used to terminate minimisation by comparison to the difference in function values between iterations.
31 maxiter - The maximum number of iterations.
32 print_flag - A flag specifying how much information should be printed to standard output during minimisation:
33
34 The print flag corresponds to:
35 0 - No output.
36 1 - Minimal output.
37 2 - Full output.
38
39
40 Returned objects
41 ~~~~~~~~~~~~~~~~
42
43 The minimised parameter vector, function value at the minimum, number of iterations, and a warning flag are returned.
44 The warning flag corresponds to:
45 0 - Minimisation terminated successfully.
46 1 - Maximum number of iterations have been reached.
47 """
48
49
51 """Generic code for iterative minimisers.
52
53 """
54
55
56 self.k = 1
57
58
59 if self.print_flag:
60 print "%-6s%-8i%-12s%-65s%-16s%-20s" % ("Step:", self.k, "Min params:", `self.xk`, "Function value:", `self.fk`)
61 self.k2 = 1
62
63
64 while 1:
65 if self.print_flag == 2:
66 print "\n\n<<<Main iteration k=" + `self.k` + " >>>"
67
68
69 if self.print_flag:
70 if self.print_flag == 2:
71 print "%-6s%-8i%-12s%-65s%-16s%-20s" % ("Step:", self.k, "Min params:", `self.xk`, "Function value:", `self.fk`)
72 else:
73 if self.k2 == 101:
74 print "%-6s%-8i%-12s%-65s%-16s%-20s" % ("Step:", self.k, "Min params:", `self.xk`, "Function value:", `self.fk`)
75 self.k2 = 1
76
77
78 self.new_param_func()
79
80
81 try:
82 self.backup_current_data()
83 except AttributeError:
84 "No need to backup the current data."
85
86
87 try:
88 self.update_data()
89 except AttributeError:
90 "No need to update the current data."
91
92
93 if self.k >= self.maxiter:
94 self.warning = "Maximum number of iterations reached"
95 break
96
97
98 if self.tests():
99 break
100
101
102 self.k = self.k + 1
103
104
105 if self.print_flag:
106 self.k2 = self.k2 + 1
107
108 if self.full_output:
109 return self.xk, self.fk, self.k, self.f_count, self.g_count, self.h_count, self.warning
110 else:
111 return self.xk
112
113
115 "Test for the local minimum."
116
117 if abs(self.fk_last - self.fk) <= self.func_tol:
118 return 1
119