-
Notifications
You must be signed in to change notification settings - Fork 2
/
algorithms.py
209 lines (159 loc) · 6.52 KB
/
algorithms.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-
from time import time
from collections import deque
import numpy as np
import math, random, logging
from concurrent.futures import ProcessPoolExecutor, as_completed
import multiprocessing as mp
from collections import defaultdict
from utils import *
def generate_parameters_random_walk(workers):
logging.debug('Loading distances_nets from disk...')
sum_weights = {}
amount_edges = {}
layer = 0
while (isPickle('distances_nets_weights-layer-' + str(layer))):
logging.debug('Executing layer {}...'.format(layer))
weights = restoreVariableFromDisk('distances_nets_weights-layer-' + str(layer))
for k, list_weights in weights.items():
if (layer not in sum_weights):
sum_weights[layer] = 0
if (layer not in amount_edges):
amount_edges[layer] = 0
for w in list_weights:
sum_weights[layer] += w
amount_edges[layer] += 1
# print('Layer {} executed.', layer)
layer += 1
average_weight = {}
for layer in sum_weights.keys():
average_weight[layer] = sum_weights[layer] / amount_edges[layer]
# print("Saving average_weights on disk...")
saveVariableOnDisk(average_weight, 'average_weight')
amount_neighbours = {}
layer = 0
while (isPickle('distances_nets_weights-layer-' + str(layer))):
# print('Executing layer {}...', layer)
weights = restoreVariableFromDisk('distances_nets_weights-layer-' + str(layer))
amount_neighbours[layer] = {}
for k, list_weights in weights.items():
cont_neighbours = 0
for w in list_weights:
if (w > average_weight[layer]):
cont_neighbours += 1
amount_neighbours[layer][k] = cont_neighbours
logging.debug('Layer {} executed.'.format(layer))
layer += 1
# print("Saving amount_neighbours on disk...")
saveVariableOnDisk(amount_neighbours, 'amount_neighbours')
def chooseNeighbor(v, graphs, alias_method_j, alias_method_q, layer):
v_list = graphs[layer][v]
idx = alias_draw(alias_method_j[layer][v], alias_method_q[layer][v])
v = v_list[idx]
return v
def exec_random_walk(graphs, alias_method_j, alias_method_q, v, walk_length, amount_neighbours):
original_v = v
t0 = time()
initialLayer = 0
layer = initialLayer
path = deque()
path.append(v)
while len(path) < walk_length:
r = random.random()
if (r < 0.3):
v = chooseNeighbor(v, graphs, alias_method_j, alias_method_q, layer)
path.append(v)
else:
r = random.random()
limiar_moveup = prob_moveup(amount_neighbours[layer][v])
if (r > limiar_moveup):
if (layer > initialLayer):
layer = layer - 1
else:
if ((layer + 1) in graphs and v in graphs[layer + 1]):
layer = layer + 1
t1 = time()
logging.debug('RW - vertex {}. Time : {}s'.format(original_v, (t1 - t0)))
return path
def exec_ramdom_walks_for_chunck(vertices, graphs, alias_method_j, alias_method_q, walk_length, amount_neighbours):
walks = deque()
for v in vertices:
walks.append(exec_random_walk(graphs, alias_method_j, alias_method_q, v, walk_length, amount_neighbours))
return walks
def generate_random_walks_large_graphs(num_walks, walk_length, workers, vertices):
logging.debug('Loading distances_nets from disk...')
graphs = restoreVariableFromDisk('distances_nets_graphs')
alias_method_j = restoreVariableFromDisk('nets_weights_alias_method_j')
alias_method_q = restoreVariableFromDisk('nets_weights_alias_method_q')
amount_neighbours = restoreVariableFromDisk('amount_neighbours')
logging.debug('Creating RWs...')
t0 = time()
walks = deque()
initialLayer = 0
parts = workers
with ProcessPoolExecutor(max_workers=workers) as executor:
for walk_iter in range(num_walks):
random.shuffle(vertices)
logging.debug("Execution iteration {} ...".format(walk_iter))
walk = exec_ramdom_walks_for_chunck(vertices, graphs, alias_method_j, alias_method_q, walk_length,
amount_neighbours)
walks.extend(walk)
logging.debug("Iteration {} executed.".format(walk_iter))
t1 = time()
logging.debug('RWs created. Time : {}m'.format((t1 - t0) / 60))
logging.debug("Saving Random Walks on disk...")
save_random_walks(walks)
def generate_random_walks(num_walks, walk_length, workers, vertices):
# print('Loading distances_nets on disk...')
graphs = restoreVariableFromDisk('distances_nets_graphs')
alias_method_j = restoreVariableFromDisk('nets_weights_alias_method_j')
alias_method_q = restoreVariableFromDisk('nets_weights_alias_method_q')
amount_neighbours = restoreVariableFromDisk('amount_neighbours')
# print('Creating RWs...', list(amount_neighbours.keys()))
t0 = time()
walks = deque()
initialLayer = 0
if (workers > num_walks):
workers = num_walks
with ProcessPoolExecutor(max_workers=workers) as executor:
futures = {}
for walk_iter in range(num_walks):
random.shuffle(vertices)
job = executor.submit(exec_ramdom_walks_for_chunck, vertices, graphs, alias_method_j, alias_method_q,
walk_length, amount_neighbours)
futures[job] = walk_iter
# part += 1
# print("Receiving results...")
for job in as_completed(futures):
walk = job.result()
r = futures[job]
# print("Iteration {} executed.", r)
walks.extend(walk)
del futures[job]
t1 = time()
logging.debug('RWs created. Time: {}m'.format((t1 - t0) / 60))
logging.debug("Saving Random Walks on disk...")
save_random_walks(walks)
def save_random_walks(walks):
with open('random_walks.txt', 'w') as file:
for walk in walks:
line = ''
for v in walk:
line += str(v) + ' '
line += '\n'
file.write(line)
return
def prob_moveup(amount_neighbours):
x = math.log(amount_neighbours + math.e)
p = (x / (x + 1))
return p
def alias_draw(J, q):
'''
Draw sample from a non-uniform discrete distribution using alias sampling.
'''
K = len(J)
kk = int(np.floor(np.random.rand() * K))
if np.random.rand() < q[kk]:
return kk
else:
return J[kk]