Skip to content
Free public beta until December 4, 2026: every course is open, no card needed. After that, the first chapter of each course stays free and full access is a one-time $39 ($19 for up to 100 founding members chosen for helpful feedback). Your progress is kept. How the beta works

Recipe

Update one particle's velocity and position

Use when: you know a particle's current position x, velocity v, its own personal best p, and the swarm's global best g

Goal: the particle's new velocity and position for the next iteration

Taught in Particle swarm optimization

  1. 1

    c_1 r_1 (p - x), using this iteration's own random draw r_1.

    Example

    p=1, x=2, c_1=1.5, r_1=0.6: 1.5(0.6)(1-2) = -0.9.

    Why: this term is zero exactly when the particle already sits at its own best, positive/negative otherwise, always pulling it back there.

  2. 2

    c_2 r_2 (g - x), using this iteration's own random draw r_2.

    Example

    g=4, x=2, c_2=1.5, r_2=0.4: 1.5(0.4)(4-2) = 1.2.

    Why: this is the only term that lets one particle's discovery influence every OTHER particle's motion.

  3. 3

    v \leftarrow w\,v + \text{cognitive} + \text{social}.

    Example

    w=0.7, v=0.5: v \leftarrow 0.7(0.5) + (-0.9) + 1.2 = 0.35 - 0.9 + 1.2 = 0.65.

    Why: the inertia term wv is what keeps the particle from turning on a dime every single iteration.

  4. 4

    x \leftarrow x + v (the NEW velocity from the step above).

    Example

    x \leftarrow 2 + 0.65 = 2.65.

    Why: velocity and position update in this fixed order -- the position move always uses THIS iteration's freshly-updated velocity.

Common mistakes

  • Updating position using the OLD velocity before recomputing it -- the order above is fixed: velocity first, then position.
  • Reusing the same random draw for r_1 and r_2 -- each needs its own independent draw every iteration.
  • Forgetting to check whether the new position beats p (update personal best) or g (update global best) after moving.