Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- If you want to watch a common purpose organically emerge from chaotic, simple agents, here is an absolute classic experiment you can build or run: **The "Boids" Flocking Engine with Predators.**
- It requires no complex machine learning to start—just basic math—yet it produces incredibly complex, lifelike behavior that looks like a collective mind.
- ---
- ## The Concept: The Virtual Starling Flock
- In the late 1980s, a programmer named Craig Reynolds realized you don't need a "leader bird" to make a flock fly together. You just need every single individual agent (a "Boid") to follow **three incredibly simple local rules**:
- 1. **Separation:** Don't crowd your neighbors (steer away if too close).
- 2. **Alignment:** Steer toward the average heading of your neighbors.
- 3. **Cohesion:** Steer toward the center of mass of your neighbors.
- When you put 500 of these simple triangles on a screen, they instantly look like a living, breathing school of fish or a flock of birds, swirling and splitting around obstacles.
- ---
- ## How to Make It Fascinating: Add "Evolutionary Pressure"
- To see true emergence and a "common purpose," you add a few more variables to the sandbox:
- ### 1. Introduce Predators & Limited Food
- Add a few "Predator" agents that chase the closest Boid, and scatter static "Food" points around the map. Boids need food to survive and reproduce; getting caught by a predator kills them.
- ### 2. Give Them a Tiny Brain (Neural Network + Genetic Algorithm)
- Instead of hardcoding those 3 rules perfectly, give each Boid a tiny neural network.
- * **Inputs:** Distance to closest food, distance to closest predator, distance to nearest flockmates.
- * **Outputs:** Steer Left, Steer Right, Speed Up.
- * **The Twist:** randomize their starting network weights. Most will die instantly. But the ones that survive long enough to eat get to clone themselves with a tiny bit of mutation.
- ### What Emerges (The "Common Purpose")
- If you let this run overnight, you will watch behaviors emerge that you never explicitly programmed:
- * **The "Selfish Herd":** Boids will accidentally realize that if they clump together, the predator can only catch the ones on the outside. They will evolve a frantic collective drive to constantly push toward the center of the group, creating tightly packed, twisting bait balls.
- * **Flock Splitting:** When a predator charges the center of the mass, the flock will seamlessly divide into two waves, pass around the predator, and fuse back together behind it—exactly like real starlings or sardines escaping a shark.
- * **Lookout Emergence:** Some agents on the fringes might evolve to react earlier to predators, effectively sounding a visual "alarm" as their sudden turn ripples through the rest of the flock.
- ## Want to Try It Right Now?
- You don't even have to code this from scratch to see it in action. There are fantastic web-based simulators where you can just tweak sliders and watch it happen in your browser:
- * Search for **"p5.js Boids simulation"** or **"Flocking simulation itch.io"**.
- * If you want to code it yourself, you can write a basic 2D Boids simulator in Python (using `pygame`) or JavaScript (using `HTML5 Canvas`) in under 100 lines of code.
Advertisement