-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindTopology.py
executable file
·63 lines (48 loc) · 1.6 KB
/
findTopology.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
"""
Script to generate topology.txt automatically on any given grid size
"""
# find j node's neighbours for grid size D and range rng
def findNeighbourhood(grid, j, D, rng):
# array holding j's neighbours
neighbourhood = []
# find j's position in grid (row,col)
row = j/D
col = j%D
# find j's distance from each node in the grid
for x in range(D):
for y in range(D):
# j's distance from node (x,y)
dist = ((abs(x-row)**2) + (abs(y - col)**2)) ** 0.5
# if distance is within j's range, we have a new neighbour!
if 0 < dist <= rng:
neighbourhood.append(grid[x][y])
# return j's neighbours
return neighbourhood
def findTopology(D , rng):
# Create and fill grid
grid = [[x + y*D for x in range(D)] for y in range(D)]
# configure topology.txt
file = open('topology.txt', 'w')
file.truncate()
# Find each node's neighbours
for j in range(D**2):
neighbours = findNeighbourhood(grid, j, D, rng)
print("node's %d neighbours: " % j)
print(neighbours)
# append connected nodes in topology.txt
for i in range(len(neighbours)):
file.write("%s %s -50.0\n" % (grid[j/D][j%D], neighbours[i]))
file.write('\n')
file.close()
# MAIN
if __name__ == '__main__':
# Wait for input on grid size and range
D = 9
try:
while not (D <= 8 and D > 0) : D = int(input("Give me grid size (max 8): "))
rng = float(input("Give me grid range: "))
# error
except:
D = 8
rng = 1
findTopology(D, rng)