Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. % cities2
  2. x = [0 2 6 7 15 12 14 9.5 7.5 0.5];
  3. y = [1 3 5 2.5 -0.5 3.5 10 7.5 9 10];
  4.  
  5. evaporation = 0.1;
  6. tau0 = 0.1;
  7. alpha = 1;
  8. beta = 5;
  9. ro = 0.5;
  10. m = 10; % number of ants
  11. n = m; % number of towns equal to the number of ants
  12. Tmax = 200; % number of tours (cycles)
  13.  
  14. mostestShortest = inf; % best path
  15. % Pheromone matrix with tau0 on every path
  16. T(1:n, 1:n) = tau0;
  17. T = T - diag(diag(T));
  18. D = zeros(n, n); % distances between towns
  19. L = zeros(m, 1); % length of each ants path
  20.  
  21. % distance between towns
  22. for i=1:n
  23. for j=1:n
  24. D(i,j) = pdist([x(i),y(i); x(j),y(j)]);
  25. end
  26. end
  27.  
  28. for i=1:Tmax
  29. %towns each ant has visited
  30. C = zeros(m, n);
  31. %choose a random initial town for each ant
  32. for k=1:m
  33. C(k, 1) = randi(n);
  34. end
  35. for a=1:m
  36. for p=2:n
  37. town = C(a,(find(C(a,1:n), 1, 'last'))); % find in which town the ant is right now, which is the last non-zero value
  38. remTowns = 1:size(T, 1); % vector of towns the ant can still go to
  39. remTowns = setdiff(remTowns, C(a,1:n)); % difference of those two vectors
  40. A = zeros(n); % decision table
  41. %Calculate sum (denominator)
  42. denom = 0;
  43. for j=1:size(remTowns, 2)
  44. denom = denom + (T(town, remTowns(j))^alpha)*(1/D(town,remTowns(j))^beta);
  45. end
  46. % find the decision vector A for this ant at this moment
  47. % assign a value to every available town
  48. for j=1:size(remTowns, 2)
  49. A(remTowns(j)) = ((T(town,remTowns(j))^alpha)*(1/D(town, remTowns(j))^beta))/denom;
  50. end
  51. %probability for choosing each path
  52. Aprob = A/sum(A);
  53. % choose one path
  54. C(a,p)= randsample( 1:n, 1, true, Aprob );
  55. end
  56. %Calculates the length of this tour
  57. for z=1:n-1
  58. L(a) = L(a) + D(C(a,z), C(a,z+1));
  59. if(z==n-1)
  60. L(a) = L(a) + D(C(a,z), C(a,1));
  61. end
  62. end
  63. end
  64. %Finds the optimal tour in this cycle
  65. [shortestLength, minI] = min(L);
  66. %check if the optimal tour in this cycle is also the best overall
  67. if shortestLength < mostestShortest
  68. mostestShortest = shortestLength;
  69. bestPath = C(minI, :);
  70. bestPath(n+1) = bestPath(1);
  71. end
  72. tauD = zeros(n);
  73.  
  74. %for every ant
  75. for a=1:m
  76. %for every arc in the path
  77. for p=1:n-1
  78. %how much pheromone is added to this arc
  79. tauD(C(a,p), C(a,p+1)) = tauD(C(a,p), C(a,p+1)) + 1/L(a);
  80. end
  81. %ant returns to the initial town
  82. tauD(C(a,n), C(a,1)) = tauD(C(a,n), C(a,1)) + 1/L(a);
  83. end
  84.  
  85. %update the pheromones
  86. for p=1:n
  87. for k=1:n
  88. T(p, k) = (1-evaporation)*T(p,k) + tauD(p, k);
  89. end
  90. end
  91. end
  92.  
  93. bestPath
  94. mostestShortest
  95.  
  96. %plot
  97. hold on
  98. scatter(x, y, 'm')
  99. a = [1:10]'; %labels
  100. b = num2str(a);
  101. c = cellstr(b);
  102. text(x+0.1, y+0.1, c); %0.1 not to overlap
  103. plot(x(bestPath), y(bestPath), 'c');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement