## Blog #2: Tic Tac Toe!

Hello!

In the last two weeks, I divided my time mostly between working on the Gaussian Process Regression model and creating a Monte Carlo Tree Search algorithm.

I realized early on that my accuracy function as well as the way I plotted results could both be improved. Previously, I defined by accuracy as acc = 1 - ((testY - y_pred) / testY).mean() where testY is an array of correct labels and y_pred is an array of predictions. I fixed it to MAE and RMSE, and was able to get more accurate accuracy results after that. Futhermore, I learned that plotting labels against predictions was a better way to showcase results, as seen below. The closer to the diagonal the data points are, the more accurate the model. I also started on the second part of the project: designing a Monte Carlo Tree Search (MCTS) algorithm. To familiarize myself with the algorithm, I started by creating a tic-tac-toe agent that uses MCTS to determine the next move to make.

Monte Carlo Tree Search is an algorithm that predicts future moves of a game in order to take the most optimal action. There are four steps in the Monte Carlo Tree Search algorithm:

• Selection
• Expansion
• Simulation
• Backpropagation

At the very base of the Monte Carlo Tree Search algorithm is a search tree, where every node represents a specific move in the game. For tic tac toe, each node might store the player that made the move, the location before the move, the gameboard before the move, as well as the number of simulations and simulated wins stemming from this node (see *simulation*). Let us consider a half-played tic tac toe game like the one in the diagram above, and pretend that we are the agent using MCTS to determine what move to make. At the very beginning, we only have node 1: the gameboard before playing any moves.

In selection, the node on the tree with the highest probability of winning is chosen. There’s only one node, so we choose node 1.

In expansion, we create nodes for all possible moves we can make. Thus, we create nodes 2, 3, and 4.

In simulation, we run a random simulation of a game, recording whether or not we win. Starting from node 1, let’s simulate playing nodes 3, 8, and 13, ending in a tie.

In backpropagation, we record whether or not there was a win. Since it was a tie, we record 0.

Then, we start the iteration all over again. Now we have two levels in our tree: nodes 1 to 4.

In selection, we choose the node with the highest probability of winning. Since all 4 nodes have a win record of 0, we choose one randomly. Let’s pick node 3.

In expansion, we create nodes for all possible moves we can make. Note that these are the possible moves that the opponent can make. This corresponds to nodes 7 and 8.

In simulation, we run a random simulation of a game. Starting from node 3, let’s simulate playing nodes 7 and 12, ending in a win for green.

In backpropagation, we record the win. Since we are the green player, a win is recorded as +1. Thus, node 3 as well as all it’s parent nodes (node 1) get 1 added to it’s win record.

This process is repeated over and over again. The more repetitions, the more nodes we can create, and the more simulations we can win. In the diagram above, all nodes have been drawn out, and one simulation has been run on every possible outcome of the game. Ultimately, node 2 has a win record of 0 (loses once and wins once), node 3 has a win record of 1, and node 4 has a win record of 0. Thus, the move we will take will be node 3, playing an X in the bottom right corner.

This tic tac toe agent is what I created! I made a game environment class as well as a MCTS algorithm class, then ran MCTS for both players in the tic tac toe game. After running multiple simulated games, I realized most of the games end in a draw – which makes sense since there’s no way to force a win in tic tac toe if both players are playing optimally.

I also played around with larger tic tac toe boards (4x4, for example), but realized that a larger board corresponds to a larger game tree, meaning a larger computation is needed to make an accurate decision on what move to make.
{"ft_lang_label":"__label__en","ft_lang_prob":0.948853,"math_prob":0.96954536,"size":4123,"snap":"2020-34-2020-40","text_gpt3_token_len":967,"char_repetition_ratio":0.12672979,"word_repetition_ratio":0.045513652,"special_character_ratio":0.23114237,"punctuation_ratio":0.11731207,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9567837,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-12T09:31:40Z\",\"WARC-Record-ID\":\"<urn:uuid:e4cb077e-b860-47fa-825f-df62a71e412d>\",\"Content-Length\":\"39775\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8cfb5b63-e2a7-4916-8846-58dbc27b3fdd>\",\"WARC-Concurrent-To\":\"<urn:uuid:fe432a05-1315-4079-9521-3569da79ec3d>\",\"WARC-IP-Address\":\"23.185.0.4\",\"WARC-Target-URI\":\"https://calenergycorps.berkeley.edu/blog/blog-2-tic-tac-toe\",\"WARC-Payload-Digest\":\"sha1:IMJ4DDI67KQPNLEFX5UNNHCXNKB2LHID\",\"WARC-Block-Digest\":\"sha1:P4FNF6JGSK5IGRYZCBQJNND45LQ56T2V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439738888.13_warc_CC-MAIN-20200812083025-20200812113025-00110.warc.gz\"}"}