Skip to main content
https://badge.fury.io/py/astar.svg https://github.com/jrialland/python-astar/actions/workflows/pythonpackage.yml/badge.svg https://coveralls.io/repos/github/jrialland/python-astar/badge.svg?branch=master https://img.shields.io/github/stars/jrialland/python-astar https://img.shields.io/github/forks/jrialland/python-astar https://repobeats.axiom.co/api/embed/2e7245f5b553bf4fec1eea2e9ba9040b8e7992c7.svg

python-astar

This is a simple implementation of the a-star path finding algorithm in python

Documentation

The astar module defines the AStar class, which has to be inherited from and completed with the implementation of several methods.

The functions take/return _node_ objects. The astar library only requires the following property from these objects:

  • They must be hashable (i.e. implement __hash__).

For the default implementation of is_goal_reached, the objects must be comparable for same-ness (i.e. implement __eq__).

The computation of the hash may be implemented by several means :
  • base types like strings, floats, integers, tuples. already implement __hash__

  • dataclass https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass ` objects declared with `@dataclass(frozen=True) directly implement __hash__ if possible.

  • by overriding the __hash__() https://docs.python.org/3/reference/datamodel.html#object.__hash__ method as a combination of the fields that you consider relevant for your object.

neighbors

@abstractmethod
def neighbors(self, node)

For a given node, returns (or yields) the list of its neighbors.

This is the method that one would provide in order to give to the algorithm the description of the graph to use during for computation.

Alternately, your override method may be named “path_neighbors”. Instead of your node, this method receives a “SearchNode” object whose “came_from” attribute points to the previous node; your node is in its “data” attribute. You might want to use this if your path is directional, like the track of a train that can’t do 90° turns.

One of these methods must be implemented in a subclass.

distance_between

@abstractmethod
def distance_between(self, n1, n2)

Gives the real distance/cost between two adjacent nodes n1 and n2 (i.e n2 belongs to the list of n1’s neighbors). n2 is guaranteed to belong to the list returned by a call to neighbors(n1).

Alternately, you may override “path_distance_between”. The arguments will be a “SearchNode”, as in “path_neighbors”. You might want to use this if your distance measure should include the path’s attainable speed, the kind and number of turns on it, or similar. You can use the nodes’ “cache” attributes to store some data, to speed up calculation.

One of these methods must be implemented in a subclass.

heuristic_cost_estimate

@abstractmethod
def heuristic_cost_estimate(self, current, goal)

Computes the estimated (rough) distance/cost between a node and the goal. The first argument is the start node, or any node that have been returned by a call to the neighbors() method.

This method is used to give to the algorithm an hint about the node it may try next during search.

This method must be implemented in a subclass.

is_goal_reached

def is_goal_reached(self, current, goal)

This method shall return a truthy value when the goal is ‘reached’. By default it checks that current == goal.

“Functional” API.

If you dislike to have to inherit from the AStar class and create an instance in order to run the algorithm, the module also provides a “find_path” function, which takes functions as parameters and provides reasonnable defaults for some of them.

See <https://github.com/jrialland/python-astar/blob/master/tests/basic/test_basic.py>

def find_path(
    start,
    goal,
    neighbors_fnct,
    reversePath=False,
    heuristic_cost_estimate_fnct = lambda a, b: Infinite,
    distance_between_fnct = lambda a, b: 1.0,
    is_goal_reached_fnct = lambda a, b: a == b
    )

Examples

Maze solver

This script generates an ascii maze, and finds the path between the upper left corner and the bottom right

PYTHONPATH=. python tests/maze/test_maze.py

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|####    |     |              |        |              |     |
+--+# +  +  +  +  +--+--+--+  +  +--+  +--+--+--+  +--+  +  +
| ### |  |  |  |  |        |  |     |     |        |     |  |
+ #+--+--+  +  +  +--+  +--+  +  +--+--+  +  +--+--+  +--+  +
| #|        |  |  |     |     |  |        |  |     |  |     |
+ #+  +--+--+  +  +  +--+  +--+  +  +--+--+  +  +  +  +  +--+
| #|        |  |  |     |     |  |     |        |     |     |
+ #+--+--+  +  +  +--+  +--+  +  +--+--+  +--+--+--+--+--+  +
| #      |  |  |  |        |     | ### |  |     |        |  |
+ #+--+--+  +  +  +  +--+--+--+--+ #+# +  +--+  +  +--+  +  +
| #         |     |       ####| ####|# |  |     |     |  |  |
+ #+--+--+--+--+--+--+--+ #+ #+ #+--+# +  +  +  +--+  +  +  +
| #|    ####|       #######| ####| ### |     |     |  |     |
+ #+--+ #+ #+--+--+ #+--+--+--+--+ #+--+  +--+--+--+  +--+--+
| ####| #| ##########|           | ### |  | ###### |        |
+--+ #+ #+--+--+--+--+  +--+--+  +--+# +--+ #+--+# +--+--+  +
|  | ####|        |     |           |########|  |##| ### |  |
+  +--+--+  +--+  +  +--+  +--+--+  +--+--+--+  + #+ #+# +  +
|        |     |  |  |     |                    | ####|#### |
+  +--+--+--+  +  +  +  +--+  +--+--+--+--+--+  +--+--+--+# +
|  |           |     |     |     | ####|     |     | ###### |
+  +  +--+--+--+--+--+  +  +--+--+##+ #+--+  +--+  + #+--+--+
|     |  |           |  |  | ###### | ####|        | ### |  |
+  +--+  +  +--+--+  +--+  + #+--+--+--+ #+--+--+--+--+# +  +
|        |  |     |        | ###### |  | ############ |# |  |
+--+--+--+  +  +  +--+--+  +--+--+# +  +--+--+--+--+# +# +  +
|           |  |  |        | ###### | ##########|  |#### |  |
+  +--+  +--+--+  +  +--+--+ #+--+--+ #+--+--+ #+  +--+--+  +
|  |     |     |        | ####|     | #######| ############ |
+  +--+--+  +  +--+  +--+ #+--+--+  +  +--+ #+--+--+--+--+# +
|        |  |     |  | ####| ####|        | #| ### |     |##|
+--+--+  +  +--+  +  + #+--+ #+ #+--+--+  + #+ #+# +  +  + #+
|        |  |     |  | #######| ####|     | #| #|# |  |  | #|
+  +--+  +  +  +--+--+--+--+--+--+ #+--+--+ #+ #+# +--+  + #+
|  |     |  |  |                 | #| ####| ####|# |     | #|
+  +  +--+  +  +  +--+--+--+--+  + #+ #+ #+--+--+# +  +  + #+
|  |  |     |  |        |     |  | ####| ######### |  |  | #|
+  +--+  +--+  +--+--+  +  +  +  +--+--+--+--+--+--+  +--+ #+
|           |              |  |                            #|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

London Underground

This script finds the shortest path between two underground stations, based on a list of London’s stations

PYTHONPATH=. python tests/london/test_london_underground.py Chesham Beckton

Chesham
Chalfont & Latimer
Chorleywood
Rickmansworth
Moor Park
Northwood
Northwood Hills
Pinner
North Harrow
Harrow-on-the-Hill
Northwick Park
Preston Road
Wembley Park
Finchley Road
Baker Street
Bond Street
Oxford Circus
Tottenham Court Road
Holborn
Chancery Lane
St. Paul's
Bank
Shadwell
Limehouse
Westferry
Poplar
Blackwall
East India
Canning Town
Royal Victoria
Custom House
Prince Regent
Royal Albert
Beckton Park
Cyprus
Gallions Reach
Beckton

TAN Network

A solution for a codingame’s puzzle (https://www.codingame.com/training/hard/tan-network)

PYTHONPATH=. python tests/tan_network/test_tan_network_5.py

.
----------------------------------------------------------------------
Ran 1 test in 0.010s

OK
https://api.star-history.com/svg?repos=jrialland/python-astar&type=date&legend=top-left

Metadata

Release files for astar 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astar 1.1
File Size Uploaded Provenance Actions
astar-1.1.tar.gz 6.6 kB Without provenance Details for astar-1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for astar 1.1
File Interpreter ABI Platform Actions
astar-1.1-py3-none-any.whl Python 3 none any Details for astar-1.1-py3-none-any.whl

Total release size: 14.0 kB

Release files / astar-1.1.tar.gz

Download URL astar-1.1.tar.gz
Size 6.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5627ca3db21f8a6cd9df483610355069def0dd687a857ce6349f50a8e93759be
BLAKE2b-256 checksum
How to use checksums
c519028ff09c0fdb2c185816768c2557698f1aa7ecf09466fb246d8bfe13556d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / astar-1.1-py3-none-any.whl

Download URL astar-1.1-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c8c013a58e2de4ffd0fb97bd994387539484fdd416afe4a540404ab9289ef86
BLAKE2b-256 checksum
How to use checksums
5fb7d6142de5410839f11557460555cc1a753f6a9fc7c6114ea9515a9c358522
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for astar 1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page