Having slightly changed the game for the neural network, for example, I added variables for the distance from the bird to the pipe. There are 3 by 3, since we need to know the height of each pipe (y) and the distance by x, and there were no more than three pairs of pipes on the screen, so there are three by three (nine in total). Also after the collision the function was restarted and the third parameter, which is called rep of the function, was passed what kind of restart it was, if it was equal to three, then the game returned the fitness function to the Genetic Algorithm, and if it was zero, then we assign the value 0 to the time variable. Also, I did not write two very similar functions, but simply checked if the checkNN variable is True, then the screen needs to be updated. I also modified the training code
while True:
for event in pg.event.get():
if event.type == KEYDOWN:
if event.key == K_1:
showNN = True
epoch += 1
print(str(epoch) + " epoch")
if epoch < 10:
for s in goodNet:
timeNN = anp.NPong(s, False, 0, 0)
listNet.update({
s : timeNN
})
if epoch >= 10:
for s in goodNet:
timeNN = anp.NPong(s, False, 0, 1)
listNet.update({
s : timeNN
})
After the tenth epoch, due to the last parameter, which we change to one (in the game code I called this parameter varRe from the words variant of return), the game returns not the time, but the number of pipes before the collision (this way the neural network learns better)
howALot = 1000 - len(NewNet)
if howALot < 40:
howALot = 40
These three lines of code are needed if in the previous era of AI there were very, very many with the same result and the algorithm may stop learning, since it will have nothing to learn :-).
Afterwards I updated and accelerated my GA for FlappyBird, now all birds are launched simultaneously, so training accelerated from ~3-5 hours to 5-10 minutes when launched on CPU, that is, 50 times! How it works - I suggest you see for yourself: a small useful repetition of what has been covered!
That's all, if you have any questions, write in the comments, bye!
Codes:
SOCIAL SHARE CARD GENERATOR