You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inspired by [Pathfinding.JS](https://github.com/qiao/PathFinding.js)
21
+
Inspired by [Pathfinding.JS](https://github.com/qiao/PathFinding.js)
22
+
23
+
## Installation
21
24
22
-
Installation
23
-
------------
24
25
This library is provided by pypi, so you can just install the current stable version using pip:
25
-
```
26
+
27
+
```python
26
28
pip install pathfinding
27
29
```
28
30
29
-
see https://pypi.org/project/pathfinding/
31
+
see [pathfinding on pypi](https://pypi.org/project/pathfinding/)
32
+
33
+
## Usage example
30
34
31
-
usage example
32
-
-------------
33
35
A simple usage example to find a path using A*.
34
36
35
37
1. import the required libraries:
38
+
36
39
```python
37
40
from pathfinding.core.diagonal_movement import DiagonalMovement
38
41
from pathfinding.core.grid import Grid
39
42
from pathfinding.finder.a_star import AStarFinder
40
43
```
41
44
42
45
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 complexmapor use some sensor data asinputfor it.
46
+
43
47
```python
44
48
matrix = [
45
49
[1, 1, 1],
@@ -48,33 +52,38 @@ A simple usage example to find a path using A*.
48
52
]
49
53
```
50
54
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.
52
56
53
57
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 mapis a square, so the size height is defined by the length of the outer listand the width by the length of the first list inside it.
54
58
55
59
```python
56
60
grid = Grid(matrix=matrix)
57
61
```
62
+
58
63
1. we get the start (top-left) and endpoint (bottom-right) from the map:
59
64
60
65
```python
61
66
start = grid.node(0, 0)
62
67
end = grid.node(2, 2)
63
68
```
69
+
64
70
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.
65
71
66
72
```python
67
73
finder = AStarFinder(diagonal_movement=DiagonalMovement.always)
68
74
path, runs = finder.find_path(start, end, grid)
69
75
```
76
+
70
77
1. thats it. We found a way. Now we can print the result (or do something elsewith it). Note that the start and end points are part of the path.
@@ -83,6 +92,7 @@ A simple usage example to find a path using A*.
83
92
| e|
84
93
+---+
85
94
```
95
+
86
96
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 isnot using the upper-right corner. You can access `print(path)` to get the specific list of coordinates.
87
97
88
98
Here The whole example if you just want to copy-and-paste the code and play with it:
Take a look at the _`test/`_ folder for more examples.
113
123
114
-
Rerun the algorithm
115
-
--------------------
124
+
## Rerun the algorithm
125
+
116
126
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!
117
127
118
-
implementation details
119
-
----------------------
128
+
## Implementation details
129
+
120
130
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.
121
131
122
132
The normal process works like this:
133
+
123
134
1. You call `find_path` on one of your finder implementations
124
135
1. `init_find` instantiates `open_list`and resets all values and counters.
125
136
1. The main loop starts on the `open_list`. This list gets filled withall 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:
130
141
1. finally`process_node` updates the openlist so `find_path` can run `check_neighbors` on it in the next node in the next iteration of the main loop.
0 commit comments