<< back to list of programming projects
The genetic algorithm approach towards solving the SnakeTron game relies on the biological principle of evolution. The snakes are created in generations, with each generation retrieving gameplay information from the prior generation. Each snake learns to adapt to its environment and pass down the traits that lead to the highest score to the subsequent generations.
The evolution process starts by randomly initializing the genes for the first generation of snake. Each snake is represented by a chromosome, which consists of genes. A set of chromosomes collectively constitute the population for each generation.
For each run of the game, the snakes’ behaviors are determined by using the genes as weights in a feed-forward neural network. 8 different information points regarding the game’s state are entered as input to the network, and the weights are used to determine whether the snake should move to the left, right or continue moving forward. The following information is provided to the neural network: whether the front, left and right blocks are empty or occupied, the snake’s current direction, angle and distance to the food block, angle and distance to the other snake’s head.
The input values are propagated forward through the neural network using tanh() activation function. The output is processed through the sigmoid function to yield a 3x1 vector. Finally, the maximum value in the vector is set equal to 1 while the remaining 2 values are set to 0. The index of the maximum value determines if the snake moves to the front, left or right.
At the end of each generation, the 12 fittest chromosomes are selected to breed and generate the next generation of offspring. The breeding process is performed using a simple crossover method, where genes from either parent have an equal 50% chance to define the gene of the offspring. As a final step before proceeding iterating with the next generation, each chromosome is subjected to a randomized mutation of its genes. This step is taken to avoid genetic repetition in the population, and to increase population variance. The mutation allows the population to take decision steps it may not have considered before.
The program is written in Python.
Check out the code at: https://github.com/BrandonFoster/csci76100-project.git
Copyright © 2022 Nablul - All Rights Reserved.