jps icon indicating copy to clipboard operation
jps copied to clipboard

Broken algorithm.

Open ProductOfAmerica opened this issue 8 years ago • 0 comments

Hello, I believe the algorithm is broken. Here's my test code:

package jps;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class JPSTest {
   public static void main(String[] args) throws ExecutionException, InterruptedException {
      Tile[][] tiles = new Tile[100][100];

      for (int i = 0; i < 100; i++) {
         for (int j = 0; j < 100; j++) {
            tiles[i][j] = new Tile(i, j);
         }
      }

      List<List<Tile>> tileList = JPSTestUtil.arraysToLists(tiles);

      Tile start = tileList.get(0).get(0);
      Tile end = tileList.get(99).get(99);

      for (Tile tile : tileList.get(50)) {
         tile.walkable = false;
      }

      tileList.get(50).get(20).walkable = true;
      tileList.get(50).get(21).walkable = true;
      tileList.get(50).get(22).walkable = true;

      for (List<Tile> list : tileList) {
         for (Tile tile : list) {
            System.out.print((tile.walkable ? 0 : "*") + " ");
         }
         System.out.println();
      }

      JPS<Tile> jps = JPS.JPSFactory.getJPS(new Graph<>(tileList), Graph.Diagonal.ALWAYS);
      Future<Queue<Tile>> futurePath = jps.findPath(start, end);
      Queue<Tile> path = futurePath.get();

      System.out.println();

      for (List<Tile> list : tileList) {
         for (Tile tile : list) {
            if (path != null) {
               Tile element = path.peek();
               if (tile.x == element.x && tile.y == element.y) {
                  System.out.print("-" + " ");
                  path.remove();
               } else
                  System.out.print((tile.walkable ? 0 : 1) + " ");
            } else {
               System.out.println("No path");
               return;
            }
         }
         System.out.println();
      }
   }
}

If you comment out the lines, you can see the path that looks like this: https://pastebin.com/raw/2DaUSqt5

However, if you un-comment the lines, you notice an error message "no path" because the queue Queue<Tile> path = futurePath.get(); is empty. However, there is clearly an opening in the path. Seen here: https://pastebin.com/raw/zsUPVJdt In this example, there are 3 walkable tiles on line 50, so the algorithm SHOULD be able to find a path through, yet it does not.

ProductOfAmerica avatar Oct 11 '17 05:10 ProductOfAmerica