-
Notifications
You must be signed in to change notification settings - Fork 1
/
testerexec.py
177 lines (132 loc) · 4.39 KB
/
testerexec.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
import sys
from queue import PQueue
import math
import StringIO
import sqlite3
codeOut = StringIO.StringIO()
codeErr = StringIO.StringIO()
sys.stdout = codeOut
sys.stderr = codeErr
# HARDCODED DATA
startNode = {'id': 3428474040, 'lat': 7.4185151, 'lon': 43.7353633}
endNode = {'id': 3428473993, 'lat': 7.4180124, 'lon': 43.7341179}
## define GreenNav class
class GreenNav():
def __init__(self):
# connect to sqlite3 database
conn = sqlite3.connect('data/data.db')
c = conn.cursor()
# setup node and way caches
self.nodeCache = {}
self.wayCache = {}
nodes = c.execute('SELECT * FROM nodes;')
for node in nodes:
self.nodeCache[node[0]] = {
'lat': node[4],
'lon': node[3],
'id': node[0]
}
ways = c.execute('SELECT * FROM ways;')
for way in ways:
self.wayCache[way[0]] = {}
waynodes = c.execute('SELECT * FROM way_nodes;')
for waynode in waynodes:
self.wayCache[waynode[0]][waynode[2]] = True
# setup visualization for circles/line
self.circles = []
self.path = {}
def drawCircle(self, node):
self.circles.append(node)
def drawLine(self, path):
self.path = path
def clearData(self):
self.circles = []
self.path = {}
# instantiate a greennav object
gn = GreenNav()
runFunction = """
def overallFunc():
# define helper methods
def nodeWithShortestDistance(frontier):
distance = float('inf')
shortestId = None
for key, val in frontier.iteritems():
if val['distance'] < distance:
distance = val['distance']
shortestId = key
return gn.nodeCache[shortestId]
def getNextNodes(node):
for key in gn.wayCache:
for inode in gn.wayCache[key]:
if gn.nodeCache[inode]['id'] == node['id']:
retArr = []
for key2 in gn.wayCache[key]:
retArr.append(key2)
return retArr
return []
def costBetweenTwoNodes(node1, node2):
radlat1 = math.pi * node1['lat']/180
radlat2 = math.pi * node2['lat']/180
radlon1 = math.pi * node1['lon']/180
radlon2 = math.pi * node2['lon']/180
theta = node1['lon'] - node2['lon']
radtheta = math.pi * theta/180
dist = math.sin(radlat1) * math.sin(radlat2) + math.cos(radlat1) * math.cos(radlat2) * math.cos(radtheta)
dist = math.acos(dist)
dist = dist * 180/math.pi
dist = dist * 60 * 1.1515
dist = dist * 1.609344 * 1000
return dist
def costFromStart(node):
return costBetweenTwoNodes(startNode, node)
def costTillGoal(node):
return costBetweenTwoNodes(node, endNode)
# start the computation!
maxSteps = 1000
node = startNode
node['prevCost'] = 0
pq = PQueue()
alreadyExplored = {}
# cost function = costTillNow + costTillGoal
fstart = 0 + costTillGoal(node)
pq.put(node, fstart)
print("Starting computation.")
while not pq.isEmpty() and maxSteps > 0:
curNode = pq.get()
if curNode['id'] in alreadyExplored:
maxSteps = maxSteps - 1
continue
alreadyExplored[curNode['id']] = True
# if goal found
if curNode['id'] == endNode['id']:
print("Found a path.")
latLng = []
while 'previous' in curNode:
latLng.append({'nodeLat': curNode['lat'], 'nodeLong': curNode['lon']})
curNode = curNode['previous']
gn.drawLine({'pTime': 10000 - maxSteps*10, 'pathLine': latLng})
break
# for each neighbor node
nextNodes = getNextNodes(curNode)
for i in range(0, len(nextNodes)):
nextNode = gn.nodeCache[nextNodes[i]]
if nextNode['id'] in alreadyExplored:
continue
nextNode['previous'] = curNode
nextNode['prevCost'] = curNode['prevCost'] + costBetweenTwoNodes(curNode, nextNode)
# cost function = costTillNow + costTillGoal
fn = nextNode['prevCost'] + costTillGoal(nextNode)
pq.put(nextNode, fn)
gn.drawCircle({'circleLat': curNode['lat'], 'circleLong': curNode['lon'], 'circleTime': 10000 - maxSteps*10})
maxSteps = maxSteps - 1
"""
exec runFunction
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
overallFunc()
se = codeErr.getvalue()
print "Error: \n%s\n" % se
so = codeOut.getvalue()
print "Output: \n%s\n" % so
codeOut.close()
codeErr.close()