This code is for my NEA project, A level computer science.
Optimisation using a genetic algorithm to make an AI that plays good Tetris
- train_agent_scratch.py uses a genetic algorithm written from scratch, without next piece knowledge (hasn't been optimised yet)
- With next piece knowledge, the agent does very well. Results are shown here: https://www.youtube.com/watch?v=F8TkvoR6etk&t=656s
The population and nueral net are coded in neuralnet.py
Update log
- train_agent_scratch.py now has capability to plot graphs after running all epochs, as well as saving a population every 2 epochs
- tetris.py -> human playable version now has complex moves implemented such as T-spins
- Ablation tests are now possible in tetris_agent_scatch.py
References:
- Used a gameboy emulator and pytorch to build a Tetris AI from scratch: https://towardsdatascience.com/beating-the-world-record-in-tetris-gb-with-genetics-algorithm-6c0b2f5ace9b
- Explains use of a genetic algorithm without neural networks: https://codemyroad.wordpress.com/2013/04/14/tetris-ai-the-near-perfect-player/
- The neural network was built upon an the example in Tariq Rashid's book (Build your own Neural Network) which uses a neural network to classify handwritten digits from the MNIST database: https://www.goodreads.com/en/book/show/29746976-make-your-own-neural-network
- Explanation of genetic algorithms: https://lethain.com/genetic-algorithms-cool-name-damn-simple/
- Inspiration from Greer Viau, who also used a genetic algorithm to build a Tetris agent: https://www.youtube.com/watch?v=1yXBNKubb2o&t=482s
- The original paper on NEAT-Python: http://nn.cs.utexas.edu/downloads/papers/stanley.cec02.pdf
- Using NEAT-Python for Sonic the Hedgehog: https://www.youtube.com/watch?v=pClGmU1JEsM&list=PLTWFMbPFsvz3CeozHfeuJIXWAJMkPtAdS
- Explanation of wall kicks and the Super Rotation System: https://harddrop.com/wiki/SR
- Uses a local search algorithm to build a Tetris agent: https://github.com/saagar/ai-tetris/blob/master/paper/tetrais.pdf
- Used this as an idea of how to write a line clearing algorithm: https://www.youtube.com/watch?v=HcGQB1nHOOM&t=934s
- Foundational ideas for Tetris Implementation by Javidx9 on the OneLoneCoder channel: https://www.youtube.com/watch?v=8OK8_tHeCIA&t=1083s
How to train:
- Run tetris_ai_scratch.py to train the agent. It gives prompt for whether you want to load a population. If you want to, type "Y", it then asks from which epoch, type 2, 4, 6 ,8 or 10.
- If you don't want to load a population, just enter.
Results
- Over 1 million highscore after 10 epochs of training
Graphing: If graphs don't work automatically, program will print out all the data with text so you know what is what, you'll have to manually pass it into the visualization class. Make an object in that file and pass data in there, then run the visualisation file.