-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen_network.py
52 lines (48 loc) · 1.71 KB
/
gen_network.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
#!/usr/bin/python
# Generates a GraphML file representing the network with the specified
# topology and properties.
#
# Usage: python gen_network.py <topology> <size> <params> <outfile>
#
# The allowed topologies are:
#
# Complete, Erdos_Renyi, Barabasi_Albert, Powerlaw_Homophilic,
# Powerlaw_Clustered, Random_Regular, Grid_2D, KNN,
# Watts_Strogatz, Random_Geometric, Delaunay
#
# The params argument is a map specifying the parameters for the
# requested network topology. Allowed parameters are:
#
# Complete: {}
# Erdos_Renyi: {"p": ..., "seed": ...}
# Barabasi_Albert: {"m": ..., "seed": ...}
# Powerlaw_Homophilic: {"m": ..., "r": ..., "maxiter": ...,
# "seed": ...}
# Powerlaw_Clustered: {"m": ..., "c": ..., "seed": ...}
# Random_Regular: {"degree": ..., "seed": ...}
# Grid_2D: {"m": ..., "n": ..., "periodic": ...}
# KNN: {"n": ..., "k": ..., "periodic": ...}
# Watts_Strogatz: {"k": ..., "p": ..., "seed": ...}
# Delaunay: {"seed": ...}
# Random_Geometric: {"r": ...}
import networkx, sys
from network import *
def main(args):
if len(args) != 4:
print \
"Usage: python gen_network.py <topology> <size> <params> <outfile>"
sys.exit()
topology = args[0]
size = int(args[1])
params = eval(args[2])
outfile = args[3]
vertices = [Vertex(i) for i in range(0, size)]
net = build_network(vertices, topology, params)
g = net.get_structure()
if g == None:
E = [(u, v) for u in range(size) for v in range(size) if u < v]
g = networkx.Graph()
g.add_edges_from(E)
networkx.write_graphml(g, outfile)
if __name__ == "__main__":
main(sys.argv[1:])