-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.py
61 lines (47 loc) · 1.72 KB
/
controller.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
import const as const
import Point as Point
import random
# Escolher aleatoriamente k valores para centroides dos clusters
def define_samples():
samples = []
for i in range(0, const.number_samples):
samples.append(Point.Point(const.start,const.end))
return samples
def define_centroids(samples):
centroids = []
probabilistic_space = []
for sample in samples:
probabilistic_space.append(sample)
for i in range(0,const.k):
random_index = round(random.uniform(0,len(probabilistic_space)-1))
centroids.append(probabilistic_space[random_index])
del probabilistic_space[random_index]
return centroids
def match_samples(samples,centroids):
clusters = []
for centroid in centroids:
clusters.append([])
i=0
for sample in samples:
min = const.max_num
index = -1
for i in range(0,len(centroids)):
if sample.distance(centroids[i])<min:
index = i
min = sample.distance(centroids[i])
clusters[index].append(sample)
sample.match(index)
return clusters
def refresh_centroids(samples,centroids, clusters):
centroid_has_change = False
for i in range(0,len(clusters)):
central_point = Point.Point(const.start,const.end)
central_point.x = central_point.y = 0
num=0
for j in range(0,len(clusters[i])):
central_point.x+=clusters[i][j].x
central_point.y+=clusters[i][j].y
num=num+1
central_point.x/=num
central_point.y/=num
clusters[i] = [central_point]