Jeet Mody
Jeet Mody

Reputation: 21

Understanding this implementation of A-star algorithm in Python

I am new to Python. I have this Code with me http://code.activestate.com/recipes/577519-a-star-shortest-path-algorithm/history/1/

its the A star algorithm code. I wanted to know how exactly is it generating the obstacles. Basically I have a black and white image. My task is to reach the end point from the start point avoiding the white spaces. Please help me Thank you!

Upvotes: 2

Views: 6486

Answers (1)

smci
smci

Reputation: 33938

The path-finding algorithm in function pathFind() is well-commented and decomposed. If there's something specific confusing you, tell us what it is. The important lines are after # generate moves (child nodes) in all possible directions, which admittedly is dense code and not very OO.

I suggest you reduce the parameters (m,n) to a 4x5 or 5x6 then step through its execution, either with pdb debugger (single-step or set breakpoints at key points), or hacking in as many print statements as you need:

# map matrix
n = 30 # horizontal size
m = 30 # vertical size

First try giving it your own custom maps to get a feel for how it works (modify the # fillout the map matrix... lines). Then run it interactively in the debugger.

Upvotes: 2

Related Questions