
Fireflies - bpierre
http://ncase.me/fireflies/
======
drueter
Apologies for what might be shameless self-promotion, but if you're interested
in this idea, you might like my 2012 project , "The Kuramoto Model (1,000
Fireflies)." I built 1,000 microcontroller-based LED devices that synchronized
with each other using radio communication, with a similar algorithm to what
Nicky used. Link is here: [http://davidrueter.com/projects/2012-the-kuramoto-
model.html](http://davidrueter.com/projects/2012-the-kuramoto-model.html)

~~~
Faaak
Very good result although the green mind in me screams a bit when thinking
about all this waste.

~~~
blacksmith_tb
Hmm, my city gives away bike lights pretty frequently - they aren't as
sophisticated (sadly) but it doesn't seem especially wasteful either way (at
least I'd assume that a fair percentage of the riders using them would have
bought some kind of light otherwise; and as long as they continue to work, I
wouldn't expect them to end up in a landfill). The synchronization is
especially fun.

------
mettamage
Yay ncase did it again! Don't forget to (re)view his project on The Parable of
the Polygons:
[https://news.ycombinator.com/item?id=8716538](https://news.ycombinator.com/item?id=8716538)

Playable posts are amazing. Interactivity in texts is amazing.

<3

~~~
RKoutnik
If you like interactive posts with lots of play, check out Bret Victor's work
(if you haven't already). Good start here:
[http://worrydream.com/ClimateChange/](http://worrydream.com/ClimateChange/)

~~~
mettamage
Fun fact which I heard on a conference about gaming: Vihart (who ncase
collaborated with) worked together with Bret Victor.

Disclaimer: not sure if memory serves me right though O:)

------
tptacek
This is similar to a pretty famous result in networking and distributed
systems about self-synchronization, which used to cause problems.

[http://ee.lbl.gov/papers/sync_94.pdf](http://ee.lbl.gov/papers/sync_94.pdf)

~~~
demands
Fascinating! I can see how synchronization across distributed systems would
cause problems -- lots of requests against the same type of resource at the
same time, for example, resulting in a thundering herd
([https://en.wikipedia.org/wiki/Thundering_herd_problem](https://en.wikipedia.org/wiki/Thundering_herd_problem))
-- but also how it could be absolutely what you want (like if you want to
synchronize clocks across systems, which I guess is exactly what's going on in
the OP
[https://en.wikipedia.org/wiki/Clock_synchronization](https://en.wikipedia.org/wiki/Clock_synchronization)).

------
fogleman
I did the same thing a few years ago! My fireflies didn't move around though.

[https://www.michaelfogleman.com/static/fireflies/](https://www.michaelfogleman.com/static/fireflies/)

~~~
JKCalhoun
Nice.

I saw your comment: "The cells need not have identical individual frequencies
to reach sychronization."

This seems counter-intuitive to me.

~~~
fogleman
They can't be too out of whack, but they can vary.

------
michaelmior
I did not realize you could scroll down on first viewing. If this weren't at
400+ upvotes when I saw it, I would have thought that was it.

~~~
KennyCason
Same here. I'm glad I scrolled down and resized my screen. I didn't notice it
my first viewing actually. I only noticed it when I called my wife down to
check it out later.

------
dyarosla
I think the example works more quickly than it should due to the line
"if(ff.clock>1) ff.clock=1;" Every time the clock goes over 1, wouldn't you
mod the clock value, not set it to 1 exactly?

~~~
IanCal
No, partly because fireflies take a certain amount of time to "charge" so
jumping mid cycle might not be plausible physically.

So what this line does (which is in the section of code where a flash alters
the fireflies around it) is that after advancing the other fireflies clocks,
if any are over 1 then it puts them in the right state to flash on the next
time they're evaluated. If you mod it, then you would have the logic:

"if I'm a firefly and see my neighbour flash while just about to flashing
myself, don't flash but reset my clock to part way through the cycle"

rather than

"if I'm a firefly and see my neighbour flash while just about to flashing
myself, flash as well"

Edit - setting it to one ensures that each neighbour can't keep adding to the
clock, advancing it to a random point in the cycle.

~~~
narrowtux
If the fireflies take that time in their clocks to "charge the light", could
it be that when they nudge their clock once or more in the cycle, the
intensity of the light is a bit lower than it would normally be?

~~~
IanCal
Interesting question. I honestly thought that the process was actually to
_reset_ the clocks not move them forwards, that's how I've simulated it in the
past.

------
DelaneyM
Beautiful, educational and clever. I love it.

On the subject of fireflies and emergent patterns, RadioLab did a wonderful
podcast on the subject as well a few years ago:
[http://www.radiolab.org/story/91500-emergence/](http://www.radiolab.org/story/91500-emergence/)
This piece very much reminded me of that.

------
m-j-fox
Nature's PLL.

[https://en.m.wikipedia.org/wiki/Phase-
locked_loop](https://en.m.wikipedia.org/wiki/Phase-locked_loop)

------
ozaark
> But whenever you shine, you inspire those close to you to shine too, a
> little bit sooner than they otherwise would have. And those close to you, in
> turn, inspire those close to them.

This is adaptation to input disruption on a network of brain impulses.
Fascinating that so much correlates with our own perception and technologies.

Really great interactive post both informative and engaging.

------
cwal37
Not on the coding side, but the inspiration. . . For those of us in the USA,
there is a much more accessible version of synchronous fireflies at Great
Smoky Mountains National Park in Tennessee. I lived there for a few years and
went multiple times[1].

The dates get announced a month or so beforehand, and tickets are based on a
lottery system since it only[2] occurs on one side of one river for less than
a mile. You can technically also hike in if you're ok with 8 or so miles near
sunset and then the same amount out in the pitch black. I believe that is
frowned upon, but very few people do it that way (I was the only one the year
I couldn't get tickets and decided to hike).

[1]
[https://www.flickr.com/photos/23215983@N02/albums/7215764507...](https://www.flickr.com/photos/23215983@N02/albums/72157645075559534)

[2] Not exactly only, but certainly the most strongly.

------
janwillemb
What an extremely nice interactive read. Works good on mobile. And it applies
the biological lesson to human life, which is a nice way to finish a sermon.
Article. Well done.

~~~
codedevgoo
Yup ditto. A quick superb from me

------
ge96
Kind of curious how it works, when you inspect it, there's just a canvas, the
fireflies don't seem to be individual, so no sense of "objects" or "individual
entities"

open source time to read

but with regard to say if it was a real-time rendered animation, then
calculating the point of origin and affecting the nearby fireflies... hmm

the view clock part was particularly impressive though I could see it as a
state thing, still so many of them on the same page, not crashing the browser
or something interesting. Maybe I'm over thinking it.

edit: I didn't look at the scripts though below the canvas.

~~~
Aeolun
Canvas is pretty performant. WebGL is even more performant. You can easily
have a few thousand things on the same page at a time.

~~~
ge96
They have states right? I'm curious how that is like a JSON string or
something to access each thing and change individually? I can go read haha

------
leovander
Reminds me of the Fireflies[0] model included in NetLogo[1].

[0]
[https://ccl.northwestern.edu/netlogo/models/Fireflies](https://ccl.northwestern.edu/netlogo/models/Fireflies)
[1]
[http://ccl.northwestern.edu/netlogo/index.shtml](http://ccl.northwestern.edu/netlogo/index.shtml)

------
eridius
This is neat.

Did they ever figure out _why_ Thailand fireflies do this, instead of just
how?

~~~
sliken
I suspect it helps avoid being eaten. Anything trying to eat them is presented
with too many bright spots to track, then nothing to see.

~~~
eridius
Wouldn't that also be true of normal fireflies? It's pretty hard to track a
single firefly if there's a bunch of them in the area.

~~~
sliken
Not so much, there's no competition, so it's easier to track. I used to
collect freflies in a jar as a kid. Seems like a similar idea to schools of
fish, herds of antelope, etc. Stay together (even if defenseless) and
predators have a hard time picking an individual to hunt.

~~~
thecosas
Ah, the paradox of choice (Firefly Edition).

------
daleroberts
This is fasinating topic, I recently tried to understand what happens to this
type of model if you perturb it with heavy-tailed noise.

[http://www.sciencedirect.com/science/article/pii/S0378437116...](http://www.sciencedirect.com/science/article/pii/S0378437116306768)

------
whalesalad
The background noise is so soothing.
[http://ncase.me/fireflies/sounds/forest.mp3](http://ncase.me/fireflies/sounds/forest.mp3)

~~~
crusso
Yeah, although my volume is down low and I opened the page in another tab
while I was reading. By the time the sound permeated my consciousness, I
thought that I had a hard drive going bad in my NAS next to my laptop.

------
cossatot
If anyone is interested in this synchronization process or how it relates to
synchronization in general, I highly recommend 'Sync' by Steven Strogatz. It's
one of the best popular science/math books I've ever read, and Strogatz is a
mathematician at the heart of the research, and a great writer as well, so you
really get the story from someone who is a world expert, rather than an
interested journalist.

------
JKCalhoun
My only hang-up with the explanation/simulation is, why do the fireflies all
have clocks that run at the same duration as one another?

I understand advancing your clock to synchronize flashes, but doesn't this
presuppose that the clocks run at the same speed?

When I turned on "Show Clocks" in the simulation it indeed looked like the
clocks ran at identical durations. Seems like this would be difficult in
nature.

~~~
bovik
I imagine if they started at wildly different speeds it would still converge
but the convergence would take longer.

~~~
golergka
The article only talks about changing the phase on fixed amount, nothing about
fixing the frequency.

------
dharma1
Very cool. This was an interesting paper on synchrony in the brain, and how
it's related to consciousness -
[https://www.nature.com/articles/ncomms10340](https://www.nature.com/articles/ncomms10340)

------
merraksh
Reminded me of another phenomenon, the one occurring to metronomes on the same
surface:
[https://www.youtube.com/watch?v=5v5eBf2KwF8](https://www.youtube.com/watch?v=5v5eBf2KwF8)

------
osense
It would be even more impressive if the fireflies' internal clock was ticking
at slightly different speeds. As it is now, once they're synchronized, turning
the sync mechanism off has no effect and they stay synced forever.

------
quadcore
Why would a firefly that sync survive more than one which doesn't?

~~~
kyberias
Good question. My initial guess: it is easier for predators to detect/catch
fireflies that blink out of sync with the rest of the flock. If all blink in
perfect synch, it's just a big flash of light to the predators.

------
jonathanpoulter
After the point when the whole swarm is synchronised there seems to be no
further nudging, does this make sense? Am I missing something? Very
interesting, just not sure I understand.

~~~
KeyboardFire
It's because of what dyarosla pointed out in their comment:
[https://news.ycombinator.com/item?id=14452832#14456254](https://news.ycombinator.com/item?id=14452832#14456254)

------
zachrose
Is there some inverse of the firefly's sync protocol that if they all followed
would turn a synchronized swarm back into a chaotic one?

~~~
panic
In real fireflies, the individual clocks have slightly different frequencies.
The inverse of the protocol is no protocol: stop syncing and let the blinks
slowly fall back into chaos.

------
rullelito
I wonder if you need the random scattering for this to work, or if it would
still work if the fireflies formed a hollow circle.

------
joelcollinsdc
The view source of this is amazingly clean and refreshing to read. Great
learning example, thanks!

------
omginternets
Incidentally, this is a very powerful model for neural synchrony as well.

------
GrumpyNl
Does the 137.5 degrees has something todo with this?

------
a_imho
ot, the patreon link comes up with an empty white page and pukes all kinds of
js errors to the console for me

------
SpicyPython
This is nice, I really enjoy this

------
duke360
this is pure genius! i love fireflies!

------
19eightyfour
Nature has the best algorithms, don't it folks?

I have a question about a detail.

The rule is:

1\. When you see a nearby neighbour flash, nudge your own clock forward.

2\. That's it.

My question is -- when the fireflies are firing at the same time, they see
each other flash, so they all nudge their clocks forward, correct?

Assuming they don't nudge their clocks forward at the same amount, they would
fall out of sync, correct? But they stay in sync, so they must nudge forward
at the same amount...is this right?

So my question is -- if everyone keeps nudging their clocks forward, why don't
they keep speeding up? They do appear instead to continue flashing at the same
steady rate.

Does this mean that there ought to be another detail in the rule, such as,

If you see a neighbour nearby flash, when you are not flashing, nudge your
clock forward by a bit.

? Or is the perspective I just said missing something?

~~~
tsbertalan
At least in the Kuramoto model, it's a little more complicated than the OP
makes it sound (but not much). Step 1 is more like "if you see a flash, bump
your clock closer to midnight."

Specifically, an ODE is given for firefly $i$'s phase angle $ \theta_i$

$$\dot\theta_i = \omega_i + K / N \sum_{j=1}^N \sin(\theta_j - \theta_i)$$

This gets interesting because the flies natural frequencies $\omega_i$ are
also assumed to be randomly distributed. So you don't get perfect phase-
synchronization--flies with fast natural frequencies lead the pack as it loops
around the phase ring, and flies with slow natural frequencies are dragged
along at the back. Relative to the mean phase, your excess phase approaches a
smooth increasing function of your natural frequency. But, for high enough
values of $K$, you do get frequency-synchronization--everyone oscillates at
the average frequency.

(For low values of $K$, or too-large natural frequencies, "rogue oscillators"
emerge in a SNIPER bifurcation. They zoom around the phase ring at a different
frequency, briefly slowing as they pass through the cloud of synchronized
oscillators. Also applies for too-slow rogues.)

In the video and the OP's simulation, it looked the natural frequencies were
all pretty similar, if not the same. However, there was a second addition that
is not in the original Kuramoto model (but is in most subsequent models):
rather than observing all other flies, only observe nearest neighbors. This
can be added by putting a symmetric boolean adjacency matrix $A_{i,j}$ right
before the $\sin$ in the previous equation.

This has the effect of making excess phase a smooth function of not only the
natural frequency, but also some structural feature imposed by the network. In
random networks like Erdos-Renyi, this feature is the node degree, but in the
video it looks like it might be the long axis of the bush (so, like, one of
the eigenvectors of the graph Laplacian).

(In general, coupled oscillators, such as circadian gene clocks, show this
smooth dependence of excess phase on per-unit heterogeneities, which is the
topic for the first half of my PhD dissertation. The second half is figuring
out what the heterogeneities are when you only have recordings of the dynamics
to go by.)

~~~
lucb1e
> $$\dot\theta_i = \omega_i + K / N \sum_{j=1}^N \sin(\theta_j - \theta_i)$$

What's the quickest way to preview that (without needing a hosted service)?

I just made this small LaTeX document (I'm amazed I knew this by heart, I
typed this manually maybe thrice in my life):

    
    
        \documentclass{paper}
        \begin{document}
        $$\dot\theta_i = \omega_i + K / N \sum_{j=1}^N \sin(\theta_j - \theta_i)$$
        \end{document}
        <esc>:wq
        pdflatex tmp.tex && evince tmp.pdf && rm tmp.*
    

but that's still quite a bit of extra work just to view it. Do you know a
better way to do it?

~~~
ebalit
I use this solution sometimes.

[http://www.HostMath.com/Show.aspx?Code=%5Cdot%5Ctheta_i%20%3...](http://www.HostMath.com/Show.aspx?Code=%5Cdot%5Ctheta_i%20%3D%20%5Comega_i%20%2B%20K%20%2F%20N%20%5Csum_%7Bj%3D1%7D%5EN%20%5Csin\(%5Ctheta_j%20-%20%5Ctheta_i\))

------
johnfn
Probably one of my favorite things I've seen on HN in a long time. The perfect
synthesis of tech explanations to biological phenomena. The way the entire
page fits together to teach is just beautiful.

Why it's sitting down on the second page with just a few upvotes is beyond me.

------
nether
One of the things I miss from growing up in rural Virginia.

------
tnone
Parable of the polygons is extremely questionable. For one, it attempts to
have its cake while eating it too, by pretending it's an abstract
demonstration about choices while explicitly calling out to hot topics in
diversity, implying a clear causal link. For another, the model involves
discrete agents on a discrete grid making decisions based on their immediate 8
square neighborhood, with no persistence or hysteresis, which does not at all
match the real life domain it is attempting to model.

If you changed the grid geometry, changed the action radius, or added a more
complicated decision mechanism, it is likely to result in a completely
different outcome. It is more like a cellular automaton than a sociological
model, compounding design choices, not agent choices.

It should probably be called Parable of the Quantization Error instead and
held up as an example of an explorable explanation being misused for
propaganda purposes, using its medium to present a false image of
impartiality.

~~~
j2kun
If you're so passionate about it, you should go implement these changes to
show how the model fails. I would read that, if it were done well. It's not
obvious to me that simply changing the radius or grid geometry would have the
effect you claim.

Your objection is just that the model is too simple, but everyone knows linear
regression predicts quite a lot, quadratic polynomials estimate physical
motion very well, etc.

On the other hand, I don't think the authors make the claims about faithfully
and objectively measuring reality that you say they do. You're building up a
straw man there.

~~~
geofft
FWIW, the majority of the first page of this user's comment history (6/10) are
complaining about feminism in some fashion.

(I don't generally like looking at people's comment history, but one, you're
absolutely spot-on that it would be easy to change the simulation - it's
public domain and it's in JavaScript, this is _Hacker_ News, if you don't know
how to edit the JS console yourself there are tons of people who will gladly
help if you ask, so the lack of curiosity is interesting. And two, I've seen
some sort of shift in more vocal MRA-adjacent / alt-right-adjacent viewpoints
on this site in recent weeks and I've been kind of curious where it's coming
from.)

Anyway, in the hope of not dragging this too far off-topic: 'tnone, do you
have a specific change you'd like to see implemented in the simulation? I'd be
happy to implement the change and rehost it and see what happens. I think
there _are_ genuine criticisms to be made (for instance,
[https://github.com/ncase/polygons/issues/9](https://github.com/ncase/polygons/issues/9)
sounds worth some investigation, despite the phrasing of the comment thread).
But given how easy it is to apply actual data to these questions, I think we
should do that to avoid FUD.

In particular, how about changing the radius to be the surrounding 24 squares
(two steps out, instead of one), or making it less likely that an individual
polygon will move again shortly after it has moved?

~~~
j2kun
Since I wrote that comment I feel the itch to do it myself :) He said
topology, but I don't think he meant what I'm thinking of: a torus! A Mobius
band! The projective plane! My curiosity is getting the better of me.

