-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTB_help.py
More file actions
300 lines (260 loc) · 10.4 KB
/
Copy pathSTB_help.py
File metadata and controls
300 lines (260 loc) · 10.4 KB
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
299
300
import numpy
import math
import os
import random
from constants import *
# Get minimum of spectrum bandwidths available at two nodes i and j at time t
# This is important because we can only use the common channels (available at both the nodes) as the total bandwidth of the band
# Moreover, note that here we assumed that the channels available at the node (with lower bandwidth) is also existent at the node
# with higher bandwidth of a certain band
def getMinBWFromDMFiles(directory, i, j, s, t):
with open(directory + "/" + str(i) + ".txt") as fi:
next(fi)
iLines = fi.readlines()
fi.close()
with open(directory + "/" + str(j) + ".txt") as fj:
next(fj)
jLines = fj.readlines()
fj.close()
currBW = 0
for iLine in iLines:
iLineArr = iLine.strip().split(' ')
for jLine in jLines:
jLineArr = jLine.strip().split(' ')
# print ("At time " + str(t) + " Values: " + iLineArr[0] + " " + jLineArr[0])
if int(iLineArr[0]) == t and int(jLineArr[0]) == t:
# print("At time " + str(t) + " Values: " + iLineArr[3] + " " + jLineArr[3])
if int(iLineArr[s + 3]) < int(jLineArr[s + 3]):
currBW = int(iLineArr[s + 3])
return currBW
else:
currBW = int(jLineArr[s + 3])
return currBW
# else:
# print (str(t) + " " + iLineArr[0] + " " + jLineArr[0])
return currBW
# Compute tau defined as the least message transmission delay in transmitting a message of least size over the spectrum band with
# highest bandwidth across all times t = 0, 1, .... T
def computeTau():
return 1
# Get the dynamic bandwidth of any given band in the set S, between any node pair at any time epoch t
def getSpecBW(V, S, T):
specBW = numpy.zeros(shape=(V, V, numSpec, T)) # Initialize the dynamic spectrum bandwidth
for i in range(V):
for j in range(V):
for s in range(len(S)):
for t in range(0, T, tau):
specBW[i, j, s, t] = minBW[s]
# getMinBWFromDMFiles(directory, i, j, s, t) # P_comment : What does this function do?
# print ("SpecBW: i= " + str(i) + " j= " + str(j) + " s= " + str(s) + " t= " + str(t) + " BW= " + str(specBW[i, j, s, t]))
return specBW
# Check if a pair of nodes i and j are sufficienctly in communication range over any band type s, starting at time ts until time te
def createLinkExistenceADJ_testing():
LINK_EXISTS = numpy.empty(shape=(max_nodes, max_nodes, numSpec, T))
LINK_EXISTS.fill(math.inf)
for i in range(max_nodes):
for s in S:
for t in range(T - 1, 1):
LINK_EXISTS[i, i, s, t + 1] = 1
# t = [0,1]
# LINK_EXISTS[0, 1, 0, 0] = 1
# LINK_EXISTS[1, 0, 0, 0] = 1
# LINK_EXISTS[0, 1, 1, 0] = 1
# LINK_EXISTS[1, 0, 1, 0] = 1
# LINK_EXISTS[1, 3, 0, 0] = 1
# LINK_EXISTS[3, 1, 0, 0] = 1
#
# #t = [1,2]
# LINK_EXISTS[1, 3, 0, 1] = 1
# LINK_EXISTS[3, 1, 0, 1] = 1
# LINK_EXISTS[1, 3, 1, 1] = 1
# LINK_EXISTS[3, 1, 1, 1] = 1
# LINK_EXISTS[2, 3, 0, 1] = 1
# LINK_EXISTS[3, 2, 0, 1] = 1
# LINK_EXISTS[2, 3, 1, 1] = 1
# LINK_EXISTS[3, 2, 1, 1] = 1
#
# # t= [2,3]
# LINK_EXISTS[0, 1, 0, 2] = 1
# LINK_EXISTS[1, 0, 0, 2] = 1
# LINK_EXISTS[1, 3, 0, 2] = 1
# LINK_EXISTS[3, 1, 0, 2] = 1
# LINK_EXISTS[2, 3, 0, 2] = 1
# LINK_EXISTS[3, 2, 0, 2] = 1
# LINK_EXISTS[2, 3, 1, 2] = 1
# LINK_EXISTS[3, 2, 1, 2] = 1
#
# # t = [3,4]
# LINK_EXISTS[0, 3, 0, 3] = 1
# LINK_EXISTS[3, 0, 0, 3] = 1
# LINK_EXISTS[0, 3, 1, 3] = 1
# LINK_EXISTS[3, 0, 1, 3] = 1
# LINK_EXISTS[2, 3, 0, 3] = 1
# LINK_EXISTS[3, 2, 0, 3] = 1
# t = [0,1]
LINK_EXISTS[0, 1, 0, 0] = 1
LINK_EXISTS[0, 1, 1, 0] = 1
LINK_EXISTS[0, 2, 0, 0] = 1
LINK_EXISTS[0, 2, 1, 0] = 1
LINK_EXISTS[0, 4, 2, 0] = 1
LINK_EXISTS[1, 2, 0, 0] = 1
LINK_EXISTS[1, 0, 0, 0] = 1
LINK_EXISTS[1, 0, 1, 0] = 1
LINK_EXISTS[2, 0, 0, 0] = 1
LINK_EXISTS[2, 0, 1, 0] = 1
LINK_EXISTS[2, 1, 0, 0] = 1
LINK_EXISTS[2, 3, 1, 0] = 1
LINK_EXISTS[3, 2, 1, 0] = 1
LINK_EXISTS[4, 0, 2, 0] = 1
# t = [1,2]
LINK_EXISTS[0, 2, 0, 1] = 1
LINK_EXISTS[0, 3, 2, 1] = 1
LINK_EXISTS[0, 4, 1, 1] = 1
LINK_EXISTS[0, 4, 2, 1] = 1
LINK_EXISTS[1, 2, 1, 1] = 1
LINK_EXISTS[1, 2, 2, 1] = 1
LINK_EXISTS[2, 0, 0, 1] = 1
LINK_EXISTS[2, 1, 1, 1] = 1
LINK_EXISTS[2, 1, 2, 1] = 1
LINK_EXISTS[2, 3, 1, 1] = 1
LINK_EXISTS[3, 0, 2, 1] = 1
LINK_EXISTS[3, 2, 1, 1] = 1
LINK_EXISTS[3, 4, 2, 1] = 1
LINK_EXISTS[4, 0, 2, 1] = 1 # P_comment : Was 4, 0 ,0, 1, 2 mistake i think. changed it to 4, 0, 2, 1, 2
LINK_EXISTS[4, 0, 1, 1] = 1
LINK_EXISTS[4, 3, 2, 1] = 1
# t = [2,3]
LINK_EXISTS[0, 3, 0, 2] = 1
LINK_EXISTS[0, 3, 1, 2] = 1
LINK_EXISTS[0, 3, 2, 2] = 1
LINK_EXISTS[0, 4, 0, 2] = 1
LINK_EXISTS[0, 4, 1, 2] = 1
LINK_EXISTS[0, 4, 2, 2] = 1
LINK_EXISTS[1, 2, 0, 2] = 1
LINK_EXISTS[2, 1, 0, 2] = 1
LINK_EXISTS[3, 0, 0, 2] = 1
LINK_EXISTS[3, 0, 1, 2] = 1
LINK_EXISTS[3, 0, 2, 2] = 1
LINK_EXISTS[3, 4, 0, 2] = 1
LINK_EXISTS[3, 4, 1, 2] = 1
LINK_EXISTS[3, 4, 2, 2] = 1
LINK_EXISTS[4, 0, 0, 2] = 1
LINK_EXISTS[4, 0, 1, 2] = 1
LINK_EXISTS[4, 0, 2, 2] = 1
LINK_EXISTS[4, 3, 0, 2] = 1
LINK_EXISTS[4, 3, 1, 2] = 1
LINK_EXISTS[4, 3, 2, 2] = 1
# t = [3,4]
LINK_EXISTS[0, 1, 0, 3] = 1
LINK_EXISTS[0, 3, 1, 3] = 1
LINK_EXISTS[1, 0, 0, 3] = 1
LINK_EXISTS[1, 3, 0, 3] = 1
LINK_EXISTS[1, 3, 2, 3] = 1
LINK_EXISTS[2, 4, 1, 3] = 1
LINK_EXISTS[3, 0, 1, 3] = 1
LINK_EXISTS[3, 1, 0, 3] = 1
LINK_EXISTS[3, 1, 2, 3] = 1
LINK_EXISTS[4, 2, 1, 3] = 1
# t = [4,5]
LINK_EXISTS[0, 1, 0, 4] = 1
LINK_EXISTS[0, 1, 1, 4] = 1
LINK_EXISTS[1, 0, 0, 4] = 1
LINK_EXISTS[1, 0, 1, 4] = 1
LINK_EXISTS[1, 3, 2, 4] = 1
LINK_EXISTS[2, 3, 0, 4] = 1
LINK_EXISTS[3, 1, 2, 4] = 1
LINK_EXISTS[3, 2, 0, 4] = 1
LINK_EXISTS[3, 4, 0, 4] = 1
LINK_EXISTS[4, 3, 0, 4] = 1
return LINK_EXISTS
def funHaversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = math.sin(dlat/2)**2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon/2)**2
c = 2 * math.asin(math.sqrt(a))
# Radius of earth in kilometers is 6371
m = 1000 * 6371* c
# print(" dist: " + str(km))
return m
def euclideanDistance(coor1X, coor1Y, coor2X, coor2Y):
return (math.sqrt((float(coor1X) - float(coor2X))**2 + (float(coor1Y) - float(coor2Y))**2))
def isFile(object):
try:
os.listdir(object) # tries to get the objects inside of this object
return False # if it worked, it's a folder
except Exception: # if not, it's a file
return True
def findfiles(directory):
# if directory is not 'DieselNet-2007/gps_logs/.DS_Store':
objects = os.listdir(directory) # find all objects in a dir
files = []
for i in objects: # check if very object in the folder ...
if isFile(directory + i): # ... is a file.
files.append(i) # if yes, append it.
return files
def print5d(adj):
#print("i j ts m x")
for i in range(len(adj)):
for j in range(len(adj[0])):
for s in range(len(adj[0][0])):
for ts in range(len(adj[0][0][0])):
for te in range(len(adj[0][0][0][0])):
if adj[i, j, s, ts, te] != math.inf:
print(str(i) + " " + str(j) + " " + str(s) + " " + str(ts) + " " + str(te) + " = " + str(adj[i, j, s, ts, te]))
def print4d(adj, adj2):
# print("i j ts m x")
for i in range(len(adj)):
for j in range(len(adj[0])):
for ts in range(len(adj[0][0])):
for m in range(len(adj[0][0][0])):
if (adj[i, j, ts, m] != math.inf or adj2[i, j, ts, m]!= math.inf):
print(str(i) + " " + str(j) + " " + str(ts) + " " + str(M[m]) + " = " + str(adj[i, j, ts, m]) + " " + str(adj2[i, j, ts, m]))
def save_4D_in_file(filename, adj):
#adj[i, j, s, t] for specBW, adj[i, j, t, m] for ADJ_T
with open(filename, "w") as f:
f.write("#i j s t\n")
for i in range(len(adj)):
for j in range(len(adj[0])):
for s in range(len(adj[0][0])):
for t in range(len(adj[0][0][0])):
if adj[i, j, s, t] != math.inf and i != j and adj[i, j, s, t] != -1:
f.write(str(i) + " " + str(j) + " " + str(s) + " " + str(t) + " = " + str(adj[i, j, s, t]) + "\n")
f.close()
def save_5D_in_file(filename, adj):
with open(path_to_folder + filename, "w") as f:
f.write("#i j t dt m\n")
for i in range(len(adj)):
for j in range(len(adj[0])):
for t in range(len(adj[0][0])):
for dt in range(len(adj[0][0][0])):
for m in range(len(adj[0][0][0][0])):
if adj[i, j, t, dt, m] != math.inf and i != j:
f.write(str(i) + " " + str(j) + " " + str(t) + " " + str(dt) + " " + str(m) + " = " + str(adj[i, j, t, dt, m]) + "\n")
f.close()
def save_in_file(filename, adj):
with open(filename, "w") as f:
f.write("#i j s ts \n")
for i in range(len(adj)):
for j in range(len(adj[0])):
for s in range(len(adj[0][0])):
for ts in range(len(adj[0][0][0])):
# for te in range(len(adj[0][0][0])):
if (adj[i, j, s, ts] != math.inf and i != j):
f.write(str(i) + " " + str(j) + " " + str(s) + " " + str(ts) + " = " + str(
adj[i, j, s, ts]) + "\n")
f.close()
def save_co_ordinates_in_file(filename, x_coords, y_coords):
with open(filename, "w") as f:
f.write
def print3D(adj):
for i in range(len(adj)):
for j in range(len(adj[0])):
for t in range(len(adj[0][0])):
print(str(i) + " " + str(j) + " " + str(t) + " = " + str(adj[i, j, t]))