Giter VIP home page Giter VIP logo

python-astar's Introduction

https://travis-ci.org/jrialland/python-astar.svg?branch=master

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 :

neighbors

@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

@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

@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

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.

Example

The maze solver example is executed by running

PYTHONPATH=./src python ./src/test/maze.py

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

python-astar's People

Contributors

jrialland avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.