A detailed description of particle swarm algorithm

Source: Internet
Author: User
Tags pow

A. Create a background

? Particle swarm optimization (Particleswarm OPTIMIZATION,PSO), proposed by Kennedy and Eberhart in 1995, modifies the model of the Hepper simulated bird population (fish) so that the particles can fly to the solution space and land at the best solution, The particle swarm optimization algorithm is obtained.

? Similar to genetic algorithms , it is a group-based iteration, but there is no crossover and mutation for genetic algorithms, but particles that follow the optimal particle in the solution space are searched.

? The advantage of PSO is simple, easy to implement, no gradient information, few parameters, especially its natural real-coded character is particularly suitable for dealing with real optimization problems. At the same time, there is a deep intelligent background, both for scientific research, and particularly suitable for engineering applications.

Imagine a scene in which a flock of birds searches for food randomly. There is only one piece of food in this area, and all the birds don't know where the food is. But they know how far they are from their current location to the food.

So what's the best strategy for finding food ?

The simplest and most effective way is to search the area around the bird that is currently closest to the food .

Two. Introduction to Algorithms (1) Description

? Each solution to the problem of optimization is imagined as a bird, called a "particle". All particles are searched in a D-dimensional space.

? All particles are determined by an fitness-function to determine the current position.

? Each particle must be given a memory function to remember the best location to search for.

? Each particle also has a speed to determine the distance and direction of the flight. This speed is dynamically adjusted based on its own flight experience and companion's flight experience.

(2)Basic PSO Algorithm

A. In D-dimensional space, there are m particles;

Particle i position: xi= (xi1,xi2,... XiD)

Particle i speed: vi= (vi1,vi2,... viD), 1≤i≤m,1≤d≤d

The history of particle I has been best placed: pi= (pi1,pi2,... PiD)

The best place in a group (or field) where all particles have experienced:

pg = (pg1,pg2,... pgD)

PS: In general, the position and velocity of a particle are evaluated in a continuous real space .


B. basic PSO formula


(3) Basic PSO algorithm flowchart


The formula for the update speed and position of each particle is as follows:


Three. Simple Application

(1) Code: Because the dimension of the problem is 5, each particle is a real vector of 5 dimensions. (2) initialization scope: According to the problem request, set to [ -30,30]. Based on the previous parameter analysis, we know that the maximum speed can be set to vmax=60. (3) Population size: For illustrative purposes, a smaller population size, m=5, is used here. (4) Stop criteria: Set to the maximum number of iterations 100 times. (5) Inertia weight: Using fixed weight 0.5. (6) Neighborhood topology: Using a star topology, the global version of Particle swarm optimization algorithm

The algorithm performs the following procedures:









