Picture source: The Royal Society, Flickr
The programming section of this blog has been quite inactive lately. Hence, here’s an interesting little project I did last year.
The idea started off as a biophysics class presentation in collaboration with Abhilash Sahoo. From there, I adopted it into an optimization tool through Genetic Algorithms.
I’ve used MATLAB for all purposes. (Click here to view the code.)
The final animation:
The herding concept is inspired from Strogatz’s TED Talk: “How things in nature tend to sync up”.
Here is the basic skeleton:
- Green particles are prey
- Red particles are predators
- Each prey has 3 forces acting on it:
- a weak long-distance attractive force towards other prey
- a strong short-distance repulsive force away from other prey
- a strong repulsive force from predators
- The first two forces make the prey form a herd, while maintaining an equilibrium distance between themselves within the herd. The third force makes them flee from incoming predators.
- Each predator has 2 forces acting on it:
- A long-distance medium attractive force towards the general direction of a herd of prey
- A short-distance targeted strong attractive force towards a particular prey
- The predator starts its hunt using the first force. When it gets close to a prey, it switches to the second force to target the particular prey while ignoring the rest of the herd.
- The targeting is denoted in the video through red lines connecting the prey to the predator that appear when a prey is targeted.
- The green enclosure contains food for the prey.
- Predators have higher top speeds but are more massive. They can catch up to the prey in a straight line, but can’t turn as fast.
- The prey swerve automatically due to the nature of the forces, which sometimes allow them to escape.
Things get more interesting when we introduce natural selection. This can be done by creating survival parameters and the concepts of death, reproduction and imperfect copying.
Death happens when:
- A particle starves, that is:
- If a prey stays outside the green boundary for a long time
- If a predator cannot catch a prey for a long time
- A particle gets old, i.e. it runs out of its natural lifespan
- A prey gets caught by a predator
The survival parameters have different values for each prey/predator. I call them survival parameters as they are directly linked to the chances/duration of survival in our little virtual reality. The ones that I’ve included in this model are:
- The ability to accelerate (or inverse mass)
- Top speed
- Range of vision (e.g. from how far a prey can detect an approaching predator)
- Maximum duration of survival without food
- Natural lifespan
- The frequency of reproduction
- The strength of herding tendency (in case of preys)
Reproduction here is defined as a particle spawning a copy of itself. Each particle reproduces at a certain frequency. The child inherits the same values of parameters from the parent, but with small random errors. Over time these errors lead to a natural selection of parameter values in a way that maximizes the probable survival duration of the particles (since more survival duration means more number of offspring).
Leaving the model here would not be of any real use though. It’s a no-brainer that all the survival parameters will keep increasing in value over time; we don’t need a code to tell us that!
Hence we introduce interdependencies between the parameters to make them competitive. For example, we decide that a greater ability to accelerate (or less mass) would imply that the particle cannot survive without food for a long time. Thus we have created a trade-off between two desirable parameters.
This simple tweak makes this model a powerful tool of optimization. The code will ultimately maximize the survival duration and we can find out the values of the parameters that comprise the optimal solution. Basically, we will be able to quantify exactly how important each of these abstract survival parameters are for the particles in the simulation.
We can use similar techniques to optimize highly complex systems with several interdependent variables.