{"id":56,"date":"2008-05-26T16:49:12","date_gmt":"2008-05-26T23:49:12","guid":{"rendered":"https:\/\/realtimecollisiondetection.net\/blog\/?p=56"},"modified":"2008-09-25T00:25:25","modified_gmt":"2008-09-25T07:25:25","slug":"dont-follow-the-shortest-path","status":"publish","type":"post","link":"https:\/\/realtimecollisiondetection.net\/blog\/?p=56","title":{"rendered":"Don&#8217;t follow the shortest path!"},"content":{"rendered":"<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/A*_search_algorithm\">A* algorithm<\/a> is perhaps the most ubiquitous algorithm in games but also seemingly one of the more misunderstood algorithms. Not in the sense that people don&#8217;t know how to implement it (they do) but in failing to use it properly.<!--more--><\/p>\n<p>As you probably know, A* is a best-first graph-searching algorithm frequently used for finding a path from the start node to a goal node<sup>1<\/sup>. A* is typically cast as operating on an evaluation function<\/p>\n<p>f(x) = g(x) + h(x)<\/p>\n<p>where g(x) describes the true cost from the start node to the current node x, and h(x) is a heuristic function that estimates the cost in moving from x to the nearest goal node (nearest, according to the metric).<\/p>\n<p>When h(x) is <i>admissible<\/i>, meaning it doesn&#8217;t overestimate the true cost of reaching a goal, A* is guaranteed to find a shortest path (if one exists). And herein lies the problem: much too much effort is spent in games in finding the shortest paths! There is a near obsession with admissible heuristics, which is completely misguided! Who the heck cares about the shortest path?! In our everyday lives we rarely, if ever, take a shortest path. Instead, we often optimize for search effort, taking a path we&#8217;re familiar with (which we&#8217;ve <a href=\"http:\/\/en.wikipedia.org\/wiki\/Chunking_%28psychology%29\">chunked<\/a> or otherwise memorized so as to require no search). Well, the same applies to games and the A* algorithm. We can reduce search effort, sometimes drastically, by forfeiting the guarantee of an optimal shortest-path using nonadmissible heuristics.<\/p>\n<p>Though they are (often) ignored in games, the power of nonadmissible heuristic functions is nothing new, of course. Going back to the original paper [Hart68] and the section entitled &#8220;The Heuristic Power of the Estimate h-hat&#8221; we find the authors pointing out the value of non-admissible heuristics, stating<\/p>\n<blockquote><p>\nThe algorithm would no longer be admissible, but it might be more<br \/>\ndesirable, from a heuristic point of view, than any admissible algorithm.\n<\/p><\/blockquote>\n<p>We can find this echoed in many places, including in Barr &#038; Feigenbaum&#8217;s Handbook of AI [Barr82, pg. 65, in regards to A*]<\/p>\n<blockquote><p>\nSecond, it may be less important to find a solution whose cost is absolutely minimum than to find a solution of reasonable cost within a search of moderate length. In such a case, one might prefer an h* that evaluates nodes more accurately in most cases but sometimes overestimates the distance to a goal, thus yielding an inadmissible algorithm.\n<\/p><\/blockquote>\n<p>and a paper by Dechter and Pearl [Dechter85]:<\/p>\n<blockquote><p>\nIt has been found, however, that maintaining the admissibility of A* is too restrictive; it limits the selection of heuristics to those that only underestimate costs, and it forces A* to spend a disproportionately long time discriminating among roughly equal candidates. As a result, several attempts have been made to execute A* with nonadmissible estimates while simultaneously limiting the degree of suboptimality [5, 17, 18, 20] and minimizing the computational difficulties that overestimates may induce [1].\n<\/p><\/blockquote>\n<p>I wish AI programmers all over the world would think more about the heuristic function, if they haven&#8217;t already, and stop being totally obsessed with shortest paths. <b>Instead, use nonadmissible heuristics and cut your search effort in half<sup>2<\/sup>!<\/b><\/p>\n<p>BTW, the A* algorithm turns 40 years this year, and it&#8217;s hard to say things that haven&#8217;t already been said about the algorithm. That said, in a recent <a href=\"http:\/\/11011110.livejournal.com\/135302.html\">livejournal post<\/a>, <a href=\"http:\/\/www.ics.uci.edu\/~eppstein\">David Eppstein<\/a> (a UCI professor and computational geometer) talks about the A* algorithm in a fresh perspective. David likens A* to a preconditioner that adjusts the weights of a graph to allow another algorithm (notably, but not necessarily, Dijkstra) to work more efficiently. Though David too makes the mistake of thinking that (or, at least, presenting it as if) A* is for shortest paths only. It&#8217;s not, dammit. Still worth reading!<\/p>\n<p><b>Footnotes<\/b><\/p>\n<ol>\n<li>The observant will note that I said &#8220;a goal&#8221; instead of &#8220;the goal.&#8221; It is not always clearly stated that A* is capable of searching for multiple goals at once (in the sense that reaching any of the goals is our, uh, goal), but it is.\n<li>Factor of two is not a promise.\n<\/ol>\n<p><b>References<\/b><\/p>\n<ul>\n<li>[Barr82] Barr, Avron. Edward Feigenbaum. <a href=\"http:\/\/www.amazon.com\/exec\/obidos\/tg\/detail\/-\/0273085530?tag=realtimecolli-20\">The Handbook of Artificial Intelligence, Volume I<\/a>.<br \/>\nMorgan-Kaufmann, 1982.<\/p>\n<li>[Dechter85] Dechter, Rina. Judea Pearl. <a href=\"http:\/\/doi.acm.org\/10.1145\/3828.3830\">&#8220;Generalized best-first search strategies and the optimality of A*&#8221;<\/a> Journal of the ACM, Volume 32, Issue 3, pp. 505-536, July 1985.\n<li>[Hart68] Hart, Peter. Nils Nilsson. Bertram Raphael. <a href=\"http:\/\/ai.stanford.edu\/~nilsson\/OnlinePubs-Nils\/PublishedPapers\/astar.pdf\">&#8220;A Formal Basis for the Heuristic Determination of Minimum Cost Paths&#8221;<\/a> IEEE Trans. Syst. Science and Cybernetics, SSC-4(2):100-107, 1968.\n<li>[Hart72] Hart, Peter. Nils Nilsson. Bertram Raphael. <a href=\"http:\/\/ai.stanford.edu\/~nilsson\/OnlinePubs-Nils\/PublishedPapers\/correctionastar.htm\">&#8220;Correction to `A Formal Basis for the Heuristic Determination of Minimum-Cost Paths'&#8221;<\/a> SIGART Newsletter, no. 37, pp. 28-29, December 1972.\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The A* algorithm is perhaps the most ubiquitous algorithm in games but also seemingly one of the more misunderstood algorithms.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[7,3,6],"tags":[],"class_list":["post-56","post","type-post","status-publish","format-standard","hentry","category-ai","category-code","category-from-hell"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/56","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=56"}],"version-history":[{"count":0,"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/56\/revisions"}],"wp:attachment":[{"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=56"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=56"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/realtimecollisiondetection.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=56"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}