-
Notifications
You must be signed in to change notification settings - Fork 0
/
16201018_16200947_16203106.py
353 lines (246 loc) · 11.7 KB
/
16201018_16200947_16203106.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import networkx as nx
import matplotlib.pyplot as plt
from geopy.distance import vincenty
import random
#minimum number of nodes required for communication
# 1- CC; 1- convoy and 1 - Boat
minNumNodes = 3
minTransRange = 300
minViewRange = 50
# total number of times
tn = 50
def set_edges(graph):
for eachNode in graph.nodes():
for nextNode in graph.nodes():
# not same node in two loops and no conncetion
if (eachNode != nextNode) and \
not graph.has_edge(eachNode, nextNode):
# for Command Center node
if(graph.node[eachNode]["type"] == "CC"):
if (graph.node[nextNode]["type"] == "CC") or \
( (graph.node[nextNode]["type"] == "Uboat") \
and (graph.node[nextNode]["listeningZone"] == 0) ):
graph.add_edge(eachNode,nextNode)
graph.edge[eachNode][nextNode]["weight"] = 1
# for Uboat node
elif(graph.node[eachNode]["type"] == "Uboat"):
eachNodeLoc= (graph.node[eachNode]["lat"],graph.node[eachNode]["lon"])
nextNodeLoc= (graph.node[nextNode]["lat"],graph.node[nextNode]["lon"])
if(graph.node[nextNode]["type"] == "Uboat") and \
( vincenty(eachNodeLoc, nextNodeLoc).kilometers <= minTransRange ):
graph.add_edge(eachNode,nextNode)
graph.edge[eachNode][nextNode]["weight"] = 1
elif(graph.node[nextNode]["type"] == "convoy") and \
(vincenty(eachNodeLoc, nextNodeLoc).kilometers <= minViewRange ):
graph.add_edge(eachNode,nextNode)
graph.edge[eachNode][nextNode]["weight"] = 1
return graph
def set_CC(graph, CCNode):
# Sanity checks for input
if(graph.number_of_nodes() < minNumNodes):
raise ValueError("Graph doesnt contain sufficient number of nodes to continue.")
if not graph.has_node(CCNode):
raise ValueError("CC node " + str(CCNode) + " not present")
#CC Setup
graph.node[CCNode]["lat"]=47.74
graph.node[CCNode]["lon"]=-3.36
graph.node[CCNode]["type"] = "CC"
return graph
def set_convoy(graph, ConvoyNode, location):
# Sanity checks for input
if not graph.has_node(ConvoyNode):
raise ValueError("Convoy Node " + str(ConvoyNode) + " not present")
#Convoy setup
graph.node[ConvoyNode]["lat"] = location[0]
graph.node[ConvoyNode]["lon"] = location[1]
graph.node[ConvoyNode]["type"] = "convoy"
return graph
def set_Uboats(graph, numOfUboats, UboatLocations=()):
# Sanity checks for input
if(graph.number_of_nodes() < numOfUboats):
raise ValueError("Graph doesnt contain sufficient number of Uboat nodes to continue.")
# check enough location points sent for Uboats
if (len(UboatLocations) != numOfUboats):
raise ValueError("Enough Locations not provided for Uboats.")
#Uboat setup
UboatCount = 0
for eachNode in graph.nodes():
if(graph.node[eachNode]["type"] == ""):
graph.node[eachNode]["lat"] = UboatLocations[UboatCount][0]
graph.node[eachNode]["lon"] = UboatLocations[UboatCount][1]
graph.node[eachNode]["type"] = "Uboat"
graph.node[eachNode]["listeningZone"] = 1
UboatCount = UboatCount+1
if(UboatCount == numOfUboats):
graph.node[eachNode]["listeningZone"] = 0
break
return graph
def initalize(graph):
for eachNode in graph.nodes():
graph.node[eachNode]["lat"] = 0.0
graph.node[eachNode]["lon"] = 0.0
graph.node[eachNode]["type"] = ""
graph.node[eachNode]["state"] = "receive"
graph.node[eachNode]["listeningZone"] = 0
return graph
def get_graph(numOfNodes=16):
graph = nx.Graph()
# Generate a graph and intialize node attributes
graph.add_nodes_from(range(numOfNodes))
graph = initalize(graph)
#Set Nodes as CC location
graph = set_CC(graph,graph.nodes()[0])
#set Nodes as Uboats
UboatLocations = ([52.48, -59.28], # Node -2
[52.00, -55.78], # Node -3
[54.50, -57.78], # Node -4
[53.68, -54.28], # Node -5
[53.48, -50.58], # Node -6
[53.00, -46.98], # Node -7
[52.98, -43.26], # Node -8
[51.50, -39.65], # Node -9
[52.45, -35.98], # Node -10
[52.43, -32.31], # Node -11
[51.10, -28.67], # Node -12
[54.18, -28.96], # Node -13
[52.91, -35.21], # Node -14
[52.50, -26.00]) # Node -15
numOfUboats = 14
graph = set_Uboats(graph, numOfUboats, UboatLocations)
#set convoy node
graph = set_convoy(graph,graph.nodes()[-1],(52.48,-59.98))
#set edges
graph = set_edges(graph)
return graph
def gen_rand_coord():
lon=random.uniform(-63.44, -5.93)
lat=random.uniform(60.52,42.09)
return((lat,lon))
def gen_rand_coord_dist(ptlat, ptlon, distrange):
flag=0
while(flag!=1):
lon=random.uniform(-63.44, -5.93)
lat=random.uniform(ptlat,42.09)
dist=vincenty((lat,lon),(ptlat,ptlon)).kilometers
if(distrange==50 and (40<=dist<=50)):
flag=1
elif(distrange==300 and (250<=dist<=300)):
flag=1
else:
flag=0
return ((lat,lon))
def draw_graph(graph):
pos = {}
for eachNode in graph.nodes():
pos[eachNode]=(graph.node[eachNode]["lon"],graph.node[eachNode]["lat"])
# get three subsets of nodes: susceptible, infected, removed
convoy_node = [n for n in graph.nodes() if graph.node[n]["type"] == "convoy"]
CC_nodes = [n for n in graph.nodes() if graph.node[n]["type"] == "CC"]
Uboat_lz_nodes = [n for n in graph.nodes() if ( graph.node[n]["type"] == "Uboat" and \
graph.node[n]["listeningZone"] == 1 ) ]
Uboat_nlz_nodes = [n for n in graph.nodes() if ( graph.node[n]["type"] == "Uboat" and \
graph.node[n]["listeningZone"] == 0 ) ]
empty_nodes = [n for n in graph.nodes() if graph.node[n]["type"] == ""]
# draw edges, then draw each subset in different colour
nx.draw_networkx_nodes(graph, pos, nodelist=convoy_node, node_color="r")
nx.draw_networkx_nodes(graph, pos, nodelist=CC_nodes, node_color="c")
nx.draw_networkx_nodes(graph, pos, nodelist=Uboat_lz_nodes, node_color="y")
nx.draw_networkx_nodes(graph, pos, nodelist=Uboat_nlz_nodes, node_color="g")
nx.draw_networkx_nodes(graph, pos, nodelist=empty_nodes, node_color="w")
nx.draw_networkx_labels(graph, pos)
nx.draw_networkx_edges(graph, pos)
nx.draw_networkx_edge_labels(graph, pos)
plt.savefig("UboatSurvival.png")
plt.close()
def draw_graphER(graph):
pos = nx.layout.random_layout(graph)
# get three subsets of nodes: susceptible, infected, removed
convoy_node = [n for n in graph.nodes() if graph.node[n]["type"] == "convoy"]
CC_nodes = [n for n in graph.nodes() if graph.node[n]["type"] == "CC"]
Uboat_lz_nodes = [n for n in graph.nodes() if ( graph.node[n]["type"] == "Uboat" and \
graph.node[n]["listeningZone"] == 1 ) ]
Uboat_nlz_nodes = [n for n in graph.nodes() if ( graph.node[n]["type"] == "Uboat" and \
graph.node[n]["listeningZone"] == 0 ) ]
empty_nodes = [n for n in graph.nodes() if graph.node[n]["type"] == ""]
# draw edges, then draw each subset in different colour
nx.draw_networkx_nodes(graph, pos, nodelist=convoy_node, node_color="r")
nx.draw_networkx_nodes(graph, pos, nodelist=CC_nodes, node_color="c")
nx.draw_networkx_nodes(graph, pos, nodelist=Uboat_lz_nodes, node_color="y")
nx.draw_networkx_nodes(graph, pos, nodelist=Uboat_nlz_nodes, node_color="g")
nx.draw_networkx_nodes(graph, pos, nodelist=empty_nodes, node_color="w")
nx.draw_networkx_labels(graph, pos)
nx.draw_networkx_edges(graph, pos)
plt.savefig("UboatSurvivalER.png")
plt.close()
def get_erdos_renyi_graph(numofNodes=16, edgeProb = 0.2):
oceanGraph = nx.erdos_renyi_graph(numofNodes - minNumNodes, edgeProb)
oceanGraph = initalize(oceanGraph)
#Set Land Graph
landGraph = nx.Graph()
landGraph.add_nodes_from(range(numofNodes-minNumNodes, numofNodes))
landGraph = initalize(landGraph)
landGraph = set_CC(landGraph,landGraph.nodes()[0])
#set uboat outside of listening zone
landGraph.node[landGraph.nodes()[1]]["type"] = "Uboat"
landGraph.node[landGraph.nodes()[1]]["state"] = "receive"
landGraph.node[landGraph.nodes()[1]]["listeningZone"] = 0
#set convoy node
landGraph = set_convoy(landGraph,landGraph.nodes()[-1],gen_rand_coord())
#connecting both graphs
graph = nx.compose(oceanGraph, landGraph)
graph.add_edge(landGraph.nodes()[0],landGraph.nodes()[1])
graph.add_edge(oceanGraph.nodes()[0],landGraph.nodes()[-1])
graph.add_edge(oceanGraph.nodes()[-1],landGraph.nodes()[1])
#Set Uboats in listening zone
for eachNode in oceanGraph.nodes() :
graph.node[eachNode]["type"] = "Uboat"
graph.node[eachNode]["state"] = "standBy"
graph.node[eachNode]["listeningZone"] = 1
return graph
def realWorldScenario(numOfNodes=16):
plt.figure(figsize=(15,15))
graph = get_graph(numOfNodes)
draw_graph(graph)
timeTaken = 0
startNode = graph.nodes()[-1]
print("Real World Scenario : ")
try:
for te in range(tn):
nextNode = nx.dijkstra_path(graph,startNode,graph.nodes()[0])
if(graph.node[nextNode[1]]["state"] != "standBy" ):
graph.node[startNode]["state"] = "transmit"
timeTaken = timeTaken + 1
graph.node[startNode]["state"] = "recieve"
startNode = nextNode[1]
else:
print(nextNode[1], " Node is in standBy state")
print(startNode, " uboat starts travelling out of zone")
break
if(startNode == graph.nodes()[0]):
print("Total time taken to send message - ",timeTaken," units.")
break
except Exception as e:
print("No connection available to complete the graph - " + e.args[0])
print("Nodes count :", len(graph.nodes()))
print("Edge Count : ", len(graph.edges()))
return
def researchQScenario(numOfNodes=17, probability=0.2):
graph = get_erdos_renyi_graph(numOfNodes,probability)
draw_graphER(graph)
#Convoy ship
convoyNode = [n for n in graph.nodes() if graph.node[n]["type"] == "convoy"]
CCNode = [n for n in graph.nodes() if graph.node[n]["type"] == "CC"]
print("Scalable graph Output:")
print("Nodes count :", len(graph.nodes()))
print("Edge Count : ", len(graph.edges()))
try:
print("Shortest Path - " ,nx.dijkstra_path(graph,convoyNode[0],CCNode[0]))
print("Shortest path Length - ",nx.dijkstra_path_length(graph,convoyNode[0],CCNode[0]))
except Exception as e:
print("No connection available to complete the graph - " + e.args[0])
return
if __name__ == "__main__":
# Real time scenario
realWorldScenario(16)
# Probability Version
researchQScenario(17, 0.2)