Four. Code implementation: Using Particle swarm optimization algorithm to solvetsp problem1.matlab implementations
Close All;clear all; popsize=500;% population size Citynum = 14;% number of cities oldbestfitness=0;% old optimal fitness value iteration=0;% iterations maxiteration =2000;% Maximum iterations isstop=0;% Program Stop Flag num=0;% The number of iterations of the same fitness value c1=0.5;% cognitive coefficients c2=0.7;% social learning coefficients w=0.96-iteration/maxiteration;% inertial coefficients, decreasing% node coordinates as the number of iterations increases node=[ 16.47 96.10; 16.47 94.44; 20.09 92.54; 22.39 93.37;     25.23 97.24;.. 22.00 96.05; 20.47 97.02; 17.20 96.29; 16.30 97.38;     14.05 98.12;.. 16.53 97.38; 21.52 95.59; 19.41 97.13;   20.09 94.55];% Initialize each particle, that is, the generation of the path population group=ones (citynum,popsize); For I=1:popsize Group (:, i) =randperm (citynum) '; Endgroup=arrange (group);% initialize particle velocity (ie, exchange order) Velocity =zeros (Citynum,   Popsize); For I=1:popsize Velocity (:, i) =round (rand (1,citynum) ' *citynum);   %round Rounding end% calculates the distance between each city Citybetweendistance=zeros (citynum,citynum);    For I=1:citynum for J=1:citynum citybetweendistance (i,j) =sqrt ((Node (i,1)-node (j,1)) ^2+ (node (i,2)-node (j,2)) ^2); endend% calculates the distance for each path for i=1:popsize eachpathdis (i) = Pathdistance (Group (:, i) ', citybetweendistance); endindivdualbe st=group;% Record eachThe individual extreme point position of the particle, the shortest path found by the individual, indivdualbestfitness=eachpathdis;% records the optimal fitness value, that is, the length of the shortest path found by the individual [Globalbestfitness,index]=min ( Eachpathdis);% to find the global optimal value and corresponding ordinal% initial random solution figure;subplot (2,2,1); Pathplot (node,citynum,index,indivdualbest); title (' Random Solution ');%-optimized while (Isstop = = 0) & (Iteration < maxiteration)% Iteration          Number of increments iteration = Iteration +1;          % Update global extremum point position, here refers to path for I=1:popsize globalbest (:, i) = Group (:, index);      End% for Pij-xij, Pgj-xij Exchange sequence, and the reserved Exchange sequence Pij_xij=generatechangenums (group,indivdualbest) with probability c1,c2;     Pij_xij=holdbyodds (PIJ_XIJ,C1);    Pgj_xij=generatechangenums (group,globalbest);        Pgj_xij=holdbyodds (PGJ_XIJ,C2);    % retains previous generation Exchange Order Velocity=holdbyodds (VELOCITY,W) with probability w; Group = Pathexchange (group,velocity);    % path Exchange according to the exchange order Group = Pathexchange (Group,pij_xij);    Group = Pathexchange (Group,pgj_xij);        For i = 1:popsize% update each path total distance Eachpathdis (i) = Pathdistance (Group (:, i) ', citybetweendistance); End ischange = eachpathdis<indivdualbestfitness;% moreNew distance is better than pre-update, record ordinal indivdualbest (:, Find (ischange)) = Group (:, Find (Ischange));% update individual best path indivdualbestfitness = INDIVD Ualbestfitness.* (~ischange) + eachpathdis.*ischange;% update individual best path distance [globalbestfitness, index] = min (eachpathdis);%        Update the global best path, record the corresponding ordinal if globalbestfitness==oldbestfitness% to compare the pre-update and post-update fitness values; num=num+1;    % equal when record plus one;        Else oldbestfitness=globalbestfitness;% update the fitness value when unequal, and record 0;    num=0;    End If Num >= 20 multiple iterations of the fitness value close to the program stop Isstop=1; End Bestfitness (iteration) =globalbestfitness;% optimal adaptation of each generation end% optimal solution subplot (2,2,2); Pathplot (Node,citynum,index,indivdualbest), title (' Optimized solution '),% evolutionary curve subplot (2,2,3);p lot ((1:iteration), bestfitness (1:i teration)); Grid on;title (' evolutionary curve '); The% minimum path value globalbestfitness The result of the operation is as follows:

2.java implementations
Package Pso;import Java.awt.*;import Java.awt.event.*;import java.io.bytearrayinputstream;import Java.io.inputstream;import javax.swing.*;import javax.swing.event.*;p ublic class Pso extends Frame implements Runnable  {private static int particlenumber;      The number of particles is private static int iterations;             Number of iterations private static int k=1;    Number of times the iteration was recorded final private static float c1=2;    Learning factor final private static float c2=2;    Final private static float wmin=-200;    Final private static float wmax=200;    Final private static float vmax=200;           private static float R1;    Random number 0-1 private static float R2;    private static float x[][];    private static float v[][];    private static float xpbest[][];          private static float pbest[];    private static float gbest=0;    private static float xgbest[];           private static float W;    Inertia factor private static float s;    private static float H;    private static float fit[];    public Sounds Sound;    The iteration function of the particle swarm public void Lzqjs () {w= (float) (0.9-k* (0.9-0.4)/iterations); for (int i=0;i<particlenumber;i++) {fit[i]= (float) (1/(Math.pow (x[i][0],2) +math.pow (x[i][1],2) ));                   Fitness function Maximum value System.out.print ("particle" +i+ "This adaptive value function f is:" + fit[i]);                   System.out.println ();                   if (Fit[i]>pbest[i]) {pbest[i]=fit[i];                   XPBEST[I][0]=X[I][0];                   XPBEST[I][1]=X[I][1];                   } if (pbest[i]>gbest) {gbest=pbest[i];                   XGBEST[0]=XPBEST[I][0];                   XGBEST[1]=XPBEST[I][1];                   }} for (int i=0;i<particlenumber;i++) {for (int j=0;j<2;j++) {//particle velocity and position iterative equation: v[i][j]= (float) (W*v[i][j]+c1*math.random () * (Xpbest[i][j]-x[i][j] ) +c2*math.random () * (XGBEST[J]-X[I][J));                                      x[i][j]= (float) (x[i][j]+v[i][j]);                } System.out.print ("Particle" +i+ "the speed variation of this X1:" +v[i][0]+ "; the speed of this X2:" +v[i][1]);            System.out.println ();                System.out.print ("particle" +i+ "This time X1 is:" +x[i][0]+ "; this time X2 is:" +x[i][1]);         System.out.println (); }}public static void Main (string[] args) {Particlenumber=integer.parseint (Joptionpane.showinputdialog (" Please enter the number of particles 1-500)), Iterations=integer.parseint (Joptionpane.showinputdialog ("Enter the number of iterations")); x=new float [particlenumber    ][2];v=new float [particlenumber][2];fit=new float [particlenumber];  Store Fitness function Value pbest=new float [particlenumber]; Stores the most position of the entire particle swarm xpbest=new float [particlenumber][2];xgbest=new float [2];for (int i=0;i<particlenumber;i++) {// Initialize operation of the array pbest[i]=0;xpbest[i][0]=0;xpbest[i][1]=0;} xgbest[0]=0;xgbest[1]=0; SYSTEM.OUT.PRINTLN ("Start Initialization:"), for (int i=0;i<particlenumber;i++) {for (int j=0;j<2;j++) {//) any given position value and speed value for each position x[ i][j]= (float) (Wmax*math.random () +wmin); v[i][j]= (float) (Vmax*math.random ());}  System.out.print ("particle" +i+ "the magnitude of this X1 change:" +v[i][0]+ "; the magnitude of the X2 change:" +v[i][1]); System.out.println (); System.out.print ("particle" +i+ "This time X1 is:" +x[i][0]+ "; this time X2 is:" +x[i][1]); System.out.println ();} SYSTEM.OUT.PRINTLN ("Initialize data end, start iteration ..."); PSO threada=new PSO (threada.settitle) ("Particle position dynamic display based on particle swarm"); Threada.setsize (800,800); Threada.addwindowlistener (new        Gbck ()); Threada.setvisible (true);        Thread threadc=new thread (Threada); Threadc.start ();} Static class Gbck extends Windowadapter{public void windowclosing (WindowEvent e) {system.exit (0);}}                Open extra thread for sound playback public void run () {repaint ();        for (int. i=0;i<iterations;i++) {sound ();       }}public void Paint (Graphics g) {G.setcolor (new Color (0,0,0));       for (int i=0;i<particlenumber;i++) {g.drawstring ("*", (int) (x[i][0]+200), (int) (x[i][1]+200));       } g.setcolor (New Color (255,0,0));   g.DrawString ("Global optimal fitness function value:" +gbest+ "Parameter 1:" +xgbest[0]+ "  Parameter 2: "+xgbest[1]+" Iteration Number: "+ k,50,725);  Try{lzqjs (); Start iteration if (k>=iterations) {thread.sleep ((int) (5000)); System.exit (0);}  k=k+1;    Add 1 Action thread.sleep ((int) (1000)) at a time for each iteration;}    catch (Interruptedexception e) {System.out.println (e.tostring ()); } repaint ();}  public void Sound () {Sound =new Sounds ("050.wav");  InputStream Stream =new Bytearrayinputstream (Sound.getsamples ());  Play the Sound Sound.play (stream); Exit}}
The results of the operation are as follows:

Algorithmic Code Address: http://download.csdn.net/detail/u012017783/9700118 (Matlab, two versions of Java)

A detailed description of particle swarm algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.