├── README.md └── SmartSnakesCombine ├── Food.pde ├── Matrix.pde ├── NeuralNet.pde ├── Population.pde ├── SmartSnakesCombine.pde ├── Snake.pde ├── SuperSnake.pde ├── SuperSnakeClone.pde ├── World.pde └── data └── snakesStored.csv /README.md: -------------------------------------------------------------------------------- 1 | # SnakeFusion 2 | Using the genetic algorithm and neural networks I trained up 5 snakes who will then fuse to become the ultimate snake, this is how I did it 3 | 4 | The program I used to write and run this code was processing https://processing.org/download/ 5 | 6 | If you get it working some instructions 7 | press Space to just show the current best from each population 8 | press d to double the mutation rate 9 | press h to halve it 10 | after you run it some snakes will be saved press 0 to 4 to check them out 11 | to further train the snakes of legend (the saved snakes) press L 12 | to fuse snakes together to create a super snake press f 13 | 14 | 15 | I hope my commenting makes sense 16 | 17 | Have fun :) 18 | -------------------------------------------------------------------------------- /SmartSnakesCombine/Food.pde: -------------------------------------------------------------------------------- 1 | class Food { 2 | PVector pos;//position of food 3 | 4 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 5 | //constructor 6 | Food() { 7 | 8 | 9 | pos = new PVector(); 10 | // set position to a random spot 11 | pos.x = 400+floor(random(0, 40)) * 10; 12 | pos.y = floor(random(0, 40)) * 10; 13 | } 14 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 15 | //show the food as a red square 16 | void show() { 17 | fill(255, 0, 0); 18 | rect(pos.x, pos.y, 10, 10); 19 | } 20 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 21 | //return a clone of this food 22 | Food clone() { 23 | Food clone = new Food(); 24 | clone.pos = new PVector(pos.x, pos.y); 25 | 26 | 27 | return clone; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /SmartSnakesCombine/Matrix.pde: -------------------------------------------------------------------------------- 1 | 2 | class Matrix { 3 | 4 | //local variables 5 | int rows; 6 | int cols; 7 | float[][] matrix; 8 | 9 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 10 | //constructor 11 | Matrix(int r, int c) { 12 | rows = r; 13 | cols = c; 14 | matrix = new float[rows][cols]; 15 | } 16 | 17 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 18 | //constructor from 2D array 19 | Matrix(float[][] m) { 20 | matrix = m; 21 | cols = m.length; 22 | rows = m[0].length; 23 | } 24 | 25 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 26 | //print matrix 27 | void output() { 28 | for (int i =0; i1) { 231 | matrix[i][j] = 1; 232 | } 233 | if (matrix[i][j] <-1) { 234 | matrix[i][j] = -1; 235 | } 236 | } 237 | } 238 | } 239 | } 240 | //--------------------------------------------------------------------------------------------------------------------------------------------------------- 241 | //returns a matrix which has a random number of values from this matrix and the rest from the parameter matrix 242 | Matrix crossover(Matrix partner) { 243 | Matrix child = new Matrix(rows, cols); 244 | 245 | //pick a random point in the matrix 246 | int randC = floor(random(cols)); 247 | int randR = floor(random(rows)); 248 | for (int i =0; i