The Sampling Algorithm that powers AlphaGo: A technical breakdown of MCTS
Go was considered a big challenge to be conquered by AI. The game has more than 10 to the power of 170 possible board configurations - more than the number of atoms in the known universe. AlphaGo was a breakthrough demonstration that AI systems can solve the most challenging problems. Most of the training was done entirely by the model playing out millions of games to learn and predict how good/bad a given position is and the winning possibilities given the position with two separate networks respectively. This was then combined with a powerful sampling algorithm that provided a shell for move evaluation by branching out and exploring the rest of the possible games moves given the hypothesis move upto a certain depth. The intention of this blog is to shed light into Monte Carlo Tree Search, that very sampling algorithm, by understanding the theoretical motivations and by looking through a practical implementation.
Monte Carlo Tree Search applies to mainly a “Finite Two Person Zero-Sum Sequential Game” setup, where its used as a means to sample through the possible states(moves) and choose one that is the most optimal by the algorithm. Such setups show up in games like Chess, Go, Poker, Tic-Tac-Toe where the algorithm has popularly been made to use. These algorithms also account for the outputs from a predictive reward model and use that in the judgement of the best move to take. A very straightforward way to represent a turn based zero sum game is by a game tree. Here, every node represents a state of the game. The transition from a node to one of its children is a move. Number of node’s children represents possible moves from that particular state of the game. Terminal nodes are nodes with no children, i.e the states of the game that suggest an end to the possible legal moves when reaching such a position. The entirety of of MCTS just comes down to one thing- the UCT score.
Understand the essence of what this equation is trying to do and you’ve understood MCTS. Here, denotes the total simulation score explored for a particular move. This is essentially like trying out different possibilities branching out from that move by either play it out randomly and checking the result. In the case of a game where lot of possible sequence of moves to be played, it is calculated by branching out from that move and checking the reward(from a reward model) for the descendants of that node. Sum of the simulation results from the descendants of the node is brought up back to the node to find how fairly good it is. denotes the number of times a particular node (possible move) has been visited. This ratio is often referred as exploitation/exploration. If is in comparison higher than other moves, then that particular move has had more success in simulations in similar amount of visits.
Naturally, if the UCT score were just determined by the exploitation/exploration ratio, the algorithm would prefer the moves that would have had good success in the initial explorations. This would prevent other possible good moves to being explored more further. To tackle this we have the second term in the equation which describes how over or under explored a particular move is in relation to others. Here describes the total number of visits made to all the possible moves and describes the number of visits made to a node. So when a particular move has been explored/visited far lesser than it’s counterparts, the algorithm incentivises to explore/visit that move further to see if its a possible competitor.This ratio is further weighted by a constant ‘c’. This constant acts as a hyper-parameter that controls how aggressively the algorithm explores moves that have not been visited as frequently. If ‘c’ is higher, the algorithm is more willing to explore under-visited moves. If it is lower, the algorithm becomes more greedy and prefers moves that have already shown good results in simulations. So that entire term tells us if a particular move is under or over explored and how strongly the algorithm should push exploration of those moves.
I made a quick and naive implementation of MCTS to play Tic-Tac-Toe to get a better understanding of the algorithm a code level. Let’s go through it by playing out a game. You make the first move at position 8 to start off the game, now the next move is the computers to make. It starts off by checking if the node has been fully expanded, i.e if each possible move from that state (1,2,3,4,5,6,7,9) has been simulated to get a score such that we can get the best move. Because its our first iteration, we have not expanded to sim out other moves yet(hence no UCT score to compare and get the best moves from), so we skip that loop. Now the next check is if node.isResult result is false, i.e if game has reached an end state given the particular move played. Now that we have not reached an end state, the node is expanded to return one of its child, i.e from the current game state it branches out to a possible move. The board is copied and then that move is played onto the new copy of the board which this child Node owns. Now that possible move(child node) is simulated to play the entire game out on random so that we get a result. Using this result we update the node and its parent, we update the nodes result(no of wins) and the visits and then do the same for the parent. This is done until the root node is fully expanded and each of the possible move (node) is created and simulated out to get a result.
Now that we have all the possible move nodes created (ie the root node is fully expanded), we can finally check for the best node child directly so that we can consider that move more. We calculate the UCT score of each node representing a possible move and check to see which has the highest. The node with the highest UCT score is considered and then it is explores a leaf node(representing the next move to be played). Now, because this possible move is ‘0’, which is played by the opponent, if the simulated run yields in a victory, it is considered a loss for the parent node as it represents a move played by the player ‘X’. So when the loss is propagated back to the respective parents their board state turn is checked with the result that is being propagated back, if they are the same they get an increment, if its the opposite, i.e a loss its decremented.
After expanding to one of the grandchild node, the root node (the very initial state) is selected again and the best child among the 9 possible children nodes, is calculated again. And the best child, then gets a chance to expand to a possible ‘0’ move and then explore it. This happens for the number of iterations that we define(can be treated like a hyperparameter) each one of these iterations, expands to a possible move which is a leaf node and then a random game is simulated to get quick result of the position. The parent nodes are updated based on these naive results of these respective positions. Finally after completing the n number of iterations we have set(ie, after exploring the n possible moves that could be played), we break out of the loop and then we check the child with the most visits (the best possible first move). This implementation however is very naive in its way that it approaches simulation, random simulation of the game can actually be replaced by an ML model that would output probabilities of a particular move and then that can be used to evaluate a better move, and it would be less stochastic in its evaluations. This process then can run once again once after the user has made their first ‘0’ move and it will will have 7 possible moves to sample from in the process. And so on each process can run for every time a user makes a new move.
Another interesting occurrence comes up in Pokémon battle based environment, another case of a two player adversarial setup. Equipped with the practical and theoretical understanding of this sampling approach, you can use it in similar problems/games that exactly falls into the category “Finite, Two person, Zero Sum“ game.