当我运行我的程序时,我得到这个错误:
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 扩展功能:
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()); } return 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;
}