TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Member-only story

Monte Carlo Tree Search (MCTS) AI Gameplay in Swift

Plus setting up S4TF in Jupyter Notebooks!

Niamh Kingsley
TDS Archive
Published in
5 min readOct 4, 2020

--

  1. Swift is not just for Apple
  2. Game-playing AI is awesome
  3. Swift is not just for Apple!

In this article, I’m going to explain how I set up Swift in a Jupyter Notebook, and then how I used Swift to create a Time-Bounded Monte Carlo Tree Search algorithm agent that can play 2048 in 17 seconds. Neat!

The reason why 2048 is such an interesting game to test an AI agent against is because it is not deterministic: it’s random. Therefore — unlike some other traditional games used to test AI such as CartPole and noughts & crosses — it’s not a problem that a neural network is suited to. Instead, we can use a stochastic algorithm that asks “which move will do the least damage?” over and over again, and can run through scenarios at pace.

Pace, you say? Yeah — using Swift allows us to create an agent that can run 55 moves per second which is pretty damn fast.

The Game of 2048

Designed by Italian web developer Gabriele Cirulli, 2048 is played on a 4x4 board, where at initialization you have two random tiles with either ‘2’ or ‘4’ on them. By swiping left, right, up, or down, you cause matching value tiles that come…

--

--

TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Niamh Kingsley
Niamh Kingsley

Written by Niamh Kingsley

Passionate about technology, AI, & neuroscience. You can generally find me @nifereum, @niamhkingsley or connect via https://www.linkedin.com/in/niamhkingsley

No responses yet