-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerateTopology.py
executable file
·298 lines (252 loc) · 9.09 KB
/
GenerateTopology.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
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
#!/usr/bin/env python3
from optparse import OptionParser
import numpy as np
import random
import math
import sys
import json
from collections import OrderedDict
import networkx as nx
#Global arguments
args = None
#Parse commandline arguments
def parseOptions():
parser = OptionParser()
#Width
parser.add_option("-x", "--width", action="store", type="int",
dest="width", default=500, help="Width of topology in meters. Default: 100")
#Height
parser.add_option("-y", "--height", action="store", type="int",
dest="height", default=500, help="Height of topology in meters. Default: 100")
#Spacing
parser.add_option("-s", "--space", action="store", type="int",
dest="spacing", default=10,
help="Minimum spacing between nodes (access points). Default: 10")
#Number of nodes
parser.add_option("-n", "--nodes", action="store", type="int",
dest="nodes", default=128, help="Number of nodes. Default: 128")
parser.add_option("-o", "--output", action="store", type="string",
dest="output", default="topology.json", help="Output filename")
parser.add_option("-d", "--dbithresh", action="store", type="int",
dest="thresh", default=87, help="Absolute value. Threshold to consider neighbours non-interfering")
return parser.parse_args()[0]
class Node:
_ssid = None
_channel = None
_neighbours = None
gg_neighbourMembers = None
_freq = 0
_constant = -27.55
_minimumInterference = 0
group = None
name = None
x = 0
y = 0
def __init__(self, posx, posy, n, freq, thresh, name = None):
self._neighbours = []
#Data structure
self.edges = {}
self._minimumInterference = thresh*-1
self.x = posx
self.y = posy
self._freq = freq
if (name == None):
self.name = "NODE" + str(n)
else:
self.name = name
self._ssid = self.name
def __hash__(self):
return hash(self.name)
#def __eq__(self, other):
#return self.name == other.name
def rssiNeighbour(self, node):
for n in self._neighbours:
if n["ssid"] == node.name:
return n["dbi"]
return None
def distanceTo(self, node):
x = node.x - self.x
y = node.y - self.y
return math.sqrt(x**2+y**2)
def getNeighbourCount(self):
return len(self._neighbours)
def getMostDisturbing(self, nodeList=None):
highest = -100
nodeinfo = None
if nodeList == None:
for n in self._neighbours:
if n["dbi"] > highest and n["obj"].group.name != self.group.name and not n["obj"].group.locked:
nodeinfo = n
highest = n["dbi"]
else:
for n in self._neighbours:
if n["dbi"] > highest and n["obj"] not in nodeList and not n["obj"].group.locked:
nodeinfo = n
highest = n["dbi"]
return nodeinfo
def getLeastDisturbingCompanion(self):
lowest = 100
nodeinfo = None
for n in self._neighbours:
if n["dbi"] < lowest and n["obj"].group.name == self.group.name:
lowest = n["dbi"]
nodeinfo = n
return nodeinfo
def getDBSum(self):
sum = 0;
for n in self._neighbours:
sum += n["dbi"]
return sum
def calculateInterferenceTo(self, nodeObject):
if self == nodeObject:
return None
dist = round(self.distanceTo(nodeObject))
if (dist == 0):
dBi = -40
else:
dBi = self.measureDbi(dist)*-1
if (dBi > self._minimumInterference):
self._neighbours.append({"ssid": nodeObject._ssid, "dbi": dBi, "obj:": nodeObject})
return dBi
#print("Distance:", dist, "m, dBi:", dBi)
def getData(self):
data = OrderedDict()
data.update(posX = self.x)
data.update(posY = self.y)
data.update(frequency = self._freq)
data.update(ssid = self.name)
data.update(neighbourCount = len(self._neighbours))
data.update(neighbours = {})
for i in range(len(self._neighbours)):
data["neighbours"].update({i : {"ssid" : self._neighbours[i]["ssid"],
"dbi" : self._neighbours[i]["dbi"]}})
return data
def measureDbi(self, dist):
return (20 * math.log(self._freq, 10)) + (20 * math.log(dist, 10)) + self._constant
def __str__(self):
return self.name
def __repr__(self):
return self.name
def __unicode__(self):
return self.name
class Topology:
_map = {}
_width = None
_height = None
_spacing = None
_nodes = []
_nodesDict = {}
_nodeCount = 0
_frequency = 2437
_thresh = 0
_minimumRadiusVectors = None
premadeNodes = None
def __init__(self, width, height, spacing, nodeCount, dbThresh, premadeNodes = None):
self._thresh = dbThresh
self._width = width
self._height = height
self._spacing = spacing
self.premadeNodes = premadeNodes
if (premadeNodes != None):
self._nodeCount = len(premadeNodes)
else:
self._nodeCount = nodeCount
def newTopology(self):
#self.initMap()
self.createMinimumRadiusVectors()
self.populateMap()
self.measureInterference()
def getNodeCount(self):
return self._nodeCount
def getNodes(self):
return self._nodes;
def initMap(self):
print("Generated topo")
print("Generated filled topo with None objects")
def measureInterference(self):
print("> Calculating interference between all nodes.")
i = 0
printPercentage = 5
percentageMark = len(self._nodes) / (100 / 5)
for nodeSubject in self._nodes:
if (i % percentageMark == 0):
print(" *", i, "of", len(self._nodes), "nodes done.")
for nodeObject in self._nodes:
nodeSubject.calculateInterferenceTo(nodeObject)
i += 1
def createNode(self, posx, posy, nodeNumber, nodeFreq, nodeDbiThresh, name=None):
node = Node(posx, posy, nodeNumber, nodeFreq, nodeDbiThresh, name=name)
try:
self._map[posy][posx] = node
except KeyError:
self._map[posy] = {}
self._map[posy][posx] = node
self._nodes.append(node)
def generateRandomNodes(self):
nodeCount = 0
for i in range(self._nodeCount):
while 1:
y = random.randint(0, self._height - 1)
x = random.randint(0, self._width - 1)
if self.isPositionAvailable(x, y) == True:
self.createNode(x, y, nodeCount, self._frequency, self._thresh)
nodeCount += 1
break
def placeExistingNodes(self):
nodeCount = 0
print("> Placing nodes in topology.")
for n in self.premadeNodes:
self.createNode(n['x'], n['y'], nodeCount, self._frequency, self._thresh)
nodeCount += 1
print("> Nodes placed.")
def populateMap(self):
if self.premadeNodes == None:
self.generateRandomNodes()
else:
self.placeExistingNodes()
def isPositionAvailable(self, testx, testy):
for pos in self._minimumRadiusVectors:
x = testx + pos[0]
y = testy + pos[1]
node = None
try:
node = self._map[y][x]
except KeyError:
return True
return False
def createMinimumRadiusVectors(self):
"""Creates a list of relative positions to a node, where
no other node can be placed because of the minimum spacing
between nodes."""
positions = []
for i in range(-self._spacing, self._spacing + 1):
for j in range(-self._spacing, self._spacing + 1):
dist = math.sqrt(i**2+j**2)
if dist <= self._spacing:
positions.append((i, j))
self._minimumRadiusVectors = positions
def printTopology(self):
for y in range(len(self._map)):
print(self._map[y])
def writeData(self, outfile):
data = OrderedDict()
data.update(mapWidth = self._width)
data.update(mapHeight = self._height)
data.update(nodeCount = self._nodeCount)
allnodes = {}
for i in range(self._nodeCount):
allnodes.update({i : self._nodes[i].getData()})
data.update(nodes = allnodes)
j = json.dumps(data, indent=2)
f = open(outfile, "w")
f.write(j)
f.close()
print("> Topology data written to file: ", outfile)
def main():
args = parseOptions()
print("Width: ", args.width, " height: ", args.height, "spacing: ", args.spacing)
topo = Topology(args.width, args.height, args.spacing, args.nodes, args.thresh)
topo.newTopology()
topo.writeData(args.output)
if __name__ == "__main__":
main()