You are on page 1of 2

%% FILE: PSO.

m
% ***********************************************************************
% A MATLAB Program on a Particle Swarm Algorithm (PSO)using MATLAB
% 34567823579
% for optimization of given objective function and PSO parameters
% ***********************************************************************
% objfun: objective function
% nvar: number of variables or dimentionality
% lb: lower bound on variable vector
% ub: upper bound on variable vector
% options includes the following parameters
% options.swrmsize: swarm or population size
% options.itermax: maximum iterations
% options.c: congnizant or learning parameters (1 x 2) [c1 c2]
% options.wt: inertia weight parameters (1 x 2) [wmax wmin]
% options.plt: plotting command
% ***********************************************************************
% Example:
% clear all; close all; clc; format shortg;
% objfun=@(x)4*x(1)^2-2.1*x(1)^4+(x(1)^6)/3+x(1)*x(2)-4*x(2)^2+4*x(2)^4;
% nvar=2; lb=[-1.9 -1.1]; ub=[1.9 1.1]; options.swrmsize=20;
% options.itermax=30; options.c=[1.05 1.05]; options.wt=[1 0.3];
% options.plt=1; Out=Rajendra_PSO(objfun,nvar,lb,ub,options)
% ***********************************************************************
%%
function Out=Rajendra_PSO(objfun,nvar,lb,ub,options) % function star
ts
if nargin == 4 % if no input for option
s
options.swrmsize=20; % swarm or population si
ze
options.itermax=30; % maximum iterations
options.c=[1.05 1.05]; % cognizant parameters
options.wt=[1 0.3]; % inertia wieght paramet
ers
options.plt=0; % plotting command 0
elseif nargin < 4 && nargin > 5 % .
disp('Inputs mismatch') % else condition
end % end if
D=nvar; % D: dimentionality
N=options.swrmsize; % Option parameters
itermax=options.itermax; % .
c1=options.c(1); c2=options.c(2); % .
wmax=options.wt(1); wmin=options.wt(2); % .
plt=options.plt; % .
w=linspace(wmax,wmin,itermax); % Inertia vector
a=repmat(lb,N,1); % lb for all swarms
b=repmat(ub,N,1); % ub for all swarms
d=(b-a); % parameter space
q=(b-a)/1; % 1/1 of parameter space
x=a+d.*rand(N,D); % initial position
v=q.*rand(N,D); % initial velocity
for ni=1:N % for each swarm
f(ni,1)=objfun(x(ni,:)); % fitness or goal
end % end for each swarm
[fgbest,igbest]=min(f); % fittest minimum in all
gbest=x(igbest,:); % Best among all gbest
pbest=x; fpbest=f; % Best in each swarm
Out=[]; % Output as pbest gbest
Out=[Out;0 gbest fgbest mean(fpbest)]; % initial pbest and gbes
t
for it=1:itermax % for each iteration
v(1:N,1:D)=w(it)*v(1:N,1:D)+... % update velocities
c1*rand*(pbest(1:N,1:D)-x(1:N,1:D))... % .
+c2*rand*(repmat(gbest,N,1)-x(1:N,1:D)); % .
x(1:N,1:D)=x(1:N,1:D)+v(1:N,1:D); % update positions
for ni=1:N % for each swarm
f(ni,1)=objfun(x(ni,:)); % fitness or goal
end % end for each swarm
[minf,iminf]=min(f); % best in each iteration
if minf<= fgbest % find gbest and fgbest
fgbest=minf; gbest=x(iminf,:); % .
end % .
inewpb=find(f<=fpbest); % find pbest and fpbest
pbest(inewpb,:)=x(inewpb,:); fpbest(inewpb)=f(inewpb); % .
Out=[Out;it gbest fgbest mean(fpbest)]; % saving pbest and gbest
end % end of each iteration
disp('Maximum number of iterations reached'); % .
if plt==1 % plotting condition
figure;plot(Out(:,1),Out(:,1+D+1),'.-b',Out(:,1),Out(:,1+D+2),'.-k');% plot
title('PSO Output in each generation'); % title
xlabel('Generations or Iterations'); ylabel('Fitness'); % labels
legend('Global best','Mean best'); % .
end % end of conditon
end % end of function