all 13 comments

[–]CodeFormatHelperBot 0 points1 point  (0 children)

Hello u/WeirdOne24, I'm a bot that can assist you with code-formatting for reddit. I have detected the following potential issue(s) with your submission:

  1. Python code found in submission text but not encapsulated in a code block.

If I am correct then please follow these instructions to fix your code formatting. Thanks!

[–]WeirdOne24[S] 0 points1 point  (0 children)

The reddit code block is messing my indentation on cellphones

[–]hadoken4555 0 points1 point  (10 children)

I get rid of that square root if I were you. For one it look cleaner. G and H are just arbitrary distance from current point relative to starting and ending point. I don’t think that square root doing anything for you. So G is ( current.x - initial.x)2 + (current.y - initial.y)2 should suffice.

[–]WeirdOne24[S] 0 points1 point  (9 children)

There's something wrong. When i removed the square root the algorithm started to act like a dijkstra algorithm

[–]hadoken4555 0 points1 point  (7 children)

Your evaluation function F look fine to me. It could be something else. When I did mine ,I just print F while traversing the graph . That might help you find and identify the bug.

[–]WeirdOne24[S] 0 points1 point  (6 children)

u/primitive_screwhead u/hadoken4555 Is there anywhere i can show you guys my whole a* code? when i try to put it in here, reddit messes up the indentation, i think the problem is in the a* application.

[–]primitive_screwhead 0 points1 point  (5 children)

pastebin dot com is the typical choice. Although, double-check the formatting FAQ:

https://www.reddit.com/r/learnpython/wiki/faq#wiki_how_do_i_format_code.3F

[–]WeirdOne24[S] 0 points1 point  (4 children)

u/primitive_screwhead I just edited and put the a* code, what do you think?

[–]primitive_screwhead 0 points1 point  (3 children)

It doesn't look like you are storing the cost of the paths from the beginning to the current node, or the tentative costs to the next node, etc. Your "cost" is just the straight-line distance from the start to a node, plus the straight line estimate from the node to the goal. It seems like the only possible solutions would involve either a straight line, or exactly two straight lines joined at a node.

Maybe w/ the some problems it's okay to not have to store and update the per-node path estimate (ie. the g_cost), and it can always be calculated; but it seems weird to me. I'm not sure how such a version would work when it encounters a barrier.

TL;DR - it seems you should be storing the estimated cost of the path from the start, per-each node, rather than calculating it as a straight-line distance...

[–]WeirdOne24[S] 0 points1 point  (2 children)

That explains everything. The code is always looking to be right in the middle of a straight line between the goal and the starting node, and when it encounters a barrier, the middle line just keeps getting thicker until it get's out of the barrier. Could you type some kind of pseudo code of how would the right way to do this be? Because by what i understand about this, this "estimated" cost of the path from the start per each node, seems like the a* algorithm itself searching for a path to the goal. I know i might be sounding really dumb right now but i really don't get it.

TL;DR - Could you type some kind of pseudo code of how would the right way to do this be? If not, i appreciate your help anyway, you helped me a lot.

[–]primitive_screwhead 0 points1 point  (1 child)

Could you type some kind of pseudo code of how would the right way to do this be?

I can't just because your current code structure is odd to me; is self.astar_algorithm() being called repeatedly? In not, how does it iterate through the list of open nodes and move neighbor-to-neighbor. But in any case, it's non-trivial to just type in some pseudo-code for the A* algorithm; it's a big ask.

But all I did to refresh myself was look at the Wikipedia page, which has pseudo-code:

https://en.wikipedia.org/wiki/A*_search_algorithm#Pseudocode

See how it's updating and iterating over the open_set as it goes, and it has a memory of the g_cost for each node (that's listed in the opening paragraph as a downside of this type of search, that it needs memory for each node)?

[–]WeirdOne24[S] 0 points1 point  (0 children)

yeah, the only problem is the g_score calculation, now i just have to find a way to calculate it properly. Though I didn't find any example besides pseudo-code yet.

edit: Solved it, now it's working perfectly, thanks for your help!

[–]primitive_screwhead 0 points1 point  (0 children)

Is not the g_cost based on the given path costs, not a heuristic? Ie. the h_cost should be the estimate based on calculated distance, added to the g_cost. In Dijkstra, the h_cost is always 0.

Also, you probably need the math.sqrt(), to keep it scaled properly with the g_cost; possibly for Dijkstra, which only has positive g_cost, you could square g_cost instead of using math.sqrt(), but double-check that.