4/9/19 11:37 PM C:/Users/Anabil Samanta/Desk... /pso.m 1 of 2
4/9/19 11:37 PM C:/Users/Anabil Samanta/Desk... /pso.m 1 of 2
m 1 of 2
clc
clear all
pop=input('Enter the population size :- ');
ite=input('Enter the no of iteration:- ');
d=4;
range=10;
velocity=zeros(pop,4);
velocity_maxlimit=3;
velocity_minlimit=-3;
c1=1.49455;
c2=1.49455;
wmin=0.49;
wmax=.95;
c=unifrnd(-5,5,pop,d);
for q=1:ite
for i=1:pop
sum=0;
for j=1:d
y=c(i,j)^2;
sum=sum+y;
end
z(i)=sum;
end
[pbest_value,index]=min(z);
pbest_solution=c(index,:);
if q==1
gbest_value=pbest_value;
gbest_solution=pbest_solution;
elseif pbest_value<gbest_value
gbest_solution=pbest_solution;
gbest_value=pbest_value;
else
end
m(q)=gbest_value;
% w=0.5+rand()/2;
w=((wmax-wmin)*q)/ite;
for l=1:pop
for k=1:d
velocity(l,k)=w*velocity(l,k)+c1*unifrnd(0,1)*(pbest_solution(k)-(c(l,
k)))+c2*unifrnd(0,1)*(gbest_solution(k)-(c(l,k)));
if velocity(l,k)>velocity_maxlimit
velocity(l,k)=velocity_maxlimit;
elseif velocity(l,k)<velocity_minlimit
velocity(l,k)=velocity_minlimit;
4/9/19 11:37 PM C:\Users\Anabil Samanta\Desk...\pso.m 2 of 2
else
end
end
end
for l=1:pop
for k=1:d
c(l,k)=c(l,k)+velocity(l,k);
if c(l,k)>range
c(l,k)=range;
elseif c(l,k)<-range
c(l,k)=range;
else
end
end
end
if gbest_value<=0.00001
break
else
continue
end
end
fprintf('Gbest value=%f',gbest_value);
fprintf('\n');
x=length(m);
fprintf('no.of iteration executed %d',x)
fprintf('\n')