Press "Enter" to skip to content

Genetic Algorithms & Virtual Creatures

I’ve been reading a lot recently about neural networks and genetic algorithms, and I wanted to try a project based around these concepts… but what should I make? I searched for projects other people have done, and I found some cool projects which simulate evolution of little virtual creatures. I decided I wanted to create something similar because: a) there’s so many ideas to be implemented and different directions to be taken, and also b) who wouldn’t want to create a platform for evolution and watch the cool & crazy things that evolve in it!?

The basis for this project is as follows:

  • Virtual creatures will be put into an environment which contains food and other creatures.
  • These creatures will each be equipped with a neural network which takes inputs (angle to closest food, distance to closest food, health, etc) and gives outputs (move forward, turn left, etc).
  • The creatures will each contain DNA which consists of data from the neural net (weights, biases, etc) and information about attributes such as speed and size.
  • The creatures will live their lives out and if they survive long enough to do so, they will pass their DNA on.
  • After simulating for a while we should begin to notice intelligent behaviour, due to the simulated effects of evolution.

I decided to create the project in JavaScript so that it sped up development (plus it means anyone can view it in their browser!).

neuralclose

What you see here is several creatures (colourful circles) and food (small, green circles) scattered. One of the creatures has debugging enabled, so that we can see information about it such as: main vision, peripheral vision, direction, closest food (shown by line connecting it to the creature). In this case the creature can see food, so the neural net will process the information about angle and distance of the food and make a decision on whether it should turn left or right.

In the start these decisions are essentially meaningless because the neural network doesn’t know what it is doing. It doesn’t understand that it should turn towards it, or even that it must eat food. This means that a lot of creatures will die without ever eating food. However it is inevitable that some creatures will inadvertently find and eat food, and those that do so will live long enough to create a child and pass on their DNA. The diagram of the neural net to the left-below is a demonstration of a creature’s neural network. The inputs are angle to food and distance to food, which are processed through multiple layers to make a decision such as turn left or turn right.

net

The model is currently based on asexual reproduction, where a creature’s child will be a clone. The DNA will not be completely identical to the parent though, as mutations are applied to the DNA. These mutations change a value of one or more weights/biases in the neural network which alters the decision making process, which may be of benefit to the creature’s chances of survival. If it does help then it will be passed on, and if the mutation is detrimental, then the DNA will die with the creature and not be passed on – essentially simulating evolution!

The most common mutation that helps these creatures survive is an ability to ‘steer’ themselves towards food. Once the creatures have this ability their population quickly overtakes that of species unable to do so.

 

 

neural5cropped

These creatures are split into different ‘species’; each have their own traits and act differently than other species. Currently a species is defined by the setup of the neural network. For example, if the creature had 2 hidden layers with 6 and 4 nodes respectively, then it would be classed as 6-4. This species ID is used as a seed to create a pattern, which is rendered on the creature. A creature’s children will have the same pattern, although it will be offset so it’s not totally identical to the parent. This makes it easier to recognise the different types of species, but allows each creature to be unique.

There are several attributes in the DNA which can mutate to change how the creature looks/acts such as: speed, size, field of vision & view distance. These things differ creature to creature, but differ most between species. In order to prevent creatures simply evolving to be huge and fast, each creature has an energy cost function. Essentially a creature has X amount energy to begin with, and every step the amount of energy lost is proportional to the size + speed of the creature. So if the creature large and fast, then it will use a huge amount of energy. Over time this can cause species to evolve differently. The 2 major species types that evolve are small-fast creatures and large-slow ones.

neural7

After simulating for 30 minutes the population will boom. Sometimes this ends up with one species dominating, however often there is an equilibrium reached. In the simulation above only 3 species survived, with the pink species doing significantly better than brown or green. This is because it has evolved to become smaller and faster- this allows it to beat other creatures to the food which helps its proliferation.

The next aims for this project are:

  • Introduce predators and a concept of herbivore, omnivore and carnivore.
  • Create creatures that look more unique (perhaps generating polygons)
  • Alter vision so that a better sense of vision costs more energy.
  • Add a sense of smell to find food and detect pheromones.
  • Balance the number of creatures on screen to prevent lag…
  • Add more detailed information about the creature being debugged.

If you’d like to try the project out, you can run it in browser here. Additionally the source code can be viewed on GitHub.