|
| 1 | +package com.baeldung.algorithms.mcts.montecarlo; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import com.baeldung.algorithms.mcts.tictactoe.Board; |
| 6 | +import com.baeldung.algorithms.mcts.tree.Node; |
| 7 | +import com.baeldung.algorithms.mcts.tree.Tree; |
| 8 | + |
| 9 | +public class MonteCarloTreeSearch { |
| 10 | + |
| 11 | + private static final int WIN_SCORE = 10; |
| 12 | + private int level; |
| 13 | + private int oponent; |
| 14 | + |
| 15 | + public MonteCarloTreeSearch() { |
| 16 | + this.level = 3; |
| 17 | + } |
| 18 | + |
| 19 | + public int getLevel() { |
| 20 | + return level; |
| 21 | + } |
| 22 | + |
| 23 | + public void setLevel(int level) { |
| 24 | + this.level = level; |
| 25 | + } |
| 26 | + |
| 27 | + private int getMillisForCurrentLevel() { |
| 28 | + return 2 * (this.level - 1) + 1; |
| 29 | + } |
| 30 | + |
| 31 | + public Board findNextMove(Board board, int playerNo) { |
| 32 | + long start = System.currentTimeMillis(); |
| 33 | + long end = start + 60 * getMillisForCurrentLevel(); |
| 34 | + |
| 35 | + oponent = 3 - playerNo; |
| 36 | + Tree tree = new Tree(); |
| 37 | + Node rootNode = tree.getRoot(); |
| 38 | + rootNode.getState().setBoard(board); |
| 39 | + rootNode.getState().setPlayerNo(oponent); |
| 40 | + |
| 41 | + while (System.currentTimeMillis() < end) { |
| 42 | + // Phase 1 - Selection |
| 43 | + Node promisingNode = selectPromisingNode(rootNode); |
| 44 | + // Phase 2 - Expansion |
| 45 | + if (promisingNode.getState().getBoard().checkStatus() == Board.IN_PROGRESS) |
| 46 | + expandNode(promisingNode); |
| 47 | + |
| 48 | + // Phase 3 - Simulation |
| 49 | + Node nodeToExplore = promisingNode; |
| 50 | + if (promisingNode.getChildArray().size() > 0) { |
| 51 | + nodeToExplore = promisingNode.getRandomChildNode(); |
| 52 | + } |
| 53 | + int playoutResult = simulateRandomPlayout(nodeToExplore); |
| 54 | + // Phase 4 - Update |
| 55 | + backPropogation(nodeToExplore, playoutResult); |
| 56 | + } |
| 57 | + |
| 58 | + Node winnerNode = rootNode.getChildWithMaxScore(); |
| 59 | + tree.setRoot(winnerNode); |
| 60 | + return winnerNode.getState().getBoard(); |
| 61 | + } |
| 62 | + |
| 63 | + private Node selectPromisingNode(Node rootNode) { |
| 64 | + Node node = rootNode; |
| 65 | + while (node.getChildArray().size() != 0) { |
| 66 | + node = UCT.findBestNodeWithUCT(node); |
| 67 | + } |
| 68 | + return node; |
| 69 | + } |
| 70 | + |
| 71 | + private void expandNode(Node node) { |
| 72 | + List<State> possibleStates = node.getState().getAllPossibleStates(); |
| 73 | + possibleStates.forEach(state -> { |
| 74 | + Node newNode = new Node(state); |
| 75 | + newNode.setParent(node); |
| 76 | + newNode.getState().setPlayerNo(node.getState().getOpponent()); |
| 77 | + node.getChildArray().add(newNode); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + private void backPropogation(Node nodeToExplore, int playerNo) { |
| 82 | + Node tempNode = nodeToExplore; |
| 83 | + while (tempNode != null) { |
| 84 | + tempNode.getState().incrementVisit(); |
| 85 | + if (tempNode.getState().getPlayerNo() == playerNo) |
| 86 | + tempNode.getState().addScore(WIN_SCORE); |
| 87 | + tempNode = tempNode.getParent(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private int simulateRandomPlayout(Node node) { |
| 92 | + Node tempNode = new Node(node); |
| 93 | + State tempState = tempNode.getState(); |
| 94 | + int boardStatus = tempState.getBoard().checkStatus(); |
| 95 | + |
| 96 | + if (boardStatus == oponent) { |
| 97 | + tempNode.getParent().getState().setWinScore(Integer.MIN_VALUE); |
| 98 | + return boardStatus; |
| 99 | + } |
| 100 | + while (boardStatus == Board.IN_PROGRESS) { |
| 101 | + tempState.togglePlayer(); |
| 102 | + tempState.randomPlay(); |
| 103 | + boardStatus = tempState.getBoard().checkStatus(); |
| 104 | + } |
| 105 | + |
| 106 | + return boardStatus; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments