-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathFinder.py
191 lines (158 loc) · 5.88 KB
/
PathFinder.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
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time : 20/8/19 16:36
@Author : Jedidiah
@Contact : yanzhe_zhang@qq.com
@File : PathFinder.py
@Software: PyCharm
"""
import sys
import time
import numpy as np
from matplotlib.patches import Rectangle
from functools import total_ordering
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
@total_ordering
class Node:
heuristic = True
def __init__(self, point, g=sys.maxsize, h=sys.maxsize):
self.point = point
self.parent = None
self.g = g
self.h = h
def HeuristicCost(self, end_node):
if Node.heuristic:
dx = abs(end_node.point.x - self.point.x)
dy = abs(end_node.point.y - self.point.y)
self.h = dx + dy + (np.sqrt(2) - 2) * min(dx, dy)
else:
self.h = 0
def setBaseCost(self, g):
self.g = g
def setParent(self, node):
self.parent = node
def get_cost(self):
return self.g + self.h
def __eq__(self, other):
return self.get_cost() == other.get_cost()
def __lt__(self, other):
return self.get_cost() < other.get_cost()
def __repr__(self):
return "(" + str(self.point.x+1) + ", " + str(self.point.y+1) + ")"
class FindPath:
def __init__(self, graph, heuristic=True):
self.graph = graph
Node.heuristic = heuristic
self.open_list = [] # nodes to be evaluate
self.close_list = [] # nodes already evaluated
self.start_node = Node(Point(0, 0), g=0)
self.end_node = Node(Point(self.graph.size-1, self.graph.size-1), h=0)
self.start_node.HeuristicCost(self.end_node)
def IsInOpenList(self, point):
for each in self.open_list:
if point == each.point:
return True
return False
def IsInCloseList(self, point):
for each in self.close_list:
if point == each.point:
return True
return False
def GetNodeInOpenList(self, node):
for each in self.open_list:
if node.point == each.point:
return each
return node
def GetIndexInOpenList(self, node):
for i in range(len(self.open_list)):
if node.point == self.open_list[i].point:
return i
return -1
def UpdateOpenList(self, node):
for i in range(len(self.open_list)):
if self.open_list[i].point == node.point:
self.open_list[i] = node
@staticmethod
def Neighbours(node):
point = node.point
process_points = [Point(point.x-1, point.y+1),
Point(point.x-1, point.y),
Point(point.x-1, point.y-1),
Point(point.x, point.y+1),
Point(point.x, point.y-1),
Point(point.x+1, point.y+1),
Point(point.x+1, point.y),
Point(point.x+1, point.y-1)]
return [Node(i) for i in process_points]
def IsValidPoint(self, point):
if point.x < 0 or point.y < 0:
return False
if point.x >= self.graph.size or point.y >= self.graph.size:
return False
return not self.graph.IsObstacle(point)
@staticmethod
def CalcBaseCost(node, parent):
g = parent.g
dx = abs(parent.point.x - node.point.x)
dy = abs(parent.point.y - node.point.y)
g += abs(dx-dy) + min(dx, dy) * np.sqrt(2)
return g
def SelectNode(self):
min_node = min(self.open_list)
for each in self.open_list:
if each == min_node:
if each.g < min_node.g:
min_node = each
return min_node
def Run(self, ax, plt):
start_time = time.time_ns()
self.open_list.append(self.start_node)
while True:
if not self.open_list:
print("No path found")
break
current_node = self.SelectNode()
if current_node.point == self.end_node.point:
return self.BuildPath(current_node, start_time, ax, plt)
del self.open_list[self.GetIndexInOpenList(current_node)]
self.close_list.append(current_node)
neighbours = self.Neighbours(current_node)
for each in neighbours:
if self.IsValidPoint(each.point) and not self.IsInCloseList(each.point):
rec = Rectangle((each.point.x, each.point.y), 1, 1, color='c')
ax.add_patch(rec)
g_cost = self.CalcBaseCost(each, current_node)
if self.IsInOpenList(each.point):
each = self.GetNodeInOpenList(each)
each.HeuristicCost(self.end_node)
if g_cost + each.h < each.get_cost() or not self.IsInOpenList(each.point):
each.setBaseCost(g_cost)
each.parent = current_node
if not self.IsInOpenList(each.point):
self.open_list.append(each)
def BuildPath(self, last, start_time, ax, plt):
path = []
while True:
path.append(last)
if last.point == self.start_node.point:
break
else:
last = last.parent
path.reverse()
end_time = time.time_ns()
print("Shortest Distance:", path[-1].g)
# print("Path:", path)
print('===== Algorithm finish in', end_time - start_time, 'nanoseconds =====')
for p in path[1:-1]:
if Node.heuristic:
rec = Rectangle((p.point.x, p.point.y), 1, 1, color='g')
else:
rec = Rectangle((p.point.x, p.point.y), 1, 1, color='y')
ax.add_patch(rec)
plt.draw()