datarekha

Swarm intelligence & optimization

Most multi-agent design has agents pursuing goals. Swarm intelligence is a different angle: global optimization that emerges from many simple agents following local rules, with no central controller. Particle swarm optimization and ant colony optimization are gradient-free metaheuristics inspired by flocks and ant trails — robust, decentralized, and useful where calculus can't help.

7 min read Advanced Agentic AI Lesson 27 of 71

What you'll learn

  • Swarm intelligence — global behavior from simple local rules
  • Particle swarm optimization — pull toward personal and global best
  • Ant colony optimization and stigmergy (coordination through the environment)
  • Why these gradient-free metaheuristics are robust and decentralized

Before you start

Most multi-agent design has agents pursuing goals. Swarm intelligence comes at it from a different direction: complex, useful global behavior that emerges from many simple agents following local rules — with no central controller. A flock of birds, an ant colony, a school of fish: none has a leader, yet the collective solves problems no individual could. Turned into algorithms, this gives a family of gradient-free optimizers.

Particle swarm optimization

In PSO, a swarm of particles drifts through the search space looking for the best solution. Each particle’s move is pulled by just two things: its own best position so far (cognitive) and the swarm’s best so far (social). Those simple local rules, plus sharing one global best, make the swarm converge on the optimum:

import numpy as np
rng = np.random.default_rng(0)

def f(x): return (x - 3.0)**2                          # minimize; optimum at x=3
pos = rng.uniform(-10, 10, 5); vel = np.zeros(5)        # 5 particles
pbest = pos.copy(); gbest = pbest[int(np.argmin([f(x) for x in pbest]))]

for step in range(1, 7):
    for i in range(5):
        vel[i] = 0.5*vel[i] + 0.8*(pbest[i]-pos[i]) + 0.8*(gbest-pos[i])   # inertia + toward pbest + gbest
        pos[i] += vel[i]
        if f(pos[i]) < f(pbest[i]): pbest[i] = pos[i]
    gbest = pbest[int(np.argmin([f(x) for x in pbest]))]
    print(f"step {step}: global best x={gbest:.2f}  f={f(gbest):.4f}")
step 1: global best x=2.74  f=0.0680
step 2: global best x=2.74  f=0.0680
step 3: global best x=3.08  f=0.0060
step 4: global best x=3.01  f=0.0001
step 5: global best x=3.01  f=0.0001
step 6: global best x=3.00  f=0.0000

No gradients, no central plan — just particles each nudged toward their own and the swarm’s best, and the collective homes in on x=3. That’s the whole trick: local rules plus shared information yield global optimization.

Particles pulled toward personal & global bestsearch spaceoptimum (global best)simple local rules + a shared global best → the swarm converges (no central control)
Each particle moves toward its own best and the swarm’s best; together they converge on the optimum without any central plan.

Ant colony optimization, and the principle

Ant colony optimization (ACO) solves a different shape of problem (routing, the traveling salesman) with a different mechanism: pheromone trails. Ants wander, laying pheromone on the paths they take; shorter paths accumulate more pheromone (they’re traversed faster and more often), which attracts more ants, which lay more pheromone — a positive feedback that converges on short routes. This is stigmergy: agents coordinate indirectly, through changes they make to the environment, never communicating directly.

In one breath

  • Swarm intelligence: useful global behavior emerging from simple local rules with no central controller (flocks, ant colonies) — turned into gradient-free optimizers.
  • Particle swarm optimization (PSO): particles move toward their own best and the swarm’s best; simple local rules + a shared global best converge on the optimum (the demo reaches x=3).
  • Ant colony optimization (ACO): ants lay pheromone, shorter paths accumulate more and attract more ants — positive feedback that finds short routes via stigmergy (coordinating through the environment, not directly).
  • They’re gradient-free metaheuristics (evaluate, don’t differentiate) for black-box/combinatorial problems, decentralized and robust (no single point of failure).
  • They’re a clean model of emergence — intelligence in the interaction, the same idea behind generative agents and collective LLM-agent behavior.

Quick check

Quick check

0/4
Q1What is the core idea of swarm intelligence?
Q2How does particle swarm optimization (PSO) work?
Q3What is stigmergy in ant colony optimization?
Q4Why are swarm algorithms useful where gradient methods aren't?

Next

Swarm optimization is collective behavior optimizing rather than learning; it’s a clean instance of the emergence behind generative agents. Modeling what other agents think — for coordination — is theory of mind.

Sign in to track your progress

Completed lessons, your XP, level, and streak save to your account — it's free and takes a few seconds.

Related lessons

Explore further

Skip to content