-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyPathFindingDemo.py
More file actions
271 lines (219 loc) · 12.7 KB
/
pyPathFindingDemo.py
File metadata and controls
271 lines (219 loc) · 12.7 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import random
from enum import Enum
from itertools import product
from operator import attrgetter
from argparse import ArgumentParser, RawTextHelpFormatter
import contextlib
with contextlib.redirect_stdout(None):
import pygame
class Game:
class TileType(Enum):
empty = 0
wall = 1
start = 2
end = 3
open = 4
closed = 5
path = 6
def __init__(self, width, height, percentage_of_wall_fields, fps, optimization, seed):
random.seed(seed)
pygame.init()
pygame.display.set_caption("pyPathFindingDemo")
self._size = self._width, self._height = width, height
self._fps = fps
self._screen = pygame.display.set_mode(self._size)
self._clock = pygame.time.Clock()
self._empty_tile = pygame.image.load(r"Textures\TileWhite.png").convert()
self._wall_tile = pygame.image.load(r"Textures\TileBlack.png").convert()
self._start_tile = pygame.image.load(r"Textures\TileStart.png").convert()
self._end_tile = pygame.image.load(r"Textures\TileEnd.png").convert()
self._open_tile = pygame.image.load(r"Textures\TileGreen.png").convert()
self._closed_tile = pygame.image.load(r"Textures\TileRed.png").convert()
self._path_tile = pygame.image.load(r"Textures\TileBlue.png").convert()
self._tile_mapping = {Game.TileType.empty: self._empty_tile,
Game.TileType.wall: self._wall_tile,
Game.TileType.start: self._start_tile,
Game.TileType.end: self._end_tile,
Game.TileType.open: self._open_tile,
Game.TileType.closed: self._closed_tile,
Game.TileType.path: self._path_tile}
self._number_of_tiles_x = int(self._width / self._empty_tile.get_width())
self._number_of_tiles_y = int(self._height / self._empty_tile.get_height())
self._board = [[None for _ in range(self._number_of_tiles_y)] for _ in range(self._number_of_tiles_x)]
for x, y in product(range(self._number_of_tiles_x), range(self._number_of_tiles_y)):
if x == 0 or y == 0 or x == self._number_of_tiles_x - 1 or y == self._number_of_tiles_y - 1 or random.random() < percentage_of_wall_fields:
self._board[x][y] = Game.TileType.wall
else:
self._board[x][y] = Game.TileType.empty
self._start_node = (random.randint(1, self._number_of_tiles_x - 2), random.randint(1, self._number_of_tiles_y - 2))
self._board[self._start_node[0]][self._start_node[1]] = Game.TileType.start
self._end_node = (random.randint(1, self._number_of_tiles_x - 2), random.randint(1, self._number_of_tiles_y - 2))
self._board[self._end_node[0]][self._end_node[1]] = Game.TileType.end
self._search_algorithm = TreeSearch(self._start_node,
self._end_node,
optimization,
get_all_connected_nodes=self.get_all_connected_empty_tiles,
report_open_node=self.report_open_node,
report_closed_node=self.report_closed_node)
def get_all_connected_empty_tiles(self, x, y):
node_list = []
all_connected_tiles = ((x - 1, y),
(x + 1, y),
(x, y + 1),
(x, y - 1))
for x, y in all_connected_tiles:
if x > 0 and x < self._number_of_tiles_x - 1 and y > 0 and y < self._number_of_tiles_y - 1:
if self._board[x][y] == Game.TileType.empty or self._board[x][y] == Game.TileType.end:
node_list.append((x, y))
return node_list
def report_open_node(self, x, y):
if (x != self._start_node[0] or y != self._start_node[1]) and \
(x != self._end_node[0] or y != self._end_node[1]):
self._board[x][y] = Game.TileType.open
def report_closed_node(self, x, y):
if (x != self._start_node[0] or y != self._start_node[1]) and \
(x != self._end_node[0] or y != self._end_node[1]):
self._board[x][y] = Game.TileType.closed
def run(self):
finished = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if not finished:
result = self._search_algorithm.run_step()
finished = result in (TreeSearch.Result.no_solution, TreeSearch.Result.solution_found)
if result == TreeSearch.Result.solution_found:
for x, y in self._search_algorithm.get_founded_path():
self._board[x][y] = Game.TileType.path
self._screen.fill([0, 255, 255])
for x, y in product(range(self._number_of_tiles_x), range(self._number_of_tiles_y)):
tile = self._tile_mapping[self._board[x][y]]
self._screen.blit(tile,
tile.get_rect().move(x * tile.get_width(), y * tile.get_height()))
pygame.display.flip()
self._clock.tick(self._fps)
class TreeSearch:
class SearchOptimization(Enum):
breadth_first = 0
depth_first = 1
greedy = 2
a_star = 3
class Result(Enum):
no_solution = 0
solution_found = 1
step_done = 2
class TreeNode:
def __init__(self, x, y, previous_node, previous_costs, estimated_costs):
self.x = x
self.y = y
self.previous_node = previous_node
self.previous_costs = previous_costs
self.estimated_costs = estimated_costs
self.total_costs = previous_costs + estimated_costs
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __init__(self, start_node, end_node, optimization, get_all_connected_nodes, report_open_node, report_closed_node):
self._end_node = TreeSearch.TreeNode(end_node[0],
end_node[1],
previous_node=None,
previous_costs=0,
estimated_costs=0)
self._optimization = optimization
self._get_all_connected_nodes = get_all_connected_nodes
self._report_open_node = report_open_node
self._report_closed_node = report_closed_node
self._step_cost = 1
self._open_node_list = [TreeSearch.TreeNode(start_node[0],
start_node[1],
previous_node=None,
previous_costs=0,
estimated_costs=abs(self._end_node.x - start_node[0]) + abs(self._end_node.y - start_node[1]))]
def run_step(self):
if len(self._open_node_list) == 0:
return TreeSearch.Result.no_solution
if self._optimization == TreeSearch.SearchOptimization.breadth_first:
self._open_node_list.sort(key=attrgetter("previous_costs"), reverse=True)
elif self._optimization == TreeSearch.SearchOptimization.depth_first:
self._open_node_list.sort(key=attrgetter("previous_costs"), reverse=False)
elif self._optimization == TreeSearch.SearchOptimization.greedy:
self._open_node_list.sort(key=attrgetter("estimated_costs", "total_costs"), reverse=True)
elif self._optimization == TreeSearch.SearchOptimization.a_star:
self._open_node_list.sort(key=attrgetter("total_costs", "estimated_costs"), reverse=True)
actual_node_to_expand = self._open_node_list.pop()
if actual_node_to_expand == self._end_node:
return TreeSearch.Result.solution_found
self._report_closed_node(actual_node_to_expand.x, actual_node_to_expand.y)
for x, y in self._get_all_connected_nodes(actual_node_to_expand.x, actual_node_to_expand.y):
self._report_open_node(x, y)
node = TreeSearch.TreeNode(x,
y,
previous_node=actual_node_to_expand,
previous_costs=actual_node_to_expand.previous_costs + self._step_cost,
estimated_costs=abs(self._end_node.x - x) + abs(self._end_node.y - y))
if node == self._end_node:
self._end_node = node
self._open_node_list.append(node)
return TreeSearch.Result.step_done
def get_founded_path(self):
path = []
actual_node = self._end_node.previous_node
while actual_node is not None:
path.append((actual_node.x, actual_node.y))
actual_node = actual_node.previous_node
return path[:-1]
if __name__ == "__main__":
argument_parser = ArgumentParser(description="""
pyPathFindingDemo:
- pyPathFindingDemo is a little python program which implements some tree search algorithms to solve a path finding problem visualized with the pygame framework.
- Create a randomly generated playground with walls, start and end point and search a path from start to end with the given optimization strategy.
- See readme.md for more informations.""",
epilog="https://github.com/WinterWonderland/pyPathFindingDemo",
formatter_class=RawTextHelpFormatter)
argument_parser.add_argument("--width",
metavar="",
type=int,
default=1920,
help="The width of the game window (minimum=32, default=1920 [Full HD])")
argument_parser.add_argument("--height",
metavar="",
type=int,
default=1080,
help="The width of the game window (minimum=32, default=1080 [Full HD])")
argument_parser.add_argument("--walls",
metavar="",
type=float,
default=1 / 3,
help="Percentage of wall tiles to generate (minimum=0, maximum=1, default=1/3)")
argument_parser.add_argument("--fps",
metavar="",
type=int,
default=25,
help="The target frames per second to run the simulation (default=25)")
argument_parser.add_argument("--optimization",
metavar="",
type=str,
choices=("breadth", "depth", "greedy", "a_star"),
default="greedy",
help="The optimization strategy for the search algorithm [breadth, depth, greedy, a_star] (default=a_star)")
argument_parser.add_argument("--seed",
metavar="",
type=int,
default=None,
help="A seed for the random number generator to get identical play boards")
args = argument_parser.parse_args()
if args.optimization == "breadth":
optimization = TreeSearch.SearchOptimization.breadth_first
elif args.optimization == "depth":
optimization = TreeSearch.SearchOptimization.depth_first
elif args.optimization == "greedy":
optimization = TreeSearch.SearchOptimization.greedy
elif args.optimization == "a_star":
optimization = TreeSearch.SearchOptimization.a_star
board = Game(width=args.width,
height=args.height,
percentage_of_wall_fields=args.walls,
fps=args.fps,
optimization=optimization,
seed=args.seed)
board.run()