forked from MaterSim/ComputationalPhysics300
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestinggroundfinal.py
More file actions
476 lines (414 loc) · 11 KB
/
testinggroundfinal.py
File metadata and controls
476 lines (414 loc) · 11 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.optimize as optimize
import numdifftools as nd
import math
import timeit
N = 100
prec = .0001
# Limits
# -(3 char identifier) <= x,y <= +(3 char identifier)
# Title: given function
# Func ID: f1
# Limits: -2 <= x,y <= 2
# Expected returns:
# Min = f(0,0) = 0
# Limit
f10 = 2
# Expected values
f1E = [0.0,0.0,0.0]
# Title
f1N = 'Given'
# Benchmark/test functions
# given function for project
# -2 <= x,y <= 2
# f(x,y) = x**2/s + y**2/3 -xy/4
# Min = f(0,0) = 0
def f1(x):
x, y = x[0], x[1]
return x**2/2 + y**2/3 - x*y/4
# magnitude of a vector
def mag(x):
return np.sqrt(x[0]**2 + x[1]**2)
# variable stepwidth
def derivative2(f, x, d=0.001):
x, y = x[0], x[1]
fx = (f([x+d/2,y])-f([x-d/2,y]))/d
fy = (f([x,y+d/2])-f([x,y-d/2]))/d
return np.array([fx,fy])
# Defines a random starting point for the optimization function as given in lecture
def init(x_min, x_max, y_min, y_max):
x0 = x_min+np.random.random()*(x_max-x_min)
y0 = y_min+np.random.random()*(y_max-y_min)
#print('[x0,y0]:', [x0,y0])
return [x0, y0]
# if gk = 0, stop, else dk = -Hk*g(k)
def term_test(xk,f):
G = nd.Gradient(f)
if np.linalg.norm(G(xk)) <= prec:
#print('term_test: True', np.linalg.norm(G(xk)))
return True
else:
#print('term_test: False')
return False
# reshapes results appropriately
def array_fix(x):
x = [x[0,0],x[1,1]]
x = np.asarray(x,order = 1)
return x
# wrap the function to sent to timeit
# from https://www.pythoncentral.io/time-a-python-function/
def wrapper(func,f,x):
def wrapped():
return func(f,x)
return wrapped
# Conjugate Gradient Descent
# Accept the function, its limits,
# and the expected value provided by benchmark
# x(n+1) = x[n] - gamma[n]*GradF(x[n])
# variable stepsize
def GD_min1(f,x_init):
# N = Number of intervals
# xa = storage array for values to return
# i = counter
# init(ax,bx,ay,by) = function call to gen random
# values for initial guess
# prec = .0001
# gamma = prec is initial step size so algo
# doesn’t overshoot on the first run
xa = []
i = 0
x_init = x_init[0:2]
x_now = x_init
gamma = prec
converged = False
xa.append(x_now)
# loop the algo until the term_test conditions
# are satisfied and it returns a ‘True’ value
while converged == False or i < N:
converged = term_test(x_now,f)
if converged == True:
break
else:
df = nd.Gradient(f)
x_next = x_now - gamma*df(x_now)
a = (x_next - x_now)
b = a.T
c = (df(x_next)- df(x_now))
gamma = b*c/np.linalg.norm(c)**2
x_now = x_next
xa.append(x_now)
i += 1
# reshape ‘xa’ as a (-1,2) array to return
# f_min: return the minimum value, or how close
# the algo got to zero
# return the number of iterations
# for comparison later
xa = np.array(xa)
f_min = f(x_now)
return i, f_min, x_now
def CD_min1(f, x_init):
#print('x_init:', x_init)
x_init = x_init[0:2]
#print('x_init:', x_init)
xa = []
np.asarray(xa)
# counters
k = 0
# inital values
x_now = x_init
x0 = x_init
A = nd.Hessian(f)
A = A(x_now)
# internal functions
def CD_algo_init(x):
r = -A*x
p = r
return r,p
def a_now(rk,pk):
ak = rk.T*rk/(pk.T*A*pk)
return ak
def x_k1(xk,ak,pk):
xk1 = xk+ak*pk
return xk1
def r_k1(rk,A,ak,pk):
rk1 = rk - ak*A*pk
return rk1
def Beta_k(rk1,rk):
Bk = rk1.T*rk1/(rk.T*rk)
return Bk
def p_k1(rk1,Bk,pk):
pk1 = rk1 +Bk*pk
return pk1
# iterate through algo until a zero appears
rk, pk = CD_algo_init(x0)
while np.abs(f(x_now)) > prec or k < N:
ak = a_now(rk,pk)
#print('ak:',ak)
x_next = x_k1(x_now,ak,pk)
#print('x_next: ',x_next)
rk1 = r_k1(rk,A,ak,pk)
#print('rk1:', rk1)
if np.linalg.norm(rk1) <= prec:
xa.append(x_next)
k += 1
break
else:
Bk = Beta_k(rk1,rk)
pk = p_k1(rk1,Bk,pk)
x_now = x_next
xa.append(x_now)
#xa.append(x_now[0,1])
k += 1
# reshape the data from a mixed list
# of floats and arrays to a list of
# useable values
for i in range(k):
x = xa[i]
x1 = x[0]
x1a = x1[0]
x1b = x1[1]
x2 = x[1]
x2a = x2[0]
x2b = x2[1]
x0.append(x1a)
x0.append(x1b)
x0.append(x2a)
x0.append(x2b)
# reshape list into an array of coordinates
x0 = np.asarray(x0, dtype = np.float32)
x0.shape = (-1,2)
f_min = f(x_now)
#print('f_min:',f_min)
return k, f_min, x_now
# Quasi-Newton
# Symmetric Rank-1
# Accept the function, its limits,
# and the expected value
# provided by benchmark
def Rank1_min(f, x_init):
xa = []
x_now = x_init[0:2]
x_now = np.asarray(x_now)
xa.append(x_now)
converged = False
G = nd.Gradient(f)
H = nd.Hessian(f)
# initial function values
x0 = x_now[0:2]
f0 = f(x0)
g0 = G(x0)
H0 = np.identity(2)
k = 0
L = 1
# termination condition
epsilon = prec
# Direction d(k)
def dk(H,xk):
return -H*G(xk)
# Step Size a(k)
def a_now(H,xk,f):
a = np.linalg.norm(f(xk))
tau = np.linspace(1,0,N)
c = np.linspace(0,1,N)
c = c[np.random.randint(0,N)]
m = dk(H,xk)
m = array_fix(m)
p = m
m = np.dot(np.transpose(m),G(xk))
t = -c*m
j = 0
a_converge = False
# Backtracking Line Search to find
# appropriate value of ‘a’
while a_converge == False or j <= N:
ajt_test = f(xk) - f(xk + np.multiply(a,p))
j += 1
if a*t <= ajt_test:
a_converge = True
return a
if j == N:
return a
else:
a = a*tau[j]
# Compute x(k+1)
def x_next(xk,f,H):
direction = dk(H,xk)
direction = array_fix(direction)
return xk + a_now(H,xk,f)*direction
# Delta x(k)
def delta_xk(xk1, xk):
return xk1 - xk
# Delta x(k), or change in the gradient
def delta_gk(xk,xk1):
return H(xk1 - xk)
# H(k+1) = H + some stuff
def H_formula(H,xk,xk1,f):
a = delta_gk(xk,xk1)
b = delta_xk(xk,xk1)
c = H*a
d = b - c
e = (H + d*d.T/a.T/c)
return e
def algo(xk,H,f):
xk1 = x_next(xk,f,H)
Hk1 = H_formula(H,xk,xk1,f)
return xk1, Hk1
x_now, H_now = algo(x_now,H0,f)
xa.append(x_now)
converged = term_test(x_now,f)
k += 1
while converged == False:
x_now, H_now = algo(x_now, H_now,f)
xa.append(x_now)
k += 1
converged = term_test(x_now,f)
xa = np.array(xa)
xa.shape = (-1,2)
f_min = f(x_now)
return k, f_min, x_now
scipy.optimize.fmin_cg(f1,x0y0[0],full_output =True)
XY = [-2,2]
# initial values
x0y0 = []
# indexed iteration counters
iG = []
iC = []
iS = []
# indexed f_minimums
fG = []
fC = []
fS = []
# indexed XY where minimum is found
xG = []
xC = []
xS = []
# indexed timer
TG = []
TC = []
TS = []
# generate standardized data
for i in range(N):
XYZ = init(-2,2,-2,2)
x0y0.append(XYZ)
# time the functions
for i in range(N):
wrappedC = wrapper(CD_min1,f1,x0y0[i])
tC = timeit.timeit(wrappedC, number = 10)
print('CD timeit', tC)
wrappedG = wrapper(GD_min1,f1,x0y0[i])
tG = timeit.timeit(wrappedG, number = 10)
print('GD timeit', tG)
wrappedS = wrapper(Rank1_min,f1,x0y0[i])
tS = timeit.timeit(wrappedS, number = 10)
print('SR1 timeit', tS)
TG.append(tG)
TC.append(tC)
TS.append(tS)
# run some data to make comparisons
for i in range(N):
jg, fg, xg = GD_min1(f1, x0y0[i])
jc, fc, xc = CD_min1(f1,x0y0[i])
js, fs, xs = Rank1_min(f1,x0y0[i])
iG.append(jg)
fG.append(fg)
xG.append(xg)
iC.append(jc)
fC.append(fc)
xC.append(xc)
iS.append(js)
fS.append(fs)
xS.append(xs)
xmagG = []
xmagC = []
xmagS = []
for i in range(N):
x = 0 - np.abs(mag(xG[i]))
xmagG.append(x)
x = 0 - np.abs(mag(xC[i]))
xmagC.append(x)
x = 0 - np.abs(mag(xS[i]))
xmagS.append(x)
# iteration comparison
# exclude CD as it universally
# takes 1 step
a, b = 0, N
x = np.linspace(a,b,N)
yg1 = iG
#yc1 = iC
ys1 = iS
fig1 = plt.figure()
plt.title('iteration comparison')
plt.xlabel('index number')
plt.ylabel('# of iterations to find a zero')
plt.plot(x,yg1, label = 'Grad Desc', color = 'red')
plt.plot(x,ys1, label = 'Quas Newt', color = 'blue')
plt.legend(loc='lower right')
plt.show()
# minimization comparison
# exclude CD as it is in a league
# of its own
x = np.linspace(a,b,N)
yg2 = fG
ys2 = fS
fig2 = plt.figure()
plt.title('minimization comparison GD vs SR1')
plt.xlabel('index number')
plt.ylabel('distance from zero')
plt.plot(x, yg2, color = 'red',label = 'Grad Desc')
plt.plot(x,ys2, color = 'blue', label = 'Quas Newt')
plt.legend(loc='lower right')
plt.show()
# CD specific minimaztion view
x = np.linspace(a,b,N)
yc2 = fC
fig3 = plt.figure()
plt.title('minimization view of Conj Desc algo')
plt.xlabel('index number')
plt.ylabel('distance from zero')
plt.plot(x,yc2, color = 'black', label = 'Conj Desc')
plt.legend(loc='lower right')
plt.show()
#Magnitude of distance from analytic zero
x = np.linspace(a,b,N)
yc3 = xmagC
fig5 = plt.figure()
plt.title('Magnitude of distance from analytic zero, Conj Desc')
plt.xlabel('index number')
plt.ylabel('distance from zero')
plt.plot(x,yc3, color = 'black', label = 'Conj Desc')
plt.legend(loc='lower right')
plt.show()
x = np.linspace(a,b,N)
yg3 = xmagG
ys3 = xmagS
fig6 = plt.figure()
plt.title('Magnitude of distance from analytic zero, GD vs SR1')
plt.xlabel('index number')
plt.ylabel('distance from zero')
plt.plot(x, yg3, color = 'red',label = 'Grad Desc')
plt.plot(x,ys3, color = 'blue', label = 'Quas Newt')
plt.legend(loc='lower right')
plt.show()
x = np.linspace(a,b,N)
yg4 = TG
ys4 = TS
fig8 = plt.figure()
plt.title('Runtime Comparison')
plt.xlabel('index number')
plt.ylabel('Average runtime for 100 iterations of given [x0,y0], GD and SR1')
plt.plot(x, yg4, color = 'red',label = 'Grad Desc')
plt.plot(x,ys4, color = 'blue', label = 'Quas Newt')
plt.legend(loc='lower right')
plt.show()
x = np.linspace(a,b,N)
yc4 = TC
fig9 = plt.figure()
plt.title('Runtime Comparison')
plt.xlabel('index number')
plt.ylabel('Average runtime for 100 iterations of given [x0,y0], Conj Desc')
plt.plot(x,yc4, color = 'black', label = 'Conj Desc')
plt.legend(loc='lower right')
plt.show()