This commit is contained in:
2025-03-26 20:08:20 +08:00
commit f868afc84c
5 changed files with 109 additions and 0 deletions

24
calculateTourDistance.m Normal file
View File

@@ -0,0 +1,24 @@
function [route, totalDistance] = calculateTourDistance(adjacencyMatrix, startCity)
%
% adjacencyMatrix -
% startCity -
% route -
% totalDistance -
numCities = size(adjacencyMatrix, 1);
%
otherCities = setdiff(1:numCities, startCity);
%
otherCities = otherCities(randperm(length(otherCities)));
%
route = [startCity, otherCities, startCity];
%
totalDistance = 0;
for i = 1:length(route)-1
totalDistance = totalDistance + adjacencyMatrix(route(i), route(i+1));
end
end