forked from tinuh/qRoute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaps.py
115 lines (90 loc) · 3.38 KB
/
maps.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
import requests
import json
def closestOpenNode(graph,n,openNodes):
"""
takes in a graph, starting node, and visited dictionary and finds the closest avalible node to n
graph: weighted graph
n: root node
openNodes: dictionary of open nodes
"""
c = -1
for i in openNodes:
if (graph[n][i] < graph[n][c] or c == -1) and i != n:
c = i
return c
def greedyFlexibleTurnPriority(graph,n,maxGap):
"""
simple greedy algorithm that updates the path with the shortest best next move as well as the fewest nodes within a range in the path and recalculates the best next moves
graph: weighted graph
n: number of paths to take
maxGap: amount bigger one path is allowed to go from another
"""
paths = []
distances = []
nextDistance = []
nextNode = []
openNodes = {}
for i in range(len(graph)):
openNodes[i] = True
del openNodes[0]
for i in range(n):
paths.append([0])
distances.append(0)
nextNode.append(closestOpenNode(graph,0,openNodes))
nextDistance.append(graph[0][nextNode[i]])
node = 0
while len(openNodes) > 0:
minLength = -1
for i in range(n):
if minLength == -1 or len(paths[i]) < minLength:
minLength = len(paths[i])
nextPathDists = {}
for i in range(n):
if len(paths[i]) <= minLength + maxGap:
nextPathDists[i] = nextDistance[i]
node = -1
for i in nextPathDists:
if node == -1 or nextPathDists[i] < nextPathDists[node]:
node = i
last = paths[node][len(paths[node])-1]
paths[node].append(nextNode[node])
distances[node] += graph[last][nextNode[node]]
del openNodes[nextNode[node]]
dest = nextNode[node]
for i in range(n):
if nextNode[i] == dest:
nextNode[i] = closestOpenNode(graph,paths[i][len(paths[i])-1],openNodes)
nextDistance[i] = graph[paths[i][len(paths[i])-1]][nextNode[i]]
return paths, distances
def convertAddressesToCoordinates(addresses):
BASE_URL = 'https://maps.googleapis.com/maps/api/geocode/json'
coordinates = ''
for address in addresses:
url = BASE_URL + '?address=' + address + '&key=' + API_KEY
response = requests.request("GET", url)
results = response.json()['results'][0]
lat = results['geometry']['location']['lat']
lng = results['geometry']['location']['lng']
coordinates += str(lat) + ',' + str(lng) + '%7C'
return coordinates
def createDistanceMatrix(coordinates):
BASE_URL_Route = 'https://maps.googleapis.com/maps/api/distancematrix/json?'
RouteURL = BASE_URL_Route + 'origins=' + coordinates + '&destinations=' + coordinates + 'mode=driving&key=' + API_KEY
payload={}
headers = {}
response = requests.request("GET", RouteURL, headers=headers, data=payload)
results = response.json()
rows = results['rows']
matrix = []
for row in rows:
matrixRow = []
for e in range(len(row["elements"])-1):
matrixRow.append(row["elements"][e]["duration"]["value"])
matrix.append(matrixRow)
return(matrix)
addresses = ['Montgomery Blair HS','GALWAY ES', 'GREENCASTLE ES', 'BURTONSVILLE ES']
coordinatesOfLocations = convertAddressesToCoordinates(addresses)
matrix = createDistanceMatrix(coordinatesOfLocations)
print(matrix)
p, d = greedyFlexibleTurnPriority(matrix, 2, 0)
print(p)