Implementation of Particle Swarm Optimization


Introduction

The Particle Swarm Optimization algorithm is inspired by nature and is based on the social behavior of birds in a flock or the behavior of fish and is a population-based algorithm for search. It is a simulation to discover the pattern in which birds fly and their formations and grouping during flying activity.

Particle Swarm Optimization Algorithm

In the PSO Algorithm, each individual is considered to be a particle in some high-dimensional search space. Inspired by the social and psychological behavior of people, which they tend to copy from other people's success, similar changes are made to the particles in the particular search space. How a particular particle changes in a particular swarm is based on knowledge gained by the particle from its neighbors. In other words, we can say that a particle's behavior in search space is influenced by other particles' activity within the swarm. Thus, PSO is a kind of symbiotic behavior. Thus as a result of this social tendency and behavior particles probabilistically tend to return to a search space where it was previously successful.

Example

Let us see an implementation of the Particle Swarm Optimization Algorithm below.

## Particle Swarm
from __future__ import division
import random
import math
def cost_func(y):
   tot=0
   for i in range(len(y)):
      tot+=y[i]**2
   return tot
class Part:
   def __init__(self,x0):
      self.pos_i=[]
      self.vel_i=[]
      self.position_best_i=[]
      self.error_best_i=-1
      self.error_i=-1
      
      for i in range(0,n_dim):
      self.vel_i.append(random.uniform(-1,1))
      self.pos_i.append(x0[i])
      
   def eval(self,cFunction):
      self.error_i=cFunction(self.pos_i)
      
      if self.error_i < self.error_best_i or self.error_best_i==-1:
         self.position_best_i=self.pos_i
         self.error_best_i=self.error_i
   def upd_vel(self,position_best_g):
      w=0.5
      c1=1
      c2=2
      for i in range(0,n_dim):
         r1=random.random()
         r2=random.random()
         vel_cognitive=c1*r1*(self.position_best_i[i]-self.pos_i[i])
         vel_social=c2*r2*(position_best_g[i]-self.pos_i[i])
         self.vel_i[i]=w*self.vel_i[i]+vel_cognitive+vel_social
   def upd_pos(self,bnds):
      for i in range(0,n_dim):
         self.pos_i[i]=self.pos_i[i]+self.vel_i[i]
         
         if self.pos_i[i]>bnds[i][1]:
            self.pos_i[i]=bnds[i][1]
         if self.pos_i[i] < bnds[i][0]:
            self.pos_i[i]=bnds[i][0]
            
class ParticleSwarmOpt():
   def __init__(self,cFunction,x0,bnds,number_particles,maxiter):
   global n_dim
   n_dim=len(x0)
   err_best_g=-1
   position_best_g=[]
   
   swm=[]
   for i in range(0,number_particles):
      swm.append(Part(x0))
   i=0
   while i < maxiter:
   for j in range(0,number_particles):
      swm[j].eval(cFunction)
      if swm[j].error_i < err_best_g or err_best_g == -1:
         position_best_g=list(swm[j].pos_i)
         err_best_g=float(swm[j].error_i)
   for j in range(0,number_particles):
      swm[j].upd_vel(position_best_g)
      swm[j].upd_pos(bnds)
   i+=1
   
   print('Result:')
   print("Best g : " ,position_best_g)
   print("Best error g: ", err_best_g)
   
init=[5,5]
b =[(-1,10),(-20,20)]
ParticleSwarmOpt(cost_func,init,b,number_particles=15,maxiter=50)

Output

Result:
Best g: [-7.414272934827646e-06, 3.1121531435141664e-06]
Best error g: 6.465694034080286e-11

Conclusion

The Particle Swarm is an Optimization algorithm based on the natural behavior of birds and fish in social settings.

Updated on: 23-Mar-2023

449 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements