This is something known as “emergent behavior” where a couple simple rules create something approaching complex intelligence. For instance here are a couple simple rules to model flock behavior
1) keep x distance from your neighbor to not crash into them
2) align your direction of flight and match the speed with your neighbor unless that violates rule 1
3) always try to be surrounded by the most individuals possible unless that violates rules 1 or 2
If you input these rules into a computer simulation you can model real life swarm behavior. It’s called a “boids” simulation. You’ll notice that there is no accounting for the overall behavior of the swarm. Each individual only needs to know the behavior of its immediate behaviors. Then small individual decisions within the rules can create really complex movements of the overall flocks as they all respond to the actions of their neighbors.
Latest Answers