Code Monkey home page Code Monkey logo

snakeai's Introduction

SnakeAI

Download and Run

To run the program you will need Processing

Snake

Neural Network

Each snake contains a neural network. The neural network has an input layer of 24 neurons, 2 hidden layers of 16 neurons, and one output layer of 4 neurons. Note: Network can now be customized with the number of hidden layers as well as the number of neurons in the hidden layers.

Vision

The snake can see in 8 directions. In each of these directions the snake looks for 3 things:

  • Distance to food
  • Distance to its own body
  • Distance to a wall

3 x 8 directions = 24 inputs. The 4 outputs are simply the directions the snake can move.

snakeai-1

Evolution

Natural Selection

Each generation a population of 2000 snakes is created. For the first generation, all of the neural nets in each of the snakes are initialized randomly. Once the entire population is dead, a fitness score is calculated for each of the snakes. Using these fitness scores, some of the best snakes are selected to reproduce. In reproduction two snakes are selected and the neural nets of each are crossed and then the resulting child is mutated. This is repeated to create a new population of 2000 new snakes.

Fitness

A snakes fitness is dependant on how long the snake stays alive as well as its score. However they are not equally important, having a higher score is rewarded more than a snake who simply stays alive. There is the possibility however that a snake may evolve a strategy where it loops in a certain pattern and never dies. Even though having a high score is prioritized more, if a snake can stay alive forever then that is a clear problem. To avoid this each snake is giving 200 starting moves at the beginning of its life. Every time it eats a piece of food it gains 100 more moves, with a maximum of 500 moves. This means that snakes who evolve to go in loops will eventually die and snakes who go for the food will not only have a higher score, but stay alive longer.

Crossover & Mutation

When two snakes are selected for reproduction, what happens is that the snakes brains are crossed with each other. What this means is that part of one parents brain is mixed with part of the second parents and the resulting brain is assigned to the child. After the crossover the brain is also mutated according to a mutation rate. The mutation rate determines how much of the brain will be randomly altered.

snakeai-2

Data Collection

Graphing

The graph represents the score of the best snake from each generations. In some generations the graph may dip bellow the previous, this is because even though the score may have been worse, some trait allowed the snake to live longer and gain a higher overall fitness.

snakeai-graph

Save & Load

Models can be saved and loaded in order to test a model in new situations. The weights for each connection are saved in a CSV file. The evolution graph is also saved in order to view the evolution progress of the model.

snakeai's People

Contributors

greerviau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

snakeai's Issues

add input

I wonder what will happen if there is added a input for 'hunger' ie. the longer after last apple, the hungrier the snake gets

InvocationTargetException when saving in a file with an extension other than .csv

Operating System

Windows 10

Steps to reproduce

  1. Start SnakeAI
  2. Click on Save Button
  3. Enter a filename with an extension other than .csv, "model.dat" for e.g.
  4. Click on Save

A file is created but its size is 0 Kb and the error bellow appears in console

Error

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at processing.core.PApplet.selectCallback(PApplet.java:6719)
at processing.core.PApplet.access$1(PApplet.java:6712)
at processing.core.PApplet$4.run(PApplet.java:6623)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.IllegalArgumentException: No extension specified for saving this Table
at processing.data.Table.save(Table.java:1184)
at processing.data.Table.save(Table.java:1175)
at processing.core.PApplet.saveTable(PApplet.java:6299)
at processing.core.PApplet.saveTable(PApplet.java:6276)
at SnakeAI.fileSelectedOut(SnakeAI.java:226)
... 21 more

Suggestion

The "save as" dialog should automatically add the .csv extension

Saved model file does not load

Loading the saved .csv model file failed if we trained model with more generations than are weights in level L0.

Method in SnakeAI.pde:
void fileSelectedIn(File selection)

157:
while(genscore != 0)

it should be
while((genscore != 0) & (g<modelTable.getRowCount()-1))

E.g. For topology 1x4 we have 25x4 L0 weights => rows in saved output .csv file
If the simulation is performed for more than 100 generations (101) than 100th row in saved .csv file it will have in Graph column genscore<>0 and 101st row will not exist. An error exception will be thrown and the model will not be loaded.

It's just work around. In case we need a Graph of progress, the results of the score per generation should be saved in a separate file.

Dont do this

boolean foodCollide(float x, float y) {  //check if a position collides with the food  
     if(x == food.pos.x && y == food.pos.y) {
         return true;
     }
     return false;
}

What about:

boolean foodCollide(float x, float y) {  //check if a position collides with the food    
     return x == food.pos.x && y == food.pos.y;
}

Otherwise cool!

Size of the snake as an input

I really wonder what happens if you add one more input neuron that represents size of the snake. If the size also be taken into account, better strategies will be developed, i hope.

Manhattan distance (see Wikipedia)

For the diagonal directions, it seems your distance calculation is wrong, it might be the correct distance as the crow flies, but you cannot walk like that in Manhattan (nor in Snake). The distance is the sum of the absolute differences of the x's and the y's (whether you go zig-zag or not makes no difference).

PyTorch model

Thanks for sharing the great code!
I am working most of the time with PyTorch. My question is

How can I plug my PyTorch model to your snake's brain?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.