-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scenerio.py
198 lines (150 loc) · 6.96 KB
/
scenerio.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
import numpy as np
from sensor import *
import sim
import networkx as nx
from helper_func import *
from dsp_memo import DspMemo
class Scenerio:
def __init__(self,
graph,
dsp_memo,
all_stops,
all_routes,
routes_per_stop,
sensor_count,
):
self.G = graph
self.all_routes = all_routes
self.sensor_count = sensor_count
self.routes_per_stop = routes_per_stop
self.error = 0
self.routes_per_gateway = None
self.gateways_per_route = None
self.route_subgraphs = None
self.all_gateways = None
self.all_sensors = None
self.sensor_objects = {}
self.dsp_memo = dsp_memo
self.place_sensor(all_stops)
def place_sensor(self, all_stops, sensors=None):
if sensors is None:
self.randomly_select_sensor_locations(all_stops)
self.assign_sensors_to_nodes()
self.generate_sensors()
self.generate_route_subgraphs()
def calculate_penalty_reduction(self, gateways):
self.place_gateways(gateways)
return self.run_simulation()
def place_gateways(self, gateways):
# gateways here is asumed to be G.name_node
self.all_gateways = {get_stopid(gateway) for gateway in gateways}
self.routes_per_gateway = {gateway: self.routes_per_stop[gateway] for gateway in self.all_gateways} #defaultdict(set)
self.gateways_per_route = invert_dict(self.routes_per_gateway)
self.assign_gateways_to_nodes(gateways)
def assign_gateways_to_nodes(self, gateways):
attr = {gw: True for gw in gateways}
nx.set_node_attributes(self.G, name='is_gateway', values=attr)
def randomly_select_sensor_locations(self, all_stops):
self.all_sensors = np.random.choice(all_stops, size=self.sensor_count, replace=False)
def assign_sensors_to_nodes(self):
attr = {sensor: True for sensor in self.all_sensors}
nx.set_node_attributes(self.G, name='is_sensor', values=attr)
def generate_sensors(self):
msg_gen_rate = np.random.randint(low=sim.msg_gen_rate_range[0], high=sim.msg_gen_rate_range[1],
size=self.sensor_count) # 10mins to 12 hours
start_time = np.random.randint(low=sim.msg_gen_rate_range[0], high=sim.msg_gen_rate_range[1],
size=self.sensor_count) # 0 to 1 hour
np.random.shuffle(start_time)
for i, sensor_name in enumerate(self.all_sensors):
# TODO:: add routes_per_stop
r = self.routes_per_stop[get_stopid(sensor_name)]
s = OnRouteSensor(name=sensor_name, routes=r, start_time=start_time[i],
msg_gen_rate=msg_gen_rate[i], msg_ttl=None, data_size=None)
self.sensor_objects[sensor_name] = s
self.assign_sensors_to_nodes()
self.generate_route_subgraphs()
def generate_route_subgraphs(self):
self.route_subgraphs = {}
stops_per_route = invert_dict(self.routes_per_stop)
for r in self.all_routes:
sub_nodes = [namify_stop(self.G.name, s) for s in stops_per_route[r]]
sub_graph = self.G.subgraph(sub_nodes).copy()
self.route_subgraphs[r] = sub_graph
def run_simulation(self):
total_delay = 0
total_generated = 0
for time in range(int(sim.start / 60), sim.duration + 1):
for name, sensor in self.sensor_objects.items():
if sensor.generate_msg(time):
total_generated += 1
routes = self.routes_per_stop[get_stopid(sensor.name)]
# change time to secs
delay = self.calculate_delay(routes, sensor, time * 60)
if delay is None:
total_delay += sim.upper_bound_delay
else:
total_delay += delay
return total_delay/total_generated
#print(self.error)
def calculate_delay(self, routes, sensor, time):
"""
find shortest path from sensor node to a gateway node in the graph, weight is edge cost,
while factoring in duration from current time to next next dept time for that edge.
save gen_time and latency to sensor object
remember departure time, distance is in seconds
while "time", gen_time,start_time is in minutes.
so remember to convert it.
"""
# print(routes)
# print(self.all_gateways)
# print(self.routes_per_gateway)
# print(self.gateways_per_route)
# return 0
import sys
if not self.all_gateways:
print("no gateways selected")
return None
waiting_time = None
shortest_distance, shortest_path = sys.float_info.max, None # to any gateway
for r in routes:
for gateway in self.gateways_per_route[r]:
g = self.route_subgraphs[r].copy()
wait_time = None
try:
distance, path = self.dsp_memo.getDsp(g, r, sensor.name, namify_stop(self.G.name, gateway))
# distance, path = nx.single_source_dijkstra(g, sensor.name, namify_stop(self.G.name, gateway),
# weight='length')
except Exception as e:
continue
while len(path) > 1:
'''
make sure then you limit duration to 24 hours. later if time is greater than 24
message is not delivered
'''
# TODO:: error rate too high.. fix it.
# print(path)
departure_list = g[sensor.name][path[1]][0]['departure_time'].get(r, None)
# print(departure_list)
if departure_list == None:
# print("no departure time found")
break
# g.remove_node(path[1])
# continue
else:
wait_time = get_time_to_next_departure(current_time=time, departure_list=departure_list)
break
if wait_time != None:
#print (distance, wait_time)
if distance + wait_time < shortest_distance:
shortest_distance, shortest_path = distance + wait_time, path
waiting_time = wait_time
# break
if waiting_time == None:
shortest_distance = None
self.error += 1
# sensor.gen_times.append(time) # in sec
# sensor.msg_latencies.append(shortest_distance) # in sec
# sensor.waiting_time.append(waiting_time)
# sensor.hops.append(shortest_path)
#print(shortest_distance, self.error)
return shortest_distance