Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. tours = 5;
  2. alpha = 1;
  3. beta = 5;
  4. ro = 0.5; %evaporation coeff
  5. %cities 3
  6. x = [0 3 6 7 15 10 16 5 8 1.5];
  7. y = [1 2 1 4.5 -1 2.5 11 6 9 12];
  8. N = 10; %number of cities
  9. %number of ants is equal to number of cities
  10. m = N;
  11. %xy = [3,1; 2,4; 12,2; 7,4.5; 9,9; 3,1.5; 16,11; 11,8; 9,10;2,7];
  12. %distance between cities
  13. D = zeros(N);
  14. for i=1:N
  15. for j=1:N
  16. D(i,j) = pdist([x(i),y(i); x(j),y(j)]);
  17. end
  18. end
  19. %display(D);
  20. tau0 = 1/(min(max(D))); %initial pheromones
  21. tau = zeros(N);
  22. for i=1:N
  23. for j=1:N
  24. if(i~=j) %if not the same city
  25. tau(i,j) = tau0;
  26. end
  27. end
  28. end
  29.  
  30. for t=1:tours
  31. %creating ants
  32. ant = zeros(m,3+N); %starting city = 1, current city = 2, distance traveled = 3
  33. for i=1:m
  34. ant(i,1) = randi([1 10],1,1); %choosing starting city
  35. ant(i,2) = ant(i,1); %ant is in the same city
  36. ant(i,3+ant(i, 1)) = 1; %first city in sequence
  37. end
  38. dtau = zeros(N); %increase of pheromone
  39. for iant=1:m
  40. for icity=2:N+1
  41. if(icity<N+1)
  42. numerator = zeros(N);
  43. denominator = zeros(N,1);
  44. A = zeros(N);
  45. for city=1:N
  46. for dest=1:N
  47. if(city==dest)
  48. numerator(city,dest) = 0; %can't go to city you're in
  49. else
  50. if(ant(iant,3+dest)>0)
  51. numerator(city,dest) = 0; %can't go to city you've been
  52. else
  53. numerator(city,dest) =(tau(city,dest)^alpha)*((1/D(city,dest))^beta);
  54. denominator(city) =denominator(city)+numerator(city,dest);
  55. end
  56. end
  57. end
  58. end
  59. for city=1:N
  60. for dest=1:N
  61. if(numerator(city,dest)==0)
  62. A(city,dest)=0;
  63. else
  64. A(city,dest)=numerator(city,dest)/denominator(city);
  65. end
  66. end
  67. end
  68.  
  69. p = zeros(N);
  70. pd = zeros(N,1);
  71. %probability matrix
  72. for city=1:N
  73. for dest=1:N
  74. if(ant(iant,3+dest)==0) %if city not yet visited
  75. pd(city)=pd(city)+A(city,dest);
  76. %denominator sum for probability
  77. end
  78. end
  79. end
  80. for city=1:N
  81. for dest=1:N
  82. p(city,dest)=A(city,dest)/pd(city);
  83. %probability
  84. end
  85. end
  86. %choosing destination
  87. choice = 0;
  88. prob = 0;
  89.  
  90. for dest=1:N
  91. if(ant(iant,3+dest)==0) %if city not visited
  92. if(choice==0)
  93. prob = prob + p(ant(iant,2),dest);
  94. %probability of current -> dest
  95. if(prob>rand
  96. choice = dest; %chosen destination
  97.  
  98. break
  99. end
  100. end
  101. end
  102. end
  103. dest=choice;
  104. else
  105. dest=ant(iant,1); %no cities left, go home
  106. end
  107. ant(iant,3) = ant(iant,3) + D(ant(iant,2),dest);
  108. %distant increases
  109. ant(iant,2) = dest; %new current city
  110. if(icity < 11) %end of journey, return to starting city
  111. ant(iant, 3+dest) = icity;
  112. end
  113. end
  114. %pheromone trail
  115. for city=1:N
  116. for dest=1:N
  117. if(city~=dest)
  118. if(ant(iant, 3+dest)-ant(iant, 3+city)==1)
  119. dtau(city, dest) = dtau(city, dest) + (1/ant(iant, 3));
  120. dtau(dest, city) = dtau(dest, city) + (1/ant(iant, 3)); %direction doesn't matter
  121. end %calc of pheromone increase after total distance for ant is known
  122. end
  123. end
  124. end
  125. end
  126. %pheromone update
  127. for city=1:N
  128. for dest=1:N
  129. tau(city, dest) = (1 - ro) * tau(city, dest) + dtau(city, dest);
  130. end
  131. end
  132. end
  133.  
  134. %mrufka miszcz
  135. best = 1;
  136. for iant=1:m
  137. if(ant(iant, 3)<ant(best, 3))
  138. best = iant;
  139. end
  140. end
  141.  
  142. %result
  143. %ant(best, :)
  144. distance = ant(best, 3);
  145. x1 = zeros(11, 1);
  146. y1 = zeros(11, 1);
  147. sequence = zeros(1, 10);
  148. for city=1:N
  149. for i=4:13
  150. if(ant(best, i)==city)
  151. sequence(city) = i-3;
  152. x1(city) = x(i-3);
  153. y1(city) = y(i-3);
  154. end
  155. end
  156. end
  157.  
  158. x1(11) = x1(1); %back to starting city
  159. y1(11) = y1(1);
  160. sequence
  161. distance
  162. %plot
  163. hold on
  164. scatter(x, y, 'm')
  165. a = [1:10]'; %labels
  166. b = num2str(a);
  167. c = cellstr(b);
  168. text(x+0.1, y+0.1, c); %0.1 not to overlap
  169. plot(x1, y1, 'c');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement