forked from culdo/NPTU_AI_course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_search.py
58 lines (45 loc) · 1.53 KB
/
random_search.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
import numpy as np
import matplotlib.pyplot as plt
from util_plot import AddPlot
is_3d = True
ax, particle_dim = AddPlot(is_3d).returns
n_particles = 10
particles_shape = (n_particles, particle_dim)
test_goal = np.random.rand(particle_dim)
iteration = 0
best_goal = None
best_distant = None
while True:
distant_list = []
if best_goal is None:
# First time we generate particles using Uniform Distribution
rand_particles = np.random.rand(*particles_shape)
else:
# Randomly generate particles using Normal Distribution
sigma = 1/(4*iteration)
rand_particles = best_goal + sigma * np.random.randn(*particles_shape)
ax.scatter(*rand_particles.T, s=50, alpha=0.5)
ax.scatter(*test_goal, s=200, marker="*", alpha=1.0)
for p in rand_particles:
distance = np.linalg.norm(p - test_goal)
distant_list.append(distance)
min_idx = np.argmin(distant_list)
min_particle = rand_particles[min_idx]
if best_distant is None or distant_list[min_idx] < best_distant:
best_distant = distant_list[min_idx]
best_goal = min_particle
ax.scatter(*best_goal, s=200, marker="+", alpha=1.0)
plt.ylim(-1, 1)
plt.xlim(-1, 1)
if is_3d:
ax.set_zlim3d(-1, 1)
plt.title("iteration %s, Error: %.4f" % (iteration, best_distant))
plt.pause(0.5)
plt.draw()
ax.clear()
# We assume converged when centroid no more updated that same as k-means.
# if mean_distance < 0.0001:
# break
iteration += 1
# Show end plot forever
plt.show()