-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
74 lines (64 loc) · 2.52 KB
/
main.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
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
from matplotlib.animation import PillowWriter
from Gradient_Descent import f
import pickle
args = {
'f': f, 'paths' :0
}
def creat_animation(f, paths, minimum,
x_lim, y_lim,
colors, labels, n_seconds=7,
figsize = (14,16)):
try :
path_length = max(len(path) for path in paths)
n_points = 300
x = np.linspace(*x_lim, n_points)
y = np.linspace(*y_lim, n_points)
X, Y = np.meshgrid(x, y)
Z = f([X,Y])
fig, ax = plt.subplots(figsize=figsize)
ax.contour(X, Y, Z, 90, cmap="jet")
scatters = [ax.scatter(None,
None,
label=label,
c=c) for c, label in zip(colors, labels)]
ax.legend(prop={"size": 25})
ax.plot(*minimum, "rD")
#ax.plot(optimal_points[:,0], optimal_points[:,1], colors)
def animate(i):
for path, scatter in zip(paths, scatters):
scatter.set_offsets(path[:i, :])
ax.set_title(str(i))
ms_per_frame = 1000 * n_seconds / path_length
anim = FuncAnimation(fig, animate, frames=path_length, interval=ms_per_frame)
plt.show()
except Exception as e :
print(e)
return anim
if __name__ == "__main__":
pickle_filename = 'data.pkl'
# Read the data from the Pickle file
with open(pickle_filename, 'rb') as pickle_file:
data = pickle.load(pickle_file)
labels = list(data.keys())
paths =[
list(list(data.values())[0].values())[0],
list(list(data.values())[1].values())[0]
]
optimal_points = [
paths[0][-1],
paths[1][-1]
]
paths[ 0] = np.array(list(paths[0][:1000:10]) + list(paths[0][1000::100]))
colors = ['y', 'b']
minimum = (1,1)
x_lim, y_lim = (-4,4),(-4,4)
print(f"for Gradient Descent with pas optimal | x* = {optimal_points[0]} | number of iteration is = {len(paths[0])}")
print(f"for Gradient Descent with pas optimal | x* = {optimal_points[1]} | number of iteration is = {len(paths[1])}")
anim = creat_animation(f, paths, minimum,
x_lim, y_lim,
colors, labels, n_seconds=5,
figsize = (14,16))
anim.save('animation1.gif', writer=PillowWriter(fps=30))