Creating a genetic algorithm for a neural network and a neural network for graphic games and video games using Python and NumPy

submited by
Style Pass
2024-12-13 21:00:08

Today I will tell and show how to make a Genetic Algorithm (GA) for a neural network so that it can play different games with it. I tried it on the game Pong and Flappy bird. It showed itself very well. I advise you to read it if you haven’t read the first article: “Creating a simple and efficient genetic algorithm for a neural network with Python and NumPy”, since I have modified my code that was shown in that article.

I divided the code into two scripts, in one the neural network plays a game, in the other it learns and makes decisions (the genetic algorithm itself). The code with the game is a function that returns a fitness function (it is needed to sort neural networks, for example, how long it lasted, how many points it earned, etc.). Therefore, the code with the games (there are two of them) will be at the end of the article. The genetic algorithm for the neural network for the game Pong and the game Flappy Bird differ only in parameters. Using the script I wrote and described in the previous article, I created a heavily modified genetic algorithm code for the game Pong, which I will describe most of all, since it was what I relied on when I created the GA for Flappy Bird.

AnnPong is a script with a game listNet, NewNet, goodNet – lists of neural networks (we’ll go into more detail later) timeNN – fitness function MoveRight, moveLeft – select neural network where to move epoch – epoch counter

Leave a Comment