Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent locally wandering paths by making diagonals cost 1.4x more #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dicethrow
Copy link

@dicethrow dicethrow commented Jan 3, 2022

Hi all,

I noticed that diagonals do not have a heuristic cost that is less than the taxicab distance, so I've added it in.

Previously, this resulted in paths that seemed to wander around the shortest path somewhat, but now it looks good.

The technique I used is to approximate the diagonal distance of a unit square as the relative heuristic.
However to keep the math using integers, I chose a square of side length 10 as the base cost, meaning that the diagonal would have cost 10*sqrt(2) = 14.14..., which I've approximated as 14. This seemed to work well so I haven't tried to see if there's any more optimization that can be done here.

@hjweide
Copy link
Owner

hjweide commented Jan 15, 2022

Thanks for taking the time to open this. Could you please attach an image showing the behavior you mention? This code was originally written to extract contours from images (where the weights do not form a uniform cost grid) so adding arbitrary values may not be the best solution.

@dicethrow
Copy link
Author

No worries, here's an example:

So if I run the maze example code as in the readme file, but enable diagonals, the top-left corner of the small maze looks like this:

maze_small_soln_diagonalsenabled_original_topleftcorner

With my pull request, it looks like this

maze_small_soln_diagonalsenabled_pullreq_topleftcorner

I can see some non-shortest-path segments with the original implementation. Although the pull request isn't perfect, it looks like it does the best job possible with the 8-direction options. I came across this approach for a sub-heuristic somewhere but can't find the source, alas.

I haven't considered using this in non-black-and-white images, maybe it would have less of an effect there.

Here are the full images:
original
maze_small_soln_diagonalsenabled_original

pull request
maze_small_soln_diagonalsenabled_pullreq

@hjweide
Copy link
Owner

hjweide commented Jan 15, 2022

Thanks! Let me think about the best way to handle this. I'm wondering:

  1. if this can be fixed easily by changing the order in which nodes are added to the priority queue, or
  2. if the user should be able to specify an additional penalty cost for diagonal moves that defaults to zero (similar to your idea).

Any other suggestions?

@dicethrow
Copy link
Author

on 1) - I originally wrote this fix a year ago, so I'm a bit rusty on how the priority queue works here, I'll have a play with this approach later, I haven't got thoughts on this yet

on 2) - the 10-or-14 thing is based on diagonal distance in a square grid being sqrt(2) of the side length, so I'm not sure that it's best left as a user adjustable penalty cost because it does have geometric significance

I think the pull request currently only works for black-and-white images, otherwise pixel values would swamp this 10-or-14 heuristic. I had a play around and haven't yet worked out how to fix it. Maybe this wandering path thing only happens for black-and-white maps?

I'll comment back if I work it out. Thanks for looking at this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants