YAAST (Yet another AStar Thread)

Hey,

my AStar implementation is sooo slow, maybe someone could look over it and give some hints? :frowning:
It works basically OK for small paths, but when I calculate a path on my Map from say 500,500 to 400,400 (with no obstacles in between) it takes like 12 seconds, which is far too long.
Using a PriorityQueue is my latest test, but didn’t greatly (at all?) improve performance, unfortunately.

Semi-long wall of code incoming ->


public class PathFind {
    private World world;

    public Node thePath;
    public Node oneStep;

    public PriorityQueue<Node> open_list = new PriorityQueue<Node>();
    public PriorityQueue<Node> closed_list = new PriorityQueue<Node>();


    public long timeDelta;

    public PathFind(World world) {
        this.world = world;
        thePath = new Node();
        oneStep = new Node();
    }

    public void calculatePath(int startX, int startY, int tarX, int tarY) {
        timeDelta = System.nanoTime();
        open_list.clear();
        closed_list.clear();
        Node n = new Node();
        n.posX = startX;
        n.posY = startY;
        n.parent = null;
        n.g = 1;
        //n.h = Math.abs( startX - tarX  + startY - tarY );
        n.h = (int)Math.sqrt( Math.abs(Math.pow(startX-tarX,2)) + Math.abs(Math.pow(startY-tarY,2)));
        n.f = n.g + n.h;
        open_list.add(n);
        while( !open_list.isEmpty()) {
            //Node current = getLowestFScore(open_list);
            Node current = open_list.poll();
            if( isInList(current, closed_list))
                continue;
            if( current.posX == tarX && current.posY == tarY) {
                thePath = current;
                while( current.parent.parent != null ) {
                    current = current.parent;
                }
                oneStep = current;
                break;
            }
            if( world.getWorldDataAt(current.posX-1, current.posY).walkAble == true) { // tile left of current
                Node next = new Node();
                next.posX = current.posX-1;
                next.posY = current.posY;
                next.parent = current;
                calculateCost(next, next.posX, next.posY, tarX, tarY);
                if( !isInList(next, closed_list))
                    open_list.add(next);
            }
            if( world.getWorldDataAt(current.posX+1, current.posY).walkAble == true) { // tile right of current
                Node next = new Node();
                next.posX = current.posX+1;
                next.posY = current.posY;
                next.parent = current;
                calculateCost(next, next.posX, next.posY, tarX, tarY);
                if( !isInList(next, closed_list))
                    open_list.add(next);
            }
            if( world.getWorldDataAt(current.posX, current.posY-1).walkAble == true) { // tile top of current
                Node next = new Node();
                next.posX = current.posX;
                next.posY = current.posY-1;
                next.parent = current;
                calculateCost(next, next.posX, next.posY, tarX, tarY);
                if( !isInList(next, closed_list))
                    open_list.add(next);
            }
            if( world.getWorldDataAt(current.posX, current.posY+1).walkAble == true) { // tile bot of current
                Node next = new Node();
                next.posX = current.posX;
                next.posY = current.posY+1;
                next.parent = current;
                calculateCost(next, next.posX, next.posY, tarX, tarY);
                if( !isInList(next, closed_list))
                    open_list.add(next);
            }
            closed_list.add(current);
            //open_list.remove(current);
        }
        timeDelta = System.nanoTime() - timeDelta;
    }

    private boolean isInList(Node next, PriorityQueue<Node> closed_list) {
        for( Node n : closed_list )
        {
            if( n.posX == next.posX &&
                    n.posY == next.posY )
                return true;
        }
        return false;
    }

    private Node getLowestFScore(PriorityQueue<Node> open_list) {
        Node lowest = open_list.peek();
        for( Node n : open_list )  {
            if( lowest.f > n.f )
                lowest = n;
            if( lowest.f == n.f )
                if( lowest.g > n.g )
                    lowest = n;
        }
        return lowest;
    }

