Simulating Evolution With "Jellies" and Neural Networks
In this post, I share some interesting results from a neuroevolution simulation. My goal here was to observe what happens when neural network powered “jellies” are left to fend for themselves as they compete to collect food pebbles and to avoid contact with each other.
Firstly, I put together a simple 3-layer neural network class for them to control their movements.
The jellies will be able to see their environment by shooting lasers out of their eyes (cool). I’ll use raycasting for this.
With this raycasting system, the jellies can “see” their environment. We’ll use this as input for their neural networks.
We can also visualize the neural networks as the jellies move around.
C# is cool because you can create some very simple and readable high-level code with respectable performance. On every new frame, the following update function is called:
Let’s talk about the Evolve function. Only the best jellies are allowed to breed due to stochastic universal sampling, which chooses two pseudo-random optimal jellies for chromosomal crossover. the crossover function works by choosing a point in each chromosome and switching everything after that point with the other chromosome's values. This is called (surprise) "single-point crossover".
Here’s an excerpt from the Crossover function:
Now that my oversimplified briefing of how the simulation works is over, we can watch them evolve.
On the 1st generation, they reacted to their environment with no rhyme or reason. After all, I didn't expect to see anything astounding; It was a group of randomly generated chromosomes.
On the 5th generation, we see that some learning has happened. The pink jellies are outperforming the others and evolution is making sure that they reproduce more.
On the 25th generation, the pink jellies have figured it all out. They don’t bump into each other and they have mastered the fine art of collecting pebbles
This is cool, but nothing too unexpected. Let’s play around and see what happens when we remove food pebbles from the equation!
Wow! They developed a herd instinct! By circling together like this, the entire colony can benefit and the jellies can better avoid contact with each other.
This result stunned me. It may be a simple simulation, but we can use it learn how real-world behaviours came to be. If you want to learn more and see some even cooler results, I highly recommend you check out Karl Sims’ research, or try building something like this on your own!