AI that playes classic Tetris.
The goal is to develop a program that can play classic Tetris by itself. The goal is achieved by using OpenAI's retro emulator that can run retro games within Python programs. The game can analyzed using the classic Tetris Ram Map which contains hexadecimal values of elements of the game such as lines cleared, current piece and the current board state.
This program uses neat-python and gym-retro libraries to have the algorithm learn to play classic Tetris. The program did not know anything about the controls but eventually came to understand the game.
- OpenAI gym-retro Library:
Emulator on Python that can run over 1000 games from several systems. It is a tool for reinforcement learning research on these games. - Tetris-Nes Rom File:
A puzzle video game released for the NES. A rom file of the game is required and should be imported. - NEAT-Python Library:
NEAT (Neuroevolution of Augmening Topologies) is a genetic algorithm for evolving artificial neural networks. The next generation is produced by reproduction and mutation based on the fitness score (how well the network performs) of the previous generation. Neat-Python implements NEAT into Python in a convinient method. - config-feedforward-tetris File:
Configuration file for the neural network.
NOTE: When continuing training from a checkpoint file, if the config file is changed, it will not train properly. In other words, if you want to train the program with different configuration, you would have to train the program from the first generation. - data.json File:
... - main.py File:
...
Here is how the program plays Tetris:
- The program reads the current state of the board by extracting the RAM data. (The field start address is 0x0400)
- Get all possible fields (board states) with the current piece based on the current board state and the current falling piece.
- Calculate factors the program uses later like column heights, bumpiness, and holes of each possible field.
- From the factors of each possible field, get the best field.
- The program will move the current piece in the current board to match the best field.