0

I've tried looking this up for any suggestions or help but haven't really come across anything.

I'm writing an A* search for searching an image for a path from a start coordinate to an end coordinate.

For this example, the start coordinate is at somewhere in the upper-left corner of the image and the goal coordinate is in the bottom-left corner. However, in the image, there are black bars across the screen at different levels, with only only hole in them. So you start at the top, navigate towards the right side of the screen, go down through the hole, navigate back left because the hold is over there now, and you keep going until you get to the bottom of the image and the goal coordinate.

Here is my implementation currently (hybrid of java/python, but actually written in java):

void run(start, end):
  openList = new HashSet<Node>()
  closedList = new HashSet<Node>()

  openList.add(start)
  while (true):
    Node current = grabBest(openList)
    openList.remove(current)
    closedList.add(current)

    if (current == end/goal):
      return genPath(current)

    Node[] neighbors = genNeighbors(current)
    for (Neighbor neighbor : neighbors) {
      if neighbor == null || closedList.contains(neighbor):
        continue;

      if neighbor in openList:
        // Check if current + cost to go to neighbor < gValue already at neighbor
        // update neighbor if our current path costs less than the already found path
      else:
        neighbor.setG(current.gValue + neighbor.costToWalk)
        neighbor.setParent(current)
        openList.add(neighbor)
    }
void grabBest -> returns the node in openList that has the lowest fValue
void genNeighbors(current) -> generates an array of 4 neighbors, x + 1, x - 1, y + 1, y - 1

// Here, the g value is set to 0 (zero) for testing now
// heuristic is the euclidean distance from the current pixel coordinate to the goal pixel coordinate
// f value is the combination of those two 

However, when I run this, it takes forever to run and I eventually just stop it. It seems to be searching all pixels above each level before eventually finding the path down to the next level, where it does the same thing. The paths where I stop it I have drawing out to see it's current path: it goes straight down from the start, hits the wall, and then goes left, and then back up to the very top of the image, and just does crazy stuff like that.

I'm wondering if anyone can see where in my pseudo-code I'm going wrong... I've been trying to step through/debug my code for the past 2 hours and cannot figure out what is going on.

EDIT: Image here (This is a run where the path starts in the upper top right corner of the image and tries to go straight down but hits the black bar): Maze path

Some Guy
  • 351
  • 1
  • 4
  • 20
  • Post the image that you are working on. – SomeDude Feb 16 '20 at 21:11
  • `grabBest(openList)` looks like it's way too slow. You need to use a proper priority queue – Matt Timmermans Feb 16 '20 at 21:40
  • There's lots of second level slowness too. If you want to know if it's doing the right thing (slowly)... As the set of searched squares grows, it should look like the image is filling up with water, with gravity pointing toward the target. – Matt Timmermans Feb 16 '20 at 21:47
  • 1
    @SomeDude Added image – Some Guy Feb 16 '20 at 21:59
  • @MattTimmermans I implemented the priority queue instead of grabBest. It does look like it's filling up with water, but it just takes way too long to actually finish (I let it run for like 10 minutes and it still didn't finish). I was just curious if there was something I was doing wrong – Some Guy Feb 16 '20 at 22:08
  • @SomeGuy I have one thought of why the ball is going left and then upwards. Your `g` is 0 from your comments, so you are relying completely on heuristic which is the euclidean distance. After hitting the wall, I think the hole on the right is not the next closest euclidean distance, it is infact the point above it because the goal is almost straight above the point where you landed, so it tries to go above. Whereas the hole on the right side of first bar is almost diagonal to the goal. Maybe change heuristic? – SomeDude Feb 16 '20 at 22:52
  • here [How to speed up A* algorithm at large spatial scales?](https://stackoverflow.com/a/23779490/2521214) for searching in 2D grids like Images ... However what you have in code is graph instead of image ... and you did not describe how you store your input nor you got any sample input ... so we can only guess ... from a quick look I do not see any condition to store only the smaller cost while filling the graph ... as you use priority ques it can occur that filled node is already filled with better cost ... overwriting it will break the A* ... – Spektre Feb 17 '20 at 08:31

0 Answers0