This project implements a Snake game agent powered by a Spiking Neural Network (BrainSNN). The agent uses latency-based coding for decision making and can evolve through training.
- Spiking Neural Network Brain: Uses the BrainSNN implementation with plasticity mechanisms
- Latency Coding: Makes decisions based on which output neuron spikes first
- Vision System: Uses ray casting to detect walls, food, and body segments
- Evolvable Architecture: Network can grow neurons and connections through plasticity
- Evolutionary Training: Population-based training to evolve better agents
- Python 3.8+
- NumPy
- Matplotlib (for plotting training progress)
Install requirements:
pip install -r requirements.txtTrain a new agent with evolutionary techniques:
python train_snake_snn.py --mode train --generations 50 --population 20 --renderOptions:
--generations: Number of generations to train (default: 50)--population: Population size (default: 10)--render: Enable visualization of the best agent during training
The training process will:
- Create a population of agents with varied parameters
- Evaluate each agent's performance on multiple Snake game episodes
- Select the best-performing agents for reproduction
- Create a new population with mutation
- Repeat for the specified number of generations
The best agent will be saved to best_agent.pkl.
Test a trained agent:
python train_snake_snn.py --mode test --agent best_agent.pklPlay the Snake game manually using arrow keys:
python train_snake_snn.py --mode manual- Vision Rays (24 neurons): 8 rays × 3 values (distance to wall, food, body)
- Direction (4 neurons): One-hot encoding of current direction
- Danger (3 neurons): Immediate danger in forward, right, and left directions
- Food Direction (2 neurons): Normalized vector pointing to food
Hidden Layer (50-200 neurons)
- Starts with 50 neurons
- Can grow up to 200 neurons through plasticity
- UP, RIGHT, DOWN, LEFT actions
- Uses latency coding (first neuron to spike wins)
-
Sensory Processing:
- Cast vision rays in 8 directions to detect objects
- Encode current direction and immediate danger signals
- Convert state information to spike probabilities
-
Decision Making:
- Run the SNN for multiple time steps
- Record when each output neuron first spikes
- Choose action based on earliest spike (latency coding)
-
Learning:
- Apply plasticity after receiving positive rewards
- Form new connections between active hidden neurons
- Potentially grow new neurons
-
Evolution:
- Select agents with highest fitness (score + survival time)
- Create new generation through mutation of parameters
- Gradually improve performance over generations
brain_snn_numpy.py: The core SNN implementationsnake_environment.py: Snake game environmentsnake_agent.py: Agent that interfaces between the environment and SNNtrain_snake_snn.py: Training and evaluation code
You can customize various parameters in the SnakeAgent class:
- Number of hidden neurons
- Maximum number of hidden neurons
- Latency window for decision making
- Connection probability
- Plasticity threshold
- Neuron and connection growth rates
