简体   繁体   English

了解Python中A-star算法的这种实现

[英]Understanding this implementation of A-star algorithm in Python

I am new to Python. 我是Python的新手。 I have this Code with me http://code.activestate.com/recipes/577519-a-star-shortest-path-algorithm/history/1/ 我有此代码http://code.activestate.com/recipes/577519-a-star-shortest-path-algorithm/history/1/

its the A star algorithm code. 其A星算法代码。 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! 请帮帮我,谢谢!

The path-finding algorithm in function pathFind() is well-commented and decomposed. 函数pathFind()的寻路算法经过了很好的注释和分解。 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. 重要的代码行是# generate moves (child nodes) in all possible directions ,这无疑是密集的代码,而不是非常面向对象的。

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: 我建议您将参数(m,n)减小为4x5或5x6,然后使用pdb调试器(单步或在关键点设置断点)逐步执行它,或者根据需要入侵尽可能多的打印语句:

# 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). 首先尝试给它提供自己的自定义地图,以了解其工作方式(修改# fillout the map matrix...行)。 Then run it interactively in the debugger. 然后在调试器中以交互方式运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM