Particle swarm algorithm in detail __ algorithm

Source: Internet
Author: User
Tags min pow sleep static class

I. Creating a background

The ❃ particle swarm algorithm (Particleswarm OPTIMIZATION,PSO), proposed by Kennedy and Eberhart in 1995, modifies the model of Hepper's simulated bird population (shoal) so that particles can fly to the solution space, and landed at the best solution, the particle swarm optimization algorithm was obtained.

❃ is similar to genetic algorithm, is also based on group iteration, but does not have the crossover and mutation of the genetic algorithm, but the particle in the solution space to follow the best particle search.

The advantages of ❃PSO are simple, easy to implement, no gradient information, few parameters, especially its natural real-coded characters are 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 optimization problem solution is imagined as a bird, called a "particle". All particles are searched in a D-dimensional space.

❃ all particles are determined by a 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 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) • Encode: Because the dimension of the problem is 5, each particle is a real vector of 5 dimensions. (2) • Initialization range: set to [ -30,30] According to the problem request. 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: 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 algorithm to solve TSP problem 1.matlab implementation

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];   
% initializes each particle, which is the generation of a path population group=ones (citynum,popsize);
For I=1:popsize Group (:, i) =randperm (citynum) ';

End Group=arrange (Group);   
% initialized 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)
    ; End end% calculates the distance for each path for i=1:popsize eachpathdis (i) = Pathdistance (Group (:, i) ', CitYbetweendistance); End indivdualbest=group;% records the position of individual extreme points of each particle, that is, the shortest path the individual finds indivdualbestfitness=eachpathdis;% records the optimal fitness value, that is, the length of the shortest path found by the individual [
Globalbestfitness,index]=min (Eachpathdis);% finds the global optimal value and corresponding ordinal number% initial random solution figure;
Subplot (2,2,1);
Pathplot (node,citynum,index,indivdualbest);

Title (' Random Solution ');  
    
    % optimization while (isstop = = 0) & (Iteration < maxiteration)% iterations increment 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;% updated distance is better than pre-update, record ordinal indivdualbest (:, Find (ischange)) = Group (:, Find (Ischange));% update individual best path indivdualbestfitness = indivdualbestfitness.* (~ischange) + eachpathdis.*ischange;% update individual best way Path distance [globalbestfitness, index] = min (eachpathdis),% update global best path, record corresponding ordinal if globalbestfitness==oldbestfitness% comparison update
        Pre-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);
Plot ((1:iteration), bestfitness (1:iteration));
Grid on;
Title (' Evolutionary Curve '); % 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.*;  public 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 iteration 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 ("Please 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++) {//array initialization operation 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 x[i][j]= (float)
				Wmax*math.random () +wmin);
			v[i][j]= (float) (Vmax*math.random ());
		 	 } System.out.print ("Particle" +i+ "the amplitude of this X1:" +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 the extra thread for the playback of the sound 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]+ "Number of iterations:" + k,50,725);  try {lzqjs ();
		Start iteration if (k>=iterations) {thread.sleep ((int) (5000));
	System.exit (0);  } k=k+1;
	Add 1 Operation thread.sleep ((int) (1000) per 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)

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.