👨🎓个人主页:研学社的博客
💥💥💞💞欢迎来到本博客❤️❤️💥💥
🏆博主优势:🌞🌞🌞博客内容尽量做到思维缜密,逻辑清晰,为了方便读者。
⛳️座右铭:行百里者,半于九十。
📋📋📋本文目录如下:🎁🎁🎁
目录
本文用于 wsn 的 MATLAB WSN 代码查找两个节点之间的最短路径并发送数据。
它的 WSN 在节点之间具有范围容差(您可以选择范围 - 以米为单位)。一旦节点建立连接,适当的范围公差(位置在1km x 1km地形中随机排序)。最短路径 alg 获取路由并开始将数据从节点 1(源)发送到节点 2(目标),直到路由路径中涉及的任何节点节点因能量故障而死亡(路由中涉及的所有节点都会降低其能量 - 也是随机定义的)。路由失败后,它会获得下一个最短的路由并继续发送。依此类推,直到源和目标之间没有路径。
部分代码:
% Edges matrix definition due "range" variable value
dataset.weights = lt(dataset.euclidiana,dataset.range);
% Graph construction
G=graph(dataset.weights,'omitselfloops'); %Graph creation based on adjacency matrix (Edges matrix) built above
% Euclidean distance extraction for all existente end-to-end formed by
% "distance tolerance" (range variable value)
for a = 1 : height(G.Edges)
garbage.s = G.Edges.EndNodes(a,1);
garbage.t = G.Edges.EndNodes(a,2);
garbage.Z(a,:) = dataset.euclidiana(garbage.s,garbage.t);
end
G.Edges.Euclidiana = garbage.Z(:,1);
%Initial energy sortition (from 70% to 100% - minEnergy and maxEnergy variable valeu)
[dataset.nodePosition(:,4)] = dataset.maxEnergy -(dataset.maxEnergy-dataset.minEnergy)*rand(dataset.nodeNo,1);
dataset.nodePosition(1:2,4)=STenergy;
%All "G" (Graph object) based nodes degree to use as "node processing
%status overload" (more connections, busier!)
for a = 1: length(dataset.nodePosition(:,1))
dataset.nodePosition(a,5) = degree(G,dataset.nodePosition(a,1));
end
% Pathloss calc of each Edges based in a freespace (1.8 factor)
[G.Edges.Pathloss] = (10*dataset.atenuationFactor)*log10(G.Edges.Euclidiana);
%End points coordinates and energy migration to G object
for a = 1 : height(G.Edges)
garbage.Sourcenode = G.Edges.EndNodes(a,1);
garbage.Targetnode = G.Edges.EndNodes(a,2);
G.Edges.SourcenodeXpos(a) = dataset.nodePosition(garbage.Sourcenode,2);
G.Edges.SourcenodeYpos(a) = dataset.nodePosition(garbage.Sourcenode,3);
G.Edges.TargetnodeXpos(a) = dataset.nodePosition(garbage.Targetnode,2);
G.Edges.TargetnodeYpos(a) = dataset.nodePosition(garbage.Targetnode,3);
G.Edges.ActiveEdge(a) = 1;
end
% Graph objects plot
figure('units','normalized','innerposition',[0 0 1 1],'MenuBar','none')
subplot(1,1,1) %1,3,1 (Line number,collumn number, graph id) - if you want to show more than 1 graph in same windows
garbage.Xmax = 1500;
garbage.Xmin = 0;
garbage.Ymax = 1500;
garbage.Ymin = 0;
p = plot(G,'XData',(dataset.nodePosition(:,2)),'YData',(dataset.nodePosition(:,3)));
line(dataset.nodePosition(1:2,2),dataset.nodePosition(1:2,3),'color','green','marker','o','linestyle','none','markersize',50)
garbage.ax = gca;
garbage.ax.XAxis.TickValues = 0:100:1000;
garbage.ax.YAxis.TickValues = 0:100:1000;
grid on
hold on
title(['Original WSN | ','Nodes number: ',num2str(dataset.nodeNo),' | Nodes range: ', num2str(dataset.range)])
pause(2)
%Creation of some stuff needed
garbage.deadnodelist=[];
garbage.deadnodeneighbors=[];
%% finding routes
% Finding shortest path route
G2 = shortestpathtree(G,1,2);
%% Initialize patch existance test for loops
fileID = fopen('report-noACO-simulation.txt','w'); %Open file to collect data to external file
fprintf(fileID,'%6s %20s %20s %20s %20s\r\n','|NodeNo|','|No ACO Scene|','|Hops|','|Packets sent|','|Dead node|');
while ~isempty(G2.Edges)
G2 = shortestpathtree(G,1,2);
iterationcounter=iterationcounter+1;
% Test if there is connection between node 1 and 2. If not, terminate!
if isempty(G2.Edges)
break
end
部分理论来源于网络,如有侵权请联系删除。
致谢:cesar silva (2022).