PARTICLE SWARM OPTIMIZATION (PSO) MATLAB CODE EXPLANATION

5/5 - (4 votes)

Overview and Development:

Particle Swarm Optimization is one of the most important algorithms used in modern data analysis and mathematical programming. This algorithm aims to find the best solution or feasible solution for a given optimization problems. The basic idea is that you keep a set of similar differentiable functions placed in a cluster, and then you try to optimize the objective function until it converges to a solution.

It has a very limited hyperparameter:

1.       Procedure has many representatives that comprise a swarm roaming in the search section, finding the finest result.

2.  Here algorithms work to solve a problem, the social interaction concept is used.

3. Global best that is known as Gbest offers the best value which is tracked by PSO.

4.   In the solution space, each particle finds its positional coordinates in relation to the optimal

What is a particle swarm?

Particle Swarm Optimization is a method used to search for important specifications for a new machine to be built. It is based on the observation that immobility in a fluid is a property shared by many objects of similar structure, and that immobility of a part or part of a system is a property of the whole.

Particle swarm optimization problem seeks to find the most efficient way to screen through scenarios that occur in the real world, to quickly find the most efficient path for a specific set of business challenges. Optimization algorithms are used for industrial applications in the field of AI.

The behavior of Optimization problem & Optimal solution

Different algorithms have been introduced at different times and many of them were dissipated but PSO is still in the attention of many researchers. PSO was developed by Eberhart and Kennedy in the year 1995.

Particle swarm optimization simulation is a mathematical program that involves organized groups of particles, or particles that are not in an isolated state, as well as how to organize them in a way that minimizes the search space and provides optimum output through a fixed number of iterations.

There are a variety of different aspects to PSO and most of them are related to how the group members’ behavior is reflected in the search space. The main goal of PSO is to create smaller and closer-together groups that minimize the search space. It is one of the most important algorithms in the field of computer science.

As the flock bird’s movement can be simulated, here each is capable to find the best feasible solutions in the solution space of high dimension, and hence the greatest solution is found by the flock. This is called a heuristic solution. As the real global optimal solution cannot be approved.

Determine the developed model of Optimization Problems:

To design the system with the optimal values of the lowest cost with the modern scientific parameter is known as Optimization. If you want to find the greatest solution to any problem, so optimization provides it.

If a CEO needs to take different types of plans at different periods of time. That person with try to minimize the hard work he is doing through different steps to maximize the profit for him.

For maximizing and minimizing tasks Optimization is highly referred to. Now the problems in optimization can be linear or nonlinear functions. The Non-Liner problems are more difficult to solve.

There are different types of Optimizations, constraints, dynamic, local, and global.

PSO Algorithm Parameters to solve an objective function

For different optimization problems, there are different parameters

1.       Swarm size

2.       Iteration number

3.       Velocity components

4.       Acceleration Coefficients

Building the PSO Algorithm

The Particle swarm is majorly based on the algorithms. It is the most natural algorithm. A class of independents known as debris move in specific steps in a complete area. Evaluation is done at every position which in terms decides and lots a completely new pace to each of the elements.

The Particle Swarm Algorithm’s major steps are Initialization, objective function evaluation, Iteration, and stopping. The complete process is as:

1.       Making the Initial particles

2.       Particles should be assigned with initial velocities

3.       At every particle location, the objective function needs to be evaluated, referred to as the personal best pBest

4.       It will find the best shortest value and location

5.       The updated new velocities will be selected by the simulation, which is based on current velocities and the best positions of particles and their nearest particles

6.       Then repetitively modernize the location of the particles

7.       This repetition will continue until the stopping criteria are reached by the algorithm

Particle swarm optimization algorithm Matlab code Explanation

In this example, the requirement is to find the global minimum, in order to implement PSO Matlab code to an objective function.

The optimized method is affected by a variety of control parameters, including the dimension of the problem, the number of swarms, inertia weight, acceleration coefficients, number of iterations, and the random value that influence social and cognitive components. 

PSO Matlab Initialization

