import math import numpy as np from numpy linalg import inv def rosenb

  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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import math
import numpy as np
from numpy.linalg import inv
def rosenbrock(*a):
return 70 * (a[0]**2 - a[1])**2 + 5 * (a[0] - 1)**2 + 70 * (a[1]**2 - a[2])**2 + 5 * (a[1] - 1)**2 + 30
def derX1(x):
return 280 * (x[0]**2 - x[1]) * x[0] + 10 * (x[1] - 1)
def derX2(x):
return -140 * (x[0]**2 - x[1]) + 280 * (x[1]**2 - x[2]) * x[1] + 10 * (x[1] - 1)
def derX3(x):
return -140 * (x[1]**2 - x[2])
def derX1X1(x):
return 840 * x[0] ** 2 - 280 * x[1]
def derX1X2(x):
return -280 * x[0] + 10
def derX1X3(x):
return 0
def derX2X2(x):
return 140 + 840 * x[1] - 280 * x[2] + 10
def derX2X3(x):
return -280 * x[1]
def derX3X3(x):
return 140
def H(x):
return np.array([[derX1X1(x), derX1X2(x), derX1X3(x)],
[derX1X2(x), derX2X2(x), derX2X3(x)],
[derX1X3(x), derX2X3(x), derX3X3(x)]
])
def grad(x):
return np.array([derX1(x), derX2(x), derX3(x)])
def norma(x):
return math.sqrt(x[0]**2 + x[1]**2 + x[2]**2)
def scalMul(x1, x2):
return (x1 * x2).sum()
def fffq(l0):
a = [1., 1.]
while a[-1] < l0:
a.append(a[-1] + a[-2])
a.append(a[-1] + a[-2])
return a
def fibchiq(a, b, epsilon, q):
fib = fffq(math.fabs(b - a)/epsilon)
n = len(fib) - 1
midL = a + fib[n - 2] * (b - a) / fib[n]
midR = a + fib[n - 1] * (b - a) / fib[n];
while n != 2:
if q(midL) <= q(midR):
b = midR
midR = midL
midL = a + fib[n - 2] * (b - a) / fib[n]
else:
a = midL
midL = midR
midR = a + fib[n - 1] * (b - a) / fib[n]
n -= 1
return (a + b)/2
def golden(a, b, e, q):
A = a
D = b
B = A + (3 - math.sqrt(5)) * (D - A) / 2
C = A + (math.sqrt(5) - 1) * (D - A) / 2
while 1:
if D - A <= 2 * e:
break
if q(B) <= q(C):
D = C
else:
A = B
B = A + (3 - math.sqrt(5)) * (D - A) / 2
C = A + (math.sqrt(5) - 1) * (D - A) / 2
return A + (D - A) / 2
def bibi(a, b, e, q):
x1 = a
x2 = b
xmid = (x2 + x1) / 2.
i = 2
while 1:
if ((x2 - x1) <= 2 * e):
break;
if q(xmid - (x2 - x1) / 2. ** i) <= q(xmid + (x2 - x1) / 2. ** i) :
x2 = xmid
xmid = (x2 + x1) / 2.
else:
x1 = xmid
xmid = (x2 + x1) / 2.
i += 1
return (x2 + x1) / 2.
#алгоритм Флетчера-Ривза
def FletcherReeves(x0, e1, e2, sigma, M, func):
dk_1 = -grad(x0)
dk = 0
xk_1 = x0
xk = 0
xk1 = 0
wk_1 = 0
k = 0
def q(a):
return rosenbrock(xk_1[0] + a * dk_1[0], xk_1[1] + a * dk_1[1], xk_1[2] + a * dk_1[2])
alphaK = func(-1000, 1000, e1 / 10000, q)
xk = xk_1 + alphaK * dk_1
while 1:
#print xk_1, xk, xk1
if norma(grad(xk)) <= e1:
return xk, rosenbrock(xk[0], xk[1], xk[2])
if k >= M:
return xk, rosenbrock(xk[0], xk[1], xk[2])
if norma(xk_1) == 0:
wk_1 = 0
else:
wk_1 = norma(grad(xk)) ** 2 / norma(grad(xk_1)) ** 2
dk = -grad(xk) + wk_1 * dk_1
def q(a):
return rosenbrock(xk[0] + a * dk[0], xk[1] + a * dk[1], xk[2] + a * dk[2])
alphaK = func(-1000, 1000, e1 / 10000, q)
xk1 = xk + alphaK * dk
if norma(xk1 - xk) < sigma and math.fabs(rosenbrock(xk1[0], xk1[1], xk1[2]) - rosenbrock(xk[0], xk[1], xk[2])) < e2:
return xk, rosenbrock(xk[0], xk[1], xk[2])
k += 1
xk_1 = xk
xk = xk1
dk_1 = dk
xxx0 = np.array([0., 0., 0.])
print "Fletcher-Reeves:"
print FletcherReeves(xxx0, 0.1, 0.001, 0.001, 100, fibchiq)
%timeit FletcherReeves(xxx0, 0.1, 0.001, 0.001, 25, fibchiq)
#алгоритм Полака-Рибьеры
def PolakRivera(x0, e1, e2, sigma, M, func, n):
dk_1 = -grad(x0)
dk = 0
xk_1 = x0
xk = 0
xk1 = 0
wk_1 = 0
k = 0
def q(a):
return rosenbrock(xk_1[0] + a * dk_1[0], xk_1[1] + a * dk_1[1], xk_1[2] + a * dk_1[2])
alphaK = func(-1000, 1000, e1 / 10000, q)
xk = xk_1 + alphaK * dk_1
while 1:
#print xk_1, xk, xk1
if norma(grad(xk)) <= e1:
return xk, rosenbrock(xk[0], xk[1], xk[2])
if k >= M:
return xk, rosenbrock(xk[0], xk[1], xk[2])
if k % n == 0:
wk_1 = 0
else:
wk_1 = scalMul(grad(xk), grad(xk) - grad(xk_1))/ norma(grad(xk_1)) ** 2
#print xk_1, xk, xk1, wk_1, k
dk = -grad(xk) + wk_1 * dk_1
def q(a):
return rosenbrock(xk[0] + a * dk[0], xk[1] + a * dk[1], xk[2] + a * dk[2])
alphaK = func(-1000, 1000, e1 / 10000, q)
xk1 = xk + alphaK * dk
if norma(xk1 - xk) < sigma and math.fabs(rosenbrock(xk1[0], xk1[1], xk1[2]) - rosenbrock(xk[0], xk[1], xk[2])) < e2:
return xk, rosenbrock(xk[0], xk[1], xk[2])
k += 1
xk_1 = xk
xk = xk1
dk_1 = dk
xxx0 = np.array([0., 0., 0.])
print "Polak-Rivera:"
print PolakRivera(xxx0, 0.1, 0.001, 0.001, 25, fibchiq, 2)
%timeit PolakRivera(xxx0, 0.1, 0.001, 0.001, 25, fibchiq, 2)
#алгоритм Девидона-Флетчера-Пауэлла
def DFP(x0, e1, e2, sigma, M, func):
xk = x0
xk1 = 0
Gk = np.identity(3)
Gk1 = Gk
dk = -grad(xk)
k = 0
while 1:
if norma(grad(xk)) < e1 and k != 0:
return xk, rosenbrock(xk[0], xk[1], xk[2])
if k >= M:
return xk, rosenbrock(xk[0], xk[1], xk[2])
if k != 0:
dgk = grad(xk1) - grad(xk)
dxk = xk1 - xk
if dxk.sum() == 0 or dgk.sum() == 0:
s = (3, 3)
dGk = np.zeros(s)
else:
dGk = np.dot(np.transpose(dxk), dxk) / np.dot(dxk, dgk) \
- np.dot(np.dot(np.dot(Gk, np.transpose(dgk)), dgk), np.transpose(Gk)) \
/ np.dot(np.dot(dgk, Gk), np.transpose(dgk))
Gk1 = Gk + dGk
dk = - np.dot(Gk1, np.transpose(grad(xk))) # multiply doesn't work :()
def q(a):
return rosenbrock(xk[0] + a * dk[0], xk[1] + a * dk[1], xk[2] + a * dk[2])
alpha = func(-1000, 1000, e1 / 100, q)
xk1 = xk + alpha * dk
if norma(xk1 - xk) < sigma and math.fabs(rosenbrock(xk1[0], xk1[1], xk1[2]) - rosenbrock(xk[0], xk[1], xk[2])) < e2:
return xk1, rosenbrock(xk1[0], xk1[1], xk1[2])
k += 1
xk = xk1
xxx0 = np.array([0., 0., 0.])
print "DFP:"
print DFP(xxx0, 0.1, 0.001, 0.001, 80, fibchiq)
%timeit DFP(xxx0, 0.1, 0.001, 0.001, 80, fibchiq)
#алгоритм Левенберга-Марквардта
def LM(x0, e1, e2, sigma, M):
xk = x0
xk1 = 0
mk = 10 ** 4
dk = 0
k = 0
while 1:
if norma(grad(xk)) < e1:
return xk1, rosenbrock(xk1[0], xk1[1], xk1[2])
if k >= M:
return xk1, rosenbrock(xk1[0], xk1[1], xk1[2])
dk = - np.dot(inv(H(xk) + mk * np.identity(3)), grad(xk))
xk1 = xk + dk
if rosenbrock(xk1[0], xk1[1], xk1[2]) < rosenbrock(xk[0], xk[1], xk[2]):
mk = mk / 2
else:
mk = 2 * mk
k += 1
xk = xk1
xxx0 = np.array([0., 0., 0.])
print "LM:"
print LM(xxx0, 0.1, 0.001, 0.001, 80)
%timeit LM(xxx0, 0.1, 0.001, 0.001, 80)