|
| 1 | + |
1 | 2 | # python-pathfinding |
2 | 3 | Pathfinding algorithms based on [Pathfinding.JS](https://github.com/qiao/PathFinding.js) for python 2 and 3. |
3 | 4 |
|
@@ -41,7 +42,6 @@ A simple usage example to find a path using A*. |
41 | 42 | ```python |
42 | 43 | grid = Grid(matrix=matrix) |
43 | 44 | ``` |
44 | | - |
45 | 45 | 1. we get the start (top-left) and endpoint (bottom-right) from the map: |
46 | 46 |
|
47 | 47 | ```python |
@@ -96,4 +96,30 @@ print('operations:', runs, 'path length:', len(path)) |
96 | 96 | print(grid.grid_str(path=path, start=start, end=end)) |
97 | 97 | ``` |
98 | 98 |
|
| 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 | + |
99 | 101 | 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