Dancing with friends and enemies: boids' swarm intelligence
Adapted from Wolfram Community. Written by Simon Woods
Download original notebook- 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
Randomness is deliberately injected. Here is the dance...
Evaluate initialization cells first
or go one by one
n = 1000; r := RandomInteger[{1, n}]; f := (#/(.01 + 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}];
Main render function
EventHandler["frame", Function[Null, x = 0.995 x + 0.02 f[p] - 0.01 f[q]; If[r < 100, s]; ]];
Drawing function Evaluate this cell very last
Graphics[{ PointSize[0.007], Point[x // Offload], AnimationFrameListener[x // Offload, "Event"->"frame"] }, PlotRange -> {{-2,2}, {-2,2}}, TransitionType->None]
(*VB[*)(FrontEndRef["c2a108dd-d7c9-4f48-8ad8-8445b67ed254"])(*,*)(*"1:eJxTTMoPSmNkYGAoZgESHvk5KRCeEJBwK8rPK3HNS3GtSE0uLUlMykkNVgEKJxslGhpYpKToppgnW+qapJlY6FokpgAJExPTJDPz1BQjUxMAiq4VtA=="*)(*]VB*)
To stop
EventRemove["frame"];
To restart - evaluate EventHandler
and then a kick-starter
EventFire["frame", Null];