Recently, we have to write an article about the particle swarm algorithm, so we have to implement the local version of PSO algorithm. The realization idea of the local version of particle swarm algorithm has already been described in the particle Swarm algorithm (3)----standard particle swarm algorithm (local version). It is mainly divided into 3 functions. The first function localinitswarm the particle swarm initialization function (Swarmsize ...). ADAPTFUNC) Its main function is to initialize particles of particle swarm, and set the velocity and position of particles in a certain range. The data structures used in this function are as follows:
The table Parswarm records the position of the particle, the velocity and the current fitness value, we use W to represent the position, the velocity with V, and F to represent the current fitness value. Here we assume that the number of particles is n, and the dimension of each particle is d.
W1,1 |
W1,2 |
... |
W1,d |
V1,1 |
V1,2 |
... |
V1,d-1 |
V1,d |
F1 |
A 1th particle. |
w 2,2 |
|
w 2,d |
v 2,1 |
v 2,2 |
|
v 2,d-1 |
v 2,d |
f 2 |
2nd particle |
|
|
|
|
|
|
|
|
|
|
|
w n-1,2 |
|
w n-1,d-1 |
v n-1,1 |
v n-1,2 |
|
v n-1,d-1 |
v n-1,d |
f N-1 |
N-1 particle |
Wn,1 |
Wn,2 |
... |
Wn,d |
Vn,1 |
Vn,2 |
... |
Vn,d-1 |
Vn,d |
FN |
Nth particle |
Table Optswarm not only to record the optimal solution of its own history, but also to record the optimal solution of each particle's neighborhood (using ring neighborhood) and the global optimal solution of all the particles. Therefore, there should be 2*n+1 row, the first swarmsize Row records the particle's own historical optimal solution, and then swarmsize row to record the best solution of neighborhood.
The global optimal solution is represented by the WG, and the w.,1 represents the historical optimal solution of each particle. The first n rows in the particle swarm initialization stage table Optswarm are the same as in the table Parswarm, and the WG's value is the row for the maximum value of the fitness value in the table Parswarm.
w1,1 |
wj,2 |
... |
Wj,d-1 |
Wj,d |
The historical optimal solution of a 1th particle |
w2,1 |
wk,2 |
... |
Wk,d-1 |
Wk,d |
The historical optimal solution of a 2nd particle |
... |
... |
... |
... |
... |
... |
W (N-1), 1 |
wl,2 |
... |
Wl,d-1 |
Wl,d |
The historical optimal solution of the first N-1 particle |
W (N), 1 |
wm,2 |
... |
Wm,d-1 |
Wm,d |
The historical optimal solution of nth particle |
wl,1 |
... |
... |
... |
Wl,d |
The optimal solution of the first particle in the neighboring region |
wl,2 |
... |
... |
... |
Wl,d |
The optimal solution of the second particle's neighborhood |
... |
... |
... |
... |
..... |
.... |
Wl,n |
... |
... |
... |
... |
The optimal solution of nth particle in the adjacent region |
wg,1 |
wg,2 |
... |
Wg,d-1 |
Wg,d |
The historical optimal solution of global particles |
According to this thought MATLAB code is as follows:
function [Parswarm,optswarm]=localinitswarm (SWARMSIZE,PARTICLESIZE,PARTICLESCOPE,ADAPTFUNC)% feature Description: Local version of particle swarm algorithm, Initialize the particle swarm, define the position of the particle swarm and the velocity within the specified range%[parswarm,optswarm,badswarm]=localinitswarm (Swarmsize,particlesize,particlescope, Adaptfunc% input parameter: swarmsize: Number of population size% input parameter: particlesize: The dimension of a particle input parameter: Particlescope: The range of a particle in an operation;% Particlescope Format:% 3-D particle particlescope format:% [X1min,x1max% X2min,x2max% X3min,x3max]% input parameter: Adaptfunc: Fitness function% output: parswarm initialized particle swarm% output: optswarm The optimal solution of the current optimal solution of the particle swarm is the first initialization, the region of the neighborhood is 0, that is, the particle itself% usage [Parswarm,optswarm,badswarm]=localinitswarm (swarmsize, PARTICLESIZE,PARTICLESCOPE,ADAPTFUNC); %% Exception: first ensure that the file in the MATLAB search path, and then see the relevant information. % compiled by: XXX% Compilation time: 2010.5.6% reference: no% fault Tolerant control if nargin~=4 error (' The number of parameters entered is incorrect. ' End If nargout<2 error (' The number of parameters output is too small to guarantee future operation. '); End [Row,colum]=size (Particlesize); If row>1| | Colum>1 error (' The dimension of the input particle is wrong, is a 1 row 1 column of data. '); End [Row,cOlum]=size (Particlescope); If row~=particlesize| | colum~=2 error (' The dimension range of the input particles is wrong. '); End% initializes the particle swarm matrix to initialize the particle swarm matrix, all set to [0-1] random number%rand (' state ', 0); Parswarm=rand (swarmsize,2*particlesize+1); % the range of position and velocity in the particle swarm is regulated for k=1:particlesize parswarm (:, K) =parswarm (:, k) * (Particlescope (k,2)-particlescope (k,1)) + Particlescope (k,1); % adjust speed to align speed with position range Parswarm (:, Particlesize+k) =parswarm (:, particlesize+k) * (Particlescope (k,2)-particlescope (k,1)) +particlescope (k,1); End% calculates the value of its fitness function for each particle for k=1:swarmsize parswarm (k,2*particlesize+1) =adaptfunc (Parswarm (k,1:particlesize)); The end% initializes the optimal solution matrix of the particle swarm, which is a total swarmsize*2 row, in which the first swarmsize row records the particle's own historical optimal solution, and the Swarmsize Row records the neighborhood optimal solution Optswarm=zeros (swarmsize*2+1, Particlesize); The optimal solution matrix of the% particle swarm is all set to zero Optswarm (1:swarmsize,:) =parswarm (1:swarmsize,1:particlesize), and the optimal solution linyu=1 of the Particle neighborhood 1 is calculated; for row=1: Swarmsize if Row-linyu>0&&row+linyu<=swarmsize tempm =[parswarm (row-linyu:row-1,:); Parswarm (Row+1:row+linyu,:)]; [Maxvalue,linyurow]=max (TEMPM (:, 2*particlesize+1)); Optswarm (Swarmsize+row,:) =tempm (linyurow, 1:particlesize); else if row-linyu<=0% the upper part of the line highlights the boundary, the following will never break the boundary if Row==1 tempm=[parswarm (swarmsize+row-linyu:end,:); Parswarm (Row+1:row+linyu,:)]; [Maxvalue,linyurow]=max (TEMPM (:, 2*particlesize+1)); Optswarm (Swarmsize+row,:) =tempm (linyurow,1:particlesize); else Tempm=[parswarm (1:row-1,:); Parswarm (Swarmsize+row-linyu:end,:); Parswarm (Row+1:row+linyu,:)]; [Maxvalue,linyurow]=max (TEMPM (:, 2*particlesize+1)); Optswarm (Swarmsize+row,:) =tempm (linyurow,1:particlesize); end else% the line below highlights the boundary, which will never break the boundary if Row==swarmsize tempm= [Parswarm (swarmsize-linyu:row-1,:); Parswarm (1:linyu,:)]; [Maxvalue,linyurow]=max (TEMPM (:, 2*particlesize+1)); Optswarm (Swarmsize+row,:) =tempm (linyurow,1:particlesize); else Tempm=[parswarm (row-linyu:row-1,:); Parswarm (Row+1:end,:); Parswarm (1:linyu-(Swarmsize-row),:)]; [Maxvalue,linyurow]=max (TEMPM (:, 2*particlesize+1)); Optswarm (Swarmsize+row,:) =tempm (linyurow,1:particlesize), end-end end%for [Maxvalue,row]=max (Parswarm (:, 2* particlesize+1)); % find the maximum solution of the fitness function value in a bit of the matrix(line number) Optswarm (swarmsize*2+1,:) =parswarm (row,1:particlesize);