Advertisement
Guest User

Untitled

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