Coder Social home page Coder Social logo

fxfactorial / a-star Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hjweide/pyastar2d

0.0 2.0 0.0 7.02 MB

A very simple A* implementation in C++ callable from Python for pathfinding on a two-dimensional grid.

License: MIT License

Makefile 1.31% C++ 31.95% Python 66.74%

a-star's Introduction

A*

This is a very simple C++ implementation of the A* algorithm for pathfinding on a two-dimensional grid. The compiled astar.so file is callable from Python. See pyastar.py for the Python wrapper and examples.py for example usage. Uses 4-connectivity by default, set allow_diagonal=True for 8-connectivity.

Motivation

I recently needed an implementation of the A* algorithm in Python. Normally I would simply use networkx, but for graphs with millions of nodes the overhead incurred to construct the graph can be expensive. Considering that my use case was so simple, I decided to implement it myself.

Usage

Run make to build the shared object file astar.so.

import numpy as np
import pyastar
# The minimum cost must be 1 for the heuristic to be valid.
weights = np.array([[1, 3, 3, 3, 3],
                    [2, 1, 3, 3, 3],
                    [2, 2, 1, 3, 3],
                    [2, 2, 2, 1, 3],
                    [2, 2, 2, 2, 1]], dtype=np.float32)
# The start and goal coordinates are in matrix coordinates (i, j).
path = pyastar.astar_path(weights, (0, 0), (4, 4), allow_diagonal=True)
print(path)
# The path is returned as a numpy array of (i, j) coordinates.
array([[0, 0],
       [1, 1],
       [2, 2],
       [3, 3],
       [4, 4]])

Example Results

To test the implementation, I grabbed two nasty mazes from Wikipedia. They are included in the mazes directory, but are originally from here: Small and Large. I load the .png files as grayscale images, and set the white pixels to 1 (open space) and the black pixels to INF (walls).

To run the examples:

  1. Run make to build the shared object file astar.so.
  2. Set the MAZE_FPATH and OUTP_FPATH as desired in examples.py.
  3. Run python examples.py.

Output for the small maze:

time python examples.py
loaded maze of shape (1802, 1802)
found path of length 10032 in 0.258270s
plotting path to solns/maze_small_soln.png
done

real  0m2.319s
user  0m0.403s
sys 0m1.691s

The solution is visualized below: Maze Small Solution

Output for the large maze:

time python examples.py
loaded maze of shape (4002, 4002)
found path of length 783737 in 3.886067s
plotting path to solns/maze_large_soln.png
done

real  0m6.495s
user  0m4.007s
sys 0m2.273s

The solution is visualized below: Maze Large Solution

Tests

To run the tests, simply run py.test in the tests directory.

cd tests
py.test

The tests are fairly basic but cover some of the more common pitfalls. Pull requests for more extensive tests are welcome.

References

  1. A* search algorithm on Wikipedia
  2. Pathfinding with A* on Red Blob Games

a-star's People

Watchers

 avatar  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.