Skip to content

Commit 95185af

Browse files
committed
minor changes for better documentation and to automatically generate documentation using diaggen
1 parent d059090 commit 95185af

6 files changed

Lines changed: 48 additions & 16 deletions

File tree

pathfinding/core/node.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
from __future__ import annotations
12
import dataclasses
23

34

45
@dataclasses.dataclass
56
class Node:
7+
h: float = 0.0
8+
g: float = 0.0
9+
f: float = 0.0
10+
opened: int = 0
11+
closed: bool = False
12+
parent: Node = None
13+
retain_count: int = 0
14+
tested: bool = False
15+
616
def __post_init__(self):
717
# values used in the finder
818
self.cleanup()
@@ -94,10 +104,13 @@ def __iter__(self):
94104
yield self.grid_id
95105

96106
def connect(self, other_node):
107+
"""
108+
Connect two nodes with each other.
109+
"""
97110
if not self.connections:
98111
self.connections = [other_node]
99112
else:
100113
self.connections.append(other_node)
101114

102115
def __repr__(self):
103-
return f'<GridNode({self.x}:{self.y} {hex(id(self))})>'
116+
return f'<GridNode({self.x}:{self.y} {hex(id(self))})>'

pathfinding/finder/a_star.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55

66

77
class AStarFinder(Finder):
8+
"""
9+
Find shortest path using A* algorithm
10+
"""
11+
812
def __init__(self, heuristic=None, weight=1,
913
diagonal_movement=DiagonalMovement.never,
1014
time_limit=TIME_LIMIT,
1115
max_runs=MAX_RUNS):
1216
"""
13-
find shortest path using A* algorithm
17+
Find shortest path using A* algorithm
1418
:param heuristic: heuristic used to calculate distance of 2 points
1519
(defaults to manhattan)
1620
:param weight: weight for the edges
@@ -40,7 +44,7 @@ def __init__(self, heuristic=None, weight=1,
4044
def check_neighbors(self, start, end, graph, open_list,
4145
open_value=True, backtrace_by=None):
4246
"""
43-
find next path segment based on given node
47+
Find next path segment based on given node
4448
(or return path if we found the end)
4549
4650
:param start: start node

pathfinding/finder/bi_a_star.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77

