1

This is a chunk of code I'm using for Google's antai challenge whenever I run it it seems to go into some endless loop then I get the stack trace at the bottom. I'm burnt out looking at this code I can't for the life of me figure this out.

public class Node 
{
    private final int xCoord, yCoord;
    private int F, G, H;
    private Tile location;
    Node previousNode;
    private Tile [] neighbors;

    /*  
    G 
    the exact cost to reach this node from the starting node.
    H 
    the estimated(heuristic) cost to reach the destination from here.
    F = G + H 
    As the algorithm runs the F value of a node tells us how expensive we think it will         be to reach our goal by way of that node.
    */

    public Node(Tile loc)
    {
        location = loc;
        xCoord = location.getCol();
        yCoord = location.getRow();
        F=G=H=0;
        setNeighbors();
    }

    private void setNeighbors()
    {
        if(neighbors == null)
        {
            neighbors = new Tile[4];
        }
        neighbors[0] = new Tile(xCoord+1,yCoord);
        neighbors[1] = new Tile(xCoord-1,yCoord);
        neighbors[2] = new Tile(xCoord,yCoord+1);
        neighbors[3] = new Tile(xCoord,yCoord-1);//error occurs here!!!!!!
    }
}

        /**
 * Represents a tile of the game map.
 */
public class Tile implements Comparable<Tile> {
    private final int row;

    private final int col;

    /**
     * Creates new {@link Tile} object.
     * 
     * @param row row index
     * @param col column index
     */
    public Tile(int row, int col) {
        this.row = row;
        this.col = col;
    }

    /**
     * Returns row index.
     * 
     * @return row index
     */
    public int getRow() {
        return row;
    }

    /**
     * Returns column index.
     * 
     * @return column index
     */
    public int getCol() {
        return col;
    }

    /** 
     * {@inheritDoc}
     */
    @Override
    public int compareTo(Tile o) {
        return hashCode() - o.hashCode();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return row * Ants.MAX_MAP_SIZE + col;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object o) {
        boolean result = false;
        if (o instanceof Tile) {
            Tile tile = (Tile)o;
            result = row == tile.row && col == tile.col;
        }
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return row + " " + col;
    }
    }

the actual error I'm receiving is:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at Node.setNeighbors(Node.java:37)
    at Node.<init>(Node.java:25)
    at AstarSearch.assessRoute(AstarSearch.java:73)
    at MyBot.gatherFood(MyBot.java:153)
    at MyBot.doTurn(MyBot.java:124)
    at AbstractSystemInputParser.processLine(AbstractSystemInputParser.java:54)
    at AbstractSystemInputReader.readSystemInput(AbstractSystemInputReader.java:18)
    at MyBot.main(MyBot.java:25)

any help is appreciated

5
  • 2
    You haven't shown us your Tile code - in particular, the constructor. Commented Dec 16, 2011 at 6:31
  • 1
    Have you tried attaching with a profiler and seeing what objects are leaking? VishalVM ships with the JDK. Commented Dec 16, 2011 at 6:36
  • 1
    what is the size of data that you are processing? are you sure you are not genuinely out of heap space ? like Jon pointed out, the code shown is just a plain bean, what do you do in Tile's constructor ? also, take a heap dump using jmap and see what's occupying space, this is not a stack overflow error, its OOM for heap, so you are most probably creating more objects than the space provided. Commented Dec 16, 2011 at 6:37
  • sorry about that... tile attached now Commented Dec 16, 2011 at 6:42
  • Also please show AstarSearch. I daresay that endless loop is there. Nothing criminal so far. Commented Dec 16, 2011 at 6:53

5 Answers 5

3

You need to do some debugging / clean up. From quick view I see, that in assesRoute() you are never manipulating interesetedNode tile - this loop is not going to end normally.

It is also better to keep visited nodes in hash set - you only need to assure presence or absence, not a number of nodes. Alternative would be boolean flag in a node itself, this way you can work it with one list.

Sign up to request clarification or add additional context in comments.

Comments

2

It seems like your creating more objects than your memory allows, guess your code is including an unfifite loop, you can make a static integer to count how many times the setNeighbors is called, its the routine where you create new objects, and show this integer on the catch statement of a try/catch around the main call of your class.

Comments

1

I don't know what this Google challenge is, so this may not be of much use (ie I don't know if you have access to the JVM), though it is a generic method to help diagnose this kind of problem.

I suggest turning on the JVM flag -XX:+HeapDumpOnOutOfMemoryError, and running your code again. An OutOfMemoryError will cause the heap to be dumped. You can then analyse this offline using something like Eclipse MAT.

Comments

0

The MyBot.doTurn() in the stack trace suggests your program is running over or discovering some kind of path... Are you sure this is a finite algorithm?

1 Comment

no im not actually sure what you mean this is a very amateur attempt/ first try at ai programming so im in slightly over my head i would just, for now like to get this error out of the way so i can see if my idea has worked... for the entire code you can check github.com/absolutelysmokinsoftware
0

in assesRoute is a while loop. how many iterations do you expect? print out how many are actually done. if the difference is big, you know that there is a problem. take the start state of this iteration and try to understand why this state doesn't end the loop in proper time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.