What follows are screencasts from runs of a simulation of swarming behavior I wrote in MATLAB a couple of years ago as an exercise of pure curiosity and programming interest. I have always been fascinated by the way an ant colony appears to move with all the individual ants seeming to walk individually and independently, but a sort of overall group level movement also seeming to arise. I was also interested in how macro-level behavior can sometimes emerge from micro-constituents that are each following very simple rules. In the simulations that follow, the rule is exceedingly simple: if any dots get to within a certain “threshold” distance of each other, they all adjust their direction of movement to the average direction of all dots within this distance. I have made it so that both this threshold and the total number of dots are parameters of the simulation. Below is the complete code needed to run the simulation.
Screen('FillOval',w,[255 255 255],vertcat(pos-5,pos+5))
DrawFormattedText(w,['# dots: ' num2str(size)],50,50,[255 255 255]);
DrawFormattedText(w,['thresh: ' num2str(distThresh)],50,80,[255 255 255]);
For this first run of the simulation, I have set the distance threshold to 5 pixel units and the number of dots to 120. As you can see, it seems to take a long time for any swarming behavior to emerge and the dots tend to continue traveling along their mostly independent paths.
So, I then tried increasing this threshold to 20. As expected, we can now observe more clustering emerge much more rapidly and eventually the clusters are all travelling in what seems to be a general overall direction.
Wishing to experiment with this parameter even more, I increased it to 50. At this point, the aggregate movement behavior emerges right away, and we have the appearance of the whole cloud of dots drifting together in a communal direction.
Finally, I tried to increase the number of dots simulated up to 200 while using a medium-sized threshold of 15 pixels. While the memory seems to stutter (note that this is only as a result of taxing my processors to record the screen in addition to running the simulation), we can again see the formation of several distinct groupings of dots that at the end of the simulation seem to be all moving in a similar direction and speed.