首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java堆空间问题与我的MCTS Gomoku播放器

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

Stack Overflow用户
提问于 2018-11-30 13:57:05
回答 1查看 146关注 0票数 2

当我运行我的程序时,我会得到以下错误:

代码语言:javascript
复制
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)

我使用了相同的MCTS代码的3x3板大小,这不会崩溃,并返回一个竞争迅速的移动。但是当我尝试使用它作为15x15板大小时,游戏在1235次迭代后崩溃,并给出了上面的错误。

我想我已经处理了这个问题的症状,在1235次迭代之后不允许扩展任何节点。虽然这需要很长一段时间才能实现,但最终还是会有竞争的结果。

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

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

我的问题是:

是否有更好的方法让我的程序返回一个移动而不是崩溃,而不只是减少扩展的数量,而不需要实现上面的检查?(即使最好的移动需要很长的时间来计算)。

下面是一些未经编辑的代码:

编辑** MCTS扩展功能:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
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):

代码语言:javascript
复制
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;}

选择功能:

代码语言:javascript
复制
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函数:

代码语言:javascript
复制
public boolean childrenLeft(){
  return childrenNode.size()>0;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-30 16:05:50

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

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

  • 当前子程序的列表(对应于已扩展的操作)。
  • 尚未扩大的行动清单

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

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

  • 优化每个节点所需的内存量(它存储完整的游戏状态,是否优化了游戏状态的内存使用?)
  • 使用命令行参数增加JVM的堆大小(请参阅Increase heap size in Java)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53558933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档