-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
executable file
·117 lines (100 loc) · 3.77 KB
/
__main__.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
#!/usr/bin/env python3
"""Main entry point to execute the simulations.
"""
import configparser
import os
import sys
from graph import Graph
from log import write as logwrite
from prob import Probability
from simul import simulate
from walk import Walk
# Load the setting from configuration file.
FILENAME = 'rrwg.conf'
config = configparser.ConfigParser()
if os.path.exists(FILENAME):
config.read(FILENAME)
else:
print('ERROR: Create a configuration file named "{}" '.format(FILENAME)+
'in the current directory. \n'
'See rrwg.conf.example in the project directory or '+
'https://github.com/aholanda/rrwg/blob/main/rrwg.conf.example '+
'for an example of configuration file.')
sys.exit(-1)
if __name__ == "__main__":
#Initialize a graph and walks after using the parameters from the
#configuration file.
walks = []
if 'type' in config['default']:
gtype = config['default']['type']
else:
MSG = """graph "type" is not defined in {}.
Graph "type" should be:
complete: complete graph with no partitions
partitions: non-complete graph with partitions
\t\tof complete subgraphs.""".format(FILENAME)
sys.exit('panic: {}'.format(MSG))
logwrite('type={}'.format(gtype))
# Number of vertices
if 'vertices' in config['default']:
nverts = int(config['default']['vertices'])
else:
sys.exit('panic: number of "vertices" was not set in {}'
.format(FILENAME))
logwrite('vertices={}'.format(nverts))
if gtype == 'complete':
func = config['default']['function']
prob = Probability(func)
graph = Graph(nverts)
for i in graph.vertices():
# The walk can traverse all vertices.
walk = Walk(graph.vertices(), i)
walks.append(walk)
elif gtype == 'partitions':
# For paritions function is always POWER
prob = Probability('POW')
if 'partition_size' in config['default']:
partsize = int(config['default']['partition_size'])
else:
sys.exit('panic: "partition_size" was not set in {}.'
.format(FILENAME))
if partsize > nverts:
sys.exit('panic: number of partitions > number of vertices')
graph = Graph(nverts, complete=False,
partition_size=partsize)
for i in graph.vertices():
# Each walk starts at the vertex with
# the same id and the number of partitions
# is the range where it can walk. For example,
# a walk with id 2 starts at v2, and if the
# number of partitions is 2, it can walk at
# v2 and v3.
# All subgraphs are complete.
# Partitions in terms of vertices.
part = graph.partition(i)
# Make the edges
for j in part:
graph.add_edge(i, j)
walk = Walk(part, i)
walks.append(walk)
else:
sys.exit('panic: unknown graph type "{}" in {}'
.format(gtype, FILENAME))
if 'alpha' in config['default']:
prob.alpha = float(config['default']['alpha'])
if 'epsilon' in config['default']:
prob.epsilon = float(config['default']['epsilon'])
logwrite('function={}\nalpha={}\nepsilon={}'
.format(prob.function_name(),
prob.alpha,
prob.epsilon))
if 'time' in config['default']:
nsteps = int(config['default']['time'])
else:
sys.exit('panic: "time" steps was not set in {}.'
.format(FILENAME))
SEED = None
if 'seed' in config['default']:
SEED = int(config['default']['seed'])
logwrite('seed={}'.format(SEED))
simulate(nsteps, graph, walks, prob, SEED)