TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Introduction to A*

509 点作者 phenylene将近 11 年前

12 条评论

gavanwoolery将近 11 年前
Excellent article - I recently worked on some pathfinding stuff and the material I found on A* was not that great but this is very well laid out.<p>Important side note: A* or other &quot;best path&quot; algorithms are not always preferable - there are some situations where it makes sense to use even less ideal paths in exchange for greater speed and less memory usage - even ones that are faster than greedy-best-first search with A* shown here.<p>If you use recursive backtracking with a decent set of heuristics (i.e. choose the node with the closest distance to the goal, &quot;as the crow flies&quot;), it can be be fast and ideal in many situations (it will always give an ideal path when there are few hook or concave shapes to deal with, which in many games or applications is often the case). It can be done in linear time and memory - it is important to note the cost of adding and searching nodes in the algorithm as well. It will never &quot;fail&quot; but it will produce very bad paths sometimes in complex situations, although even then it occasionally produces ideal paths. In many situations where having an ideal path is not critical (say, townspeople just walking about doing their business) this can work well. Of course, you can use multiple algorithms or even mix them depending on what you are trying to achieve. RBT can be a good starting point just because it is so easy (maybe the easiest fail-safe algorithm outside of brute force?).<p>Secondary note: it is also good to subdivide your space - it does not need to be something complex like a sparse octree (in fact, that will make it harder to write and get good performance with in many situations). Rather consider something like two levels of nodes. On a 2D map, say you were to divide it into subsets of NxN grids of nodes. Each one of these node grids attaches to its neighbor based on whether or not it is traversable in that direction (i.e. no matter where you enter the node grid, there is a path out in that direction). This allows you to easily find a &quot;high level&quot; path which can be rapidly changed or discarded based on changing circumstances.
评论 #8060159 未加载
评论 #8059707 未加载
评论 #8060972 未加载
sjwright将近 11 年前
It&#x27;s not immediately obvious, but all the diagrams on that page are actually live and interactive demonstrations. You can move the entities, and you can reshape the walls as much as you like.
MagerValp将近 11 年前
Since Amit&#x27;s apparently here reading, great job on updating your A* introduction (which was already excellent!), I really like how you break it down piece by piece. While it&#x27;s a very simple algorithm at its core, there are some really subtle nuances that are easy to miss when you first approach it, e.g. the difference between a search node and a map coordinate (assuming classic 2D game maps).<p>I had some fun earlier this year implementing A* in 6502 assembler, starting with Python and working my way down through Objective-C and C before finally hand-optimizing the critical paths in asm. Articles are here for those who enjoy that kind of thing:<p><a href="http://magervalp.github.io/2014/05/02/priority-queue-in-asm.html" rel="nofollow">http:&#x2F;&#x2F;magervalp.github.io&#x2F;2014&#x2F;05&#x2F;02&#x2F;priority-queue-in-asm....</a> <a href="http://magervalp.github.io/2014/05/07/astar-in-asm.html" rel="nofollow">http:&#x2F;&#x2F;magervalp.github.io&#x2F;2014&#x2F;05&#x2F;07&#x2F;astar-in-asm.html</a><p>The code is available on github too: <a href="https://github.com/MagerValp/AsmAstar" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;MagerValp&#x2F;AsmAstar</a>
评论 #8061409 未加载
dj-wonk将近 11 年前
Again, a great article, but I feel like some of the general thinking behind heuristics is getting lost. Heuristics can be significantly more interesting than distance between two points. This isn&#x27;t a huge criticism. In this context -- pathfinding on a grid -- complicated heuristics may not be needed.<p>When I first read about A* a long time ago I remember, for example, reading about how static summaries of chess board positions may be useful heuristics. Developing pieces towards the center of the board is often better, for example. To me, seeing this was an &quot;ah ha&quot; moment for heuristics: use them if they are cheap and (sometimes) informative, relative to the search cost.
评论 #8059555 未加载
评论 #8060314 未加载
raymondh将近 11 年前
Programming nit: The posted code should use collections.deque() rather than Queue.Queue(). The former is a high-speed C-coded datatype that is ideal for a search queue. The latter is a pure Python wrapper that adds thread synchronization around an underlying collections.deque() object. The wrapper overhead is only worth it if you&#x27;re actually using multiple threads that share the same queue.
评论 #8059918 未加载
评论 #8059863 未加载
beefsack将近 11 年前
For those who haven&#x27;t implemented A* before it can be quite an enlightening experience, and actually isn&#x27;t too difficult once you dive in. It&#x27;s a good introduction to priority queues too if you haven&#x27;t played with them before.<p>I threw together an implentation of A* as a library in Go [1] for a game I&#x27;ve been doing, it has automated tests which mock up a game world and check the length of paths with visual output.<p>[1] <a href="https://github.com/beefsack/go-astar" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;beefsack&#x2F;go-astar</a>
sgustard将近 11 年前
An earlier version of this article by Amit was posted to HN a few months ago:<p><a href="http://theory.stanford.edu/~amitp/GameProgramming/AStarComparison.html" rel="nofollow">http:&#x2F;&#x2F;theory.stanford.edu&#x2F;~amitp&#x2F;GameProgramming&#x2F;AStarCompa...</a>
评论 #8059894 未加载
dj-wonk将近 11 年前
Excellent! One suggestion. The article mentions &quot;inadmissible heuristic&quot; but does not talk about what that means.<p>Per <a href="https://en.wikipedia.org/wiki/Admissible_heuristic" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Admissible_heuristic</a>, &quot;In computer science, specifically in algorithms related to pathfinding, a heuristic function is said to be admissible if it never overestimates the cost of reaching the goal, i.e. the cost it estimates to reach the goal is not higher than the lowest possible cost from the current point in the path.[1] An admissible heuristic is also known as an optimistic heuristic.&quot;
xer0x将近 11 年前
The author, Amit Patel, makes awesome stuff! His writeup on hexagon grids is excellent also. Plus for some retro gaming Solar Realms Elite is one of the finest BBS door games of it&#x27;s time.
lenocinor将近 11 年前
Great article. This is the first A* article I&#x27;ve seen online that has good visualization. I teach this in my game AI class and the visualization is key to understanding.
kenshiro_o将近 11 年前
Awesome article! The diagrams made it extremely easy for me to recall how A* worked. I now vividly remember graph traversal theory classes an exercises from University time!<p>I guess the implementation effort greatly varies depending on the programming language and the libraries at one&#x27;s disposal. However, I believe that even for someone not too versed into data structures, the priority queue should be relatively easy to implement.
jonalmeida将近 11 年前
The visualizations are incredibly key to helping understand. It would have been much harder for me to understand what was going on without it.<p>Well done!