ast

astar

Simple implementation of the a-star algorithm in Python 🌟

Showing:

Popularity

Downloads/wk

0

GitHub Stars

101

Maintenance

Last Commit

9mos ago

Contributors

1

Package

Dependencies

0

License

BSD

Categories

Readme

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

.. image:: https://travis-ci.com/jrialland/python-astar.svg?branch=master :target: https://travis-ci.com/jrialland/python-astar

.. 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 <https://en.wikipedia.org/wiki/A*_search_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 :

neighbors


.. code:: py

    @abstractmethod
    def neighbors(self, node)

For a given node, returns (or yields) the list of its neighbors. this
method must be implemented in a subclass

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

This method 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).

This method 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 he
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.

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

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial