% particle swarm optimization algorithm
% function [fxmin, xmin] =pso
%
clc; clear;
% Parameter for swarm
%
tic;
SwarmSize= 50; % the number of particles or individuals in Swarm
Dim = 100 ; % the dimension of solution
SwarmX = zeros(SwarmSize, Dim); % particle's position matrix, which is a SwarmSize by Dim matrix
SwarmV = zeros(SwarmSize, Dim); % particle's velocity matrix, which is a SwarmSize by Dim matrix
range=ones(2,Dim);
IterMax = 2000; % maximum iteration number
fname = 'griewank'; % The funciton to be optimized
runtime=30;
Globalmins=zeros(1,runtime);
pos=zeros(15,runtime);
for r=1:runtime
XRange(1,:)=range(1,:)*(-600);
XRange(2,:)=range(2,:)*600; % matrix of 2 by Dim , which specifize the range of each variables
Vmax =4; % velocity range,i.e. [v_min, v_max]
Pibest = zeros(SwarmSize, Dim); % the individual best position
Pgbest = zeros(1, Dim); % the global best position
fhistory = [ ]; % variable to record the best fitness during iteration
% Parameter for algorithm
%
c1 = 2; c2 = 2; % parameter for c1 and c2
% innertia weight using linear decreasing
w_max = 0.9;
w_min = 0.4;
bflag = 1; % method flags to process particle's position that exceed XRange: 0--no limit, 1: limit
vflag = 1; % method flags to process particle's velocity that exceed Vmax: 0--no limit, 1: limit
fmin = 0; % variable to strore the gobal minimum fitness
% ==========Initialization==============
% initialize particle's position within VarRange
SwarmX = rand(SwarmSize, Dim);
for i = 1:Dim
SwarmX(:,i) = XRange(1,i)+ (XRange(2, i) - XRange(1,i))*SwarmX(:, i);
end
% intialize particle's velocity
SwarmV = -Vmax+ 2*Vmax*rand(SwarmSize,Dim);
% evalute the fitness of particles
fSwarm = feval(fname, SwarmX);
% initialize Pibest and Pgbest
[fg, idx] = min(fSwarm);
Pgbest = SwarmX(idx, :);
Pibest =SwarmX;
fPibest = fSwarm; % initially, each particle's best position are set equal to fSwarm
fmin = fg;
fhistory = [fhistory; fmin]; % recorde the best fitness, here the best position is not recorde, if necessary, one can using a variable to recorde it
t = 0; % counter for iteration
% =========== end initialization ==========
SwarmMaskmin = repmat( XRange(1,:), SwarmSize,1);
SwarmMaskmax = repmat( XRange(2, :), SwarmSize,1);
% ====== start pso loop =======
while t < IterMax
% update iteration counter
t = t+1;
w = w_max - t*(w_max - w_min)/IterMax; % calculate new inertia weight
% update particle's velocity
A = repmat(Pgbest, SwarmSize, 1);
SwarmV = w*SwarmV + c1*rand(SwarmSize, Dim).*(Pibest - SwarmX)+c2*rand(SwarmSize,Dim).*(A - SwarmX);
% check SwarmV wheter is in [-Vmax, Vmax]
if vflag ==1
chRows = SwarmV > Vmax;
SwarmV(find(chRows)) = Vmax;
chRows = SwarmV < -Vmax;
SwarmV(find(chRows)) = -Vmax;
end
% update particle's position
SwarmX = SwarmX + SwarmV;
% Comfirm particle's position, such that it is in VarRange
% method 0 : no position limiting
% method 1: saturation at limiting
% method 2: bounce off limit
% we adopt method 1 here
if bflag ==1
minSwarm_throw = SwarmX <= SwarmMaskmin;
minSwarm_keep = SwarmX > SwarmMaskmin;
maxSwarm_throw = SwarmX >= SwarmMaskmax;
maxSwarm_keep = SwarmX < SwarmMaskmax;
SwarmX = ( minSwarm_throw.*SwarmMaskmin ) + ( minSwarm_keep.*SwarmX );
SwarmX = ( maxSwarm_throw.*SwarmMaskmax ) + ( maxSwarm_keep.*SwarmX );
end
% evaluate new fitmess of particles
fSwarm = feval(fname, SwarmX);
% update Pibest and Pgbest
chRows = fSwarm < fPibest;
fPibest(find(chRows)) = fSwarm(find(chRows));
Pibest(find(chRows), :) = SwarmX(find(chRows), :) ;
[fg, idx] = min(fPibest); % here has a question: [fg, idx] = min(fSwarmX)
% update gbest history
if fg < fmin
Pgbest = Pibest(idx,:); % or SwarmX
fPgbest = fg;
end
fmin = min(fPibest); %
fhistory = [fhistory; fmin];
fprintf('t=%d fmin=%g\n',t,fmin);
if (t==200)
pos(1,r)=fmin;
end;
if(t==400)
pos(2,r)=fmin;
end;
if(t==600)
pos(3,r)=fmin;
end;
if(t==800)
pos(4,r)=fmin;
end;
if(t==1000)
pos(5,r)=fmin;
end;
if(t==1200)
pos(6,r)=fmin;
end;
if(t==1400)
pos(7,r)=fmin;
end;
if(t==1600)
pos(8,r)=fmin;
end;
if(t==1800)
pos(9,r)=fmin;
end;
if(t==2000)
pos(10,r)=fmin;
end;
if(t==2200)
pos(11,r)=fmin;
end;
if(t==2400)
pos(12,r)=fmin;
end;
if(t==2600)
pos(13,r)=fmin;
end;
if(t==2800)
pos(14,r)=fmin;
end;
if(t==3000)
pos(15,r)=fmin;
end;
end
% ====== end pso loop ======
% output the final result
end;
fprintf('%d\n',mean(pos,2));
save all
评论2