Dancing with friends and enemies: boids' swarm intelligence - Online Technical Discussion Groups—Wolfram Community
Answer
(Unmark)
Mark as an Answer
WOLFRAM COMMUNITY
Dashboard
Groups
People
41
102K Views
30 Replies
160 Total Likes
View groups...
Follow this post
Share
Share this post:
GROUPS:
Biological Sciences Wolfram Science Mathematica Graphics and Visualization Wolfram Language Numerical Computation
Dancing with friends and enemies: boids' swarm intelligence
Simon Woods
Simon Woods
Posted 14 years ago
The latest way I have found to use my expensive math software for frivolous entertainment is this. Here's is a way to describe it.<br>1000 dancers assume random positions on the dance-floor.<br>Each randomly chooses one "friend" and one "enemy".<br>At each step every dancer<br>moves 0.5% closer to the centre of the floor<br>then takes a large step towards their friend<br>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...<br>n = 1000;<br>r := RandomInteger[{1, n}];<br>f := (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;<br>s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];<br>x = RandomReal[{-1, 1}, {n, 2}];<br>{p, q} = RandomInteger[{1, n}, {2, n}];<br>Graphics[{PointSize[0.007], Dynamic[If[r 2]
Thanks to Vitaliy for posting this on my behalf, complete with animations :-)
Background: I had read somewhere that macro-scale behaviour of animal swarms (think of flocks of starlings or shoals of herring) is explained by each individual following very simple rules local to their vicinity, essentially 1) try to keep up and 2) try not to collide. I started trying to play with this idea in Mathematica, but it was rather slow to identify the nearest neighbours of each particle. So I wondered what would happen if each particle acted according to the locations of two other particles, regardless of their proximity. The rule was simply to move away from one and towards the other.
The contraction (x = 0.995 x) was added to prevent the particle cloud from dispersing towards infinity or drifting away from the origin. I tweaked the "towards" and "away" step sizes to strike a balance between the tendency to clump together and to spread apart (if you make the step sizes equal you get something more like a swarm of flies). With each particle's attractor and repeller fixed, the system finds a sort of dynamic equilibrium, so to keep things changing I added a rule to periodically change the attractor and repeller for one of the particles. The final adjustment was to make the "force" drop towards zero for particles at very close range. This helps to stop the formation of very tight clumps, and also prevents a division-by-zero error when a particle chooses itself as its attractor or repeller.
The description of the system as a dance was an attempt to explain the swirling pattern on the screen without using mathematical language. I'd love to see what other "dances" can be created with other simple rules.
POSTED BY: Simon Woods
Reply
Flag
30 Replies
Sort By:
Replies
Likes
Recent
Kirill Vasin
Kirill Vasin, Augsburg University
Posted 4 months ago
Fascinating work<br>13 years later here is my 3 coins. It is running in real-time using GraphicsComplex instead of Point which takes advantage of GPU pipeline on WLJS Notebook frontend
I could not upload a video, so here is a link.
POSTED BY: Kirill Vasin
Reply
Flag
Richard Gaylord
Richard Gaylord
Posted 13 years ago
What you call "frivolous entertainment," social scientists (including socio-physicists) call 'serious research.' I prefer your description, but then IMO, most social science simulations, if viewed as research (which they do), should be identified as ridiculous pseudo-science.
POSTED BY: Richard Gaylord
Reply
Flag
Mohamed Zaghloul
Mohamed Zaghloul, ETH Zurich
Posted 13 years ago
@Jari Kirma: Great, Thanks a lot
POSTED BY: Mohamed Zaghloul
Reply
Flag
Jari Kirma
Jari Kirma
Posted 13 years ago
@MohamedZaghloul: you can export per-frame data in the following way, in this case 100 frames:<br>n = 1000;<br>r := RandomInteger[{1, n}];<br>f := (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;<br>s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];<br>x = RandomReal[{-1, 1}, {n, 2}];<br>{p, q} = RandomInteger[{1, n}, {2, n}];
Scan[If[r ToString@# <> ".dat", x = 0.995 x + 0.02 f[p] - 0.01 f[q]] &, Range[100]]
This produces files swarm_1.dat, swarm_2.dat, ... swarm_100.dat.
Format of export is decided by file extension (".dat" in this case for Table format), or third parameter to Export (see documentation).
You can also "play back" such a sequence of files, albeit it may be a bit slow this way:i = 1;<br>p = {};<br>Graphics[{PointSize[0.007],<br>Dynamic[With[{f = "swarm_" <> ToString@i <> ".dat"},<br>If[FileExistsQ[f], i++; p = Point[Import[f]], p]]]},<br>PlotRange -> 2]
POSTED BY: Jari Kirma
Reply
Flag
Safi Ahmed
Safi Ahmed, National University of Sciences and Technology (NUST)
Posted 13 years...