The inertial weight greatly impacts the PSO algorithm code in the convergence of optimization. This factor maintains the particle/swarms inertial motion and redirects the change of particle velocity. PSO code correction factor is a suitable approach to ensure convergence of the Matlab particle swarm optimization. Let’s demonstrate the PSO code in Matlab

clear
clc
iterations = 1000;
inertia = 1.0;
correction_factor = 2.0;
swarms = 5000;

I will attach the download link for particle swarm optimization Matlab code at the end of this post, so keep the focus on the understanding.

Here in the below, 50 rows are created, in the case of 5000 particles, there will be 5000 rows as well. Considered columns are 7 as 1, 2 represent particle position, and 3, 4 represent the particle velocity. PSO algorithm Matlab code updates the position, and velocity on each iteration.

% ---- initial swarm position -----
swarm=zeros(5000,7);
step = 1;
for i = 1 : 5000
swarm(step, 1:7) = i;
step = step + 1;
end

swarm(:, 7) = 1000;       % Greater than maximum possible value
swarm(:, 5) = 0;          % initial velocity
swarm(:, 6) = 0;          % initial velocity
particle swarm optimization Matlab

In 5, 6 column velocity vector consideration based on inertia and correction factor is done and then it will be updated on 3, 4 columns.

Interested to implement different objective functions then you have to adjust the PSO Optimization Matlab program a bit.

    %-- position of Swarms ---
    for i = 1 : swarms
        swarm(i, 1) = swarm(i, 1) + swarm(i, 5)/1.2  ;  %update u position
        swarm(i, 2) = swarm(i, 2) + swarm(i, 6)/1.2 ;    %update v position
        u = swarm(i, 1);
        v = swarm(i, 2);
        
        value = (u - 20)^2 + (v - 10)^2;          %Objective function
        
        if value < swarm(i, 7)           % Always True
            swarm(i, 3) = swarm(i, 1);    % update best position of u,
            swarm(i, 4) = swarm(i, 2);    % update best postions of v,
            swarm(i, 7) = value;          % best updated minimum value
        end
    end

In the case of 5000 swarms, the cluster looks like this, which have to finally converge at a point closer to each other.

pso algorithm matlab code

Differences between PSO and Genetic Algorithm (GA):

Optimization techniques like Particle swarm optimization and Genetic Algorithm (GA) both have many features that are similar as mostly both are used in a similar type of problem programs, but there are some differences too.

To reach a faster convergence PSO needs to normalize the Input parameters, whereas the genetic algorithm uses discrete as well as continuous.

Swam optimization requires no evolution operators, whereas GA has crossover etc.

PSO provides both local searchers and global searchers, whereas GA provides only local searchers.

In PSO the independent individual is neither created nor deleted.

From research, the performance of PSO is found to be much better than GA, as it provides both global and local searches.

Advantages and Disadvantages of Particle Swarm Optimization

Advantages:

1.       The whole concept is quite simple

2.       Implementation is not very complex

3.       Without difficulty parallelized for concurrent processing

4.       A completely green international seek set of rules

5.       It has very few amounts of algorithm parameters

6.       Provides both global and local searches

Disadvantages:

There are very few disadvantages as well

1.       Its convergence rate is low

2.       It is weak in the local search

Conclusion:

In the field of Artificial Intelligence PSO plays an important role. It has many new advanced features. It is an optimization algorithm that is global and local and has many different unique strategies. It helps a lot in improving the solution of problems.

 You should read this article with a positive attitude, interest, and hopefully, find out really helpful what we have done and how you can implement yours.

If you are interested to get the code, click the below link

PSO algorithm Matlab code free download

Interested to learn more take a look at recent popular content

Load Flow Analysis Matlab Code

Optimal Location and Sizing of Distributed Generation

References

[1] Kennedy, J., and R. Eberhart. “Particle Swarm Optimization.” Proceedings of the IEEE International Conference on Neural Networks. Perth, Australia, 1995, pp. 1942–1945.

[2] Mezura-Montes, E., and C. A. Coello Coello. “Constraint-handling in nature-inspired numerical optimization: Past, present and future.” Swarm and Evolutionary Computation. 2011, pp. 173–194.

[3] Pedersen, M. E. “Good Parameters for Particle Swarm Optimization.” Luxembourg: Hvass Laboratories, 2010.

337 thoughts on “PARTICLE SWARM OPTIMIZATION (PSO) MATLAB CODE EXPLANATION”

  1. Hi Sir! Can you send me the code of following.
    Create network with number of users (2 users).
    Each 2 users assumed to share same sub-channel, so Number of sub-channels will be double the number of users (4Sub-Channels).
    We share the 2 users on same sub-channel randomly.
    Users /Channel Ch 1 Ch 2 Ch 3 Ch 4 U 1 U 2

    Now we need to do power Allocation using Particle Swarm Optimization to get the optimal solution for power allocation problem.
    Power Allocated to user 1 and user 2 in order to get the highest throughput for both users.
    Throughput equation of the 2 Users :
    R (1,z) = BW_sc * ( log2 (1+ PW (1,z) * (gain (1,z))^2 )/ ( PW(2, z)*(gain(1,z))^2 + Nb_subchannels*N0) ) ; %%%%%%%% Throughput for user 1 %%%%%%%
    R (2,z) = BW_sc * ( log2 (1+ PW (2,z) * (gain (1,z))^2 )/ ( Nb_subchannels*N0) ) ; %%%%%%%% Throughput for user 2 %%%%%%%
    Then we calculate the Throughput after we get the optimal solution of the PSO for Power allocation.
    My email is uzamamanzoor1271996@gmail.com

  2. Hello sir I want to minimise cos5x1+cos5x2+cos5x3+cos5x4=0
    cos7x1+cos7x2+cos7x3+cos7x4=0
    cos11x1+cos11x2+cos11x3+cos11x4=0
    Where i have 4 unknown variables x1, x2, x3, x4.
    Condition is
    0 < x1 < x2 < x3 < x4 < pi/2.
    Can you please help about the code.

  3. Hello sir I want to minimise cos5x1+cos5x2+cos5x3+cos5x4=0
    cos7x1+cos7x2+cos7x3+cos7x4=0
    cos11x1+cos11x2+cos11x3+cos11x4=0
    Where i have 4 unknown variables x1, x2, x3, x4.
    Condition is
    0 < x1 < x2 < x3 < x4 < pi/2.
    Can you please help about the code.
    Thanking you sir
    Mail: revanthroy507@gmail.com

  4. hello sir, can you send me the matlab codes for the following question?
    my email is tobegritty@163.com
    A warehouse serves a region that has 199 customers. For convenience, we use customers 2 to 200 to refer to these customers. This can be modeled by an undirected network of 200 nodes, where node 1 is the warehouse, and nodes 2 to 200 are customers. Their pair-to-pair distance is given as a 200*200 symmetric matrix. The warehouse has 8 trucks, and each truck can serve up to 25 customers on each trip. The objective is to minimize the total distance.

  5. please can i get pso m file for optimal placement of capacitor banks in ieee 33bus radial distribution system on my e-mail:kabirdregis@gmail.com

    thanks!

  6. Sir,
    Please send me the PSO code which is generalized.
    I want to run nonlinear cost function which is iterative.
    Is it possible to apply general PSO(which required only cost function,population size,iterations,Lb,Ub)
    My email id is spdiwan1984@gmail.com

    Thank you in advanced.

  7. I would like to apply it for real time. Is it possible for fast dynamic system? Is it fast?Is it convertible to VHDL using HDL coder of Matlab?

  8. Hi Sir,Can you please send me the MATLAB code PSO(particle swarm optimization) for Minimizing BER(Bit Error Rate) between two images?
    These two images are used with a coefficient in Watermark.
    The algorithm helps to select the coefficient to minimize the difference between the original image and the Watermark image (image containing Watermark).
    Thanks for your help!:)

  9. Hi Sir,Can you please send me the MATLAB code PSO(particle swarm optimization) for Minimizing BER(Bit Error Rate) between two images?
    These two images are used with a coefficient in Watermark.
    The algorithm helps to select the coefficient to minimize the difference between the original image and the Watermark image (image containing Watermark).
    email: marzi.kp20@gmail.com
    Thanks for your help!:)

  10. hi

    can you please send me PSO code of Beale function

    f(x) = (1.5 – x1 + x1.*x2).^2 + (2.25 – x1 + x1.*x2.^2).^2 + (2.625 – x1 + x1.*x2.^3).^2;

    BEALE([x1, x2]) returns the value of the value of the Beale
    function at the specified points. [x1] and [x2] may be vectors.
    The search domain is
    -4.5 < x_i < 4.5

    please mail on vishal.wankhede@yahoo.com

  11. Hi sir,
    I have an algorithm called EEEHR for wireless sensor networks, which is on the basis of LEACH algorithm. Now i am trying to implement my algorithm using optimization techniques. Is there any way of implementing the PSO or Genetic Algorithm in the same algorithm.

    Kindly do the needful.

    Mail id: ezhisang20@gmail.com

  12. Hi sir,
    I have an algorithm called EEEHR for wireless sensor networks, which is on the basis of LEACH algorithm. Now i am trying to implement my algorithm using optimization techniques. Is there any way of implementing the PSO or Genetic Algorithm in the same algorithm.

    Kindly do the needful.

    Mail id: ezhisang20@gmail.com

    If any clarification required i will send you my EEEHR protocols m-file.

  13. Hello sir!I am currently doing a project on this for my final year in university. Did you do the optimization of size and placement of distributed generation in a IEEE 9 bus and IEEE 14 bus using PSO? Can you please send me the Matlab coding? My email address: haniamelia97@gmail.com

  14. i need the pso code to implement the following transfer function
    P_11 (s) = (-0.288s^2 +0 .8825s + 0.5452)/(1.247s^3 + 5.365s^2 + 4.39s + 1)
    P_22 (s) = (5.421s^2 +1.693s – 7.229)/(s(7500s^2 + 17500s + 10000))

  15. i need the pso code to implement the following transfer function
    P_11 (s) = (-0.288s^2 +0 .8825s + 0.5452)/(1.247s^3 + 5.365s^2 + 4.39s + 1)
    P_22 (s) = (5.421s^2 +1.693s – 7.229)/(s(7500s^2 + 17500s + 10000))
    email:divvsri@gmail.com

  16. Hello,optimization of size and placement of distributed generation in a IEEE 33 bus and/or IEEE 69bus using PSO the Matlab coding, Would you please send me email:mclostavi@gmail.com

  17. Please, Sir, I am trying to use PSO to determine the coefficients of the dependent and independent variables. Those various are matrices of for example 1 by 10 sizes and the coefficients are unknown. For example: y=a0+a1x1+a2x2…anxn. The dependent and independent variables are given but the coefficients are to be determined.

  18. Here is Mr Benjamin contact Email details,lfdsloans@outlook.com. / lfdsloans@lemeridianfds.com Or Whatsapp +1 989-394-3740 that helped me with loan of 90,000.00 Euros to startup my business and I'm very grateful,It was really hard on me here trying to make a way as a single mother things hasn't be easy with me but with the help of Le_Meridian put smile on my face as i watch my business growing stronger and expanding as well.I know you may surprise why me putting things like this here but i really have to express my gratitude so anyone seeking for financial help or going through hardship with there business or want to startup business project can see to this and have hope of getting out of the hardship..Thank You.

  19. Sir, I attend webinar and known about this online blog. can you send me the PSO code of the following (congestion management, COST minimization, ATC calculation)
    if it is not available ,kindly provide the above video PSO code for TEACHING purpose.
    REPLY
    rajsingh.raj0@gmail.com

  20. Sir i need pso code for optimal placement of DVR or DG in 33 bus system ,sir could u please provide the codes ! It would be a great help!!
    Email: suresureshkumarsl123@gmail

    Thank you

  21. Sir i need pso code for optimal placement of DVR or DG in 33 bus system ,sir could u please provide the codes ! It would be a great help!!
    Email: sureshkumarsl123@gmail

    Thank you

  22. hi sir, did you do the optimization of size and placement of distributed generation in a IEEE 33 bus ( or optimization filter harmonic) ? Can you please send me the Matlab code? thank you very much

  23. HOW I GO MY DESIRED LOAN AMOUNT FROM A RELIABLE AND TRUSTED LOAN COMPANY LAST WEEK. Email for immediate response: drbenjaminfinance@gmail.com Call/Text: +1(415)630-7138 Whatsapp +19292227023

    Hello everyone, My name is Mr.Justin Riley Johnson, I am from Texas, United State, am here to testify of how i got my loan from BENJAMIN LOAN INVESTMENTS FINANCE(drbenjaminfinance@gmail.com) after i applied Two times from various loan lenders who claimed to be lenders right here this forum,i thought their lending where real and i applied but they never gave me loan until a friend of mine introduce me to {Dr.Benjamin Scarlet Owen} the C.E.O of BENJAMIN LOAN INVESTMENTS FINANCE who promised to help me with a loan of my desire and he really did as he promised without any form of delay, I never thought there are still reliable loan lenders until i met {Dr.Benjamin Scarlet Owen}, who really helped me with my loan and changed my life for the better. I don't know if you are in need of an urgent loan also, So feel free to contact Dr.Benjamin Scarlet Owen on his email address: drbenjaminfinance@gmail.com BENJAMIN LOAN INVESTMENTS FINANCE holds all of the information about how to obtain money quickly and painlessly via Whatsapp +19292227023 Email: drbenjaminfinance@gmail.com and consider all your financial problems tackled and solved. Share this to help a soul right now, Thanks..

  24. HOW I GO MY DESIRED LOAN AMOUNT FROM A RELIABLE AND TRUSTED LOAN COMPANY LAST WEEK. Email for immediate response: drbenjaminfinance@gmail.com Call/Text: +1(415)630-7138 Whatsapp +19292227023

    Hello everyone, My name is Mr.Justin Riley Johnson, I am from Texas, United State, am here to testify of how i got my loan from BENJAMIN LOAN INVESTMENTS FINANCE(drbenjaminfinance@gmail.com) after i applied Two times from various loan lenders who claimed to be lenders right here this forum,i thought their lending where real and i applied but they never gave me loan until a friend of mine introduce me to {Dr.Benjamin Scarlet Owen} the C.E.O of BENJAMIN LOAN INVESTMENTS FINANCE who promised to help me with a loan of my desire and he really did as he promised without any form of delay, I never thought there are still reliable loan lenders until i met {Dr.Benjamin Scarlet Owen}, who really helped me with my loan and changed my life for the better. I don't know if you are in need of an urgent loan also, So feel free to contact Dr.Benjamin Scarlet Owen on his email address: drbenjaminfinance@gmail.com BENJAMIN LOAN INVESTMENTS FINANCE holds all of the information about how to obtain money quickly and painlessly via Whatsapp +19292227023 Email: drbenjaminfinance@gmail.com and consider all your financial problems tackled and solved. Share this to help a soul right now, Thanks..

  25. HOW I GO MY DESIRED LOAN AMOUNT FROM A RELIABLE AND TRUSTED LOAN COMPANY LAST WEEK. Email for immediate response: drbenjaminfinance@gmail.com Call/Text: +1(415)630-7138 Whatsapp +19292227023

    Hello everyone, My name is Mr.Justin Riley Johnson, I am from Texas, United State, am here to testify of how i got my loan from BENJAMIN LOAN INVESTMENTS FINANCE(drbenjaminfinance@gmail.com) after i applied Two times from various loan lenders who claimed to be lenders right here this forum,i thought their lending where real and i applied but they never gave me loan until a friend of mine introduce me to {Dr.Benjamin Scarlet Owen} the C.E.O of BENJAMIN LOAN INVESTMENTS FINANCE who promised to help me with a loan of my desire and he really did as he promised without any form of delay, I never thought there are still reliable loan lenders until i met {Dr.Benjamin Scarlet Owen}, who really helped me with my loan and changed my life for the better. I don't know if you are in need of an urgent loan also, So feel free to contact Dr.Benjamin Scarlet Owen on his email address: drbenjaminfinance@gmail.com BENJAMIN LOAN INVESTMENTS FINANCE holds all of the information about how to obtain money quickly and painlessly via Whatsapp +19292227023 Email: drbenjaminfinance@gmail.com and consider all your financial problems tackled and solved. Share this to help a soul right now, Thanks

  26. Thank you sir, for the code but i want PSO code for finding the optimal location of PMU in IEEE 14 bus system but you have sent just PSO code.
    Please help me in finding the optimal location of PMU by using PSO.

  27. Hi sir, I kindly request you to support me by sending a MATLAB code for "distribution network reconfiguration by optimal placing of sectionalizers and tie-switches for loss minimization and reliability enhancement using PSO algorithm?''
    Can you please send me the Matlab coding for a 33 bus?
    Email: aberalem43@gmail.com

    Thanks for your help in advance.

  28. Hi. I tried copying the code as you lectured it in your youtube video. I copied it as it is, however my figure has been plotting it linear instead of scattered.

    Also, why did you divide this equation: swarm(i, 1) = swarm(i, 1) + swarm(i, 5)/1.2 by 1.2

    Many thanks

  29. from my thesis i have three wind plants, one biogas plant and 18 hydro power plants, my issue is to minimize a blackout effect by making optimal generation scheduling, using PSO MATLAB coding , could you send me the MATLAB PSO code of generation scheduling???(abkassaw@gmail.com)

  30. Hello dear
    would I get Code for Optimal placement of switches in power distribution system reliability assessment using binary PSO optimization technique?

  31. sir greetings!
    appreciate the your work . i need matlab code for optimization of size and placement of distributed generation in a IEEE 33 bus using PSO and optimal location of single tuned filter for DG system in a IEEE 33 bus using PSO algorithm . if you have please send me the following email ID: dasarinarasimharao@gmail.com Thanks in advance.

  32. I wanted to carry out the following process, will your code work out in my case. I have three parameters – Namely Cutting Speed and Depth of Cut and Feed Rate for a Machining Process in a CNC Machine, I will obtain the Surface Roughness from this experiment. In turn, find out the optimized Cutting Speed, Depth of Cut and Feed Rate. Later use this to identify if the Desired SR and Acutal SR are same – SR = Surface Roughness. Awaiting your reply

  33. I wanted to carry out the following process, will your code work out in my case. I have three parameters – Namely Cutting Speed and Depth of Cut and Feed Rate for a Machining Process in a CNC Machine, I will obtain the Surface Roughness from this experiment. In turn, find out the optimized Cutting Speed, Depth of Cut and Feed Rate. Later use this to identify if the Desired SR and Acutal SR are same – SR = Surface Roughness. Awaiting your reply – sunithbabu@msrit.edu

  34. hello i am having a had time using particle swarm or any other method to optimize Neural network weights and bias. please can you assist me with this?

  35. Hello sir, I published two ieee confernce paper regarding Electric vehicle. Now I want to do optimzation algorithm in my research work. Kindly mail me the pso matlab code for optimization of size and location of D-STATCOM. My mail id is sasmita2020@gmail.com. Thank you Sir.

  36. sir, good morning , sir am doing real time project on my pg program, if you don't mine please send me 69 bus code it will be helpful to more sir. my email id :nagarjuna.tollamadugu@gmail.com

  37. Sir can you send me the complete code for particle swarm optimisation @bhuvanaganesh2711@gmail.com

  38. Hello sir, Could you please send me pso code for optimizing network throughput and packet loss in wireless sensor networks? I have been trying to make it but I have been getting errors.

  39. B V V L Kala Bharathi

    please ,can i get matlab coding for “optimization of size and placement of distributed generation in a IEEE 33 bus using PSO”.
    thank you

  40. Hello sir, please can i get MATLAB code for optimizing throughput and packet loss in wireless sensor networks using PSO, I’m trying but i keep getting errors. I need assistance.

Leave a Comment

Your email address will not be published. Required fields are marked *