Skip to content

Commit df1cf7a

Browse files
committed
fix README
1 parent da0d67c commit df1cf7a

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

README.md

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
21
# python-pathfinding
2+
33
Pathfinding algorithms for python 2 and 3.
44

55
Currently there are 6 path-finders bundled in this library, namely:
6-
- A*
7-
- Dijkstra
8-
- Best-First
9-
- Bi-directional A*
10-
- Breadth First Search (BFS)
11-
- Iterative Deeping A* (IDA*)
6+
7+
- A*
8+
- Dijkstra
9+
- Best-First
10+
- Bi-directional A*
11+
- Breadth First Search (BFS)
12+
- Iterative Deeping A\* (IDA*)
1213

1314
Dijkstra and A* take the weight of the fields on the map into account.
1415

@@ -17,29 +18,32 @@ Dijkstra and A* take the weight of the fields on the map into account.
1718
![MIT License](https://img.shields.io/github/license/brean/python-pathfinding)
1819
![PyPI](https://img.shields.io/pypi/v/pathfinding)
1920

20-
Inspired by [Pathfinding.JS](https://github.com/qiao/PathFinding.js)
21+
Inspired by [Pathfinding.JS](https://github.com/qiao/PathFinding.js)
22+
23+
## Installation
2124

22-
Installation
23-
------------
2425
This library is provided by pypi, so you can just install the current stable version using pip:
25-
```
26+
27+
```python
2628
pip install pathfinding
2729
```
2830

29-
see https://pypi.org/project/pathfinding/
31+
see [pathfinding on pypi](https://pypi.org/project/pathfinding/)
32+
33+
## Usage example
3034

31-
usage example
32-
-------------
3335
A simple usage example to find a path using A*.
3436

3537
1. import the required libraries:
38+
3639
```python
3740
from pathfinding.core.diagonal_movement import DiagonalMovement
3841
from pathfinding.core.grid import Grid
3942
from pathfinding.finder.a_star import AStarFinder
4043
```
4144

4245
1. Create a map using a 2D-list. Any value smaller or equal to 0 describes an obstacle. Any number bigger than 0 describes the weight of a field that can be walked on. The bigger the number the higher the cost to walk that field. In this example we like the algorithm to create a path from the upper left to the bottom right. To make it not to easy for the algorithm we added an obstacle in the middle, so it can not use the direct way. We ignore the weight for now, all fields have the same cost of 1. Feel free to create a more complex map or use some sensor data as input for it.
46+
4347
```python
4448
matrix = [
4549
[1, 1, 1],
@@ -48,33 +52,38 @@ A simple usage example to find a path using A*.
4852
]
4953
```
5054

51-
Note: you can use negative values to describe different types of obstacles. It does not make a difference for the path finding algorithm but it might be useful for your later map evaluation.
55+
Note: you can use negative values to describe different types of obstacles. It does not make a difference for the path finding algorithm but it might be useful for your later map evaluation.
5256

5357
1. we create a new grid from this map representation. This will create Node instances for every element of our map. It will also set the size of the map. We assume that your map is a square, so the size height is defined by the length of the outer list and the width by the length of the first list inside it.
5458

5559
```python
5660
grid = Grid(matrix=matrix)
5761
```
62+
5863
1. we get the start (top-left) and endpoint (bottom-right) from the map:
5964

6065
```python
6166
start = grid.node(0, 0)
6267
end = grid.node(2, 2)
6368
```
69+
6470
1. create a new instance of our finder and let it do its work. We allow diagonal movement. The `find_path` function does not only return you the path from the start to the end point it also returns the number of times the algorithm needed to be called until a way was found.
6571

6672
```python
6773
finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
6874
path, runs = finder.find_path(start, end, grid)
6975
```
76+
7077
1. thats it. We found a way. Now we can print the result (or do something else with it). Note that the start and end points are part of the path.
7178

7279
```python
7380
print('operations:', runs, 'path length:', len(path))
7481
print(grid.grid_str(path=path, start=start, end=end))
7582
```
83+
7684
The result should look like this:
77-
```
85+
86+
```pseudo
7887
('operations:', 5, 'path length:', 4)
7988

8089
+---+
@@ -83,6 +92,7 @@ A simple usage example to find a path using A*.
8392
| e|
8493
+---+
8594
```
95+
8696
You can ignore the +, - and | characters, they just show the border around your map, the blank space is a free field, 's' marks the start, 'e' the end and '#' our obstacle in the middle. You see the path from start to end marked by 'x' characters. We allow horizontal movement, so it is not using the upper-right corner. You can access `print(path)` to get the specific list of coordinates.
8797

8898
Here The whole example if you just want to copy-and-paste the code and play with it:
@@ -111,15 +121,16 @@ print(grid.grid_str(path=path, start=start, end=end))
111121

112122
Take a look at the _`test/`_ folder for more examples.
113123

114-
Rerun the algorithm
115-
--------------------
124+
## Rerun the algorithm
125+
116126
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 in a loop 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!
117127

118-
implementation details
119-
----------------------
128+
## Implementation details
129+
120130
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.
121131

122132
The normal process works like this:
133+
123134
1. You call `find_path` on one of your finder implementations
124135
1. `init_find` instantiates `open_list` and resets all values and counters.
125136
1. The main loop starts on the `open_list`. This list gets filled with all nodes that will be processed next (e.g. all neighbors that are walkable). For this you need to implement `check_neighbors` in your own finder implementation.
@@ -130,7 +141,8 @@ The normal process works like this:
130141
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.
131142

132143
flow:
133-
```
144+
145+
```pseudo
134146
find_path
135147
init_find # (re)set global values and open list
136148
check_neighbors # for every node in open list

0 commit comments

Comments
 (0)