Page 63 - Algorithm Collections for Digital Signal Processing Applications using MATLAB
P. 63
1. Artificial Intelligence 51
matB=diag([ones(1,9)*9]);
%Initializing the orders selected by 4 Ants
[p1,ANT1]=sort(rand(1,8));
[p2,ANT2]=sort(rand(1,8));
[p3,ANT3]=sort(rand(1,8));
[p4,ANT4]=sort(rand(1,8));
%Intializing the Pheromone matrix
%columns are the positions
%rows are the value of the position
ANTS=[ANT1;ANT2;ANT3;ANT4];
P=ones(8,8);
col=[];
figure
hold
for i=1:1:50
%Updating pheromones
c1=computecost(ANTS(1,:),matA,matB);
c2=computecost(ANTS(2,:),matA,matB);
c3=computecost(ANTS(3,:),matA,matB);
c4=computecost(ANTS(4,:),matA,matB);
col=[col max([c1 c2 c3 c4])];
plot(col)
pause(0.01)
c=[c1 c2 c3 c4];
for i=1:1:8
for j=1:1:8
[x,y] = find(ANTS(:,i)==j);
for k=1:1:length(x)
P(j,i)=P(j,i)+(1/c(x(k)));
end
end
end
ANTS=[];
for i=1:1:4
P_cur=P;
tempant=zeros(1,8);
notation=[1 2 3 4 5 6 7 8];
[p,position]=sort(rand(1,8));