forked from qinyang-bao/MTE380
-
Notifications
You must be signed in to change notification settings - Fork 0
/
travel_time.py
167 lines (145 loc) · 7.7 KB
/
travel_time.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
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from multiprocessing import Process, Queue
from util import Coord, Location
from heapq import heappop, heappush
# DC shortest pathfinder class
class Pathfinder():
# initialization of layout
def __init__(self, grid: np.ndarray):
self.grid = grid
self.xLim, self.yLim = np.shape(grid)
self.illegalMoves = {Coord(2, 0): Coord(3, 0), Coord(3, 0): Coord(2, 0),
Coord(0, 2): Coord(0, 3), Coord(0, 3): Coord(0, 2),
Coord(1, 2): Coord(1, 3), Coord(1, 3): Coord(1, 2),
Coord(2, 2): Coord(2, 3), Coord(2, 3): Coord(2, 2),
Coord(0, 17): Coord(0, 18), Coord(0, 18): Coord(0, 17),
Coord(1, 17): Coord(1, 18), Coord(1, 18): Coord(1, 17),
Coord(2, 17): Coord(2, 18), Coord(2, 18): Coord(2, 17)}
# checks validity of desired move
def isMoveValid(self, visited: set, cell: Coord, neighbour: Coord) -> bool:
if cell in self.illegalMoves:
return (self.xLim > neighbour.x >= 0 and self.yLim > neighbour.y >= 0 and
self.grid[neighbour.x][neighbour.y] == 0 and
self.illegalMoves[cell] != neighbour and neighbour not in visited)
else:
return (self.xLim > neighbour.x >= 0 and self.yLim > neighbour.y >= 0 and
self.grid[neighbour.x][neighbour.y] == 0 and neighbour not in visited)
# heuristic calculator
def heuristic(self, cell: Coord, goal: Coord) -> int:
# Diagonal distance
# scale factors of 1 = minimum cost to move to adjacent cells
D = 1
D2 = 1
dx = abs(cell.x - goal.x)
dy = abs(cell.y - goal.y)
return D * (dx + dy) + (D2 - 2 * D) * min(dx, dy)
# shortest path search
# a star implementation
def search(self, start: Coord, goal: Coord) -> int:
# possible moves
xTranslation = [-1, 0, 1, 1, 1, 0, -1, -1]
yTranslation = [-1, -1, -1, 0, 1, 1, 1, 0]
# initial cost of move
cost = 0
# fringe priority queue
fringe = []
heappush(fringe, (cost + self.heuristic(start, goal), cost, start))
# stores visited positions
visited = set()
# loop until fringe is empty
while fringe:
# pop item from priority queue fringe
_, currCost, cell = heappop(fringe)
# goal state; goal node reached, search terminated
if cell == goal:
return currCost
# if cell already visited, skip
if cell in visited:
continue
# add current position to visited set
visited.add(cell)
# check for potential neighbours
for i in range(8):
neighbour = Coord(cell.x + xTranslation[i], cell.y + yTranslation[i])
# check validity of each neighbour
if self.isMoveValid(visited, cell, neighbour):
# append neighbour to fringe
heappush(fringe, (currCost + self.heuristic(neighbour, goal),
currCost + 1, neighbour))
class TravelTime(Process):
def __init__(self, travelTimeData: Queue):
Process.__init__(self)
self.travelTimeData = travelTimeData
self.idealTravelTimes = dict()
# initialization of ideal grid, with original unrestricted travel space
grid = [[ 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, -1, -1, -1, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, 0]]
floormap = np.array(grid).transpose(1, 0)
shortestPathfinder = Pathfinder(floormap)
exitPoints = [Location.getCoords(i)[0] for i in range(1, 9)]
entryPoints = [Location.getCoords(i)[1] for i in range(1, 9)]
for exitPoint in exitPoints:
if exitPoint == None:
continue
else:
for entryPoint in entryPoints:
self.idealTravelTimes[(exitPoint, entryPoint)] = shortestPathfinder.search(exitPoint, entryPoint)
del self.idealTravelTimes[(Coord(19, 8), Coord(19, 8))]
for path, time in self.idealTravelTimes.items():
print(f"{path}: {time}")
def autolabel(self, rects, ax):
"""Attach a text label above each bar in *rects*, displaying its height."""
for rect in rects:
height = rect.get_height()
ax.annotate('{}'.format(height),
xy=(rect.get_x() + rect.get_width() / 2, height),
xytext=(0, 3), # 3 points vertical offset
textcoords="offset points",
ha='center', va='bottom')
def run(self):
while True:
simulatedTravelTimes = self.travelTimeData.get()
idealTimes = []
labels, simTimes = simulatedTravelTimes.keys(), simulatedTravelTimes.values()
for state in labels:
idealTimes.append(self.idealTravelTimes[state])
# finding percentage difference btwn sim and ideal times
idealTimeAvg = sum(idealTimes) / len(idealTimes)
simTimeAvg = sum(simTimes) / len(simTimes)
print("Response time: ", simTimeAvg / idealTimeAvg * 100)
x = np.arange(len(labels))
width = 0.35 # the width of the bars
fig, ax = plt.subplots()
rects1 = ax.bar(x - width/2, idealTimes, width, label='Normal')
rects2 = ax.bar(x + width/2, simTimes, width, label='COVID')
# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Num of iterations')
ax.set_title('Normal vs COVID Travel Time')
ax.set_xticks(x)
# ax.set_xticklabels(labels)
ax.legend()
self.autolabel(rects1, ax)
self.autolabel(rects2, ax)
fig.tight_layout()
plt.show()