MATLAB实现洪泛路由的模拟

MATLAB实现洪泛路由的模拟,采用DFS算法,直接上代码。

flooding.m

clear;clc;
%global ttl;
ttl=7;%设置最大传输跳数为7
%% 初始化无线传感网示意图
%.传感器节点区域界限
envSize=10;
%区域内传感器数量
global numOfNodes;
numOfNodes=100;
global radius;%作用范围
radius=1.5;
global xLocation;   
global yLocation;   
xLocation = rand(numOfNodes,1) * envSize;
yLocation = rand(numOfNodes,1) * envSize;   %x,y坐标
global srcx;
global destx;
srcx = floor(rand(1,1) * numOfNodes)+1; %第srcx个节点作为源节点
destx = floor(rand(1,1) * numOfNodes)+1;%第destx个节点作为目的节点
global success;%标记是否成功访问到目的节点
success=0;
global visited;%标记节点是否被访问过
visited=zeros(1,100);%初始时都未被访问
global route;%记录传输路径
route=[];
%% 画图
figure(1);hold on;
plot(xLocation, yLocation, '.');
plot(xLocation(srcx),yLocation(srcx),'ko','Markerfacecolor','k','MarkerSize',3);
plot(xLocation(destx),yLocation(destx),'ko','Markerfacecolor','k','MarkerSize',3);
text(xLocation(srcx),yLocation(srcx), '源节点');
text(xLocation(destx),yLocation(destx), '目的节点');%标记源节点和目的节点
src=[xLocation(srcx),yLocation(srcx)]; dest=[xLocation(destx),yLocation(destx)];
%% 建立距离矩阵
global distMatrix;
distMatrix = zeros(numOfNodes);
for i=1:numOfNodes
    for j=i+1:numOfNodes
        distMatrix(i,j)=distance([xLocation(i),yLocation(i)],[xLocation(j),yLocation(j)]);
    end
    distMatrix(i,i)=10;%排除自身节点
end
distMatrix = distMatrix+distMatrix';
%% 开始查找
DFS(srcx,ttl);
%% 画出传输路径图
if(ismember(destx,route))
   disp('传输成功')
   j=length(route);
    while(j~=1)
        for i=1:j-1
            if(distMatrix(route(i),route(j))<=radius&&i~=j-1)
                route(i+1:1:j-1)=[];
            break;
            end
        end
        j=i;
    end
else
    route=[];
    disp('传输失败');
end
plot(xLocation(route),yLocation(route),'ro','Markerfacecolor','r','MarkerSize',3);

DFS.m

function [] = DFS(r,ttl)% r:源节点  TTL:生存周期
%% 
global success;
global visited;%标记节点是否被访问过
global distMatrix;%节点间的距离矩阵
global destx;%目的节点
global radius;%作用范围
global route;%记录传输路径
visited(r)=1;
if(success==1||ttl<0)
     return;
end
if(~ismember(r,route))
    route=[route,r];
else
    temp=find(route==r);
    route=[route,r];
    route(temp+1:1:end)=[];
end
neighborNodes=find(distMatrix(r,:)<=radius);%查找在作用范围内的邻节点
neighborNodes=intersect(neighborNodes, find(visited(:) == 0));%找到可访问的 neighborNodes是列向量
if(ismember(destx,neighborNodes))%如果邻节点中含有目的节点
    success=1;
    route=[route destx];
    return;
end
%% 递归程序的出口
if (isempty(neighborNodes))%邻节点为空
    route(end)=[];%删除当前源节点,返回上一层
    %route=[];
    return;
end
%% 递归--当前节点的邻节点中不含有目的节点,从其邻节点开始查找
ttl=ttl-1;
for k = 1:length(neighborNodes)
   % if (visited(neighborNodes(k)) == 0) 
    if(neighborNodes(k)==destx)%当传给中心点时结束
        success=1;
        break;
    end;
       DFS(neighborNodes(k),ttl);        
    %end;
