-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathutils.py
178 lines (137 loc) · 5.58 KB
/
utils.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import numpy as np
import networkx as nx
from random import random, randint
from math import floor, log
np.random.seed(44)
def nearest_neigbor(vec_pos,query_vec):
nearest_neighbor_index = -1
nearest_dist = float('inf')
nodes = []
edges = []
for i in range(np.shape(vec_pos)[0]):
nodes.append((i,{"pos": vec_pos[i,:]}))
if i<np.shape(vec_pos)[0]-1:
edges.append((i,i+1))
else:
edges.append((i,0))
dist = np.linalg.norm(query_vec-vec_pos[i])
if dist < nearest_dist:
nearest_neighbor_index = i
nearest_dist = dist
G_lin = nx.Graph()
G_lin.add_nodes_from(nodes)
G_lin.add_edges_from(edges)
nodes = []
nodes.append(("*",{"pos": vec_pos[nearest_neighbor_index,:]}))
G_best = nx.Graph()
G_best.add_nodes_from(nodes)
return G_lin, G_best
def layer_num(max_layers: int):
# new element's topmost layer: notice the normalization by mL
mL = 1.5
layer_i = floor(-1 * log(random()) * mL)
# ensure we don't exceed our allocated layers.
layer_i = min(layer_i, max_layers-1)
return layer_i
#return randint(0,max_layers-1)
def construct_HNSW(vec_pos,m_nearest_neighbor):
max_layers = 4
vec_num = np.shape(vec_pos)[0]
dist_mat = np.zeros((vec_num,vec_num))
for i in range(vec_num):
for j in range(i,vec_num):
dist = np.linalg.norm(vec_pos[i,:]-vec_pos[j,:])
dist_mat[i,j] = dist
dist_mat[j,i] = dist
node_layer = []
for i in range(np.shape(vec_pos)[0]):
node_layer.append(layer_num(max_layers))
max_num_of_layers = max(node_layer) + 1 ## layer indices start from 0
GraphArray = []
for layer_i in range(max_num_of_layers):
nodes = []
edges = []
edges_nn = []
for i in range(np.shape(vec_pos)[0]): ## Number of Vectors
if node_layer[i] >= layer_i:
nodes.append((i,{"pos": vec_pos[i,:]}))
G = nx.Graph()
G.add_nodes_from(nodes)
pos=nx.get_node_attributes(G,'pos')
for i in range (len(G.nodes)):
node_i = nodes[i][0]
nearest_edges = -1
nearest_distances = float('inf')
candidate_edges = range(0,i)
candidate_edges_indices = []
#######################
for j in candidate_edges:
node_j = nodes[j][0]
candidate_edges_indices.append(node_j)
dist_from_node = dist_mat[node_i,candidate_edges_indices]
num_nearest_neighbor = min(m_nearest_neighbor,i) ### Add note comment
if num_nearest_neighbor > 0:
indices = np.argsort(dist_from_node)
for nn_i in range(num_nearest_neighbor):
edges_nn.append((node_i,candidate_edges_indices[indices[nn_i]]))
for j in candidate_edges:
node_j = nodes[j][0]
dist = np.linalg.norm(pos[node_i]-pos[node_j])
if dist < nearest_distances:
nearest_edges = node_j
nearest_distances = dist
if nearest_edges != -1:
edges.append((node_i,nearest_edges))
G.add_edges_from(edges_nn)
GraphArray.append(G)
return GraphArray
## Search the Graph
def search_HNSW(GraphArray,G_query):
max_layers = len(GraphArray)
G_top_layer = GraphArray[max_layers - 1]
num_nodes = G_top_layer.number_of_nodes()
entry_node_r = randint(0,num_nodes-1)
nodes_list = list(G_top_layer.nodes)
entry_node_index = nodes_list[entry_node_r]
#entry_node_index = 26
SearchPathGraphArray = []
EntryGraphArray = []
for l_i in range(max_layers):
layer_i = max_layers - l_i - 1
G_layer = GraphArray[layer_i]
G_entry = nx.Graph()
nodes = []
p = G_layer.nodes[entry_node_index]['pos']
nodes.append((entry_node_index,{"pos": p}))
G_entry.add_nodes_from(nodes)
nearest_node_layer = entry_node_index
nearest_distance_layer = np.linalg.norm( G_layer.nodes[entry_node_index]['pos'] - G_query.nodes['Q']['pos'])
current_node_index = entry_node_index
G_path_layer = nx.Graph()
nodes_path = []
p = G_layer.nodes[entry_node_index]['pos']
nodes_path.append((entry_node_index,{"pos": p}))
cond = True
while cond:
nearest_node_current = -1
nearest_distance_current = float('inf')
for neihbor_i in G_layer.neighbors(current_node_index):
vec1 = G_layer.nodes[neihbor_i]['pos']
vec2 = G_query.nodes['Q']['pos']
dist = np.linalg.norm( vec1 - vec2)
if dist < nearest_distance_current:
nearest_node_current = neihbor_i
nearest_distance_current = dist
if nearest_distance_current < nearest_distance_layer:
nearest_node_layer = nearest_node_current
nearest_distance_layer = nearest_distance_current
nodes_path.append((nearest_node_current,{"pos": G_layer.nodes[nearest_node_current]['pos']}))
else:
cond = False
entry_node_index = nearest_node_layer
G_path_layer.add_nodes_from(nodes_path)
SearchPathGraphArray.append(G_path_layer)
EntryGraphArray.append(G_entry)
SearchPathGraphArray.reverse()
EntryGraphArray.reverse()
return SearchPathGraphArray, EntryGraphArray