Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  YAAST (Yet another AStar Thread)  (Read 3347 times)
0 Members and 1 Guest are viewing this topic.
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Posted 2013-03-07 16:57:11 »

Hey,

my AStar implementation is sooo slow, maybe someone could look over it and give some hints? Sad
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 ->

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
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;
        }
    }
}

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #1 - Posted 2013-03-07 21:31:28 »

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 Sad
Offline Jono
« Reply #2 - Posted 2013-03-07 21:47:22 »

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:
1  
n.h = Math.abs(startX - tarX) + Math.abs(startY-tarY);

which you almost had, an then commented out  Wink
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #3 - Posted 2013-03-07 22:42:37 »

Nevermind that -.-
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #4 - Posted 2013-03-07 22:48:43 »

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 Sad

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

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #5 - Posted 2013-03-07 22:59:55 »

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 Smiley

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #6 - Posted 2013-03-07 23:10:37 »

The implementation of PriorityQueue is pretty lame and easily 100x slower than it could be if it were optimised for your case.
Sorry, I don't understand it. What do you mean by that?

Quote
and the closed list should be a boolean grid (preferably made with packed integers so they're bits, not booleans).

That just made it like 100x faster, thank you Cheesy
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2013-03-07 23:31:35 »

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 Smiley

Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #8 - Posted 2013-03-07 23:49:30 »

Thanks for everything, I'm gonna have a look at it Smiley
Offline pjt33
« Reply #9 - Posted 2013-03-08 00:11:22 »

and the closed list should be a boolean grid (preferably made with packed integers so they're bits, not booleans).
Even better: use java.util.BitSet to offload some of the arithmetic to the standard library.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline seismic

Senior Member


Medals: 4
Projects: 1



« Reply #10 - Posted 2013-03-08 07:54:02 »

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.
Offline theagentd
« Reply #11 - Posted 2013-03-09 13:55:49 »

and the closed list should be a boolean grid (preferably made with packed integers so they're bits, not booleans).
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
tile[...] == jobID;
. 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).

Myomyomyo.
Offline princec

JGO Kernel


Medals: 343
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2013-03-09 20:06:27 »

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 Smiley

Offline theagentd
« Reply #13 - Posted 2013-03-10 01:30:35 »

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?

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (4 views)
2014-07-29 12:53:52

Dwinin (7 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!