-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgif_grad_ackley.py
112 lines (92 loc) · 4.14 KB
/
gif_grad_ackley.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
import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import math
# https://stackoverflow.com/questions/20228546/how-do-i-apply-some-function-to-a-python-meshgrid
@np.vectorize
# https://github.com/adhishagc/Ackley-Function
def ackley_function(x,y):
#returns the point value of the given coordinate
part_1 = -0.2*math.sqrt(0.5*(x*x + y*y))
part_2 = 0.5*(math.cos(2*math.pi*x) + math.cos(2*math.pi*y))
value = math.exp(1) + 20 - 20 * math.exp(part_1) - math.exp(part_2)
#returning the value
return value
def derivative_x(x,y):
part_1 = 2.82842712474619 * x * math.exp(-0.14142135623731 * math.sqrt(x*x + y*y))/(math.sqrt(x*x + y*y))
part_2 = math.pi * math.exp(0.5*(math.cos(2*math.pi*x) + math.cos(2*math.pi*y))) * math.sin(2*math.pi*x)
return part_1 + part_2
def derivative_y(x,y):
part_1 = 2.82842712474619 * y * math.exp(-0.14142135623731 * math.sqrt(x*x + y*y))/(math.sqrt(x*x + y*y))
part_2 = math.pi * math.exp(0.5*(math.cos(2*math.pi*x) + math.cos(2*math.pi*y))) * math.sin(2*math.pi*y)
return part_1 + part_2
delta = 0.01
x = np.arange(-5.0, 5.0, delta)
y = np.arange(-5.0, 5.0, delta)
# https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html
X, Y = np.meshgrid(x, y)
Z = ackley_function(X,Y)
# https://stackoverflow.com/questions/332289/how-do-you-change-the-size-of-figures-drawn-with-matplotlib
fig, ax = plt.subplots(figsize=[10, 10])
levels = np.arange(0.0, 15.0, 1)
CS = plt.contour(X, Y, Z, levels=levels)
plt.clabel(CS, inline=1, fontsize=10)
A = [-1, -0.5]
B = [0.2, -1.7]
C = [4.2, 0.4]
D = [-3.5, -0.6]
E = [-2.6, 2.3]
ax.plot(A[0], A[1], 'bo')
ax.plot(B[0], B[1], 'ro')
ax.plot(C[0], C[1], 'co')
ax.plot(D[0], D[1], 'mo')
ax.plot(E[0], E[1], 'ko')
# https://www.kite.com/python/answers/how-to-label-a-single-point-in-a-matplotlib-graph-in-python
ax.annotate("Point A", (A[0], A[1]))
ax.annotate("Point B", (B[0], B[1]))
ax.annotate("Point C", (C[0], C[1]))
ax.annotate("Point D", (D[0], D[1]))
ax.annotate("Point E", (E[0], E[1]))
a_history = [[],[]]
b_history = [[],[]]
c_history = [[],[]]
d_history = [[],[]]
e_history = [[],[]]
# learning_rate = 0.005
# epochs = 100
learning_rate = 0.001
epochs = 150
for iter in range(epochs):
#ax.plot(A[0], A[1], 'bo')
#ax.plot(B[0], B[1], 'ro')
#ax.plot(C[0], C[1], 'co')
#ax.plot(D[0], D[1], 'mo')
#ax.plot(E[0], E[1], 'ko')
a_history[0].append(A[0]); a_history[1].append(A[1])
b_history[0].append(B[0]); b_history[1].append(B[1])
c_history[0].append(C[0]); c_history[1].append(C[1])
d_history[0].append(D[0]); d_history[1].append(D[1])
e_history[0].append(E[0]); e_history[1].append(E[1])
A[0] = A[0] - learning_rate * derivative_x(A[0], A[1])
A[1] = A[1] - learning_rate * derivative_y(A[0], A[1])
B[0] = B[0] - learning_rate * derivative_x(B[0], B[1])
B[1] = B[1] - learning_rate * derivative_y(B[0], B[1])
C[0] = C[0] - learning_rate * derivative_x(C[0], C[1])
C[1] = C[1] - learning_rate * derivative_y(C[0], C[1])
D[0] = D[0] - learning_rate * derivative_x(D[0], D[1])
D[1] = D[1] - learning_rate * derivative_y(D[0], D[1])
E[0] = E[0] - learning_rate * derivative_x(E[0], E[1])
E[1] = E[1] - learning_rate * derivative_y(E[0], E[1])
div = 10
#for i in range(0, epochs//10):
def animate(i):
# https://stackoverflow.com/questions/509211/understanding-slice-notation
ax.plot(a_history[0][i*div:(i+1)*div + 1], a_history[1][i*div:(i+1)*div + 1], color='blue', linewidth=4)
ax.plot(b_history[0][i*div:(i+1)*div + 1], b_history[1][i*div:(i+1)*div + 1], color='red', linewidth=4)
ax.plot(c_history[0][i*div:(i+1)*div + 1], c_history[1][i*div:(i+1)*div + 1], color='cyan', linewidth=4)
ax.plot(d_history[0][i*div:(i+1)*div + 1], d_history[1][i*div:(i+1)*div + 1], color='magenta', linewidth=4)
ax.plot(e_history[0][i*div:(i+1)*div + 1], e_history[1][i*div:(i+1)*div + 1], color='black', linewidth=4)
# https://eli.thegreenplace.net/2016/drawing-animated-gifs-with-matplotlib/
anim = FuncAnimation(fig, animate, frames=np.arange(0, epochs//div), interval=200)
anim.save('images/grad-ackley.gif', dpi=80)