GithubHelp home page GithubHelp logo

zatrit / python-pathfinding Goto Github PK

View Code? Open in Web Editor NEW

This project forked from brean/python-pathfinding

0.0 0.0 0.0 187 KB

Implementation of common pathfinding algorithms

License: MIT License

Python 57.32% Jupyter Notebook 42.68%

python-pathfinding's Introduction

python-pathfinding

Pathfinding algorithms for python 3.

Currently there are 7 path-finders bundled in this library, namely:

  • A*
  • Dijkstra
  • Best-First
  • Bi-directional A*
  • Breadth First Search (BFS)
  • Iterative Deeping A* (IDA*)
  • Minimum Spanning Tree (MSP)

Dijkstra and A* take the weight of the fields on the map into account.

MIT License PyPI

If you are still using python 2 take a look at the (unmaintained) python2-branch.

Installation

This library is provided by pypi, so you can just install the current stable version using pip:

pip install pathfinding

see pathfinding on pypi

Usage examples

For usage examples with detailed descriptions take a look at the docs folder, also take a look at the test/ folder for more examples, e.g. how to use pandas

Rerun the algorithm

While running the pathfinding algorithm it might set values on the nodes. Depending on your path finding algorithm things like calculated distances or visited flags might be stored on them. So if you want to run the algorithm in a loop you need to clean the grid first (see Grid.cleanup). Please note that because cleanup looks at all nodes of the grid it might be an operation that can take a bit of time!

Implementation details

All pathfinding algorithms in this library are inheriting the Finder class. It has some common functionality that can be overwritten by the implementation of a path finding algorithm.

The normal process works like this:

  1. You call find_path on one of your finder implementations.
  2. init_find instantiates the open_list and resets all values and counters.
  3. The main loop starts on the open_list. This list gets filled with all nodes that will be processed next (e.g. all current neighbors that are walkable). For this you need to implement check_neighbors in your own finder implementation.
  4. For example in A*s implementation of check_neighbors you first want to get the next node closest from the current starting point from the open list. the next_node method in Finder does this by giving you the node with a minimum f-value from the open list, it closes it and removes it from the open_list.
  5. if this node is not the end node we go on and get its neighbors by calling find_neighbors. This just calls grid.neighbors for most algorithms.
  6. If none of the neighbors are the end node we want to process the neighbors to calculate their distances in process_node
  7. process_node calculates the cost f from the start to the current node using the calc_cost method and the cost after calculating h from apply_heuristic.
  8. finally process_node updates the open list so find_path can run check_neighbors on it in the next node in the next iteration of the main loop.

flow:

  find_path
    init_find  # (re)set global values and open list
    check_neighbors  # for every node in open list
      next_node  # closest node to start in open list
      find_neighbors  # get neighbors
      process_node  # calculate new cost for neighboring node

Testing

You can run the tests locally using pytest. Take a look at the test-folder

You can follow below steps to setup your virtual environment and run the tests.

# Go to repo
cd python-pathfinding

# Setup virtual env and activate it - Mac/Linux for windows use source venv/Scripts/activate
python3 -m venv venv
source venv/bin/activate

# Install test requirements
pip install -r test/requirements.txt

# Run all the tests
pytest

Contributing

Please use the issue tracker to submit bug reports and feature requests. Please use merge requests as described here to add/adapt functionality.

License

python-pathfinding is distributed under the MIT license.

Maintainer

Andreas Bresser, [email protected]

Authors / Contributers

Authors and contributers are listed on github.

Inspired by Pathfinding.JS

python-pathfinding's People

Contributors

bradbeattie avatar brean avatar clementj18 avatar dependabot[bot] avatar harisankar95 avatar hstehstehste avatar juliannorton avatar kingofpayne avatar peterchenadded avatar praneethjain avatar pyrooka 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.