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.
Reference
[1] pyswarms · PyPI