Files
MATLAB3/calculateTourDistance.m

24 lines
898 B
Mathematica
Raw Normal View History

2025-03-26 20:08:20 +08:00
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