1

I am trying to implement the A star algorithm in Javascript. But the problem I am facing is in the heuristic_cost_estimate function. I don't know how to implement this. As in where is the definition of this function. I don't want the whole code, just the function.

 function A*(start,goal)
         closedset := the empty set    // The set of nodes already evaluated.
         openset := {start}    // The set of tentative nodes to be evaluated, initially containing the start node
         came_from := the empty map    // The map of navigated nodes.

         g_score[start] := 0    // Cost from start along best known path.
         // Estimated total cost from start to goal through y.
    *************************************************** heurisctic function******************  

   f_score[start] := g_score[start] + ***heuristic_cost_estimate(start, goal)***

         while openset is not empty
             current := the node in openset having the lowest f_score[] value
             if current = goal
                 return reconstruct_path(came_from, goal)

             remove current from openset
             add current to closedset
             for each neighbor in neighbor_nodes(current)
                 if neighbor in closedset
                     continue
                 tentative_g_score := g_score[current] + dist_between(current,neighbor)

                 if neighbor not in openset or tentative_g_score < g_score[neighbor] 
                     add neighbor to openset
                     came_from[neighbor] := current
                     g_score[neighbor] := tentative_g_score
                     f_score[neighbor] := g_score[neighbor] + heuristic_cost_estimate(neighbor, goal)

         return failure

     function reconstruct_path(came_from, current_node)
         if came_from[current_node] is set
             p := reconstruct_path(came_from, came_from[current_node])
             return (p + current_node)
         else
             return current_node
dda
  • 6,030
  • 2
  • 25
  • 34
V.V.S Laxman
  • 39
  • 1
  • 7
  • Are you asking us to translate this pseudo-code into javascript?? – Jonathan M Jun 29 '12 at 18:12
  • no no... not at all.. i am doing it myself... i am asking about the heuristic function.... how can i define it ? i dont see its defintion in the algo. though i know its usage. but in the programe its confusing me – V.V.S Laxman Jun 29 '12 at 18:14

3 Answers3

2

This post provides a pretty good explanation on appropriate heuristic functions, in the context of an A* search.

From what I understand, it should provide a fast way to estimate the cost (whatever you define the cost to be) from the start to the end node while going through the node you are currently considering. It's used to help determine the optimal path you should take to reach the end node.

Here's some more information on heuristic functions.

Community
  • 1
  • 1
Zhihao
  • 14,758
  • 2
  • 26
  • 36
2

That function isn't pre-defined because it changes, based on what you're using A* to do. The heuristic must be appropriate for the problem you're actually trying to solve, and it must follow certain rules (the answer Zhihao links to seems to spell them all out).

So basically: you have to decide what is a meaningful heuristic to use for your actual problem, and then implement that in a function. There isn't just one.

Notice that the "better" your heuristic approaches the true cost, the faster your search will be.

Paul Phillips
  • 6,093
  • 24
  • 34
  • yes actully in the problem i will be given a graph with nodes and distance between the nodes representing the edge. and then i have to calculate the shortest path. isnt dijkstra's better in this case. i am making the code.. ill post it as sonn as i complete it – V.V.S Laxman Jun 29 '12 at 18:36
  • Djikstra's algorithm is the exact same as an `A*` implementation where the heuristic always returns zero. – Paul Phillips Jun 29 '12 at 18:37
0

Heuristic function for A* depends on the type of graph you are using and the rules applicable for movement through it. One of the simplest function I know is suitable for 4-direction movement (up, down, left, right) in a basic grid and is called Manhattan distance and may look as simple as:

function manhattan (pos0, pos1) {
  var d1 = Math.abs(pos1.x - pos0.x);
  var d2 = Math.abs(pos1.y - pos0.y);
  return d1 + d2;
}

But if your environment allows for diagonal movement then you need another heuristic type that supports 8 directions:

function diagonal (pos0, pos1) {
  var D = 1;
  var D2 = Math.sqrt(2);
  var d1 = Math.abs(pos1.x - pos0.x);
  var d2 = Math.abs(pos1.y - pos0.y);
  return (D * (d1 + d2)) + ((D2 - (2 * D)) * Math.min(d1, d2));
}

Of course, these are just examples and there're many others as pointed in other answers.

In general, this algorithm may seem rather simple when explained well. But coding it is a different task and may be quite a challenge for a beginner. I would suggest inspecting some working library written in language of your choice. Then you can adapt it to your needs or write your own from scratch.

More links:

Good example in Javascript

Heuristic function theory

hypers
  • 1,045
  • 1
  • 12
  • 30