Guest User

Untitled

a guest
Apr 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. clear all
  2. clc
  3.  
  4. % Choose between two input possibilities: Coordinates in Excel file
  5. % 'Test'from different cities or insert distance matrix direct here in script
  6.  
  7. % %%%Input coordinates of the cities via Excel file
  8. % data = xlsread('Test.xlsx',1)
  9. % dist = dist(data(:,2:5)') % how many coordinates are necessary to describe the location from one city
  10. % dist(dist==0) = inf
  11.  
  12. %%%Input directly distance matrix
  13. % Input information
  14. A = [inf, 4, 12, 7, 4, 1, 2, 8, 4, 6, 7, 12, 3, 5, 20];
  15. B = [5, inf, 20, 18, 6, 3, 4, 8, 9, 23, 1, 12, 5, 4, 9];
  16. C = [11, 5, inf, 6, 1, 5, 6, 8, 7, 12, 31, 5, 1, 14, 5];
  17. D = [10, 2, 3, inf, 5, 7, 8, 8, 6, 21, 4, 7, 5, 5, 1];
  18. E = [1, 2, 6, 9, inf, 9, 10, 8, 45, 12, 11, 5, 6, 2, 11];
  19. F = [17, 7, 6, 5, 11, inf, 12, 8, 63, 1, 2, 3, 12, 4, 8];
  20. G = [11, 8, 3, 9, 3, 5, inf, 4, 6, 8, 1, 3, 12, 4, 8];
  21. H = [12, 14, 2, 5, 4, 8, 4, inf, 12, 4, 1, 23, 7, 4, 56];
  22. I = [4, 1, 2, 8, 4, 6, 7, 12, 3, inf, 7, 5, 6, 12, 14];
  23. J = [6, 1, 5, 6, 8, 7, 12, 31, 5, 1, inf, 12, 1, 2, 6];
  24. K = [7, 8, 8, 6, 21, 4, 7, 5, 5, 1, 11, inf, 13, 1, 2];
  25. L = [1, 6, 1, 5, 6, 8, 7, 12, 8, 9, 10, 12, inf, 7, 5];
  26. M = [8, 4, 6, 7, 12, 3, 8, 4, 6, 7, 12, 3, inf, 14, 15];
  27. N = [21, 4, 7, 5, 5, 1, 21, 4, 7, 5, 5, 1,13, inf, 1];
  28. O = [4, 12, 7, 4, 1, 2, 8, 4, 6, 7, 12, 3, 5, 20, inf];
  29. % Generate distance matrix
  30. dist = [A; B; C; D; E; F; G; H; I; J; K; L; M; N; O]
  31.  
  32. % Initalization
  33. MaxCity = length(dist);
  34. i = 1;
  35. n = 1;
  36. Parentcity = 1;
  37. Childcity = 0;
  38.  
  39. % Array
  40. costarray = zeros(MaxCity, 2);
  41. wayarray = zeros(MaxCity, 2);
  42. wayarray(1,:)=[1 0]
  43.  
  44. % Reduktion und Kosten der ParentCity
  45. row_reduction = min(dist, [], 2);
  46. row_reduction(isinf(row_reduction)) = 0;
  47. M_row_reduced = dist - row_reduction;
  48.  
  49. column_reduction = min(M_row_reduced);
  50. M_working = M_row_reduced - column_reduction;
  51. M_working(isnan(M_working)) = inf;
  52. M_reduced = M_working;
  53. costparent = sum(row_reduction) + sum(column_reduction);
  54.  
  55. for n = [n:1:MaxCity] % levels der baumstruktur
  56.  
  57. for i = [i:1:MaxCity]; % childcity i= childcity
  58. if i == Parentcity ||i==1
  59. cost = inf;
  60.  
  61. else
  62. M_working= M_reduced;
  63. M_working(Parentcity, :) = inf;
  64. M_working(:, i) = inf;
  65. M_working(i, Parentcity) = inf;
  66. M_working(Parentcity,i) = 0; %!!!!
  67.  
  68. row_reduction = min(M_working, [], 2);
  69. row_reduction(isinf(row_reduction)) = 0;
  70. M_row_reduced = M_working - row_reduction;
  71.  
  72. column_reduction = min(M_row_reduced);
  73. M_working = M_row_reduced - column_reduction;
  74. M_working(isnan(M_working)) = inf;
  75.  
  76.  
  77. cost = costparent+ sum(row_reduction) + sum(column_reduction)+ M_reduced(Parentcity, i);
  78. end
  79. costarray(i, :) = [i,cost];
  80. end
  81. i = 1;
  82.  
  83. % minimum der cost array finden
  84. [M, I] = min(costarray);
  85. childcity = I(1, 2);
  86. costparent = M(1, 2);
  87. wayarray(n +1, :) = [childcity, dist(wayarray(n,1),childcity)]; %änderung!!
  88.  
  89. % nimm childcity, reduziere matrix für diese
  90. M_working=M_reduced;
  91. M_working(Parentcity, childcity) = inf;
  92. M_working(:, childcity) = inf;
  93. M_working(childcity, Parentcity) = inf;
  94. M_working(Parentcity,childcity) = 0; %!!!!
  95.  
  96. row_reduction = min(M_working, [], 2);
  97. row_reduction(isinf(row_reduction)) = 0;
  98. M_row_reduced = M_working - row_reduction;
  99.  
  100. column_reduction = min(M_row_reduced);
  101. M_working = M_row_reduced - column_reduction;
  102. M_working(isnan(M_working)) = inf;
  103.  
  104. M_reduced=M_working;
  105.  
  106. Parentcity = childcity;
  107. end
  108. wayarray(n+1,2)=dist(wayarray(n,1),1)
  109. gesamtlaenge=sum(wayarray(:,2))
Add Comment
Please, Sign In to add comment