Wednesday, June 3, 2015

algorithm - Why can't my A* implementation find a path out of an open room?


I tried to follow this article with the following example, but in my attempts it always ends up with a dead end.


If we take G being 10 in a straight line or 14 in a diagonal, and H the distance in horizontal or vertical to the target point, then we restart this search for every tile that is the nearest one to the target point.


I have this result: if we take one or the other direction with 54, it ends up with a dead end. For example, if we go up from 40 to 54, then to 60, when we restart the search with G and H, it ends up with "4" (second shema) and comes back in a dead end:


enter image description here



enter image description here


Why does this happen and how can I solve it?


I also looked at the wiki, but I did not understand the system with the colors and the code shown.



Answer



OK, so I found a good article that talked about Dijkstra algorithm (it is non-english speaking, so I will try to explain it here ) :


If you try to find the path between two cities, you will have 2 arrays : one with :



  • the name of the node,

  • the weight of the node (they all start at -1, except the first node, the starting point at 0)

  • a bool to know if we already passed through this node to look for its children



Then a second array with :



  • the name of the node,

  • the previous node that leads to this node


Then, for each children of the current node, do the same thing :
If we want to go from London to Leeds, and the first child of London is Manchester :


Node : Manchester
have we already taken Manchester to search for its children?

=> No


is the weight of Manchester > the weight of London + distance London+Manchester ?
OR
weight of Manchester still undefined (= -1) ?
=> Yes, it is undefined


because it was YES :
weight of Manchester = weight of London + distance London-Manchester;
(in my case with a tilemap, the weight value depends wether it is a diagonal or not : I chose 14 if it is a diagonal, and 10 if it is not)
weight of Manchester = 0 + 10 = 10 ; previous node before Manchester = London


we check London to YES in the third parameter in the array (the bool of the first array).



And it looks fine for my example, so I guess it should work for others.


Thanks for all the answers @ratchetfreak, @amitp, @bummzack, @UnderscoreZero


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...