Creating an Ai to play Tetris (part 3)
Last time we have been talking about the general idea of PSO. It is like a social simulation of population behavior. Basically, we stores the weights of attributes as the position of the particle and consistently update the position of the particles according to their observations of “best position” from their own experience and the society. After certain number of round of update, the population would possibly head to the best position. Then here, we are applying this idea and implement our AI agent trainer!
Pseudo code (Version 1)
Note: this is just for demonstrating the high level idea, please do not try to run this code ;)
Firstly, the most important part of the game, the main training logic
Then we break it down into functions. Firstly, have a look at InitializesParticles
Then is about how the particles are going to play the game. For simplicity, we just assume that we already have a well-defined Tetris player (an interface that will tell us the game state, the game board, the next piece, the legal moves and can execute a move in the game).
You may notice that I left the testMove function because this function can have multiple ways of implementation. The basic idea for this function is just return the sum of attributeWeight * attributeData. Then we have evaluateParticles. For simplicity, for now we just say the more lines that you have cleared, the more you fit this game. After that we have updateParticles, which will be shown below:
Yes then we have finished! Quite simple right? However, this implementation does not behave very well in the exact training. In my next chapter I will be talking about why and how to improve it!