-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
219 lines (179 loc) · 5.67 KB
/
Copy pathapp.py
File metadata and controls
219 lines (179 loc) · 5.67 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
import json
from pprint import pprint
import collections, enum
import draw
import graph
import itertools
import pygame
import random
import math
import sys
import heapq # Priority queue
from priorityQueue import PriorityQueue
def distanceBetweenPoints(lat1, lon1, lat2, lon2):
"""Returns miles between two points on the earth based on lat/lon (in degrees)."""
r = 6371e3 # earth's radius in meters
p1 = math.radians(lat1)
p2 = math.radians(lat2)
delta_p = math.radians(lat2 - lat1)
delta_l = math.radians(lon2 - lon1)
a = math.sin(delta_p / 2) * math.sin(delta_p / 2) + \
math.cos(p1) * math.cos(p2) * \
math.sin(delta_l / 2) * math.sin(delta_l / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return r * c * 0.0006214 # convert to miles
def graphAllConnected(cities):
g = graph.Graph()
for i, city in enumerate(cities):
city['id'] = i
g.addVertex(i, city)
for existingVertex in g.getVertices():
city2 = existingVertex.payload
if city2['id'] != city['id']:
d = distanceBetweenPoints(
city['latitude'], city['longitude'],
city2['latitude'], city2['longitude']
)
g.addEdge(i, existingVertex.getId(), d)
return g
def graphConnectConditional(cities, connectionCheck):
g = graph.Graph()
for i, city in enumerate(cities):
city['id'] = i
g.addVertex(i, city)
for existingVertex in g.getVertices():
city2 = existingVertex.payload
if city2['id'] != city['id'] and connectionCheck(city, city2):
d = distanceBetweenPoints(
city['latitude'], city['longitude'],
city2['latitude'], city2['longitude']
)
g.addEdge(i, existingVertex.getId(), d)
return g
def dijkstra(g, start):
# print('Finding shortest paths to ' + start.payload['city'])
g.reset()
pq = PriorityQueue()
start.setDistance(0)
pq.buildHeap([(v.getDistance(), v) for v in g])
while not pq.isEmpty():
currentVert = pq.delMin()
for nextVert in currentVert.getConnections():
newDist = currentVert.getDistance() \
+ currentVert.getCost(nextVert)
if newDist < nextVert.getDistance():
# print('Setting {0}s predecessor to {1} (distance {2})'.format(nextVert.payload['city'], currentVert.payload['city'], round(newDist)))
nextVert.setDistance(newDist)
nextVert.parent = currentVert
pq.decreaseKey(nextVert, newDist)
def findCityVertice(graph, cityName):
for v in graph:
if v.payload['city'] == cityName:
return v
return None
def findNearestCity(g, lat, lon):
"""Find city nearest the given latitude and longitude."""
closest = None
minDistance = math.inf
for v in g:
d = distanceBetweenPoints(lat, lon, v.payload['latitude'], v.payload['longitude'])
if d < minDistance:
minDistance = d
closest = v
return closest
Modes = enum.Enum('Modes', 'ALL PARTIAL')
mode = Modes.ALL
if __name__ == '__main__':
populationThreshold = 75000
if mode == Modes.PARTIAL:
citiesFile = 'cities_partial.json'
distanceThreshold = 500
fps = 5
else:
citiesFile = 'cities.json'
distanceThreshold = 190
fps = 60
with open(citiesFile) as f:
cities = json.load(f)
cities = [city for city in cities if int(city['population']) > populationThreshold]
# g = graphAllConnected(cities)
def withinMiles(city1, city2):
return distanceBetweenPoints(
city1['latitude'], city1['longitude'],
city2['latitude'], city2['longitude']
) < distanceThreshold
g = graphConnectConditional(cities, withinMiles)
############################################################################
# Below here is the graphics
# Display
screen = pygame.display.set_mode(draw.size)
clock = pygame.time.Clock()
pygame.font.init()
font = pygame.font.SysFont('monospace', 15)
def highlightEdge(v1, v2):
return False
distance = v1.getCost(v2)
if distance < 1000:
return True
return False
draw.drawCities(g, highlightEdge, screen, font)
pygame.display.flip()
# initialize search
# bfs = g.breadthFirstSearch(start)
# dfs = g.depthFirstSearch(start)
start = findCityVertice(g, 'Boston')
dijkstra(g, start)
destination = findCityVertice(g, 'Denver')
done = False
finishedDrawing = False
listenForDestination = False
v = destination
colors = itertools.cycle(draw.highlightColors)
color = draw.GREEN
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print('Quitting!')
pygame.display.quit()
pygame.quit()
done = True
continue
elif event.type == pygame.MOUSEBUTTONDOWN:
lat, lon = draw.coordsToPoint(event.pos[0], event.pos[1])
u = findNearestCity(g, lat, lon)
if listenForDestination:
v = u
listenForDestination = False
city = v.payload
print('From ' + city['city'])
x, y = draw.pointToCoords(city['latitude'], city['longitude'])
pygame.draw.circle(screen, color, (x, y), 10)
else:
start = u
dijkstra(g, start)
listenForDestination = True
color = next(colors)
city = start.payload
print('Going to ' + city['city'])
x, y = draw.pointToCoords(city['latitude'], city['longitude'])
pygame.draw.circle(screen, color, (x, y), 10)
# process vertex u here
# u = next(bfs, None)
# if u and u.parent:
# draw.lineBetweenCities(u.payload, u.parent.payload, screen, draw.RED)
# Draw shortest path from destination to start
# elif not g.searching and not finishedDrawing:
# v = destination
# while v.parent is not None:
# old = v
# new = v.parent
# draw.lineBetweenCities(old.payload, new.payload, screen, draw.GREEN)
# v = new
# finishedDrawing = True
if v.parent is not None and not listenForDestination:
old = v
new = v.parent
draw.lineBetweenCities(old.payload, new.payload, screen, color)
v = new
pygame.display.flip()
clock.tick(fps)