Optimization Techniques in ML
Optimization Techniques in ML
Date: 10/02/2025
Total Pages: 04
\[
v_i(t+1) = w \cdot v_i(t) + c_1 \cdot r_1 \cdot (pbest_i - x_i) + c_2 \
cdot r_2 \cdot (gbest - x_i)
\]
\[
x_i(t+1) = x_i(t) + v_i(t+1)
\]
Where:
- \( v_i \) – Velocity of the particle
- \( x_i \) – Position of the particle
- \( w \) – Inertia weight (controls the balance between exploration
and exploitation)
- \( c_1, c_2 \) – Acceleration coefficients
- \( r_1, r_2 \) – Random numbers between 0 and 1
- \( pbest_i \) – Best position found by an individual particle
- \( gbest \) – Best position found by the entire swarm
\[
f(x, y) = x^2 + y^2
\]
# PSO Parameters
num_particles = 10
num_iterations = 100
w = 0.5
c1, c2 = 1.5, 1.5
# Initialize particles
particles = np.random.uniform(-10, 10, (num_particles, 2))
velocities = np.random.uniform(-1, 1, (num_particles, 2))
# PSO Algorithm
for _ in range(num_iterations):
for i in range(num_particles):
r1, r2 = np.random.rand(), np.random.rand()
velocities[i] = (
w * velocities[i]
+ c1 * r1 * (pbest_positions[i] - particles[i])
+ c2 * r2 * (gbest_position - particles[i])
)
particles[i] += velocities[i]
new_score = objective_function(particles[i])
if new_score < pbest_scores[i]:
pbest_positions[i] = particles[i]
pbest_scores[i] = new_score
if new_score < gbest_score:
gbest_position = particles[i]
gbest_score = new_score
# Display results
print(f"Optimal solution found at: {gbest_position}")
print(f"Minimum function value: {gbest_score}")
```
#Example Output:
```
Optimal solution found at: [0.0023, -0.0018]
Minimum function value: 0.0000052
```
This result indicates that the algorithm successfully converged to a
solution very close to the global minimum.
5. Applications of PSO
PSO is used across multiple fields to solve complex optimization
problems, including:
Final Thoughts
Particle Swarm Optimization is a simple yet effective technique that
mimics natural swarm intelligence to solve optimization problems
efficiently. Its adaptability makes it useful in various domains, from
artificial intelligence to engineering and beyond.