    public void calculateCost(Node n, int startX, int startY, int tarX, int tarY) {
        n.g = n.parent.g + 1;
        //n.h = Math.abs( startX - tarX  + startY - tarY );
        n.h = (int)Math.sqrt( Math.abs(Math.pow(startX-tarX,2)) + Math.abs(Math.pow(startY-tarY,2)));
        n.f = n.g + n.h;
    }

    public class Node implements Comparable<Node>{
        public int posX;
        public int posY;
        public int f;
        public int g;
        public int h;
        public Node parent;

        @Override
        public int compareTo(Node o) {
            if( f > o.f )
                return 1;
            if( f < o.f )
                return -1;
            else return 0;
        }
    }
}

Here is some kind of debugging screenshot

One of the @ is the start position
The target is far to the top left (out of the screen)
Every tile that contains an ‘X’ is in the closed_list

I’m a little bit confused here, should the code even be checking the bottom right tiles of the screen? There’s like 0 possibility that the path goes that way (unless there would be an obstacle, but there are none). Somehow the tiles on the right and bottom have to be ruled out? Heuristic problem? I’m not sure anymore :frowning:

The speed issue is most likely caused by using a priority queue for the closed “list”. This should just be a set of closed nodes that you check contains() on. You’ll also find lots of posts around here that describe how to make your own set using arrays of booleans, since you’re just exploring a grid. Though that’s probably an unnecessary optimisation.

For the over-exploration, that’s because you’re estimating the distance left using the Euclidean distance but you don’t allow diagonal moves. So it greatly underestimates it. Try using the closer estimate of:

n.h = Math.abs(startX - tarX) + Math.abs(startY-tarY);

which you almost had, an then commented out :wink:

Nevermind that -.-

You’re right about the over-exploration thing. Using Manhattan distance there is now 0 over exploration.

I replaced all the isInList-stuff with closed_list.contains(next), but it did not speed up the code by much :frowning:

A large path (from 500,500 to 300,300) still takes >10 seconds to calculate.

The implementation of PriorityQueue is pretty lame and easily 100x slower than it could be if it were optimised for your case. pjt33 of these parts donated me a BinaryHeap class which was far, far faster when used in the open list; and the closed list should be a boolean grid (preferably made with packed integers so they’re bits, not booleans).

Cas :slight_smile:

Sorry, I don’t understand it. What do you mean by that?

That just made it like 100x faster, thank you :smiley:

I’ve got a BinaryHeap class which performs the function of the open list priority queue (it’s in the Revenge of the Titans source code) but here it is again.

Here’s the code: http://pastebin.java-gaming.org/f91338f5944

It uses an IntKeyHashMap which is just a fancy map that uses plain ints as keys instead of Integers. Again, important, if you’re doing a lot of pathfinding and don’t have much time to waste.

Cas :slight_smile:

Thanks for everything, I’m gonna have a look at it :slight_smile:

Even better: use java.util.BitSet to offload some of the arithmetic to the standard library.

Just implemented the BitSet for testing, but it didn’t improve the speed any further.
I can’t really measure it exactly but it seems to be exactly the same speed as the boolean grid.

I wouldn’t recommend using booleans for the closed “lists”. With booleans you have to clear the whole map to false after each pathfinding job. Instead I use an int for each tile together with a job ID. When you want to close a tile, you set it to the job ID. To check if a tile is closed, do [icode]tile[…] == jobID;[/icode]. The benefit of this is that you won’t even have to clear it later. Instead, just increase job ID by 1. With an int, you can do 4 billion jobs before having to clear it (before your job ID wrap around all the way to 0 again).

That’s fine if you’ve only got a few entities wandering around on a small map but if, like me for example, you’ve got maybe 2,000 of them running around on a 256x256 grid, that’d consume 500mb of RAM just for closed lists alone if they’re all pathfinding. Using a bit-grid consumes 1/32th of the RAM (15mb for the mathematically challenged).

Cas :slight_smile:

Huh? Why would you need a grid per unit? Just reuse the same one for everyone and you only get a couple of kilobytes of data?