# Particle Swarm Optimization| Machine Learning Algorithm inspired by Birds

Particle Swarm Optimization (PSO) algorithm is based on the pattern of food search by the flock of birds. The birds/particles initially knew the distance of the food/target from their position and with each iteration, the particle tries to reduce the distance and reach the destination in the most efficient way.

The formula for updating Velocity:

`velocity=w*velocity[i]+c1*rand()*(localbest[i]-position[i])+c2*rand()*(globalbest[i]-position[i])`

Update position using:

`position[i]=velocity[i]+position[i]`

Fitness checking formula:

`(1+2*x-x**2)`

Implementation in Python:

```import numpy as np
no_of_particles=5
inertia=5
c1=c2=0.5
w=0.3
pos=np.random.uniform(low=-5,high=5,size=(no_of_particles))
vel=0.1*pos
lbp=pos.copy()
def fitness(x):
return (1+2*x-x**2)
def update_velocity_position():
for i in range(vel.shape[0]):
vel[i]=w*vel[i]+c1*np.random.rand()*(lbp[i]-pos[i])+c2*np.random.rand()*(gbp-pos[i])
pos[i]=vel[i]+pos[i]
print("current vel ",vel)
print("current position ",pos)
print("current fitness ",fitness(pos))
def update_lbp_gbp():
lbp_f=fitness(lbp)
pos_f=fitness(pos)
for i in range(lbp.shape[0]):
if(lbp_f[i]<pos_f[i]):
lbp[i]=pos[i]
gbp=lbp[np.argmax(fitness(lbp))]
print("current local best ",lbp)
print("current local host fitness ",fitness(lbp))
print("current global best ",gbp)
gbp=lbp[np.argmax(fitness(lbp))]
for i in range(5):
print("iteration is ",i)
if(i==0):
print("current vel ",vel)
print("current vel ",vel)
print("current position ",pos)
print("current fitness ",fitness(pos))
print("current local best ",lbp)
print("current local host fitness ",fitness(lbp))
print("current global best ",gbp)
else:
update_velocity_position()
update_lbp_gbp()
print()```

Alternate code using pyswarms library:

```!pip install pyswarms

import pyswarms as ps

from pyswarms.utils.functions import single_obj as fx

# first, let's start optimizing the sphere function. recall that

###using global best pso

#setup hyper parameters
options={'c1':0.5,'c2':0.3,'w':0.9}

#call instance of pso
optimizer=ps.single.GlobalBestPSO(n_particles=10,dimensions=1,options=options)

#perform optimization

cost,pos =optimizer.optimize(fx.sphere,iters=1000)

### using localBEstPSO

# setup hyper parameters

options={'c1':0.5,'c2':0.3,'w':0.9,'k':2,'p':2}

#call instance of pso

optimizer=ps.single.LocalBestPSO(n_particles=10,dimensions=2,options=options)

#perform optimization

cost,pos =optimizer.optimize(fx.sphere,iters=1000)

# optimizing a function with  bounds

#create bounds
max_bound= 5.12*np.ones(2)
min_bound= - max_bound
bounds=(min_bound,max_bound)

#initialize swarm

options={'c1':0.5,'c2':0.3,'w':0.9}

# call instance for PSO with bounds argument

optimizer=ps.single.GlobalBestPSO(n_particles=10,dimensions=1,options=options,bounds=bounds)

#perform optimization

cost,pos =optimizer.optimize(fx.rastrigin,iters=1000)```