-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
111 lines (90 loc) · 2.99 KB
/
utility.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
import gurobipy as gp
import numpy as np
import matplotlib.pyplot as plt
import random
from random import randint
from numba import jit, njit
def load_points(file):
f = open(file, 'r')
points = []
for i,line in enumerate(f.readlines()):
#ritorna il count nella prima variabile e il value nella seconda
nums = line.split(" ")
coords = []
for num in nums:
if num != '' and num != '\n':
coords.append(float(num))
if len(coords)!=0:
coords = np.array(coords)
points.append(coords)
points = np.array(points)
f.close()
return points
@jit(nopython=True)#modalità di compilazione ad alte performance
def calc_centroids(sol, points, K):
dim = np.shape(points)[1]
centroids = np.zeros((K,dim))
num_elems = np.zeros(K)
for elem,cluster in enumerate(sol):
cluster = int(cluster)
elem = int(elem)
centroids[cluster] = centroids[cluster] + points[elem]
num_elems[cluster] = num_elems[cluster] + 1
for i in range(K):
centroids[i] = centroids[i] / num_elems[i]
return centroids
@jit(nopython=True)
def squared_inner_distance(sol, points, K):
dim = np.shape(points)[1]
centroids = np.zeros((K,dim))
num_elems = np.zeros(K)
for elem,cluster in enumerate(sol):
cluster = int(cluster)
elem = int(elem)
centroids[cluster] = centroids[cluster] + points[elem]
num_elems[cluster] = num_elems[cluster] + 1
for i in range(K):
centroids[i] = centroids[i] / num_elems[i]
tot_distance = 0
for elem,cluster in enumerate(sol):
cluster = int(cluster)
elem = int(elem)
tot_distance = np.sum((centroids[cluster]-points[elem])**2) + tot_distance
return tot_distance
def printR2sol(points, sol, K):
N = len(sol)
colors = []
for i in range(K):
colors.append('#%06X' % randint(0, 0xFFFFFF))
clusters = []
for i in range(K):
cluster = []
for j in range(N):
if(sol[j]==i):
cluster.append(points[j].copy())
cluster = np.array(cluster)
clusters.append(cluster)
for i in range(K):
to_draw = clusters[i]
plt.scatter(to_draw[:,0], to_draw[:,1], color = colors[i])
plt.show()
def create_initial_sol(points, K):
N = points.shape[0]
dimension = points.shape[1]
centroids = np.zeros((K,dimension))
clusters = np.zeros(N)
choices = np.arange(N)
for i in range(K):
choice = np.random.randint(len(choices))
centroids[i] = points[choice].copy() #centroids_list[i]
choices = np.delete(choices, choice)
for i in range(N):
dist = -1
centroid = -1
for c in range(K):
dist_c = np.linalg.norm(centroids[c]-points[i])
if(dist_c < dist or dist == -1):
centroid = c
dist = dist_c
clusters[i] = int(centroid)
return clusters