Skip to content

MaxZabarka/sonic-ai

Repository files navigation

Sonic AI:

An AI made using neat-python, that can beat the first few levels of Sonic the Hedgehog Classic on the Sega Genesis

Introduction

This is my Python source code for training a Neuroevolution of Augmenting Topologies (NEAT) artifical intellegence to play Sonic The Hedghehog on Sega Genesis, using the neat-python library implementation.

Full Playlist (Click image ^)

Why it's great:

  • Can be applied to almost any level without any changes, and high success rate
  • Uses only pixel data. No in-game variables! (Except for Sonic's X position used to calculate reward)
  • Uses uncommon NEAT approach instead of A3C
  • Multiprocessed

Drawbacks

  • Slow to train, ~2.5 hours per level on RTX 2070
  • Can't beat levels where Sonic has to go to the left

interestingly, it doesn't understand the concept of building up speed to get over ramps, and will instead find a way to glitch through them!

How to use my code:

Installation

pip install -r requirements.txt
python -m retro.import /roms/

Requirements

  • Python 3.6
  • neat-python
  • opencv2
  • numpy
  • gym-retro
  • argparse

^ All found in requirements.txt

Demo:

python replay.py winner-GreenHillZone.Act1.pkl GreenHillZone.Act1

Train:

python train.py GreenHillZone.Act1 -r

Adding the -r flag lets you watch the AI play as it trains. (significantly slower)

Replace GreenHillZone.Act1 with any level of your choice


Inspired by https://www.youtube.com/watch?v=8dY3nQRcsac