-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosenblock.py
303 lines (234 loc) · 6.31 KB
/
rosenblock.py
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import numpy as np
import matplotlib.pyplot as plt
import math
import time
def f(x, n):
f = 0
for i in range(1, n):
f = f + 100*(x[i]-x[i-1]**2)**2 + (1-x[i])**2
return f
def grad(x, n):
g = []
g.append(-400 * x[0] * (x[1]-x[0]**2))
for i in range(1, n-1):
g.append(200*(x[i]-x[i-1]**2) - 2*(1-x[i]) - 400 * x[i] * (x[i+1]-x[i]**2))
g.append(200*(x[n-1]-x[n-2]**2) - 2*(1-x[n-1]))
g = np.array(g, dtype=float)
return g
# class for steepest descent methods
class SD:
def __init__(self, n, epsilon, alpha, rho):
self.n = n
self.epsilon = epsilon
self.alpha_0 = alpha
self.rho = rho
self.f_val = []
self.itr = []
def f(self, x):
return f(x, self.n)
def grad(self, x):
return grad(x, self.n)
def backtracking(self, x, d):
alpha = self.alpha_0
while self.f(x + alpha*d) > (self.f(x) - 0.5*(np.linalg.norm(d, 2)**2)*alpha):
alpha = self.rho * alpha
return alpha
def update(self, x_k):
k = 0
self.itr.append(k)
self.f_val.append(self.f(x_k)[0])
while np.linalg.norm(self.grad(x_k), 2) >= self.epsilon:
g_k = self.grad(x_k)
d_k = - g_k
alpha_k = self.backtracking(x_k, d_k)
x_next = x_k + alpha_k*d_k
k += 1
self.itr.append(k)
self.f_val.append(self.f(x_next)[0])
x_k = x_next
if k == 1000:
break
# class for accelerated steepest descent methods
class ASD:
def __init__(self, n, epsilon, alpha, rho, tau):
self.n = n
self.epsilon = epsilon
self.alpha_0 = alpha
self.rho = rho
self.tau = []
self.tau.append(tau)
self.f_val = []
self.itr = []
def f(self, x):
return f(x, self.n)
def grad(self, x):
return grad(x, self.n)
def backtracking(self, y, d):
alpha = self.alpha_0
while self.f(y + alpha*d) > (self.f(y) - 0.5*(np.linalg.norm(d, 2)**2)*alpha):
alpha = self.rho * alpha
return alpha
def update(self, x_k):
y_k = x_k
k = 0
self.itr.append(k)
self.f_val.append(self.f(x_k)[0])
while np.linalg.norm(self.grad(x_k), 2) >= self.epsilon:
g_k = self.grad(x_k)
d_k = - g_k
alpha_k = self.backtracking(y_k, d_k)
x_next = y_k + alpha_k*d_k
if self.f(x_next) <= self.f(x_k):
tau_next = 1/2 * (1 + math.sqrt(1 + 4*self.tau[k]**2))
self.tau.append(tau_next)
y_next = x_next + ((self.tau[k]-1)/self.tau[k+1]) * (x_next-x_k)
else:
self.tau.append(1)
x_next = x_k
y_next = x_k
k += 1
self.itr.append(k)
self.f_val.append(self.f(x_next)[0])
x_k = x_next
y_k = y_next
if k == 1000:
break
# class for non-linear conjugate gradient methods
class NonlinearCG:
def __init__(self, n, epsilon, alpha, rho, omega):
self.n = n
self.epsilon = epsilon
self.alpha_0 = alpha
self.rho = rho
self.omega = omega
self.f_val = []
self.itr = []
def f(self, x):
return f(x, self.n)
def grad(self, x):
return grad(x, self.n)
def backtracking(self, x, p, g):
alpha = self.alpha_0
while self.f(x + alpha*p) > (self.f(x) + self.omega*p.T@g*alpha):
alpha = self.rho * alpha
return alpha
def dy_beta(self, gp, y, p):
return np.linalg.norm(gp,2)**2 / (p.T@y)
def update(self, x_k):
x_0 = x_k
k = 0
x_k = x_0
self.itr.append(k)
self.f_val.append(self.f(x_k)[0])
g_k = self.grad(x_k)
p_k = - g_k
while np.linalg.norm(self.grad(x_k), 2) >= self.epsilon:
g_k = self.grad(x_k)
alpha_k = self.backtracking(x_k, p_k, g_k)
x_next = x_k + alpha_k*p_k
g_next = self.grad(x_next)
y_k = g_next - g_k
p_next = - g_next + self.dy_beta(g_next, y_k, p_k)*p_k
k += 1
self.f_val.append(self.f(x_next)[0])
self.itr.append(k)
x_k = x_next
p_k = p_next
if k == 1000:
break
# class for quasi-Newton methods
class QuasiNewton:
def __init__(self, n, epsilon, alpha, rho, omega):
self.n = n
self.epsilon = epsilon
self.alpha_0 = alpha
self.rho = rho
self.omega = omega
self.f_val = []
self.itr = []
def f(self, x):
return f(x, self.n)
def grad(self, x):
return grad(x, self.n)
def backtracking(self, x, d, g):
alpha = self.alpha_0
while self.f(x + alpha*d) > (self.f(x) + self.omega*d.T@g*alpha):
alpha = self.rho * alpha
return alpha
def bfgs(self, H, s, y):
return -([email protected]@H + H@[email protected])/(s.T@y) + (1+(y.T@H@y)/(y.T@s))*(([email protected])/(s.T@y))
def update(self, x_k, H_k):
x_0 = x_k
H_0 = H_k
k = 0
x_k = x_0
H_k = H_0
self.itr.append(k)
self.f_val.append(self.f(x_k)[0])
g_k = self.grad(x_k)
while np.linalg.norm(self.grad(x_k), 2) >= self.epsilon:
g_k = self.grad(x_k)
d_k = - H_k@g_k
alpha_k = self.backtracking(x_k, d_k, g_k)
x_next = x_k + alpha_k*d_k
g_next = self.grad(x_next)
s_k = x_next - x_k
y_k = g_next - g_k
H_next = H_k + self.bfgs(H_k, s_k, y_k)
k += 1
self.f_val.append(self.f(x_next)[0])
self.itr.append(k)
x_k = x_next
H_k = H_next
if k == 1000:
break
def main():
n = 10
epsilon = pow(10,-4)
alpha = 1
rho = 0.9
tau = 1
omega = pow(10,-4)
np.random.seed(seed=10)
x_0 = np.random.rand(n,1)
H_0 = np.identity(n)
start = time.time()
sd = SD(n, epsilon, alpha, rho)
sd.update(x_0)
elapsed_time = time.time() - start
print ("sd_elapsed_time:{0}".format(elapsed_time) + "[sec]")
print("sd iteration:", sd.itr[-1])
print("-----")
start = time.time()
asd = ASD(n, epsilon, alpha, rho, tau)
asd.update(x_0)
elapsed_time = time.time() - start
print ("asd_elapsed_time:{0}".format(elapsed_time) + "[sec]")
print("asd iteration:", asd.itr[-1])
print("-----")
start = time.time()
nonlinear_cg = NonlinearCG(n, epsilon, alpha, rho, omega)
nonlinear_cg.update(x_0)
elapsed_time = time.time() - start
print ("nonlinear_elapsed_time:{0}".format(elapsed_time) + "[sec]")
print("nonlinear_cg iteration:", nonlinear_cg.itr[-1])
print("-----")
start = time.time()
quasi_newton = QuasiNewton(n, epsilon, alpha, rho, omega)
quasi_newton.update(x_0, H_0)
elapsed_time = time.time() - start
print ("quasi_elapsed_time:{0}".format(elapsed_time) + "[sec]")
print("quasi_newton iteration:", quasi_newton.itr[-1])
plt.plot(sd.itr, sd.f_val, "b", label=" SD")
plt.plot(asd.itr, asd.f_val, "g", label=" ASD")
plt.plot(nonlinear_cg.itr, nonlinear_cg.f_val, "y", label="NCG-DY")
plt.plot(quasi_newton.itr, quasi_newton.f_val, "r", label="QNWT-BFGS")
plt.ylabel("Function value")
plt.xlabel("Iteration")
plt.legend()
ax = plt.gca()
ax.set_yscale('log')
plt.show()
# plt.savefig('graph.png')
if __name__ == '__main__':
main()