用Floyd算法解决选址问题(附完整matlab代码)
作者:互联网
以2021年五一建模比赛中的消防站选址为例,此问题为重心性加权最优选址问题。可考虑各区域的出警次数和人口密度,并对其进行加权分析处理。
首先用Floyd算法求出距离矩阵,在考虑出警次数和人口密度的前提下找出已知距离图的15个重心点,数值最小点即为新建消防站点。若要在2021-2029年每隔3年新建1个消防站,则根据所求的15个顶点从最优到次优进行排序选址。
直接上代码:
function [D,R] = floyd(A)
n=size(a,1);D=a;
for i=1:n
for j=1:n
R(i,j)=j;
end
end
for k=1:n
for i=1:n
for j=1:n
if D(i,k)+D(k,j)<D(i,j)
D(i,j)=D(i,k)+D(k,j);
R(i,j)=R(i,k);
end
end
end
end
上面是Floyd函数的代码,下面是构建矩阵图的MATLAB代码,数据参考五一建模的消防选址题目。
a = [0 11.1 inf 11.4 inf inf inf inf inf inf inf inf 8.2 inf inf ;
11.1 0 8.2 12.8 inf inf inf inf inf inf inf inf inf inf inf;
inf 8.2 0 7.7 11.1 9.4 inf inf inf inf inf inf inf inf inf ;
11.4 12.8 7.7 0 inf 6.9 inf inf inf 12.7 inf inf 14.3 10 8.5 ;
inf inf 11.1 inf 0 7.4 inf inf inf inf inf inf inf inf inf;
inf inf 9.4 6.9 7.4 0 inf inf inf inf inf inf inf inf inf ;
inf inf inf inf inf inf 0 inf inf 12.9 13.4 14.5 inf 10.6 inf;
inf inf inf inf inf inf inf 0 9 inf inf 12.3 inf inf inf ;
inf inf inf inf inf inf inf 9 0 inf inf inf inf inf inf ;
inf inf inf 12.7 inf inf 12.9 inf inf 0 9.5 inf 9.6 6.9 4.2 ;
inf inf inf inf inf inf 13.4 inf inf 9.5 0 4.4 15 inf inf ;
inf inf inf inf inf inf 14.5 12.3 inf inf 4.4 0 inf inf inf ;
8.2 inf inf 14.3 inf inf inf inf inf 9.6 15 inf 0 inf inf ;
inf inf inf 10 inf 11.2 10.6 inf inf 6.9 inf inf inf 0 5.9 ;
inf inf inf 8.5 inf inf inf inf inf 4.2 inf inf inf 5.9 0];
q=[0.0578 0.052 0.0436 0.061 0.0745 0.0593 0.0607 0.0467 0.0405 0.0575 0.0573 0.0364 0.0702 0.0551 1.0256]
[D,R] =floyd(a)
s=q*D
标签:8.2,15,11.1,Floyd,matlab,选址,inf,6.9 来源: https://blog.csdn.net/m0_58024423/article/details/120659903