Skip to content

Commit 82646ac

Browse files
authored
Note about cleaning up the grid and implementation details
1 parent c096b3c commit 82646ac

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
# python-pathfinding
23
Pathfinding algorithms based on [Pathfinding.JS](https://github.com/qiao/PathFinding.js) for python 2 and 3.
34

@@ -41,7 +42,6 @@ A simple usage example to find a path using A*.
4142
```python
4243
grid = Grid(matrix=matrix)
4344
```
44-
4545
1. we get the start (top-left) and endpoint (bottom-right) from the map:
4646

4747
```python
@@ -96,4 +96,30 @@ print('operations:', runs, 'path length:', len(path))
9696
print(grid.grid_str(path=path, start=start, end=end))
9797
```
9898

99+
While running the pathfinding algorithm it might set values on the nodes. Depending on your path finding algorithm things like calculated distances or visited flags might be stored on them. So if you want to run the algorithm again you need to clean the grid first (see `Grid.cleanup`). Please note that because cleanup looks at all nodes of the grid it might be an operation that can take a bit of time!
100+
99101
Take a look at the _`test/`_ folder for more examples.
102+
103+
implementation details
104+
----------------------
105+
All pathfinding algorithms in this library are inheriting the Finder class. It has some common functionality that can be overwritten by the implementation of a path finding algorithm.
106+
107+
The normal process works like this:
108+
1. You call `find_path` on one of your finder implementations
109+
1. `init_find` instantiates open-list and resets all values and counters.
110+
1. The main loop starts on the open-list. This list gets filled with all nodes that will be processed next (e.g. all nodes that are walkable and not the end in the). For this you need to implement `check_neighbors` in your own finder implementation.
111+
1. For example in A*s implementation of `check_neighbors` you first want to get the next node closest from the current starting point from the open list. the `next_node` method in Finder does this by giving you the node with a minimum `f`-value from the open list, it closes it and removes it from the open list.
112+
1. if this node is not the end node we go on and get the neighbors of the node we just picked from the grid by calling `find_neighbors`. This just calls `grid.neighbors`.
113+
1. If none of the neighbors are the end node we want to process the neighbors to calculate their distances in `process_node`
114+
1. `process_node` calculates the cost `f` from the start to the current node using the `calc_cost` method and the cost after calculating `h` from `apply_heuristic`.
115+
1. finally `process_node` updates the open list so `find_path` can run `check_neighbors` on it in the next node in the next iteration of the main loop.
116+
117+
flow:
118+
```
119+
find_path
120+
init_find # (re)set global values and open list
121+
check_neighbors # for every node in open list
122+
next_node # closest node to start in open list
123+
find_neighbors # get neighbors
124+
process_node # calculate new cost for neighboring node
125+
```

0 commit comments

Comments
 (0)