{"id":19494603,"url":"https://github.com/jrialland/python-astar","last_synced_at":"2025-05-15T09:08:35.972Z","repository":{"id":18996896,"uuid":"22218925","full_name":"jrialland/python-astar","owner":"jrialland","description":"Simple implementation of the a-star algorithm in Python 🌟","archived":false,"fork":false,"pushed_at":"2025-01-20T18:04:58.000Z","size":1232,"stargazers_count":234,"open_issues_count":0,"forks_count":67,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-15T01:59:22.875Z","etag":null,"topics":["graph-theory-algorithms","pathfinding","shortest-path-algorithm"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jrialland.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-07-24T15:44:31.000Z","updated_at":"2025-04-13T16:16:51.000Z","dependencies_parsed_at":"2024-04-15T22:24:06.369Z","dependency_job_id":"b0de6dd4-1e83-4542-a01b-8f642ed65098","html_url":"https://github.com/jrialland/python-astar","commit_stats":{"total_commits":117,"total_committers":7,"mean_commits":"16.714285714285715","dds":0.3846153846153846,"last_synced_commit":"f644c62d3a43afbe3fcefa6cd7f8d6e34a658362"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrialland%2Fpython-astar","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrialland%2Fpython-astar/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrialland%2Fpython-astar/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jrialland%2Fpython-astar/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jrialland","download_url":"https://codeload.github.com/jrialland/python-astar/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254310520,"owners_count":22049470,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graph-theory-algorithms","pathfinding","shortest-path-algorithm"],"created_at":"2024-11-10T21:30:49.305Z","updated_at":"2025-05-15T09:08:30.962Z","avatar_url":"https://github.com/jrialland.png","language":"Python","readme":".. image:: https://badge.fury.io/py/astar.svg\n    :target: https://badge.fury.io/py/astar\n\n.. image:: https://github.com/jrialland/python-astar/actions/workflows/pythonpackage.yml/badge.svg\n    :target: https://github.com/jrialland/python-astar/actions/workflows/pythonpackage.yml\n    \n.. image:: https://coveralls.io/repos/github/jrialland/python-astar/badge.svg?branch=master\n    :target: https://coveralls.io/github/jrialland/python-astar?branch=master\n\n.. image:: https://img.shields.io/github/stars/jrialland/python-astar\n    :target: https://github.com/jrialland/python-astar\n\n.. image:: https://img.shields.io/github/forks/jrialland/python-astar\n    :target: https://github.com/jrialland/python-astar\n\npython-astar\n============\n\nThis is a simple implementation of the `a-star path finding\nalgorithm \u003chttps://en.wikipedia.org/wiki/A*_search_algorithm\u003e`__ in\npython\n\nDocumentation\n-------------\n\nThe `astar` module defines the `AStar` class, which has to be inherited from\nand completed with the implementation of several methods.\n\nThe functions take/return _node_ objects.\nThe `astar` library only requires the following property from these objects:\n\n- They must be hashable (i.e. implement `__hash__`).\n\nFor the default implementation of `is_goal_reached`, the objects must be\ncomparable for same-ness (i.e. implement `__eq__`).\n\nThe computation of the hash may be implemented by several means :\n - base types like strings, floats, integers, tuples. already implement __hash__\n - `dataclass https://docs.python.org/3/library/dataclasses.html#dataclasses.dataclass ` objects declared with `@dataclass(frozen=True)` directly implement `__hash__` if possible.\n - 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.\n\nneighbors\n~~~~~~~~~\n\n.. code:: py\n\n    @abstractmethod\n    def neighbors(self, node)\n\nFor a given node, returns (or yields) the list of its neighbors.\n\nThis is the method that one would provide in order to give to the\nalgorithm the description of the graph to use during for computation.\n\nAlternately, your override method may be named \"path\\_neighbors\". Instead of\nyour node, this method receives a \"SearchNode\" object whose \"came_from\"\nattribute points to the previous node; your node is in its \"data\" attribute.\nYou might want to use this if your path is directional, like the track of a\ntrain that can't do 90° turns.\n\nOne of these methods must be implemented in a subclass.\n\n\ndistance\\_between\n~~~~~~~~~~~~~~~~~\n\n.. code:: py\n\n    @abstractmethod\n    def distance_between(self, n1, n2)\n\nGives the real distance/cost between two adjacent nodes n1 and n2 (i.e\nn2 belongs to the list of n1's neighbors). n2 is guaranteed to belong to\nthe list returned by a call to neighbors(n1).\n\nAlternately, you may override \"path\\_distance\\_between\". The arguments\nwill be a \"SearchNode\", as in \"path\\_neighbors\". You might want to use this\nif your distance measure should include the path's attainable speed, the\nkind and number of turns on it, or similar. You can use the nodes' \"cache\"\nattributes to store some data, to speed up calculation.\n\nOne of these methods must be implemented in a subclass.\n\n\nheuristic\\_cost\\_estimate\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: py\n\n    @abstractmethod\n    def heuristic_cost_estimate(self, current, goal)\n\nComputes the estimated (rough) distance/cost between a node and the\ngoal. The first argument is the start node, or any node that have been\nreturned by a call to the neighbors() method.\n\nThis method is used to give to the algorithm an hint about the node it\nmay try next during search.\n\nThis method must be implemented in a subclass.\n\nis\\_goal\\_reached\n~~~~~~~~~~~~~~~~~\n\n.. code:: py\n\n    def is_goal_reached(self, current, goal)\n\nThis method shall return a truthy value when the goal is 'reached'. By\ndefault it checks that `current == goal`.\n\n\n\"Functional\" API.\n~~~~~~~~~~~~~~~~~\n\nIf 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.\n\nSee \u003chttps://github.com/jrialland/python-astar/blob/master/tests/basic/test_basic.py\u003e\n\n.. code:: py\n\n    def find_path(\n    \tstart,\n    \tgoal,\n    \tneighbors_fnct,\n    \treversePath=False,\n    \theuristic_cost_estimate_fnct = lambda a, b: Infinite,\n    \tdistance_between_fnct = lambda a, b: 1.0,\n    \tis_goal_reached_fnct = lambda a, b: a == b\n    \t)\n\nExamples\n--------\n\nMaze solver\n~~~~~~~~~~~\n\nThis script generates an ascii maze, and finds the path between the upper left corner and the bottom right\n\n``PYTHONPATH=. python tests/maze/test_maze.py``\n\n::\n\n    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n    |####    |     |              |        |              |     |\n    +--+# +  +  +  +  +--+--+--+  +  +--+  +--+--+--+  +--+  +  +\n    | ### |  |  |  |  |        |  |     |     |        |     |  |\n    + #+--+--+  +  +  +--+  +--+  +  +--+--+  +  +--+--+  +--+  +\n    | #|        |  |  |     |     |  |        |  |     |  |     |\n    + #+  +--+--+  +  +  +--+  +--+  +  +--+--+  +  +  +  +  +--+\n    | #|        |  |  |     |     |  |     |        |     |     |\n    + #+--+--+  +  +  +--+  +--+  +  +--+--+  +--+--+--+--+--+  +\n    | #      |  |  |  |        |     | ### |  |     |        |  |\n    + #+--+--+  +  +  +  +--+--+--+--+ #+# +  +--+  +  +--+  +  +\n    | #         |     |       ####| ####|# |  |     |     |  |  |\n    + #+--+--+--+--+--+--+--+ #+ #+ #+--+# +  +  +  +--+  +  +  +\n    | #|    ####|       #######| ####| ### |     |     |  |     |\n    + #+--+ #+ #+--+--+ #+--+--+--+--+ #+--+  +--+--+--+  +--+--+\n    | ####| #| ##########|           | ### |  | ###### |        |\n    +--+ #+ #+--+--+--+--+  +--+--+  +--+# +--+ #+--+# +--+--+  +\n    |  | ####|        |     |           |########|  |##| ### |  |\n    +  +--+--+  +--+  +  +--+  +--+--+  +--+--+--+  + #+ #+# +  +\n    |        |     |  |  |     |                    | ####|#### |\n    +  +--+--+--+  +  +  +  +--+  +--+--+--+--+--+  +--+--+--+# +\n    |  |           |     |     |     | ####|     |     | ###### |\n    +  +  +--+--+--+--+--+  +  +--+--+##+ #+--+  +--+  + #+--+--+\n    |     |  |           |  |  | ###### | ####|        | ### |  |\n    +  +--+  +  +--+--+  +--+  + #+--+--+--+ #+--+--+--+--+# +  +\n    |        |  |     |        | ###### |  | ############ |# |  |\n    +--+--+--+  +  +  +--+--+  +--+--+# +  +--+--+--+--+# +# +  +\n    |           |  |  |        | ###### | ##########|  |#### |  |\n    +  +--+  +--+--+  +  +--+--+ #+--+--+ #+--+--+ #+  +--+--+  +\n    |  |     |     |        | ####|     | #######| ############ |\n    +  +--+--+  +  +--+  +--+ #+--+--+  +  +--+ #+--+--+--+--+# +\n    |        |  |     |  | ####| ####|        | #| ### |     |##|\n    +--+--+  +  +--+  +  + #+--+ #+ #+--+--+  + #+ #+# +  +  + #+\n    |        |  |     |  | #######| ####|     | #| #|# |  |  | #|\n    +  +--+  +  +  +--+--+--+--+--+--+ #+--+--+ #+ #+# +--+  + #+\n    |  |     |  |  |                 | #| ####| ####|# |     | #|\n    +  +  +--+  +  +  +--+--+--+--+  + #+ #+ #+--+--+# +  +  + #+\n    |  |  |     |  |        |     |  | ####| ######### |  |  | #|\n    +  +--+  +--+  +--+--+  +  +  +  +--+--+--+--+--+--+  +--+ #+\n    |           |              |  |                            #|\n    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+\n    \n   \nLondon Underground\n~~~~~~~~~~~~~~~~~~\n\nThis script finds the shortest path between two underground stations, based on a list of London's stations\n\n``PYTHONPATH=. python tests/london/test_london_underground.py Chesham Beckton``\n\n::\n\n    Chesham\n    Chalfont \u0026 Latimer\n    Chorleywood\n    Rickmansworth\n    Moor Park\n    Northwood\n    Northwood Hills\n    Pinner\n    North Harrow\n    Harrow-on-the-Hill\n    Northwick Park\n    Preston Road\n    Wembley Park\n    Finchley Road\n    Baker Street\n    Bond Street\n    Oxford Circus\n    Tottenham Court Road\n    Holborn\n    Chancery Lane\n    St. Paul's\n    Bank\n    Shadwell\n    Limehouse\n    Westferry\n    Poplar\n    Blackwall\n    East India\n    Canning Town\n    Royal Victoria\n    Custom House\n    Prince Regent\n    Royal Albert\n    Beckton Park\n    Cyprus\n    Gallions Reach\n    Beckton\n\n\nTAN Network\n~~~~~~~~~~~\n\nA solution for a codingame's puzzle (https://www.codingame.com/training/hard/tan-network)\n\n``PYTHONPATH=. python tests/tan_network/test_tan_network_5.py``\n\n.. code:: sh\n\n    .\n    ----------------------------------------------------------------------\n    Ran 1 test in 0.010s\n\n    OK\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrialland%2Fpython-astar","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjrialland%2Fpython-astar","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjrialland%2Fpython-astar/lists"}