Let's walk through a simplified hypothetical example of PSO with only one-dimensional search space for clarity. Please note that PSO typically operates in higher dimensions, and real-world applications can be much more complex. For simplicity's sake, we'll use very small numbers and easy calculations:
Objective function: f(x) = (x - 3)² (we aim to minimize this, so the best x will be close to 3)
-
Initialize the locations (x_i) and velocity (v_i) of n=2 particles:
Particle 1:
- x_1 = 1 (initial position)
- v_1 = 0 (initial velocity)
Particle 2:
- x_2 = 5 (initial position)
- v_2 = 0 (initial velocity)
-
Evaluate the objective function for both particles:
f(x_1) = (1 - 3)² = 4
f(x_2) = (5 - 3)² = 4
At the start, both particles have the same objective value, so choose either as g* arbitrarily:
g* = x_1 (or x_2)
-
Set the pseudo time or iteration counter: t = 0
Let's use arbitrary values for the cognitive and social coefficients, say c1 = c2 = 1.2 (just for the sake of the example), with an inertia weight w = 0.9.
- Now we begin the loop as there is no stopping criterion met yet:
While not met stopping criterion:
For each particle and dimension (which is just 1 in this case):
Update velocity using simplified version of PSO update rule:
v_t+1 = w * v_t + c1 * random1 * (pbest - x_t) + c2 * random2 * (g* - x_t)
Let's assume random1 and random2 both return 0.5 for this iteration.
Particle 1:
v_1_t+1 = 0.9 * 0 + 1.2 * 0.5 * (1 - 1) + 1.2 * 0.5 * (1 - 1)
v_1_t+1 = 0
Update position:
x_1_t+1 = x_1_t + v_1_t+1
x_1_t+1 = 1 + 0 = 1
Particle 2:
v_2_t+1 = 0.9 * 0 + 1.2 * 0.5 * (5 - 5) + 1.2 * 0.5 * (1 - 5)
v_2_t+1 = -2.4
Update position:
x_2_t+1 = x_2_t + v_2_t+1
x_2_t+1 = 5 - 2.4 = 2.6
Evaluate objective functions at new locations:
f(x_1_t+1) = (1 - 3)² = 4
f(x_2_t+1) = (2.6 - 3)² = 0.16
Personal bests remain the same since no improvement for Particle 1, and Particle 2 has a better position now. Hence, pbest for Particle 2 is updated to 2.6.
Since f(x_2_t+1) is better than previous g*, g* is updated to 2.6.
Update iteration counter: t = t + 1
Check stopping criterion (let's say we run for 3 iterations for the sake of this example).
After three iterations, we output the final results. The best position found (g*) would likely move closer and closer to 3, which is the optimal solution for our sample objective function (x - 3)².
Please note:
- The real PSO algorithm requires random factors for each velocity update, so results vary with each run.
- The above calculations are highly simplified and would generally require a multidimensional space and more complex objective functions.
- The example assumes that the learning coefficients (c1, c2) and the inertia weight (w) are fixed, but in advanced PSO, these might also adapt over time.
- In a real application, boundary conditions and velocity clamping might be necessary.