Skip to main content

A simple implementation of the A* algorithm for path-finding on a two-dimensional grid.

Project description

Build Status Coverage Status PyPI version

PyAstar2D

This is a very simple C++ implementation of the A* algorithm for pathfinding on a two-dimensional grid. The solver itself is implemented in C++, but is callable from Python. This combines the speed of C++ with the convenience of Python.

I have not done any formal benchmarking, but the solver finds the solution to a 1802 by 1802 maze in 0.29s and a 4008 by 4008 maze in 0.83s when running on my nine-year-old Intel(R) Core(TM) i7-2630QM CPU @ 2.00GHz. See Example Results for more details.

See src/cpp/astar.cpp for the core C++ implementation of the A* shortest path search algorithm, src/pyastar2d/astar_wrapper.py for the Python wrapper and examples/example.py for example usage.

When determining legal moves, 4-connectivity is the default, but it is possible to set allow_diagonal=True for 8-connectivity.

Installation

Instructions for installing pyastar2d are given below.

From PyPI

The easiest way to install pyastar2d is directly from the Python package index:

pip install pyastar2d

From source

You can also install pyastar2d by cloning this repository and building it yourself. If running on Linux or MacOS, simply run

pip install .

from the root directory. If you are using Windows you may have to install Cython manually first:

pip install Cython
pip install .

To check that everything worked, run the example:

python examples/example.py

As a dependency

Include pyastar2d in your requirements.txt to install from pypi, or add this line to requirements.txt:

pyastar2d @ git+git://github.com/hjweide/pyastar2d.git@master#egg=pyastar2d

Usage

A simple example is given below:

import numpy as np
import pyastar2d
# The minimum cost must be 1 for the heuristic to be valid.
# The weights array must have np.float32 dtype to be compatible with the C++ code.
weights = np.array([[1, 3, 3, 3, 3],
                    [2, 1, 3, 3, 3],
                    [2, 2, 1, 3, 3],
                    [2, 2, 2, 1, 3],
                    [2, 2, 2, 2, 1]], dtype=np.float32)
# The start and goal coordinates are in matrix coordinates (i, j).
path = pyastar2d.astar_path(weights, (0, 0), (4, 4), allow_diagonal=True)
print(path)
# The path is returned as a numpy array of (i, j) coordinates.
array([[0, 0],
       [1, 1],
       [2, 2],
       [3, 3],
       [4, 4]])

Note that all grid points are represented as (i, j) coordinates. An example of using pyastar2d to solve a maze is given in examples/maze_solver.py.

Example Results

To test the implementation, I grabbed two nasty mazes from Wikipedia. They are included in the mazes directory, but are originally from here: Small and Large. I load the .png files as grayscale images, and set the white pixels to 1 (open space) and the black pixels to INF (walls).

To run the examples specify the input and output files using the --input and --output flags. For example, the following commands will solve the small and large mazes:

python examples/maze_solver.py --input mazes/maze_small.png --output solns/maze_small.png
python examples/maze_solver.py --input mazes/maze_large.png --output solns/maze_large.png

Small Maze (1802 x 1802):

time python examples/maze_solver.py --input mazes/maze_small.png --output solns/maze_small.png
Loaded maze of shape (1802, 1802) from mazes/maze_small.png
Found path of length 10032 in 0.292794s
Plotting path to solns/maze_small.png
Done

real	0m1.214s
user	0m1.526s
sys	0m0.606s

The solution found for the small maze is shown below: Maze Small Solution

Large Maze (4002 x 4002):

time python examples/maze_solver.py --input mazes/maze_large.png --output solns/maze_large.png
Loaded maze of shape (4002, 4002) from mazes/maze_large.png
Found path of length 783737 in 0.829181s
Plotting path to solns/maze_large.png
Done

real	0m29.385s
user	0m29.563s
sys	0m0.728s

The solution found for the large maze is shown below: Maze Large Solution

Motivation

I recently needed an implementation of the A* algorithm in Python to find the shortest path between two points in a cost matrix representing an image. Normally I would simply use networkx, but for graphs with millions of nodes the overhead incurred to construct the graph can be expensive. Considering that I was only interested in graphs that may be represented as two-dimensional grids, I decided to implement it myself using this special structure of the graph to make various optimizations. Specifically, the graph is represented as a one-dimensional array because there is no need to store the neighbors. Additionally, the lookup tables for previously-explored nodes (their costs and paths) are also stored as one-dimensional arrays. The implication of this is that checking the lookup table can be done in O(1), at the cost of using O(n) memory. Alternatively, we could store only the nodes we traverse in a hash table to reduce the memory usage. Empirically I found that replacing the one-dimensional array with a hash table (std::unordered_map) was about five times slower.

Tests

The default installation does not include the dependencies necessary to run the tests. To install these, first run

pip install -r requirements-dev.txt

before running

py.test

The tests are fairly basic but cover some of the more common pitfalls. Pull requests for more extensive tests are welcome.

References

  1. A* search algorithm on Wikipedia
  2. Pathfinding with A* on Red Blob Games

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyastar2d-1.0.6.tar.gz (8.4 kB view details)

Uploaded Source

Built Distributions

pyastar2d-1.0.6-cp310-cp310-win_amd64.whl (14.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

pyastar2d-1.0.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.8 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

pyastar2d-1.0.6-cp310-cp310-macosx_10_15_x86_64.whl (11.4 kB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

pyastar2d-1.0.6-cp39-cp39-win_amd64.whl (14.9 kB view details)

Uploaded CPython 3.9 Windows x86-64

pyastar2d-1.0.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.6 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

pyastar2d-1.0.6-cp39-cp39-macosx_10_15_x86_64.whl (11.4 kB view details)

Uploaded CPython 3.9 macOS 10.15+ x86-64

pyastar2d-1.0.6-cp38-cp38-win_amd64.whl (15.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

pyastar2d-1.0.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.7 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

pyastar2d-1.0.6-cp38-cp38-macosx_10_15_x86_64.whl (11.4 kB view details)

Uploaded CPython 3.8 macOS 10.15+ x86-64

pyastar2d-1.0.6-cp37-cp37m-win_amd64.whl (14.9 kB view details)

Uploaded CPython 3.7m Windows x86-64

pyastar2d-1.0.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (60.4 kB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

pyastar2d-1.0.6-cp37-cp37m-macosx_10_15_x86_64.whl (11.3 kB view details)

Uploaded CPython 3.7m macOS 10.15+ x86-64

File details

Details for the file pyastar2d-1.0.6.tar.gz.

File metadata

  • Download URL: pyastar2d-1.0.6.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.7.13

File hashes

Hashes for pyastar2d-1.0.6.tar.gz
Algorithm Hash digest
SHA256 1391cad0654bae1cb932dbdbd1581d57e5e8eea34b9d9fc797f9b1434fc71fae
MD5 cc9feabf43cde71b873a973c2ea70d21
BLAKE2b-256 ebb6211788a5afa5235113f4d32ee77e2a500c1ef1824a61d16a5db1df4d830b

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5d3ec98355bf4ac6419b7824e9348af9a38d8d87e9c36412484ea498df2f540b
MD5 feba4e87ac19253255e6f558a5b1dca7
BLAKE2b-256 f9b53e79c8f68b66bd034ec560eec4aa46006f3177346f235b7300a5ca63fe07

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 044e459f5b6bf8e97f1bb43d5434573f6f7ec11d9dc7fcf6912c3e940d3b9dac
MD5 8b12f3c8749e921f258355401c8998d2
BLAKE2b-256 72bec7f259bfe111ef255e069776c87001acac52ff9285782742ac8dbf5466c6

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ab5add8b2d72490a2e94cc5d3fcb84e89104a93c56076c3ffe4057bf462761bb
MD5 18bf3786ed8fe7bfb145cc5685ab3e89
BLAKE2b-256 35213cc6535eb9b1d9a979b175570e361807ffafa332a5a37052209cdbd77996

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: pyastar2d-1.0.6-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.12

File hashes

Hashes for pyastar2d-1.0.6-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 68cb8b1dfb3688897edbd031cef92c3429f07e5b3daf90c3522c17c9b6dcfc4d
MD5 62b41d321a6bd2e987bcc256241c27ec
BLAKE2b-256 a756f2d6b0fa65c7a7b80a8c20b3d4176a1f421600d0decc67dae51c24c789dd

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 803136c66016f7be197683bdb0d1305ecc545b019f4c10f043c173efa047589a
MD5 070bd3a32ce072b1d5e00db8cee86ac2
BLAKE2b-256 6e753644c6e98b0df6837026055c22317f642d6166ded8b7fc23064403d58d0f

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp39-cp39-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp39-cp39-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 75405bddf964cc8da2da50059517627e8eec10e3fd2674d4ea9fd29ab0c24992
MD5 7c692052d669fab4796e4330bff62c98
BLAKE2b-256 61ceac2a4b0cc771547e5e2795b61236cc7f83c1fef7d070c0668f53421747f0

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pyastar2d-1.0.6-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for pyastar2d-1.0.6-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 a92ba5bdd2d5414d314c2acc22600311f6ee6b875a54fe2cea0add1b194fe045
MD5 c8c0f93514b1f719f493897fc8c72977
BLAKE2b-256 04bc87f280638fb313926c0bbbd26c011fa80ba70974e0eb7726496d30774b1f

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 06d3478bf533047544c2d6f518074ff5b09844f44bfe5bf02ebaf4c148540ffa
MD5 069a66dac45632ed60a7ed6e268d3bf3
BLAKE2b-256 fdbac3ba47c7101e4c5e2ab302e49f643a02a1b927db6bec5d53b2798cd5bd21

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 f77c265b00403f4b4230ed355dd7e574c8e55dc521e223437d673a147daca0ae
MD5 c18f880aac9454519ef5645507d08cf7
BLAKE2b-256 7157341106b5ad6a063459a15002fcb61601d543140b0d32efabe3a1b1c1cdbb

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp37-cp37m-win_amd64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 7b66c9d1c1e5e3680f8de38d5ecbd2ec4c9bb025bfb9485aad292d1d2412ee28
MD5 b949dc85f3d6eba5d8b40b252c8e076b
BLAKE2b-256 326b3c0a634882b6ec10fed64f447f95fb18852428cd7260c13b441b7fbc5f19

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c67ddabce94c2f9cde6a8c70edcd5b474032d71e8002916725b24ab8d2dd570
MD5 0fca76267d014c46392e604a1eda6505
BLAKE2b-256 f5544a2b8d6178a674926e3d4908a8fd9c2f69205503784a39cc1a54106b1397

See more details on using hashes here.

File details

Details for the file pyastar2d-1.0.6-cp37-cp37m-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.0.6-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 dc2aa40cd33c10a7e6ef72c35a31280bb55ec97a07cbb3db18348ffb37937c41
MD5 1df5d76dcb8044ed041fefad5f7ff91d
BLAKE2b-256 044145202fb08448fd45968bb975980af99604bc63d676aafab3c88f31b7777e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page