From zebra to starlings, herring and even tadpoles, many creatures move in an organized group. The emergent behaviour arises from simple underlying movement rules, but the evolutionary pressure which favours these rules has not been conclusively identified. Various explanations exist for the advantage to the individual of group formation: reduction of predation risk; increased foraging efficiency or reproductive success. Here, we adopt an individual-based model for group formation and subject it to simulated predation and foraging; the haploid individuals evolve via a genetic algorithm based on their relative success under such pressure. Our work suggests that flock or herd formation is likely to be driven by predator avoidance. Individual fitness in the model is strongly dependent on the presence of other phenotypes, such that two distinct types of evolved group can be produced by the same predation or foraging conditions, each stable against individual mutation. We draw analogies with multiple Nash equilibria theory of iterated games to explain and categorize these behaviours. Our model is sufficient to capture the complex behaviour of dynamic collective groups, yet is flexible enough to manifest evolutionary behaviour.