forked from rpitrust/agentsimulation
/
KroneckerGenforSimulations.py
68 lines (56 loc) · 2.17 KB
/
KroneckerGenforSimulations.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
''' Returns a Kronecker Graph for Agent Simulations using BH Kronecker Generator Package
'''
#like graphgen but only for kronecker -BH
import random
import math
import networkx as nx
from KroneckerInitMatrix import InitMatrix
import KroneckerGenerator
import numpy as np
def create_graph_stats(graph):
cc_conn = nx.connected_components(graph) #How is this a generator object!?
num_cc = nx.number_connected_components(graph)
#largest_cc = len(cc_conn[0])
cc = nx.closeness_centrality(graph)
bc = nx.betweenness_centrality(graph)
deg = nx.degree_centrality(graph)
dens = nx.density(graph)
stats = {'cc':cc, 'bc':bc, 'deg':deg, \
'num_cc':num_cc, 'dens':dens}#, 'largest_cc':largest_cc}
return stats
def create_graph_kronecker(objects, properties): #uses alpha-beta method to build kron
initEdges = properties['init_edges']
alpha = properties['alpha']
beta = properties['beta']
k = properties['kron_interations']
tryAgain = True
init = InitMatrix(properties['kron_seed_nodes'])
init.make()
init.addSelfEdges()
for i in range(len(initEdges)):
pos = initEdges[i]
index1 = pos[0]
index2 = pos[1]
if(index1 > properties['kron_seed_nodes'] or index2 > properties['kron_seed_nodes']):
raise IOError("init_edges are out of range of seed matrix size")
else:
init.addEdge(index1, index2)
if(properties['kron_seed_self_loops_stochastic'] == False):
init.makeStochasticAB(alpha, beta, False)
else:
init.makeStochasticAB(alpha, beta)
while(tryAgain == True): #loop to make sure we are connected, should there be a limit to this?
final = KroneckerGenerator.generateStochasticKron(init, k, True)
is_conn = nx.is_connected(final)
if(is_conn == False):
print "Graph was not connected, trying again..."
tryAgain = True
else:
tryAgain = False
stats = create_graph_stats(final)
conn = nx.Graph()
for (i,j) in final.edges():
inode = objects[i]
jnode = objects[j]
conn.add_edge(inode, jnode)
return conn, stats