Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- iteration=50;
- x = [0 3 6 7 15 12 14 9 7 0];
- y = [1 4 5 3 0 4 10 6 9 10];
- %x = [3 2 12 7 9 3 16 11 9 2];
- %y = [1 4 2 4.5 9 1.5 11 8 10 7];
- %x = [0 2 6 7 15 12 14 9.5 7.5 0.5];
- %y = [1 3 5 2.5 -0.5 3.5 10 7.5 9 10];
- alfa=1;
- beta=5;
- ro=0.5;
- round_iterator=1;
- delta_tau=zeros(10);
- N=10; %number of ants and cities
- distance=zeros(N+1,1);
- id=0;
- dist_between_cities = zeros (10, 10);
- for i = 1 : N
- for j = 1 : N
- dist_between_cities(i,j) = sqrt(((x(j) - x(i))^2 +(y(j)-y(i))^2));
- end
- end
- eta = 1./dist_between_cities;
- for i=1:N
- dist_between_cities(i, i) = 0;
- eta(i, i) = 0;
- end
- tau0=1/max(max(dist_between_cities));
- tau=ones.*tau0;
- random_cities=[zeros(10,9),randi([1,10],10,1)]; %random dividing the ants in the cities
- while round_iterator<iteration
- tau=(1-ro)*tau+delta_tau; % new tau (6)
- for i=1:N %aij matrix calculation (4)
- for j=1:N
- a(i,j)=(tau(i,j)^alfa)*(eta(i,j)^beta)/(sum((tau(i,:).^alfa).*(eta(i,:).^beta)));
- end
- end
- for i=1:N %calculating probablilty of chosen city (5)
- for j=1:N
- if(i==j) %if the same city
- probability(i,j)=0;
- else
- probability(i,j)=a(i,j)/(sum(a(i,:)));
- end
- end
- end
- %------------------------------------
- random_cities=[random_cities(:,10),zeros(10,9)];
- for random_city_column=1:N-1
- for k=1:N
- present_city=random_cities(k,random_city_column);
- previous_cities=random_cities(k,(1:random_city_column));
- temp=0;
- %pom=0;
- %for z=1:size(purpose_city,1)
- % pom= pom+probability(present_city,purpose_city(1,z));
- %end
- prob_of_move=(1-sum(probability(present_city,previous_cities)))*rand(1); %where the ant been
- for j=1:N
- if j~=previous_cities
- if random_city_column==N-1
- delta_tau(present_city,j)=delta_tau(present_city,j)+(1/dist_between_cities(present_city,j));
- random_cities(k,random_city_column+1)=j;
- break;
- else
- temp=temp+probability(present_city,j);
- end
- end
- if (prob_of_move<=temp)
- delta_tau(present_city,j)=delta_tau(present_city,j)+(1/dist_between_cities(present_city,j));
- random_cities(k,random_city_column+1)=j;
- break;
- end
- end
- end
- end
- %%%---------------------------------
- %the last row is the shortest path
- distance=zeros(N+1,1);
- %
- for i=1:N
- for j=1:9
- distance(i,1)=dist_between_cities(random_cities(i,j),random_cities(i,j+1))+distance(i,1);
- end
- if i==1
- distance(N+1,1)=distance(i,1);
- id=i;
- elseif distance(i,1)<distance(N+1,1)
- distance(N+1,1)=distance(i,1);
- id=i;
- end
- end
- distance(N+1,1)=distance(N+1,1)+dist_between_cities(random_cities(id,1),random_cities(id,10));
- solution(round_iterator,:)=[random_cities(id,:),distance(N+1,1)];
- round_iterator=round_iterator+1;
- end
- %%%---------------------------------
- sol = sortrows(solution,N+1);
- sol2 = zeros(1,12);
- sol2 = sol(1,1:10);
- sol2(1,11) = sol(1,1);
- sol2(1,12) = sol(1,11);
- %plot the road
- string={'1','2','3','4','5','6','7','8','9','10'};
- plot(x(1,sol2(1,1:N+1)),y(1,sol2(1,1:N+1)));
- text(x,y,string);
- fprintf('Distance=%dist \nand order of the cities:', sol2(1,N+2));
- disp(sol2(1,1:N+1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement