A small Python project that implements a Pong-style game with an evolving neural network AI using a generic genetic algorithm.
Play.py: a simple playable Pong-style game where the left paddle is controlled by the keyboard and the right paddle is a hard-coded bot.PopGame.py: the Pong game engine designed to run neural networks as paddle controllers.FeedFowardNeuralNetwork.py: a feed-forward neural network implementation with support for custom activation/output functions.GenericAlgorithm.py: a generic evolutionary algorithm base class for populations ofIndividualobjects.NeuralNetworkGenericAlgorithm.py: a genetic algorithm implementation that evolvesNeuralNetworkobjects by playing games and using fitness score.
- Python 3.10+ (or compatible)
pygamenumpy
Install dependencies with:
python3 -m pip install pygame numpyThis is the easiest way to run the game.
python3 Play.pyControls:
W/S: move the left paddle up/down- Arrow keys: nudge the ball in the window for testing
The right paddle is controlled by a simple rule-based bot that tracks the ball vertically.
Run the genetic algorithm trainer:
python3 NeuralNetworkGenericAlgorithm.pyThis will:
- initialize a population of neural networks
- evaluate each network by letting it play in
PopGame.Gameas the right paddle (nn2) - use tournament selection, crossover, and mutation to create new generations
- write the best evolved neural network to
Generic Algorithm Result.txt
PopGame.py contains the game logic for using neural networks as controllers. It does not have a direct command-line entry point, but you can launch it from a Python script or interactive session.
Example to run a trained model from Generic Algorithm Result.txt:
from FeedFowardNeuralNetwork import NeuralNetwork
from PopGame import Game
nn = NeuralNetwork.getNNFromFile(3, 20, 1, "Generic Algorithm Result.txt")
game = Game(nn2=nn)
score = game.runGame()
print("Result:", score)Use two neural networks with Game(nn1=..., nn2=...):
from FeedFowardNeuralNetwork import NeuralNetwork
from PopGame import Game
nn1 = NeuralNetwork.getNNFromFile(3, 20, 1, "Generic Algorithm Result.txt")
nn2 = NeuralNetwork.getNNFromFile(3, 20, 1, "Generic Algorithm Result.txt")
game = Game(nn1=nn1, nn2=nn2)
score = game.runGame()
print("Result:", score)This will make both paddles controlled by neural networks.
Currently:
Play.pyis human vs a simple bot.PopGame.pyis built to run neural networks automatically.
The model is a classic feed-forward neural network with:
- 3 input values
- 1 hidden layer
- 1 output neuron by default
tanhactivation and output functions
The network encodes both weights and biases as a single linear genome.
PopGame.Game.nextMove() sends the following values to the neural network:
- paddle Y position
- ball Y position
- distance from the ball to the paddle
The output is interpreted as a movement command:
- with 1 output: move up or down depending on the sign
- with 2 outputs: compare values to choose up/down
- with 3 outputs: choose one of [up, none, down]
GenericAlgorithm.py implements the evolution loop:
- initialize a population of individuals
- evaluate fitness for each individual
- sort by fitness descending
- keep a small elite fraction unchanged
- generate new individuals by crossover and mutation
- repeat for the specified number of generations
Key hooks:
crossover(individuals)creates a child from parent genomesmutation(individual)randomly perturbs the genomeselectParents()chooses parents for reproductionsetPopulationFitness()evaluates the current population
This subclass specializes the generic algorithm for neural networks:
- selects parents using tournament-style selection
- performs crossover by gene-level mixing from random parents
- mutates a network by randomly replacing weights/biases with new values
- evaluates each network by letting it play a game and using its hit count as fitness
Fitness metric:
- the algorithm measures
score[1][1]fromPopGame.Game.runGame(), which reflects the number of successful paddle hits by the neural network
NeuralNetworkGenericAlgorithm.pycan take some time to run depending on population size and generation count.pygamemust be installed before running either game script.- The best evolved model is saved in
Generic Algorithm Result.txt.