0

Wiki says, that heuristic function is estimate of the distance from the current node to the goal in that case.

But there are no description of function heuristic_cost_estimate in this code: link to wikiperida

AM I RIGHT?... For example, I have graph with 3 vertexes. I must have 3 heuristics from each vertex to goal vertex and I set up with setting up vertexes?

Matrix (weight):

0 5 2
1 0 3
1 2 0

Heuristic function for each vertex (values means heuristic cost from current vertex to goal, for example cost from 2 to goal will be 3):

5 3 4

It is strange assumption, isn't it? I don't know what vertex would be goal. How I can set up heuristic function at this step?

OR?... I have 3 vertexes and 9 heuristics.

Graph matrix:

0 5 2
1 0 3
1 2 0

Heuristic cost:

0 5 10
5 0 10
3 8 0

It means this. If 3 is goal, heuristic cost from 1 to goal will be 3. If 2 is goal, cost from 1 to 2 (goal) will be 5.

OR I AM NOT RIGHT?

What is heuristic function in my case (finding optimal path on graph)?

Sharikov Vladislav
  • 7,049
  • 9
  • 50
  • 87
  • You'll need to clearly explain what problem you're actually trying to solve with A*. "Finding optimal path" still leaves us making quite a few assumptions. – Bernhard Barker Apr 10 '14 at 22:12

1 Answers1

3

The heuristic function is one function h:V->R where V is your vertices, and R is for real numbers. In other words - the heuristic function gives a value for each vertex - that estimates 'how close it is' to the target.

Heuristic function cannot be always used, you need to have some information on your graph to be able to use them.

For example, if you are solving a maze, every cell in the matrix (which is not a wall) is a vertex, and possible moves between adjacent cells represent edges.
In this example - the heuristic function can be the manhattan distances - which is, given a target cell t - the heuristic is h(v) = |t.x - v.x| + |t.y - v.y|.

If you have no idea how close you are to your target - you cannot use any heuristic function, you have no information. (In fact, the only heuristic you can use is the non-informative one, h(v) = 0 for each v - but that's pointless).

Also note, in order for A* to be optimal with your heuristic function (let it be h), you need it to be admissible - that means that for every vertex v: h(v) <= d(v,target), where d(v,target) is the real distance from v to the target.
Note that for example the manhattan distances heuristic for maze is admissible.

Hope that solves your confusion.

amit
  • 175,853
  • 27
  • 231
  • 333
  • So heuristic function is setting up with vertexes, right? I created vertex X and set up heuristic cost for it Y. Y is heuristic cost that means distance from X to goal, right? But I don't know about goal, when I created vertex. And what if goal is changed? X vertex heuristic cost will be same for another vertex too? I don't understand this moment. **ADD** `is the real distance` — real straight distance or distance throw other vertexes to goal? – Sharikov Vladislav Apr 10 '14 at 22:18
  • 1
    @SharikovVladislav If goal is changed, you need a different heuristic function. The heuristic function is specific per goal (or set of goals, to be exact). If the goal is changed - then most likely the heuristic is going to need to be changed as well. `the real distance` is the real shortest path from the node to the target, through other vertices. – amit Apr 10 '14 at 22:19
  • Oh okay. So I have graph. For example this one: [link to image](http://i.gyazo.com/eea7ca5054586f5bcc38f7cadd039631.png). 4 is goal (red one). For example, I changed goal to 3rd vertex. I have to update heuristic cost now from each vertex to goal, right? – Sharikov Vladislav Apr 10 '14 at 22:22
  • @SharikovVladislav If you want it to be admissible and informative (and you do), then yes - you have to. – amit Apr 10 '14 at 22:24
  • Heuristic cost is assumption, right? So I watch on the graph and making assumption. What do I need to pay attention to? – Sharikov Vladislav Apr 10 '14 at 22:25
  • @SharikovVladislav It is usually based on what the graph stands for, like my maze example. If you can go up/down/left/right only in a matrix, when looking at the graph representing this matrix, you are going to need at least `|target.x - v.x| + |target.y - v.y|` steps to get from `v` to the target. This is what the manhattan distance is based on. So, depending on what your graph really stands for - you can extract information for your heuristic function. If your graph stands for nothing - you have no information and you cannot really get any good heuristic. – amit Apr 10 '14 at 22:28
  • `|target.x - v.x| + |target.y - v.y|` — you meant coordinates of vertexes there? I thinked about it. Or did you mean steps from current vertex to goal? About what the graph stands for. Well it is just an lab work in my course. I have to code A*. Teacher didn't said how. I am trying to understand it now. This graph doesn't represent anything. I just have to create graph and find optimal path from start to goal. – Sharikov Vladislav Apr 10 '14 at 22:36
  • you said in your answer, that heuristic function can be Manhattan distance (result is pixels, if coords). It has to be admissible. H(v) (pixels) have to be lesser than real distance from target to goal (abstract weigh). How we can compare pixels and abstract weight? Clarify this for me, please. – Sharikov Vladislav Apr 10 '14 at 23:08
  • @SharikovVladislav The manhattan distance works when the graph represent a physical distance, so the weight is not abstract. Try following the link I provided in my answer to understand how and why it works. – amit Apr 10 '14 at 23:10
  • My graph represents nothing, so I can't use such heuristic, right? One way is to imitate heuristic cost, like adding set of heuristics for each goal target (or set it on each goal vertex change), right? I understand what you said. Thank you. Anyway I have to use it. So is my assumption correct? – Sharikov Vladislav Apr 10 '14 at 23:14
  • Thank you for you help. Sad, but it didn't helped me :P :) I had to use simple estimation, based on nothing. It is theme for my new question =P – Sharikov Vladislav May 03 '14 at 21:25