Python based Particle Swarm Optimization for a function optimization

Here is a quick introduction to Particle Swarm Intelligence based optimization and its implementation in Python. As the name states, Particle Swarm Optimization (PSO) is an optimization algorithm based on the intelligence of swarm learning. Swarms of birds, ducks, and insects often follow a pattern to achieve their activities.

  • This is about the collective action of a group of agents, the most common agents are birds.
  • The way birds find food or migrate in groups has an intelligence associated with it.
  • Each agent has a location and a velocity, that is all that is needed to guide through the solution space.
  • There is not one central agent to whose order birds follow. Rather there are several agents, and always the best agent location and velocity all agents follow.
  • There is little communication among agents here.
  • Aim is to adjust position and velocity as per best particle in the population and local best as well.
  • The velocity and position of particles in population is updated as per the following formulas:
  • velocity[i+1] = w*velocity[i] + c1*random_number_1 *(particle_best[i] — current_value_position[i]) + c2 * random_number_2 * (global_best — current_value_position[i])
  • position[i+1] = position[i]+velocity[i+1]
  • C1 and C2 are learning parameters, w is weight for updation.

PSO have many application. Lets start with basic function optimization.

Here is the code in Python for function optimization in Python using PSO.

Import and install library pyswarms

Import methods

Define the function to be optimized. Below is a sample objective function a parabola. Note this library solves the minimization problem naturally.

Now define options of PSO algorithms and call in the method optimizer and inform it about the objective function.

Here below, is the graph of convergence of PSO and optimal values given below.

The optimized values are here:

As predicted value is near zero.

Note we can achieve a much more accurate solution faster if domain bounds are set.

This is it for the introduction of PSO in python for function optimization.


[1] pyswarms · PyPI

Published by Nidhika

Hi, Apart from profession, I have inherent interest in writing especially about Global Issues of Concern, fiction blogs, poems, stories, doing painting, cooking, photography, music to mention a few! And most important on this website you can find my suggestions to latest problems, views and ideas, my poems, stories, novels, some comments, proposals, blogs, personal experiences and occasionally very short glimpses of my research work as well.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: