astar

Simple implementation of the a-star algorithm in Python 🌟

https://github.com/jrialland/python-astar

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • â—‹
    CITATION.cff file
  • ✓
    codemeta.json file
    Found codemeta.json file
  • ✓
    .zenodo.json file
    Found .zenodo.json file
  • â—‹
    DOI references
  • â—‹
    Academic publication links
  • â—‹
    Committers with academic emails
  • â—‹
    Institutional organization owner
  • â—‹
    JOSS paper metadata
  • â—‹
    Scientific vocabulary similarity
    Low similarity (11.6%) to scientific vocabulary

Keywords

graph-theory-algorithms pathfinding shortest-path-algorithm

Keywords from Contributors

interactive serializer packaging network-simulation hacking autograding observability embedded optim standardization
Last synced: 6 months ago · JSON representation

Repository

Simple implementation of the a-star algorithm in Python 🌟

Basic Info
  • Host: GitHub
  • Owner: jrialland
  • License: bsd-3-clause
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 1.24 MB
Statistics
  • Stars: 242
  • Watchers: 6
  • Forks: 67
  • Open Issues: 0
  • Releases: 0
Topics
graph-theory-algorithms pathfinding shortest-path-algorithm
Created over 11 years ago · Last pushed 7 months ago
Metadata Files
Readme License

README.rst

.. image:: https://badge.fury.io/py/astar.svg
    :target: https://badge.fury.io/py/astar

.. image:: https://github.com/jrialland/python-astar/actions/workflows/pythonpackage.yml/badge.svg
    :target: https://github.com/jrialland/python-astar/actions/workflows/pythonpackage.yml
    
.. image:: https://coveralls.io/repos/github/jrialland/python-astar/badge.svg?branch=master
    :target: https://coveralls.io/github/jrialland/python-astar?branch=master

.. image:: https://img.shields.io/github/stars/jrialland/python-astar
    :target: https://github.com/jrialland/python-astar

.. image:: https://img.shields.io/github/forks/jrialland/python-astar
    :target: https://github.com/jrialland/python-astar

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
~~~~~~~~~

.. code:: py

    @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
~~~~~~~~~~~~~~~~~

.. code:: py

    @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
~~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: py

    @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
~~~~~~~~~~~~~~~~~

.. code:: py

    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 

.. code:: 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``

.. code:: sh

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

    OK


Owner

  • Name: Julien Rialland
  • Login: jrialland
  • Kind: user
  • Location: Brest, France

GitHub Events

Total
  • Watch event: 26
  • Delete event: 2
  • Issue comment event: 2
  • Push event: 9
  • Pull request review event: 5
  • Pull request event: 6
  • Fork event: 5
  • Create event: 5
Last Year
  • Watch event: 26
  • Delete event: 2
  • Issue comment event: 2
  • Push event: 9
  • Pull request review event: 5
  • Pull request event: 6
  • Fork event: 5
  • Create event: 5

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 124
  • Total Committers: 8
  • Avg Commits per committer: 15.5
  • Development Distribution Score (DDS): 0.379
Past Year
  • Commits: 13
  • Committers: 4
  • Avg Commits per committer: 3.25
  • Development Distribution Score (DDS): 0.615
Top Committers
Name Email Commits
Julien Rialland j****d@g****m 77
Julien Rialland j****d@a****m 20
dependabot[bot] 4****] 18
Michael Kopp k****l@y****e 4
Matthias Urlichs m****s@u****e 2
bolek117 b****7 1
Giorgi v****a@g****m 1
Clinton p****k 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 9
  • Total pull requests: 37
  • Average time to close issues: 23 days
  • Average time to close pull requests: 25 days
  • Total issue authors: 9
  • Total pull request authors: 7
  • Average comments per issue: 2.11
  • Average comments per pull request: 0.32
  • Merged pull requests: 29
  • Bot issues: 0
  • Bot pull requests: 26
Past Year
  • Issues: 0
  • Pull requests: 10
  • Average time to close issues: N/A
  • Average time to close pull requests: 12 days
  • Issue authors: 0
  • Pull request authors: 3
  • Average comments per issue: 0
  • Average comments per pull request: 0.2
  • Merged pull requests: 6
  • Bot issues: 0
  • Bot pull requests: 5
Top Authors
Issue Authors
  • mccalluc (1)
  • eyaler (1)
  • jaromiru (1)
  • matching-transforming-anns (1)
  • vhamon (1)
  • elektito (1)
  • wncka (1)
  • erelsgl (1)
  • thorhunter1 (1)
Pull Request Authors
  • dependabot[bot] (34)
  • smurfix (4)
  • jrialland (3)
  • pygeek (2)
  • giorgiperad (2)
  • kopp (1)
  • bolek117 (1)
Top Labels
Issue Labels
bug (1)
Pull Request Labels
dependencies (34) python (4)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 9,386 last-month
  • Total dependent packages: 1
  • Total dependent repositories: 23
  • Total versions: 10
  • Total maintainers: 1
pypi.org: astar

generic A-Star path searching algorithm

  • Versions: 10
  • Dependent Packages: 1
  • Dependent Repositories: 23
  • Downloads: 9,386 Last month
Rankings
Dependent repos count: 3.0%
Average: 4.8%
Dependent packages count: 4.8%
Downloads: 5.3%
Stargazers count: 5.3%
Forks count: 5.5%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/pythonpackage.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
poetry.lock pypi
  • Pygments 2.14.0 develop
  • SecretStorage 3.3.3 develop
  • attrs 22.2.0 develop
  • bleach 5.0.1 develop
  • certifi 2022.12.7 develop
  • cffi 1.15.1 develop
  • charset-normalizer 2.1.1 develop
  • colorama 0.4.6 develop
  • commonmark 0.9.1 develop
  • cryptography 39.0.0 develop
  • docutils 0.19 develop
  • exceptiongroup 1.1.0 develop
  • idna 3.4 develop
  • importlib-metadata 6.0.0 develop
  • importlib-resources 5.10.2 develop
  • iniconfig 2.0.0 develop
  • jaraco.classes 3.2.3 develop
  • jeepney 0.8.0 develop
  • keyring 23.13.1 develop
  • more-itertools 9.0.0 develop
  • mypy 0.991 develop
  • mypy-extensions 0.4.3 develop
  • packaging 23.0 develop
  • pkginfo 1.9.6 develop
  • pluggy 1.0.0 develop
  • pycparser 2.21 develop
  • pytest 7.2.0 develop
  • pywin32-ctypes 0.2.0 develop
  • readme-renderer 37.3 develop
  • requests 2.28.1 develop
  • requests-toolbelt 0.10.1 develop
  • rfc3986 2.0.0 develop
  • rich 13.0.1 develop
  • six 1.16.0 develop
  • tomli 2.0.1 develop
  • twine 4.0.2 develop
  • typing-extensions 4.4.0 develop
  • urllib3 1.26.13 develop
  • webencodings 0.5.1 develop
  • zipp 3.11.0 develop
pyproject.toml pypi
  • mypy >=0.991 develop
  • pytest >=7.2.0 develop
  • twine >=4.0.2 develop
  • python >=3.8,<4.0.0