java.lang.OutOfMemoryError when trying to implement minimax algorithm

Christos_gm :

I implemented the minimax algorithm for a variant of wild tic tac toe in Java and I have stumbled upon a problem. I have a Node class that holds the game grid and an ArrayList of Node objects that are its children and a minimax method that implements the algorithm recursively.

The error that I get is:

Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
        at Grid.<init>(Grid.java:35)
        at MiniMax$Node.findChildren(MiniMax.java:27)
        at MiniMax.minimax(MiniMax.java:135)
        at MiniMax.minimax(MiniMax.java:126)
        at MiniMax.minimax(MiniMax.java:139)
        at MiniMax.minimax(MiniMax.java:126)
        at MiniMax.minimax(MiniMax.java:139)
        at MiniMax.minimax(MiniMax.java:126)
        at MiniMax.minimax(MiniMax.java:139)
        at MiniMax.nextMove(MiniMax.java:77)
        at ComputerPlayer.play(ComputerPlayer.java:12)
        at TicTacToe.main(TicTacToe.java:146)

Process finished with exit code 1

I think that the problem occurs because of the high amount of children (total nodes: 2^8 * 8!) that are created recursively every time and stored into the ArrayLists.

Here is the Node class:

 private static class Node
    {
        protected Grid grid;
        protected ArrayList<Node> children;

        public Node(Grid grid)
        {
            this.grid = grid;
            children = new ArrayList<>();
        }

        //Find all possible next moves
        public void findChildren()
        {
            char[][] board = grid.getGrid();
            for(int i = 0; i < board.length; i++)
            {
                for(int j = 0; j < board.length; j++)
                {
                    if(board[i][j] == ' ')
                    {
                        board[i][j] = 'X';
                        children.add(new Node(new Grid(board)));
                        board[i][j] = 'O';
                        children.add( new Node(new Grid(board)));
                        board[i][j] = ' ';
                    }
                }
            }
        }
    }

Here is the minimax implementation:

private int minimax(Node state, int depth, boolean isMaximizer)
    {
        //If the game is in a terminal state or has reached the desired depth
        boolean someoneWon = state.grid.someoneHasWon();
        boolean isDraw =  state.grid.isDraw();
        if(someoneWon || isDraw || depth == 3)
        {
            return evaluateState(someoneWon, isDraw, !isMaximizer);//Evaluate the state
        }
        //MAX player's turn
        if(isMaximizer)
        {
            //Find maximum score of all possible state's scores
            int bestScore = Integer.MIN_VALUE;
            state.findChildren();
            for(int i = 0; i < state.children.size(); i++)
            {
                Node child = state.children.get(i);
                int score = minimax(child, depth + 1, false);
                bestScore = Math.max(bestScore, score);
            }
            return bestScore;
        }
        else//MIN player's turn
        {
            //Find minimum score of all possible move's scores
            int bestScore = Integer.MAX_VALUE;
            state.findChildren();
            for(int i = 0; i < state.children.size(); i++)
            {
                Node child = state.children.get(i);
                int score = minimax(child, depth + 1, true);
                bestScore = Math.min(bestScore, score);
            }
            return bestScore;
        }
    }
Tom Hawtin - tackline :

Instead of creating a list of child nodes, move the iteration into Node (or equivalent. You will notice that you don't need to create a new board each time - just replace the state you changed after it is finished with.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400501&siteId=1