Hacker News new | comments | show | ask | jobs | submit login
Fireflies (ncase.me)
1243 points by bpierre on May 31, 2017 | hide | past | web | favorite | 89 comments



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


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


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.


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

Playable posts are amazing. Interactivity in texts is amazing.

<3


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/


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:)


Wow, really great - thanks for the link!


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?


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.)


> $$\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?



Jesus, that's such a great answer. Do you have an academic page people can look at for more of your writings or readings? Thanks so much!


Nudge the clock "forward", not increase speed. So the overall frequency will be a little higher, but it won't keep on increasing


Exactly. For anyone having trouble, just picture a pure sinusoidal signal, flash = sin(2π·freq·time + phase). Fireflies are changing their phase, not their frequency. Completely independent parameters.


So the frequencies of the fireflies need to be pretty close to the same. How close are they actually?


No. The fireflies are able to adjust both their frequencies and phase differences. If their frequencies are matched they are phase-locked. If their phase difference is 0, then they are also synchronized.

From: https://www.math.hmc.edu/~dyong/math164/2006/runyeon/finalre...


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

I think this is it. There could be some missing detail (e.g. a firefly is unable to "see" when it flashes) which keeps them from nudging their clocks once already in sync


This reminds me of collision detection algorithms for wireless antennae. You can't 'hear' when you're transmitting, since your own signal drowns out anything you might receive (for simple antenna arrays) - I imagine the same is true for fireflies.


> which keeps them from nudging their clocks once already in sync

Does it matter if they "nudge their clocks once already in sync" ? If they all do that, they'll stay in sync, with a slightly higher frequency.


"Speeding up" would only happen when you turn nudging on, it wouldn't continuously keep getting faster.

But the algorithm described by the author has a bigger problem: It wouldn't actually sync the fireflies!

If EVERYONE nudged their clocks forward, then an out-of-sync firefly would be nudged the same as an in-sync firefly, and synchronization wouldn't increase over time!

Your rule avoids this problem:

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

If you look in "show clocks" mode, it looks like this is exactly what is implemented.


I think the rule is not very precise written down (e.g. I do not believe that a firefly nudges its clock multiple times within one cycle, just because it has multiple neighbors).


If nudging is done proportional to the intensity (or distance) of observed flashes, I'm pretty sure it will still work fine.


They would all have to jump their clocks identically for that to work.


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


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) -- 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).


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

https://www.michaelfogleman.com/static/fireflies/


Nice.

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

This seems counter-intuitive to me.


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


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.


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.


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?


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.


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?


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.


I think that's why it works -- if you change that = to a %=, the effect no longer happens.

It does make (intuitive) sense, though, because the only time that makes a significant difference is when a lot of fireflies flash around one firefly simultaneously, in which case the firefly wouldn't decide to flash twice in a row but instead interpret it as a singular flash. And if a firefly sees another one flash when it's about to, it would naturally want to synchronize its flashing with the other.


Right- but the explanation in the 'explorable' is that the effect happens due to the simple pattern of Step 1: When you see a nearby firefly flash, nudge your clock a little bit forward, Step 2: That's it. Problem is, the description of what's happening and what is actually happening doesn't match.


Another poster here showed their own, similar demo of this. There they had each firefly charging like a capacitor. Triggering the light causes the capacitor to deplete. This seems like the better analogy, but more people know what clocks are.


In addition to being a better analogy, I am told it is possible to replicate the effect with a physical capacitor and a gas-discharge lamp: https://news.ycombinator.com/item?id=4600996


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/ This piece very much reminded me of that.



> 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.


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...

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


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.


Yup ditto. A quick superb from me


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.


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


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


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

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


This is neat.

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


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.


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.


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.


Ah, the paradox of choice (Firefly Edition).


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...


The background noise is so soothing. http://ncase.me/fireflies/sounds/forest.mp3


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.


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.


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.


This is difficult to explain succinctly, but biochemical processes can set up periodic events with a fair amount of accuracy. For instance, your heart beats roughly once a second when in a resting state.

Some of these processes use physical latency to time the events: if the fireflies were roughly of the same maturity, they would be of similar size and thus have clocks of similar duration.


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


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


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


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.


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


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


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.


One guess is that it's easier to be part of a group that way, and then easier to reproduce.


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.


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


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?


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.


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.


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


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


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


Does the 137.5 degrees has something todo with this?


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


This is nice, I really enjoy this


this is pure genius! i love fireflies!


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.


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.


We detached this subthread from https://news.ycombinator.com/item?id=14456206 and marked it off-topic.


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.


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 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?


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.


Yes, absolutely. Also worthwhile to note that it's based on Thomas Schelling's model of segregation: http://nifty.stanford.edu/2014/mccown-schelling-model-segreg...


Its interesting to see how the simulations looks like they are modeling society, and reminds me strongly on how game of life tries to be a model of artificial life. however, if someone want it to mean more then they really need to test the model and get data on how well it can predict.


I recall a study with LA demographic data that showed the statistical distribution of LA matches the distribution output by the Schelling model. Hazy on the details though :(


There's a LOT of hysteresis. Polygons stay in place unless sufficiently unhappy.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: