我的MCTS Gomoku播放器出现Java堆空间问题

时间:2018-11-30 13:57:06

标签: java montecarlo tree-search gomoku monte-carlo-tree-search

运行程序时出现此错误:

Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
        at MCTSNode.setPossibleMoves(MCTSNode.java:66)
        at MCTSNode.Expand(MCTSNode.java:167)
        at MctsPlayer.getBestMove(MctsPlayer.java:39)
        at NewBoardGUI.btnClick(NewBoardGUI.java:617)
        at NewBoardGUI.lambda$createButton$0(NewBoardGUI.java:584)
        at NewBoardGUI$$Lambda$115/558922244.actionPerformed(Unknown Source)
        at java.desktop/javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at java.desktop/javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.desktop/java.awt.Component.processMouseEvent(Unknown Source)
        at java.desktop/javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.desktop/java.awt.Component.processEvent(Unknown Source)
        at java.desktop/java.awt.Container.processEvent(Unknown Source)
        at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Component.dispatchEvent(Unknown Source)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.EventQueue.access$500(Unknown Source)
        at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
        at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.desktop/java.awt.EventQueue$4.run(Unknown Source)

对于3x3的电路板尺寸,我使用了相同的MCTS代码,该尺寸不会崩溃,并且可以迅速返回具有竞争力的动作。但是,当我尝试将其用于15x15的电路板尺寸时,游戏会在1235次迭代后崩溃,并给出上述错误。

我认为我已经通过在1235次迭代后不允许任何节点扩展来解决问题的症状。最终,这确实会带来竞争优势,尽管要花很长时间才能实现。

对我来说,根本原因是我要创建的树的大小,因为相同的代码适用于3x3板,但不适用于15x15板;包含所有节点对象的树的大小太大。因此,这只是这种方法的问题,而不是我的编码。

我确实认为我可以尝试:在x次迭代之后,如果一个节点被访问过y次,但获胜得分低于z,则删除该节点。我的想法是,如果经过x次迭代并被y次访问,但获胜分数仍然较低,那么此节点很可能会占用树中不必要的空间,因此可以删除。

我的问题是:

我的程序是否有更好的方法来返回移动而不是崩溃,而不只是减少扩展次数并且不必执行上述检查? (即使要计算最佳移动量也需要很长时间)。

这是我一些未编辑的代码:

编辑** MCTS扩展功能:

public MCTSNode Expand(BoardGame game){
    MCTSNode child = new MCTSNode(game);
    for(int k = 0;k<this.gameState[0].length;k++){
      for(int l = 0;l<this.gameState[1].length;l++){
        child.gameState[k][l] = this.gameState[k][l];
      }
    }
    Random r = new Random();
    int possibleMoveSelected = r.nextInt(getPossibleMovesList());
    int row = getPossibleMoveX(possibleMoveSelected);
    int col = getPossibleMoveY(possibleMoveSelected);
    if(this.currentPlayer==2){
      child.gameState[row][col] = 2;
      child.moveMadeRow = row;
      child.moveMadeCol = col;
      child.currentPlayer = 1;
      child.setPossibleMoves();
      child.possibleMoves.size();
    }
    else{
      child.gameState[row][col] = 1;
      child.moveMadeRow = row;
      child.moveMadeCol = col;
      child.currentPlayer = 2;
      child.setPossibleMoves();
      child.possibleMoves.size();
    }
    childrenNode.add(child);
    child.parentNode = this;
    this.removePossibleMove(possibleMoveSelected);
    this.possibleMoves.trimToSize();
    return this;
}

MCTSPlayer函数:

public class MctsPlayer {

  private static int maxIterations;

  public MctsPlayer(int i){
    maxIterations = i;
  }


  public static String getBestMove(BoardGame game){
    MCTSNode root = new MCTSNode(game);
    root.getBoardState(game);
    root.setPossibleMoves();
    for(int iteration = 0; iteration < maxIterations; iteration++){
      MCTSNode initialNode = selectInitialNode(root);
      if(initialNode.getPossibleMovesList()>0){
        initialNode.Expand(game);
      }
      MCTSNode nodeSelected = initialNode;
      if(nodeSelected.childrenLeft() == true){
        nodeSelected = initialNode.getRNDChild();
      }
      nodeSelected.Simulate();
    }

    MCTSNode best = root.getMostVisitNode();
    System.out.println("This is the selected node's best move for the row: "+best.getMoveMadeRow());
    System.out.println("This is the selected node's best move for the col: "+best.getMoveMadeCol());
    best.printNodeInfo();
  }

以下新包含**

选择初始节点功能(将继续直到可能的移动列表大小==到0):

public static MCTSNode selectInitialNode(MCTSNode node){
    MCTSNode initialNode = node;
    while (initialNode.getPossibleMovesSize()==0&&initialNode.checkForEmptySpace()==true){
      initialNode = initialNode.Select();

“ + initialNode.childrenList());           //System.out.println(“节点可能剩余移动:” + initialNode.getPossibleMovesSize());         }         返回initialNode;       }

选择功能:

public MCTSNode Select(){
  double maxUCT = Integer.MIN_VALUE;
  MCTSNode Node = this;
  if(this.possibleMoves.size()>0){
    return Node;
      }
  else{
    for(int i = 0;i<childrenNode.size();i++){
      double UCTValue = getUCTValue(getChildren(i));
      if(UCTValue > maxUCT){
        Node = getChildren(i);
        maxUCT = UCTValue;
      }
    }
    return Node;
  }

private double getUCTValue(MCTSNode childNode) {
        double UCTValue;
        if (childNode.getVisitCount() >= 1) {
          UCTValue = (Math.sqrt(2)*
              (Math.sqrt(Math.log(childNode.getParent().getVisitCount()* 1.0) / childNode.getVisitCount())) + (1.0 *childNode.getWinCount() / childNode.getVisitCount()* 1.0));
        } else {
            UCTValue = Double.MAX_VALUE;
        }
        return UCTValue;
  }

childrenLeft函数:

public boolean childrenLeft(){
  return childrenNode.size()>0;
}

1 个答案:

答案 0 :(得分:1)

我不确定是否看到childrenLeft()之类的方法代码以及其他一些方法,但我得到的印象是,您基本上在树中添加了b个新节点,其中{{ 1}}是您的分支机构。换句话说,每次迭代都将一个新的完整的子级列表添加到一个节点。这确实可能导致您很快耗尽内存。

到目前为止,最常见的策略是通过每次迭代仅添加一个新节点来扩展树。然后每个节点都需要:

  • 当前子级列表(对应于已扩展的操作)
  • 尚未展开的操作列表

您的选择阶段通常会在到达具有要扩展的非空动作列表的节点时结束。然后,MCTS将从该列表中随机选择一个动作,添加一个与该动作相对应的新节点(这意味着您的第一个列表增加一个条目,第二个列表缩小一个条目),然后从那里继续进行部署。

通过这样的实现,除非允许算法搜索很长时间,否则应该不太可能耗尽内存。如果仍然用完内存,则可以查看以下内容:

  • 优化每个节点所需的内存量(它存储完整的游戏状态,是否优化了游戏状态的内存使用?)
  • 使用命令行参数增加JVM的堆大小(请参见Increase heap size in Java