Virtual Fruit Flies

I show you how to train your own fruit flies to survive on their own.

Virtual Fruit Flies
Photo by Pedro Miguel Aires / Unsplash

A fun application of neural networks is to build virtual agents that learn to do something interesting on their own - what better application of this concept than to host a world inhabited by tiny fruit flies who, through the process of modelling natural selection, learn to mimic the behaviours of real-life fruit flies.

This is exactly what I did and now you can experiment yourself with these fruit flies at the following website:

Fruit Fly Dashboard

You can train the flies from scratch and watch the process of the top performing flies propagate their genes to future generations over time. You will notice each generation of fruit fly gradually get better and better.

If you don't want to wait you can load a handful of pre-trained brains and watch just how advanced some of them have become.

The fruit fly simulation dashboard.

Users view the simulation in real time via a dashboard comprised of a view port of the fly enclosure, a rendering of the fittest fly's brain as well as an interpretation of it's thought stream, there is also a few controls to specify the behavior of the simulation like food drops, simulation speed and even the mutation rate of the flies.

Real-time brain rendering.

Part of the fun in using this simulation is gaining a deeper understanding of neural networks and brains themselves. The neural network of the best performing fly at any given time is rendered as a beautiful diagram. Each neuron that fires illuminates the diagram as either a negative (red) or positive (green) shade. Strengths of each synaptic connection are illustrated by blue lines that darken with significance. This really helps bring the brain to life, you can see the fly thinking.

Flies navigate their environment in search of food.

The flies will learn to hunt for food throughout the bounds of their world, after many simulations it becomes apparent that this is always naturally selected as a priority behavior. Some flies learn to conserve their energy and simply wait around for the next food to spawn, others will actively dart around constantly searching for food.


The take away

This project offers a hands-on way to understand neural networks from first principles. More importantly, it demonstrates how complex, lifelike behaviour can emerge from simple, deterministic systems.

It raises an interesting question: where do we draw the line between something that is “alive” and something that isn’t?

Everything in our world is built from the same non-living components. At what point does life begin?

Maybe that’s the wrong question. Perhaps “life” isn’t a binary property at all, but an emergent phenomenon - something that arises when matter is arranged in sufficiently complex and dynamic ways.