-
Notifications
You must be signed in to change notification settings - Fork 0
/
Routing.py
70 lines (63 loc) · 2.29 KB
/
Routing.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
import MapDataParser, math, sys
import Utils
import MySQLMap
distWeight = 1
riskWeight = 1
class SearchNode:
def __init__(self, node, goal, prev=None, cost=0):
self.node = node
self.cost = cost
self.prev = prev
self.goal = goal
# BELOW IS ALL HEURISTICS HANDLING
def isGoalState(self):
return self.node.id == self.goal.id
def __eq__(self, other):
self.node.id == other.node.id
def route(p1, p2, nodes, nodeEdges, risk):
startNode, endNode = None, None
bestDistFromStart, bestDistFromEnd = 999999999, 999999999
for id in nodes.keys():
distFromStart = Utils.euclid(p1, nodes[id].point)
distFromEnd = Utils.euclid(p2, nodes[id].point)
if distFromStart < bestDistFromStart:
startNode = id
bestDistFromStart = distFromStart
if distFromEnd < bestDistFromEnd:
endNode = id
bestDistFromEnd = distFromEnd
paths = []
paths.append(findPath(nodes[startNode], nodes[endNode], nodes, nodeEdges, risk, 1, 0, 1))
paths.append(findPath(nodes[startNode], nodes[endNode], nodes, nodeEdges, risk, 1, 100, 0.5))
paths.append(findPath(nodes[startNode], nodes[endNode], nodes, nodeEdges, risk, 0.02, 10, 0.01))
return paths
def findPath(start, goal, nodes, nodeEdges, risk, a, b, c):
visited = []
curr = SearchNode(start, goal)
fringe = Utils.PriorityQueue()
fringe.push(curr, 0)
while True:
if fringe.isEmpty():
return []
curr = fringe.pop()
if curr.isGoalState():
return createPath(curr)
if curr.node.id not in visited:
visited.append(curr.node.id)
children = nodeEdges[curr.node.id]
if children:
for e in children:
d = e.dist
r = risk[e.id]
# print("distance: {}, r: {}".format(d, r))
cost = a*d + b*r + curr.cost
node = SearchNode(e.other(curr.node), goal, curr, cost)
fringe.push(node, node.cost + c*Utils.euclid(curr.node.point, goal.point))
def createPath(node):
path = []
curr = node
while curr:
path.append([curr.node.point[0], curr.node.point[1]])
curr = curr.prev
path.reverse()
return path