-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph_generator.py
More file actions
executable file
·343 lines (277 loc) · 12.6 KB
/
graph_generator.py
File metadata and controls
executable file
·343 lines (277 loc) · 12.6 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/python2
"""
Copyright (c) 2013, Arlei Silva
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer. Redistributions in binary form must
reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the
distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
@author: Arlei Silva (arleilps@gmail.com)
"""
import sys
import getopt
import math
import random
import networkx
from collections import deque
class Graph(object):
def __init__(self, num_vertices, num_edges, num_partitions, radius, max_average, sse, sse_reduction):
self.num_vertices = num_vertices
self.num_edges = num_edges
self.num_partitions = num_partitions
self.radius = radius
self.max_average = max_average
self.sse_compression = sse - sse_reduction
self.sse_reduction = sse_reduction
self.tree = []
def new_node(self, partition):
return {
"left": -1,
"right": -1,
"average": 0,
"partition": partition,
"size": 0
}
def set_graph(self):
G = networkx.barabasi_albert_graph(self.num_vertices, self.num_edges)
self.edges = []
self.values = []
for i in range(0, self.num_vertices):
self.edges.append([])
self.values.append(0)
for e in G.edges(data=True):
self.edges[e[0]].append(e[1])
self.edges[e[1]].append(e[0])
def print_tree(self):
for t in range(0, len(self.tree)):
print self.tree[t]
for t in range(0, len(self.partitions)):
print self.partitions[t]
def set_average_rec(self, node, mu, reduction_slice):
if(node["partition"] == -1):
# print "reduction_slice = %lf\n" % reduction_slice
size_partition = self.tree[node["left"]]["size"]
size_parent = node["size"]
size_complement = size_parent - size_partition
if size_parent*size_partition > 0:
mu_partition = mu + math.sqrt(float(reduction_slice * size_complement) / (size_parent*size_partition))
else:
mu_partition = 0
self.tree[node["left"]]["average"] = mu_partition
self.set_average_rec(self.tree[node["left"]], self.tree[node["left"]]["average"], reduction_slice)
if size_complement > 0:
mu_complement = float(mu*size_parent - mu_partition*size_partition) / size_complement
else:
mu_complement = 0
self.tree[node["right"]]["average"] = mu_complement
self.set_average_rec(self.tree[node["right"]], self.tree[node["right"]]["average"], reduction_slice)
else:
self.partitions[node["partition"]]["average"] = node["average"]
def set_averages(self):
self.tree[0]["average"] = random.uniform(0, self.max_average)
reduction_slice = float(self.sse_reduction) / (self.num_partitions - 1)
self.set_average_rec(self.tree[0], self.tree[0]["average"], reduction_slice)
def set_partitions(self):
self.partition_assignments = []
self.partitions = []
self.centers = {}
self.tree = []
for i in range(0, self.num_vertices):
self.partition_assignments.append(0)
partition = {
"average": 0,
"center": -1,
"size": self.num_vertices,
"node": 0
}
self.tree.append(self.new_node(0))
self.tree[0]["size"] = self.num_vertices
self.partitions.append(partition)
for p in range(1, self.num_partitions):
partition = self.create_new_partition(p, self.radius)
self.partitions.append(partition)
def create_new_partition(self, partition_id, radius):
partition = {}
partition["average"] = 0
partition["center"] = random.randint(0, self.num_vertices-1)
partition["node"] = len(self.tree)
while partition["center"] in self.centers:
partition["center"] = random.randint(0, self.num_vertices-1)
parent = self.partition_assignments[partition["center"]]
self.tree[self.partitions[parent]["node"]]["partition"] = -1
self.tree[self.partitions[parent]["node"]]["left"] = len(self.tree)
self.tree[self.partitions[parent]["node"]]["right"] = len(self.tree)+1
self.tree.append(self.new_node(partition_id))
self.tree.append(self.new_node(parent))
partition["size"] = self.set_vertices_partition(partition_id, partition["center"], parent, radius)
self.partitions[parent]["size"] = self.partitions[parent]["size"] - partition["size"]
self.tree[partition["node"]]["size"] = partition["size"]
self.partitions[parent]["node"] = self.tree[self.partitions[parent]["node"]]["right"]
self.tree[self.partitions[parent]["node"]]["size"] = self.partitions[parent]["size"]
return partition
def set_vertices_partition(self, partition_id, center, parent, radius):
distances = {}
size = 0
q = deque([center])
distances[center] = 0
self.values[center] = 0
self.partition_assignments[center] = partition_id
while len(q) > 0:
u = q.popleft()
for z in self.edges[u]:
if z not in distances or distances[z] > distances[u] + 1:
if distances[u] + 1 <= radius:
distances[z] = distances[u] + 1
q.append(z)
if self.partition_assignments[z] == parent:
self.partition_assignments[z] = partition_id
for i in range(0, self.num_vertices):
if(self.partition_assignments[i] == partition_id):
size = size + 1
return size
def set_values(self):
self.values = []
sse_partition = float(self.sse_compression) / self.num_partitions
self.set_averages()
# print "sse_partition = %lf\n" % sse_partition
for i in range(0, self.num_vertices):
self.values.append(0)
for p in range(0, self.num_partitions):
# print "partition = %d, average = %lf\n" % (p, self.partitions[p]["average"])
size_partition = self.partitions[p]["size"]
average = self.partitions[p]["average"]
std = math.sqrt(float(sse_partition) / size_partition)
for i in range(0, self.num_vertices):
if self.partition_assignments[i] == p:
self.values[i] = random.gauss(average, std)
def write_values(self, output_file_name):
output_file = open(output_file_name, 'w')
for v in range(0, len(self.edges)):
output_file.write(str(v)+","+str(self.values[v])+"\n")
output_file.close()
def write_graph(self, output_file_name):
output_file = open(output_file_name, 'w')
for v in range(0, len(self.edges)):
for u in self.edges[v]:
if v > u:
output_file.write(str(v)+","+str(u)+"\n")
output_file.close()
def write_statistics(self, output_file_name):
sse_data = self.compute_sse_data()
sse = self.compute_sse()
output_file = open(output_file_name, 'w')
output_file.write("sse_data = "+str(sse_data)+"\n")
output_file.write("sse_optimal_partitioning = "+str(sse)+"\n")
# output_file.write("optimal_sse_reduction = "+str(float(self.sse_data-self.sse)/self.sse_data)+"\n")
output_file.write("optimal_sse_reduction = "+str(float(sse_data-sse))+"\n")
output_file.write("partition_id,center,average,actual_average,sse,size\n")
for p in range(0, len(self.partitions)):
output_file.write(str(p)+","+str(self.partitions[p]["center"])+","+str(self.partitions[p]["average"])+","+str(self.partitions[p]["actual_average"])+","+str(self.partitions[p]["sse"])+","+str(self.partitions[p]["size"])+"\n")
output_file.write("vertex,partition\n")
for v in range(0, len(self.partition_assignments)):
output_file.write(str(v)+","+str(self.partition_assignments[v])+"\n")
output_file.close()
def compute_sse(self):
sse = 0
for p in range(0, len(self.partitions)):
average = 0
n = 0
for v in range(0, len(self.partition_assignments)):
if self.partition_assignments[v] == p:
average = average + self.values[v]
n = n + 1
if n > 0:
average = float(average) / n
sse_partition = 0
for v in range(0, len(self.partition_assignments)):
if self.partition_assignments[v] == p:
sse_partition = sse_partition + math.pow(average - self.values[v], 2)
self.partitions[p]["sse"] = sse_partition
self.partitions[p]["actual_average"] = average
sse = sse + sse_partition
return sse
def compute_sse_data(self):
sse = 0
average = 0
for v in self.values:
average = average + v
average = float(average) / len(self.values)
# print "average = %lf" % average
for v in self.values:
sse = sse + math.pow(average - v, 2)
return sse
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
# Parameters:
# - output file name o
# - number of vertices v
# - number of edges new vertex e (preferential attachment)
# - number of partitions p
# - partition radius r
# - max partition average m
# - sse reduction c
# - num partitions n
#
try:
try:
opts, input_files = getopt.getopt(argv[1:], "o:v:e:p:r:m:s:c:h", ["output=","num-vertices=","num-edges=","num-partitions=","radius=","max-average=","sse=","reduction=","help"])
except getopt.error, msg:
raise Usage(msg)
output_file_name = ""
num_vertices = 0
num_edges = 0
num_partitions = 0
radius = 0
max_average = 0
sse_reduction = 0
sse = 0
for opt,arg in opts:
if opt in ('-o', '--output'):
output_file_name = arg
if opt in ('-v', '--num-vertices'):
num_vertices = int(arg)
if opt in ('-e', '--num-edges'):
num_edges = int(arg)
if opt in ('-p', '--num-partitions'):
num_partitions = int(arg)
if opt in ('-r', '--radius'):
radius = int(arg)
if opt in ('-m', '--max-average'):
max_average = float(arg)
if opt in ('-s', '--sse'):
sse = float(arg)
if opt in ('-c', '--reduction'):
sse_reduction = float(arg)
if opt in ('-h', '--help'):
print "python graph_generator.py [-o <output_file>] [-v <num-vertices>] [-e <num-edges>] [-p <num-partitions] [-r <radius>] [-s <sse>] [-c <reduction>]"
sys.exit()
g = Graph(num_vertices, num_edges, num_partitions, radius, max_average, sse, sse_reduction)
g.set_graph()
g.write_graph(output_file_name + ".graph")
g.set_partitions()
g.set_values()
g.write_values(output_file_name + ".data")
g.write_statistics(output_file_name + ".stats")
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
if __name__ == "__main__":
sys.exit(main())