88
class BiAStarFinder(AStarFinder):
99
"""
10-
Similar to the default A* algorithm from a_star.
10+
Run A* from start to end and end to start during path finding loop.
1111
"""
12+
1213
def __init__(self, heuristic=None, weight=1,
1314
diagonal_movement=DiagonalMovement.never,
1415
time_limit=TIME_LIMIT,

pathfinding/finder/breadth_first.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55

66
class BreadthFirstFinder(Finder):
7+
"""
8+
Breadth-first algorithm, similar to Dijkstra or A* but without cost.
9+
"""
710
def __init__(self, heuristic=None, weight=1,
811
diagonal_movement=DiagonalMovement.never,
912
time_limit=TIME_LIMIT,
@@ -15,8 +18,6 @@ def __init__(self, heuristic=None, weight=1,
1518
diagonal_movement=diagonal_movement,
1619
time_limit=time_limit,
1720
max_runs=max_runs)
18-
if not diagonal_movement:
19-
self.diagonalMovement = DiagonalMovement.never
2021

2122
def check_neighbors(self, start, end, grid, open_list):
2223
node = open_list.pop_node()

pathfinding/finder/dijkstra.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55

66
class DijkstraFinder(AStarFinder):
7+
"""
8+
Find shortest path using the best-first Dijkstra path finding algorithm.
9+
"""
10+
711
def __init__(self, weight=1,
812
diagonal_movement=DiagonalMovement.never,
913
time_limit=TIME_LIMIT,
@@ -17,6 +21,8 @@ def __init__(self, weight=1,
1721

1822
def apply_heuristic(self, node_a, node_b, heuristic=None, graph=None):
1923
"""
20-
helper function to apply heuristic
24+
Helper function to apply heuristic
25+
26+
returns 0 as Dijkstra is not using any heuristic (but A* is).
2127
"""
2228
return 0

pathfinding/finder/finder.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515

1616

1717
class ExecutionTimeException(Exception):
18+
"""
19+
Exception that gets thrown when a certain time has been exceeded.
20+
"""
1821
def __init__(self, message):
1922
super(ExecutionTimeException, self).__init__(message)
2023

2124

2225
class ExecutionRunsException(Exception):
26+
"""
27+
Exception that gets thrown when the number of max. runs has been reached.
28+
"""
2329
def __init__(self, message):
2430
super(ExecutionRunsException, self).__init__(message)
2531

@@ -31,7 +37,7 @@ def __init__(self, heuristic=None, weight=1,
3137
time_limit=TIME_LIMIT,
3238
max_runs=MAX_RUNS):
3339
"""
34-
find shortest path
40+
Find shortest path
3541
:param heuristic: heuristic used to calculate distance of 2 points
3642
(defaults to manhattan)
3743
:param weight: weight for the edges
@@ -58,7 +64,7 @@ def __init__(self, heuristic=None, weight=1,
5864

5965
def apply_heuristic(self, node_a, node_b, heuristic=None, graph=None):
6066
"""
61-
helper function to apply heuristic
67+
Helper function to apply heuristic
6268
"""
6369
if not heuristic:
6470
heuristic = self.heuristic
@@ -84,15 +90,16 @@ def apply_heuristic(self, node_a, node_b, heuristic=None, graph=None):
8490

8591
def find_neighbors(self, grid, node, diagonal_movement=None):
8692
'''
87-
find neighbor, same for Djikstra, A*, Bi-A*, IDA*
93+
Find neighbor, same for Djikstra, A*, Bi-A*, IDA*
8894
'''
8995
if not diagonal_movement:
9096
diagonal_movement = self.diagonal_movement
9197
return grid.neighbors(node, diagonal_movement=diagonal_movement)
9298

9399
def keep_running(self):
94100
"""
95-
check, if we run into time or iteration constrains.
101+
Check, if we run into time or iteration constrains.
102+
96103
:returns: True if we keep running and False if we run into a constraint
97104
"""
98105
if self.runs >= self.max_runs:
@@ -109,7 +116,7 @@ def keep_running(self):
109116
def process_node(
110117
self, graph, node, parent, end, open_list, open_value=True):
111118
'''
112-
we check if the given node is part of the path by calculating its
119+
We check if the given node is part of the path by calculating its
113120
cost and add or remove it from our path
114121
:param node: the node we like to test
115122
(the neighbor in A* or jump-node in JumpPointSearch)
@@ -143,7 +150,7 @@ def process_node(
143150
def check_neighbors(self, start, end, graph, open_list,
144151
open_value=True, backtrace_by=None):
145152
"""
146-
find next path segment based on given node
153+
Find next path segment based on given node
147154
(or return path if we found the end)
148155
149156
:param start: start node
@@ -155,14 +162,14 @@ def check_neighbors(self, start, end, graph, open_list,
155162
'Please implement check_neighbors in your finder')
156163

157164
def clean_grid(self, grid):
158-
"""clean the map if needed."""
165+
"""Clean the map if needed."""
159166
if grid.dirty:
160167
grid.cleanup()
161168
grid.dirty = True
162169

163170
def find_path(self, start, end, grid):
164171
"""
165-
find a path from start to end node on grid by iterating over
172+
Find a path from start to end node on grid by iterating over
166173
all neighbors of a node (see check_neighbors)
167174
:param start: start node
168175
:param end: end node
@@ -191,7 +198,7 @@ def find_path(self, start, end, grid):
191198

192199
def __repr__(self):
193200
"""
194-
return a human readable representation
201+
Return a human readable representation
195202
"""
196203
return f"<{self.__class__.__name__}" \
197204
f"diagonal_movement={self.diagonal_movement} >"

0 commit comments

Comments
 (0)