Swarms

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.

function swarms(size,distThresh)
HideCursor;
screens=Screen('Screens');
screenNumber=max(screens);
[w,rect]=Screen('OpenWindow',screenNumber,0,[],32);
[width,height]=RectSize(rect);
cx=width/2;cy=height/2;
cent=[cx;cy];
pos=repmat(cent,1,size)+(200*randn(2,size));
vel=6*randn(2,size);
while 1
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]);
Screen('Flip',w);
pos=pos+vel;
pos(1,pos(1,:)<0)=width;
pos(1,pos(1,:)>width)=0;
pos(2,pos(2,:)<0)=height;
pos(2,pos(2,:)>height)=0;
for i=1:size
[~,rhoP]=cart2pol(pos(1,:)-pos(1,i),pos(2,:)-pos(2,i));
closeP=find(rhoP<distThresh);
[theV,rhoV]=cart2pol(vel(1,closeP),vel(2,closeP));
[vel(1,i),vel(2,i)]=pol2cart(mean(theV),mean(rhoV));
end
[~,~,buttons]=GetMouse;
if any(buttons);break;end
end
Screen('CloseAll')
ShowCursor;

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.