Deutsche Welle, sort of a German equivalent of the BBC, provides short current-affairs-type programs written in simple German and read slowly, for German-learners to practice on. I've been working my way through these and my particular favourite is
Gemeinsame Klug or "Together Wise", a charming story about a scientist who wants to find out how a swarm of fish are able to move as if they were a single individual. She builds a robot fish which swims among the real fish, and attempts to lead them away from food. When a group of only two fish are infiltrated by the robot, they do indeed follow it, but when Robo-fish infiltrates a larger swarm, the other fish refuse to follow its lead. The researcher concludes that the swarm will only follow if a certain minimum percentage of the fish take the lead.
I have no idea how true this story is but it really caught my imagination, and I found myself wondering exactly what determines whether the fish move as a swarm, and what advantage they gain from this.
Logo of Radical Routes network of housing co-ops

It's obvious that fish don't ALWAYS act as a swarm, otherwise there would never be any leader fish to lead the swarm in a new direction. So it must be that fish have both an autonomous-mode, where they make their own decisions, and swarm-mode, where they follow the group.
I'm pretty sure that if there was some kind of clear danger, say, a shark approaching, any given fish would autonomously swim away no matter what all the other fish (or robotic infiltrators) were doing. It seems to me the only situations where there would be an advantage to acting as a swarm would be those in which it wasn't entirely clear what the correct decision would be. For example, some of the fish in the swarm faintly detect a hint of danger, which might be an approaching predator, or might be something completely harmless (a passing submarine, say).
Let's say that one in twenty detect a faint whiff of danger. So 5% of the fish swim away, in autonomous mode. Since this is all the information we have about the situation, we can say that, there is a 5% chance of imminent shark attack.
Is it advantageous at this point for the fish to act as a swarm, all following the 'leaders' who are swimming away? There is a 95% chance that swimming away is the wrong decision, with no shark nearby (maybe the sound is really something harmless. It could be a bunch of marine biologists making a documentary, Jaques Cousteau-style. Making a correct decision has a benefit (not getting eaten by sharks) but making a wrong decision has a cost (wasting calories swimming away unnecessarily, and possibly swimming away from food and thus missing out on dinner). The benefit and cost are not equal: in this case the benefit (not getting eaten) weighs more heavily than the cost (wasting calories).
The minimum fraction F of fish swimming away which tips the rest into swarm behaviour should be determined by
FB + (1-F)C = 0,
where B is benefit and C is cost. So, when a fraction F of fish in autonomous mode swim away, then the benefit of escaping the shark, if it exists, weighted by the possibility F that there really is a hungry shark nearby, is equal to the cost of swimming away in the event that there is no shark, weighted by the possibility (1-F) that the shark is actually a distant submarine, a group of marine biologists and their film crew, or the figment of some over-anxious fishes' imaginations. The fraction F that optimizes the fishes' chances of survival has been determined by a very long series of experiments carried out by the process of natural selection, with the fraction being set randomly to different values by mutations, and those groups of fish having less-optimal values having a greater frequency of being eaten by sharks. Probably. I actually have no idea if that is right or not, since I'm neither a statistitian nor an ichthyologist.
I did a quick google images search for "Robot fish", and came up with this charming creature:

Actual robot fish. Apparently it is used to clean up pollution, somehow. It comes from this website:
http://www.bmt.org/News/?/3/0/510.