end;

distance.m

%% 求两个节点之间的距离
function dis = distance(A,B)
dis = sqrt((A(1)-B(1))^2+(A(2)-B(2))^2);
end

传输成功后的示意图:红色节点表示传输路径,源节点和目的节点都是随机生成的

输出失败后的示意图:只标记出了源节点和目的节点,没有传输路径

 

Inputs: [AorV] Either A or V where A is a NxN adjacency matrix, where A(I,J) is nonzero if and only if an edge connects point I to point J NOTE: Works for both symmetric and asymmetric A V is a Nx2 (or Nx3) matrix of x,y,(z) coordinates [xyCorE] Either xy or C or E (or E3) where xy is a Nx2 (or Nx3) matrix of x,y,(z) coordinates (equivalent to V) NOTE: only valid with A as the first input C is a NxN cost (perhaps distance) matrix, where C(I,J) contains the value of the cost to move from point I to point J NOTE: only valid with A as the first input E is a Px2 matrix containing a list of edge connections NOTE: only valid with V as the first input E3 is a Px3 matrix containing a list of edge connections in the first two columns and edge weights in the third column NOTE: only valid with V as the first input [SID] (optional) 1xL vector of starting points. If unspecified, the algorithm will calculate the minimal path from all N points to the finish point(s) (automatically sets SID = 1:N) [FID] (optional) 1xM vector of finish points. If unspecified, the algorithm will calculate the minimal path from the starting point(s) to all N points (automatically sets FID = 1:N) Outputs: [costs] is an LxM matrix of minimum cost values for the minimal paths [paths] is an LxM cell containing the shortest path arrays [showWaitbar] (optional) a scalar logical that initializes a waitbar if nonzero Note: If the inputs are [A,xy] or [V,E], the cost is assumed to be (and is calculated as) the point to point Euclidean distance If the inputs are [A,C] or [V,E3], the cost is obtained from either the C matrix or from the edge weights in the 3rd column of E3 Example: % Calculate the (all pairs) shortest distances and paths using [A,C] inputs n = 7; A = zeros(n); xy = 10*rand(n,2) tri = delaunay(xy(:,1),xy(:,2)); I = tri(:); J = tri(:,[2 3 1]); J = J(:); IJ = I + n*(J-1); A(IJ) = 1 a = (1:n); b = a(ones(n,1),:); C = round(reshape(sqrt(sum((xy(b,:) - xy(b',:)).^2,2)),n,n)) [costs,paths] = dijkstra(A,C) Example: % Calculate the shortest distance and path from point 3 to 5 n = 15; A = zeros(n); xy = 10*rand(n,2) tri = delaunay(xy(:,1),xy(:,2)); I = tri(:); J = tri(:,[2 3 1]); J = J(:); IJ = I + n*(J-1); A(IJ) = 1 [cost,path] = dijkstra(A,xy,3,5) gplot(A,xy,'b.:'); hold on; plot(xy(path,1),xy(path,2),'ro-','LineWidth',2) for k = 1:n, text(xy(k,1),xy(k,2),[' ' num2str(k)],'Color','k'); end Example: % Calculate the shortest distances and paths from the 3rd point to all the rest n = 7; V = 10*rand(n,2) I = delaunay(V(:,1),V(:,2)); J = I(:,[2 3 1]); E = [I(:) J(:)] [costs,paths] = dijkstra(V,E,3) Example: % Calculate the shortest distance and path from points [1 3 4] to [2 3 5 7] n = 7; V = 10*rand(n,2) I = delaunay(V(:,1),V(:,2)); J = I(:,[2 3 1]); E = [I(:) J(:)] [costs,paths] = dijkstra(V,E,[1 3 4],[2 3 5 7]) Revision Notes: (3/13/15) Previously, this code ignored edges that have a cost of zero, potentially producing an incorrect result when such a condition exists. I have solved this issue by using NaNs in the table rather than a sparse matrix of zeros.
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值