From f868afc84c9d7981333b6ce715b24efbd4ab9624 Mon Sep 17 00:00:00 2001 From: qq1244 <1244154570@qq.com> Date: Wed, 26 Mar 2025 20:08:20 +0800 Subject: [PATCH] test --- calculateDistanceMatrix.m | 23 ++++++++++++++++++++++ calculateTourDistance.m | 24 +++++++++++++++++++++++ main.m | 22 +++++++++++++++++++++ plotTour.m | 40 ++++++++++++++++++++++++++++++++++++++ postion_att48.mat | Bin 0 -> 973 bytes 5 files changed, 109 insertions(+) create mode 100644 calculateDistanceMatrix.m create mode 100644 calculateTourDistance.m create mode 100644 main.m create mode 100644 plotTour.m create mode 100644 postion_att48.mat diff --git a/calculateDistanceMatrix.m b/calculateDistanceMatrix.m new file mode 100644 index 0000000..ae278d0 --- /dev/null +++ b/calculateDistanceMatrix.m @@ -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 \ No newline at end of file diff --git a/calculateTourDistance.m b/calculateTourDistance.m new file mode 100644 index 0000000..9d91dcc --- /dev/null +++ b/calculateTourDistance.m @@ -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 \ No newline at end of file diff --git a/main.m b/main.m new file mode 100644 index 0000000..3f6191f --- /dev/null +++ b/main.m @@ -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); \ No newline at end of file diff --git a/plotTour.m b/plotTour.m new file mode 100644 index 0000000..05b23e1 --- /dev/null +++ b/plotTour.m @@ -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 \ No newline at end of file diff --git a/postion_att48.mat b/postion_att48.mat new file mode 100644 index 0000000000000000000000000000000000000000..49a1ef25c97c2f52fe3862383e4f4d5e62b174c0 GIT binary patch literal 973 zcmeZu4DoSvQZUssQ1EpO(M`+DN!3vZ$Vn_o%P-2cQV4Jk_w+L}(NS#e?ONPk^1au`su?v^6t>LNUV+kRDCA9s^W8!ZXs+JkDsdZJH^; zGec5>K~b9V0bxs+K$chw8*^(*D+4W=#qblP$Bz-@Fx_5z52i$kWAb13SnvP+?u*~s zFT2j0oZj@`**m&dSJd%oKyzcWpk`*Xh>3(Rt7(XWsp*xZ3u&g;u3b>@)m^LSsOcCz z{|dMLyyvy^+2eQ}PBC`Gr2fgQzVrXL#POpKMLp)!tP42v%uJy`zI4lbzj@qy^gqS4 zv9oY#gk*d;#4gMBheM%*Zw7}V5BJ;+2L&9(7j-O@F8s1Uob$^s`a_t7E_wD#FuL!=hGoj7~^PKaaF?(2@PjR@y<>u_Ws{7EsKh8Yo+gInwR7Icb`sXQS;+)@-{WCE8mH(zyIw#pX zIt%}c>Q0J&I&YrW{waxejG#~rgM@0Itqyad%(3~|>&u_-{l0E*_Ul{4`CGkKzF){J zy_j|44+)N`B`Z42qMV(RR?P5_n7}c)aj8(W;U-C*-jhjhl|Bk~p4I*{IewnaPkaAQ zFBrsF93HKD;9r0K^ZVbHd3^m2kDnB`CYfzFny)7o%zUSN&WaDe%RkOJQ7x2mFNdL# z>y)+Co(~5bIPIA@772941$Mrb*$-Sjb~m-SN6+45&vn@y)J4PAaj z>k8lDEe7v=T;k>k=NgB6%0E$=K1opK=HW*7t&bFMeD2vXeW_})_kWS+DULstnqO)C zoN)c*%4`jVm#mxDsczLQjQ**Xc6jyfw0BkZvB!@|ue{8&S44k`_20ug!c9t}ZaVsO U{XU;LF=nOtmw