1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 from Numeric import dot
25
26 from base_classes import Conjugate_gradient, Line_search, Min
27
28
29 -def polak_ribiere(func=None, dfunc=None, args=(), x0=None, min_options=None, func_tol=1e-25, grad_tol=None, maxiter=1e6, a0=1.0, mu=0.0001, eta=0.1, full_output=0, print_flag=0, print_prefix=""):
30 """Polak-Ribiere conjugate gradient algorithm.
31
32 Page 121-122 from 'Numerical Optimization' by Jorge Nocedal and Stephen J. Wright, 1999, 2nd ed.
33
34 The algorithm is:
35
36 Given x0
37 Evaluate f0 = f(x0), g0 = g(x0)
38 Set p0 = -g0, k = 0
39 while g0 != 0:
40 Compute ak and set xk+1 = xk + ak.pk
41 Evaluate gk+1
42 bk+1 = dot(gk+1, (gk+1 - gk)) / dot(gk, gk)
43 pk+1 = -gk+1 + bk+1.pk
44 k = k + 1
45 """
46
47 if print_flag:
48 if print_flag >= 2:
49 print print_prefix
50 print print_prefix
51 print print_prefix + "Polak-Ribiere conjugate gradient minimisation"
52 print print_prefix + "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
53 min = Polak_ribiere(func, dfunc, args, x0, min_options, func_tol, grad_tol, maxiter, a0, mu, eta, full_output, print_flag, print_prefix)
54 if min.init_failure:
55 print print_prefix + "Initialisation of minimisation has failed."
56 return None
57 results = min.minimise()
58 return results
59
60
62 - def __init__(self, func, dfunc, args, x0, min_options, func_tol, grad_tol, maxiter, a0, mu, eta, full_output, print_flag, print_prefix):
63 """Class for Polak-Ribiere conjugate gradient minimisation specific functions.
64
65 Unless you know what you are doing, you should call the function 'polak_ribiere' rather than
66 using this class.
67 """
68
69
70 self.func = func
71 self.dfunc = dfunc
72 self.args = args
73 self.xk = x0
74 self.func_tol = func_tol
75 self.grad_tol = grad_tol
76 self.maxiter = maxiter
77 self.full_output = full_output
78 self.print_flag = print_flag
79 self.print_prefix = print_prefix
80
81
82 self.a0 = a0
83
84
85 self.mu = mu
86 self.eta = eta
87
88
89 self.init_failure = 0
90
91
92 self.line_search_options(min_options)
93 self.setup_line_search()
94
95
96 self.f_count = 0
97 self.g_count = 0
98 self.h_count = 0
99
100
101 self.warning = None
102
103
104 self.setup_conv_tests()
105
106
107 self.fk, self.f_count = apply(self.func, (self.xk,)+self.args), self.f_count + 1
108 self.dfk, self.g_count = apply(self.dfunc, (self.xk,)+self.args), self.g_count + 1
109 self.pk = -self.dfk
110 self.dot_dfk = dot(self.dfk, self.dfk)
111
112
114 """Function to calcaluate the Polak-Ribiere beta value."""
115
116
117 return dot(self.dfk_new, self.dfk_new - self.dfk) / self.dot_dfk
118