A Compact State Representation
The state includes danger ahead and to either side, the current direction, and the food’s relative location. An 11–256–3 network maps those inputs to action values without processing raw screen pixels.
MACHINE LEARNING
Learn a Snake-playing policy from rewards instead of scripted moves.
I wanted to train an agent to choose moves from game state and rewards without scripting a route to the food. It needed to represent nearby danger, direction, and food position, explore unfamiliar moves, and learn from unsuccessful games.
The agent encodes the game as 11 state values and uses a neural network to estimate the value of three relative actions: straight, right, or left. Training happens after individual moves and from sampled replay memory after each game. The game restarts automatically so learning can continue.
The state includes danger ahead and to either side, the current direction, and the food’s relative location. An 11–256–3 network maps those inputs to action values without processing raw screen pixels.
A replay buffer stores up to 100,000 transitions. Training combines recent transitions with batches of up to 1,000 stored experiences instead of relying only on the last move.
Random exploration decreases as the game count increases. Per-game and running-average scores are plotted, and model weights are saved when a new high score is reached.
The training loop includes score tracking and saved model checkpoints. The per-game and average-score plots make changes in performance visible across a training run.