Dancing with friends and enemies: swarm intelligence

Posted by & filed under Art, Code, Community, Graphics, Mathematica, Wolfram.

This post is based on a thread from Wolfram Community. The rules by Simon Woods are simple:

1000 dancers assume random positions on the dance-floor. Each randomly chooses one “friend” and one “enemy”. At each step every dancer moves 0.5% closer to the centre of the floor, then takes a large step towards their friend and a small step away from their enemy. At random intervals one dancer re-chooses their friend and enemy. Here is the dance…

n = 5000;
r := RandomInteger[{1, n}];
f := (#/(.007 + Sqrt[#.#])) & /@ (x[[#]] - x) &;
s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];
x = RandomReal[{-1, 1}, {n, 2}];
{p, q} = RandomInteger[{1, n}, {2, n}];
Graphics[{PointSize[0.008], Opacity[.5], Dynamic[If[r < 100, s];
   Point[x = 0.995 x + 0.02 f[p] - 0.01 f[q]]]}, PlotRange -> 2]


Leave a Reply

Your email address will not be published. Required fields are marked *