Home

Awesome

Accepted WWDC19 Scholarship Submission

Swift Roll

A simple artificial intelligence made in Swift to play a version of the old Nokia game "Rapid Roll", using Neural Networks and a simple Genetic Algorithm.

Demonstration (Click to Watch):

SwiftRoll Youtube Video (Click to Watch)

AI on Generation 37

App Installation

This project was made to run on iOS devices (12.1) and is only able to be installed through MacOS computers.

  1. Download Xcode.
  2. Clone/download this folder to your computer.
  3. Open SwiftRoll.xcodeproj.
  4. Build the project.

Running the Playground

Download and unzip Playground/SwiftRoll.playground.zip then open it through Xcode.

How it works - Neural Network

We have a total of 9 inputs nodes:

  1. Ball - X Coordinate
  2. Ball - Y Coordinate
  3. Paddle 1 - X Coordinate
  4. Paddle 2 - X Coordinate
  5. Paddle 3 - X Coordinate
  6. Paddle 1 - Y Coordinate
  7. Paddle 2 - Y Coordinate
  8. Paddle 3 - Y Coordinate
  9. Current Velocity

And one output with 3 possible states:

  1. Left (<0.33)
  2. Right (>0.66)
  3. Nothing

Each Neural Network calculates it's output through the use of 2 Hidden Layers with size 10 (HiddenLayerOneSize and HiddenLayerTwoSize)

How it works - Genetic Algorithm

Each Generation generation consists of 250 (n_genomes) neural networks (Genomes).

Each genome is consisted of genes, which are the genomes Neural Network Weights and Biasses and the Player's color, in the following order:

Color + H1 Weights + H1 Bias + H2 Weights + H2 Bias + Output Weight + Output Bias

When the game is training, we keep track of each genome's fitness (score) to select the 9 (breed_top - 1) best and 1 worse players, saving them in the top_full_genomes array.

When the entire generation has completed, we take the top full genomes and for each one apply 2 evolution processes:

  1. Crossover: Given a chance of 75% of this happening (crossover_chance), we select another random top genome and cross-over their genes.
  2. Mutation: Given a chance of 75% of this happening (mutation_chance), we apply a modification to the genome by adding it to a random value between -3 and 3 (mutationRange)

These processes are repeated 25 times (breed_count), resulting back to the original size of 250 genomes.

Little Nods

Implementation

The implementation was done using Swift with a target for 12.1 iOS devices.

While there are a couple of files in the project, the main ones are:

Navigating GameScene.swift

It probably wasn't best practice to fit so many components (game, neural network and genetic algorithm) inside only one file. But I did so in order to be a bit less confusing navigating everything spread out into 5 different files. Yet, it does need some clarifications about the positioning of code.

Communication

Credits

Inspiration

IAMDinosaur