Train a NEAT genetic algorithm to play Frogger using OpenAI retro-gym
Report Bug
Table of Contents
This project utilizes the python implementation of NEAT to play Sega Genesis Frogger.
An evolving artificial neural network attempts to simultanesouly learn weight values and the structure of the neural network that will perform best (via 'rewards' tracked in retro-gym).
On Windows, ensure you install conda and git
The recommended installation method is described below, but if you prefer to install via pip
then ensure you have the following packages installed
pip install gym-retro==0.21 matplotlib neat-python opencv-python numpy
Note: During development gym==0.21 was used due to compatibility issues
- Clone the repo
git clone https://github.com/rdarneal/neat-retro-frogger.git
- Navigate to cloned directory
cd neat-retro-frogger
- Install conda environment
conda env create -n retro-env --file environment.yml
- Actiate the environment
conda activate retro-env
- Load the ROM(s) using gym-retro
python -m retro.import /path/to/your/ROMs/directory
Note: This repo does not contain the ROM file for Frogger-Genesis. Check the No-Intro Collection on Archive.org for ROMs.
To jump right to training
python app/parallel-runner.py
This will launch into training via natural selection. The script will run until an individual meets the fitness threshold 5,000 points OR 100 generations of Frogger, whichever is sooner.
If you want to see the results, run:
python app/playback-winner.py
Which shows how species performed over multiple generations
Along with the average fitness over time, determined by the retro-gym reward function (in this case tied to how well frogger crosses traffic)
By default, each generation includes a population of 100, and random values are passed to the retro-environment in order to control frogger.
Each generation is then divded into 'species', or groups of individuals with similar performance, and a new generation is created.
The .config
file specifies the parameters used in training the network - which includes how long species survive stagnation
, , check out the neat-python documentation for additional details on what these parameters do.
You can train a new Frogger model using the parallel-runner.py
script, you can play with the following parameters:
- In the
__main__
script:num_workers
, a keyword argument ofneat.ParallelEvaluator()
can be scaled to the number of CPU cores you would like to use for parallel training.n
keyword argument in thep.run()
method defines the number of generations that will be used unless the fitness condition is met.
If you want to modify this for other games:
- Each python script needs to be modified to match the retro environment to the game you want to use
- You will need to download and use retro to load the ROM
- The config file is currently setup for Sega Genesis
- Modify
num_inputs
to match the screen size of console - Modify
num_outputs
to 12, which should match # of buttons - Modify the python scripts to refactor use of buttons
- Modify
Robert Darneal - python-dev@robertdarneal.com
Project Link: https://github.com/rdarneal/neat-retro-frogger