/
NetworkAnalyzer.py
209 lines (169 loc) · 5.13 KB
/
NetworkAnalyzer.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
# # #
# Takes graphs in a file and compiles comparative information on them
#
# Usage: python NetworkAnalyzer.py <filename>
#
# @version 1 - Modified December 8 2015 11:22 AM
# @python-version 2.7
# #
import sys
import numpy as np
import itertools
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import minimum_spanning_tree
def GeneratePaths(distances, minPower, start, end, numNodes):
matrix = [x for x in range(numNodes)]
allPossiblePaths = []
allPossiblePaths.append(itertools.permutations(matrix))
allPossiblePaths = list(itertools.permutations(matrix, len(matrix)))
shortestDistance = 9999999999999
shortestPath = []
for row in allPossiblePaths:
startPos = row.index(start)
endPos = row.index(end)
distance = 0
if startPos > endPos:
temp = startPos
startPos = endPos
endPos = temp
for x in range(startPos,endPos):
if distances[row[x]][row[x+1]] > minPower:
distance = 9999999999999
break
distance = distance + distances[row[x]][row[x+1]]
if distance < shortestDistance:
shortestDistance = distance
shortestPath = row[startPos:endPos+1]
return shortestPath
def RegularControl(network,numNodes):
matrix = [[0 for x in range(numNodes)] for x in range(numNodes)]
for col in range(0,numNodes):
for row in range(0,col+1):
matrix[row][col] = network[row][col]
minTree = minimum_spanning_tree(matrix).toarray().astype(int)
minPower = np.amax(minTree)
transfers = []
for col in range(0,numNodes):
for row in range(col+1, numNodes):
matrix[row][col] = network[col][row]
if network[row][col] != 0:
if network[row][col] == 1:
transfers.append([row, col])
else:
transfers.append([col, row])
noise = []
for row in transfers:
if matrix[row[0]][row[1]] <= minPower:
if row[1] not in noise:
noise.append(row[1])
else:
path = GeneratePaths(matrix, minPower, row[0], row[1], numNodes)
for x in range (1, len(path)):
if path[x] not in noise:
noise.append(path[x])
for row in transfers:
noiseDist = []
for node in noise:
if node == row[1]:
continue
else:
noiseDist.append(1/float(matrix[node][row[1]]))
if sum(noiseDist) == 0:
totalNoise = 1
else:
totalNoise = 1/sum(noiseDist)
if totalNoise < 1:
return -1
return 1
def PowerControl(network,numNodes):
network_fail = 0
nodePower = {}
for row in range(0,numNodes):
nodePower[row] = 0
for row in range(1,numNodes):
for col in range(0,row):
if network[row][col] == 1:
if network[col][row] > nodePower[row]:
nodePower[row] = network[col][row]
if network[row][col] == -1:
if network[col][row] > nodePower[col]:
nodePower[col] = network[col][row]
for entry in nodePower:
receiving = []
if nodePower[entry] == 0:
continue
else:
for row in range(1,numNodes):
if network[row][entry] == -1:
receiving.append(row)
for col in range(0,entry):
if network[entry][col] == 1:
receiving.append(col)
for y in receiving:
nodesReached = []
pTop = 0
pBottom = 0
for x in nodePower:
if nodePower[x] == 0 or x == y:
continue
if network[x][y] <= nodePower[x]:
nodesReached.append(x)
for node in nodesReached:
if node > y:
if network[node][y] == -1:
pTop = pTop + (float(nodePower[node]) / float(network[y][node]))
else:
pBottom = pBottom + (float(nodePower[node]) / float(network[y][node]))
else:
if network[y][node] == -1:
pTop = pTop + (float(nodePower[node]) / float(network[node][y]))
else:
pBottom = pBottom + (float(nodePower[node]) / float(network[node][y]))
if pTop > 0 and pBottom == 0:
continue
if (pTop / pBottom) < 1:
network_fail = 1
if network_fail == 1:
return -1
else:
return 1
def Processor(inputs):
try:
fileName = inputs[0]
except:
print("Missing Input.")
sys.exit(1)
with open("./" + fileName, "r") as inputFile:
with open("./" + fileName.split(".")[0] + "_Output.txt", "w") as outputFile:
getNodes = 0
for line in inputFile:
if getNodes == 0:
numNodes = int(line.split("\n")[0].split(",")[0])
averageDistance = int(line.split("\n")[0].split(",")[1])
inMatrix = [[0 for x in range(numNodes)] for x in range(numNodes)]
getNodes = 1
rowPosition = 0
continue
if line.split("\n")[0] == "<end>":
getNodes = 0
powerResult = PowerControl(inMatrix,numNodes)
regularResult = RegularControl(inMatrix,numNodes)
outputFile.write("Number of Nodes - " + str(numNodes) + "\n")
outputFile.write("Average Distance - " + str(averageDistance) + "\n\n")
if powerResult == -1:
outputFile.write("Power Control Result - Failed\n")
else:
outputFile.write("Power Control Result - Passed\n")
if regularResult == -1:
outputFile.write("Regular Result - Failed\n")
else:
outputFile.write("Regular Result - Passed\n")
outputFile.write("\n--------------------\n\n")
continue
colPosition = 0
for value in line.split("\n")[0].split(","):
inMatrix[rowPosition][colPosition] = int(value)
colPosition = colPosition+1
rowPosition = rowPosition+1
if __name__ == '__main__':
Processor(sys.argv[1:])