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

标签 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 代码,它不会崩溃并能快速返回有竞争力的 Action 。但是当我尝试将它用于 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()); } 返回初始节点;

选择功能:

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;
}

最佳答案

如果没有看到 childrenLeft() 和其他一些方法的代码,我不能 100% 确定,但我的印象是你基本上添加了 b new树的节点,其中 b 是您的分支因子。换句话说,每次迭代,您都会向一个节点添加一个新的、完整的子节点列表。这可能确实会导致您很快耗尽内存。

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

  • 当前 child 的列表(对应于已经展开的 Action )
  • 尚未扩展的操作列表

一旦到达具有要展开的非空操作列表的节点,您的选择阶段通常就会结束。然后 MCTS 会从该列表中随机选择一个 Action ,添加一个与该 Action 对应的新节点(意味着您的第一个列表增加一个条目,第二个列表缩小一个条目),然后从那里继续推出。

有了这样的实现,除非您允许您的算法搜索很长时间,否则内存不足的可能性很小。如果仍然内存不足,您可以查看以下内容:

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

关于我的 MCTS Gomoku 播放器的 Java 堆空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53558933/

相关文章:

model - 可以在序言中模拟一个简单的 CPU 吗?

java - builder 模式

java - ActiveMQ 中的跨语言支持

java - 为二十一点程序返回 bufferdimage 的方法

search - 图搜索和树搜索有什么区别?

java - 找到 child 后如何停止树搜索

java - 在 Go 中是否有等同于 Java 的 String intern 函数?

python - mcint 模块 Python-Monte Carlo 集成

c - 使用 C 寻找 pi 的蒙特卡洛方法

java - math.random 在循环中生成相同的数字