clear all;
nIter = 0;
NFCCounter=0;
nRuns = 50;
VTRCounter=0;
for tIter = 1:nRuns
% clear all;
%Initialization of PSO parameters
wmax=0.9;
wmin=0.4;
itmax=5000; %Maximum iteration number
c1=1.49612;
c2=1.49612;
for iter=1:itmax
W(iter)=wmax-((wmax-wmin)/itmax)*iter;
end
%**********************************************************
%Initialization of positions of agents
% agents are initialized between -5,+5 randomly
N=100; %% Population Size
D=30; %% Dimensions
%===================================================
a=-5.12; %% Min values of Swarm parameters
b=5.12; %% Max values of Swarm Parameters
Jr=0.3; %% Probability
%===================================================
org_x = a+(b-a)*rand(N,D,1); %% Initial Positions of the PSO Agents
x = org_x;
mixedX = x;
for i=1:N
F(i,1,1)=f1(mixedX(i,:,1));
NFCCounter= NFCCounter +1;
%FbestNFCs(tIter, NFCCounter) = min(F(:,1,1));
end
[F2 indx] = sort(F);
clear F;
F(:,1,1) = F2(1:N);
x = mixedX(indx(1:N),:);
%Initialization of velocities of agents
%Between -5 , +5, (which can also be started from zero)
% m=-1;
% n=1;
m= 0.1 * (a - b );
n= 0.1 * (b - a );
V=m+(n-m)*rand(N,D,1); %% Inital velocity of the Agents
%**********************************************************
%Function to be minimized.
%x(i,2,1)=x2
%x(i,1,1)=x1
%f=100*(x2-x1^2)^2+(x1-1)^2
% for i=1:N
% F(i,1,1)=100*(x(i,2,1)-x(i,1,1)^2)^2+(x(i,1,1)-1)^2;
% % NFCCounter= NFCCounter +1
% end
%**********************************************************
[C,I]=min(abs(F(:,1,1)));
B(1,1,1)=C;
XX(1,1,1)=I;
gbest(1,:,1)=x(I,:,1); % Global Best
% gbest(1,2,1)=x(I,2,1); % Global Best
%********************************************************
%Matrix composed of gbest vector
for p=1:N
for r=1:D
G(p,r,1)=gbest(1,r,1);
end
end
graphIndex = 1;
Fbest(tIter,1)=f1(G(1,:,1));
graphFbest(tIter, graphIndex:NFCCounter) = Fbest(tIter,1);
graphIndex = NFCCounter;
for i=1:N
pbest(i,:,1)=x(i,:,1);
end
V(:,:,2)=W(1)*V(:,:,1)+c1*rand*(pbest(:,:,1)-x(:,:,1))+c2*rand*(G(:,:,1)-x(:,:,1));
x(:,:,2)=x(:,:,1)+V(:,:,2);
Fb(tIter,1)=f1(gbest(1,:,1));
%******************************************************
for j=2:itmax-1
probab = rand;
flag = 0;
% Calculation of new positions
for i=1:N
F(i,1,j)=f1(x(i,:,j));
NFCCounter= NFCCounter +1
IterationDisp = tIter
end
% F(:,:,j)
[C,I]=min(abs(F(:,:,j)));
%==========================================================================
if ((C <= 0.00000001) || (NFCCounter >=1000000)) %0.0000000000000000000000001) || (NFCCounter >=100000)) %10 raise to power -25
%0.0000000000000000000000001 BY Calculator
C
x(I,:,j)
VTRCounter=VTRCounter+1;
break;
end;
%============================================================================
B(1,1,j)=C;
gbest(1,:,j)=x(I,:,j);
% gbest(1,2,j)=x(I,2,j);
Fb(tIter,j)=f1(gbest(1,:,j));
[C,I]=min(Fb(tIter,:));
if Fb(tIter,j)<=C
gbest(1,:,j)=gbest(1,:,j);
% gbest(1,2,j)=gbest(1,2,j);
else
gbest(1,:,j)=gbest(1,:,I);
% gbest(1,2,j)=gbest(1,2,I);
end
%Matrix composed of gbest vector
for p=1:N
for r=1:D
G(p,r,j)=gbest(1,r,j);
end
end
Fbest(tIter,j)=f1(G(1,:,j));
graphFbest(tIter, graphIndex:NFCCounter) = Fbest(tIter,j);
graphIndex = NFCCounter;
for i=1:N
[C,I]=min(F(i,1,:));
if F(i,1,j)<=C
pbest(i,:,j)=x(i,:,j);
else
pbest(i,:,j)=x(i,:,I);
end
end
V(:,:,j+1)=W(j)*V(:,:,j)+c1*rand*(pbest(:,:,j)-x(:,:,j))+c2*rand*(G(:,:,j)-x(:,:,j));
% Velocity Clamping
% for individual = 1:N
% for dim = 1:D
% if V(individual,dim,j+1) < m
% V(individual,dim,j+1) = m;
% end
% if V(individual,dim,j+1) > n
% V(individual,dim,j+1) = n;
% end
%
% end
% end
% End Velocity Clamping
x(:,:,j+1)=x(:,:,j)+V(:,:,j+1);
end
nIter(tIter) = j;
NFCs(tIter) = NFCCounter;
BestFitness(tIter) = C;
% plot(x(:,1,j),x(:,2,j),'o');
clear pbest;
clear gbest;
clear Fb;
clear F;
NFCCounter = 0;
end;
MeanNFC = sum(NFCs) / nRuns
[M N] = size(graphFbest);
for i = 1:N
FbestAvg(i) = sum(graphFbest(:,i))/M;
end;
%hold on, plot(FbestAvg(1,1:MeanNFC), 'color', 'blue');
%
SuccessRate=VTRCounter/nRuns
MaxNFC = max(NFCs)
MinNFC = min(NFCs)
STDDEV= std(NFCs)
MeanBestFitness =sum(BestFitness)/nRuns
MinBestFitness =min(BestFitness)
MaxBestFitness =max(BestFitness)
MedianFitness =median(BestFitness)