Skip to main content

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

Project description

Build 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

pytest

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

Code Formatting

It's recommended that you use pre-commit to ensure linting procedures are run on any code you write. See pre-commit.com for more information.

Reference pre-commit's installation instructions for software installation on your OS/platform. After you have the software installed, run pre-commit install on the command line. Now every time you commit to this project's code base the linter procedures will automatically run over the changed files. To run pre-commit on files preemtively from the command line use:

pip install -r requirements-dev.txt
pre-commit run --all-files

The code base has been formatted by Black. Furthermore, try to conform to PEP8. You should set up your preferred editor to use flake8 as its Python linter, but pre-commit will ensure compliance before a git commit is completed. This will use the flake8 configuration within .flake8, which ignores several errors and stylistic considerations.

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.1.1.tar.gz (13.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyastar2d-1.1.1-cp39-abi3-win_amd64.whl (15.3 kB view details)

Uploaded CPython 3.9+Windows x86-64

pyastar2d-1.1.1-cp39-abi3-win32.whl (14.8 kB view details)

Uploaded CPython 3.9+Windows x86

pyastar2d-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl (1.1 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ x86-64

pyastar2d-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl (1.0 MB view details)

Uploaded CPython 3.9+musllinux: musl 1.2+ ARM64

pyastar2d-1.1.1-cp39-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (56.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.24+ x86-64manylinux: glibc 2.28+ x86-64

pyastar2d-1.1.1-cp39-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl (56.6 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.24+ ARM64manylinux: glibc 2.28+ ARM64

pyastar2d-1.1.1-cp39-abi3-macosx_11_0_arm64.whl (12.6 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

pyastar2d-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl (12.1 kB view details)

Uploaded CPython 3.9+macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: pyastar2d-1.1.1.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyastar2d-1.1.1.tar.gz
Algorithm Hash digest
SHA256 73b6375284851f53112aeb8c5c2abc81576d594928705ca76274360e67b0e6c0
MD5 66e93abcfdd7fbe3c4d0e326a51ce965
BLAKE2b-256 9f038333b690c13f245a10d9b6883b816c3fbb0721453b96a60c90b0648503da

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: pyastar2d-1.1.1-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 f30b3ba6db094c4f4a9c8572c97b65884f6217971e4c75e48b683ee5c09bb0a2
MD5 d21775f1347ad95ed842a8f71d5e971d
BLAKE2b-256 2fe259181215e05a8414d4d269042bc0da61fd7e0c0dca2a22724dc79af7098e

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-win32.whl.

File metadata

  • Download URL: pyastar2d-1.1.1-cp39-abi3-win32.whl
  • Upload date:
  • Size: 14.8 kB
  • Tags: CPython 3.9+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-win32.whl
Algorithm Hash digest
SHA256 c4957107bdb13a4018a66755614d12526a12201d563465aeb8d34ca49ca1d4bc
MD5 dd4832baef9a854fa8b3789be1990372
BLAKE2b-256 e3c3970bb1248a9d72e6a6e4dacd0528d0d1ce64cc4b2a42c21d9f9c91f9f2d0

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e58e1379992a06e67eaf951231c1c6a925e729db3534d0def3e46a12384b5255
MD5 fba5d81cd64c65734e75f4cf2e7cf488
BLAKE2b-256 d456312569fa941ec295e4cc2db56e182967349289a5e822eff2c39895eea22f

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5dd105ae437fa22245e19723e1ff8885ade43552adfc1f86bdd577194d352037
MD5 f3676fca4fd63372ea53daad9ae44b48
BLAKE2b-256 57e527aa951b3beed307481ea45ec3f4e63d9d633624f72ec57feab1c4435269

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 460e3e308ee45010b2d810fdc92c1f45a50845a28cdb7a3546205b345aa3bd29
MD5 344dc8e6fff28e874586252174dedfbc
BLAKE2b-256 387bc695c4d6c584012d534c0acb300b3f5c99685f8ad5a729f71cb86358762b

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 cceaa19f7d5e4c68124ac5881950035fac183a6229ca1ae4251f1b5445b145d0
MD5 e1dc2a3e8bf228191193cabbaf7fa4fc
BLAKE2b-256 9e3d2b5cb62b8c56a022bd3445ee0cc432a7026856c4ab1e5cac32e45cadcf62

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bfb83a12bc6e9e809e837435448a9b8d649b80118eac094186e07dafdd18c194
MD5 d5ff48c4c860deb4a8e59d95934379cd
BLAKE2b-256 d3f6e4c48044964d643b5842de36142aec3f5743ec358a4fcffb7e2f1e3bf8e2

See more details on using hashes here.

File details

Details for the file pyastar2d-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for pyastar2d-1.1.1-cp39-abi3-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4f7a8b9924d042717acf6efc94251adf40410ca2a46913b01b9d8f396334e196
MD5 7c2a8ff9660dbf43b82437d99cdbc87e
BLAKE2b-256 3fc54e7ea66138a72388805d238cd764ad8b4fbf67d8082edb645921b21686eb

See more details on using hashes here.

Supported by

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