Build an AI for a Game in Mathematica

Dan Bridges
Parallel Thinking
Published in
3 min readDec 5, 2020

Introduction

In this article we’ll be building a simple game in Mathematica, then we’ll use Mathematica’s machine learning algorithms to train an AI player. Our game will consist of a player (a penguin) that can move around a two-dimensional grid. The goal of the game is catch as many rewards (fish) as possible.

Our simple game

Creating the Game

First we load the images for the game :

Our game state will be stored in a square matrix of strings. Empty cells will be represented by a blank string, the player will be presented by “P” and the reward by “R”. NewGame initializes the board along with score parameters and an empty history list to record each move.

We can draw our game with a simple function:

Each player movement in the game will step the game forward. Each game step performs the following actions:

  1. Append the current board state and provided movement direction to the game’s history.
  2. Calculate the new player position according to the provided movement direction.
  3. If the player reaches the reward then increment the score and calculate a new random reward position.
  4. Update the game state matrix with the new player position and reward position.

We are now ready to combine our update and render functions with event handlers to create the finished game:

Training an AI Player

Our game works, now let’s train the computer to play it! Go ahead and play the game for a while to generate some history data for training. I played for 1,000 turns and have included that data below if you prefer to use that.

Mathematica’s builtin function Classify will do the heavy work for us. We only need to provide a feature extraction function and specify Neural Network as our method. Our feature extraction function will just grab the position of the player and reward and return them in a flattened list.

We can look at some of the details of the generated classifier.

Our learning curve is flattening out nicely after 250 or so training examples, indicating our training set of 1,000 examples is more than adequate. Our accuracy is quite high as well. Let’s give the classifier a try:

The entire notebook is available for download or on Wolfram Cloud.

--

--

Dan Bridges
Parallel Thinking

Software developer at Beezwax Datatools and former researcher in Physics & Neuroscience.