MEHTAB MAHIR
All projects

MACHINE LEARNING

Snake Reinforcement Learning

Learn a Snake-playing policy from rewards instead of scripted moves.

CONTRIBUTIONMachine learning projectView Source on GitHub

The Problem

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 Approach

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.

01

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.

02

Learn from New and Previous Moves

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.

03

Track Learning Across Games

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.

Experiment Output

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.

Back to projects