test
This commit is contained in:
23
calculateDistanceMatrix.m
Normal file
23
calculateDistanceMatrix.m
Normal file
@@ -0,0 +1,23 @@
|
||||
function adjacencyMatrix = calculateDistanceMatrix(positions)
|
||||
% 计算任意两个城市之间的直线距离,返回加权邻接矩阵
|
||||
% 输入:positions - 包含城市坐标的矩阵 (n行3列)
|
||||
% 输出:adjacencyMatrix - n×n的加权邻接矩阵
|
||||
|
||||
% 获取城市数量
|
||||
numCities = size(positions, 1);
|
||||
|
||||
% 初始化邻接矩阵
|
||||
adjacencyMatrix = zeros(numCities, numCities);
|
||||
|
||||
% 计算每对城市之间的欧几里得距离
|
||||
for i = 1:numCities
|
||||
for j = 1:numCities
|
||||
if i ~= j
|
||||
% 计算欧几里得距离
|
||||
dx = positions(i, 2) - positions(j, 2);
|
||||
dy = positions(i, 3) - positions(j, 3);
|
||||
adjacencyMatrix(i, j) = sqrt(dx^2 + dy^2);
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
24
calculateTourDistance.m
Normal file
24
calculateTourDistance.m
Normal 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
|
22
main.m
Normal file
22
main.m
Normal file
@@ -0,0 +1,22 @@
|
||||
% 加载数据
|
||||
load('postion_att48.mat');
|
||||
% 假设加载的变量名为positions,如果实际变量名不同,请相应调整
|
||||
|
||||
% 计算城市间距离矩阵
|
||||
distMatrix = calculateDistanceMatrix(postion);
|
||||
|
||||
% 输出距离矩阵
|
||||
disp('城市间距离矩阵:');
|
||||
disp(distMatrix);
|
||||
|
||||
% 选择起始城市(这里选择城市1)
|
||||
startCity = 1;
|
||||
|
||||
% 生成遍历路径并计算总距离
|
||||
[route, totalDistance] = calculateTourDistance(distMatrix, startCity);
|
||||
|
||||
% 输出总距离
|
||||
fprintf('从城市%d出发的遍历路径总距离: %.2f\n', startCity, totalDistance);
|
||||
|
||||
% 绘制遍历路径
|
||||
plotTour(postion, route);
|
40
plotTour.m
Normal file
40
plotTour.m
Normal file
@@ -0,0 +1,40 @@
|
||||
function plotTour(positions, route)
|
||||
% 绘制遍历路径的示意图
|
||||
% 输入:positions - 包含城市坐标的矩阵
|
||||
% route - 遍历路径
|
||||
|
||||
% 创建新图形窗口
|
||||
figure;
|
||||
|
||||
% 绘制所有城市点
|
||||
plot(positions(:, 2), positions(:, 3), 'o', 'MarkerSize', 8, 'MarkerFaceColor', 'blue');
|
||||
hold on;
|
||||
|
||||
% 绘制路径连线
|
||||
for i = 1:length(route)-1
|
||||
current = route(i);
|
||||
next = route(i+1);
|
||||
|
||||
% 绘制从当前城市到下一个城市的线
|
||||
line([positions(current, 2), positions(next, 2)], ...
|
||||
[positions(current, 3), positions(next, 3)], ...
|
||||
'Color', 'red', 'LineWidth', 1.5);
|
||||
end
|
||||
|
||||
% 标记起点和终点(相同城市)
|
||||
plot(positions(route(1), 2), positions(route(1), 3), 'gs', 'MarkerSize', 12, 'MarkerFaceColor', 'green');
|
||||
|
||||
% 添加城市标签
|
||||
for i = 1:size(positions, 1)
|
||||
text(positions(i, 2), positions(i, 3), num2str(i), ...
|
||||
'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');
|
||||
end
|
||||
|
||||
% 添加图例和标题
|
||||
xlabel('X坐标');
|
||||
ylabel('Y坐标');
|
||||
title('城市遍历路径');
|
||||
grid on;
|
||||
legend('城市', '路径', '起点/终点');
|
||||
hold off;
|
||||
end
|
BIN
postion_att48.mat
Normal file
BIN
postion_att48.mat
Normal file
Binary file not shown.
Reference in New Issue
Block a user