资源简介
DVHOP的MATLAB仿真代码,用于DVHOP路由算法的仿真,用于无线网络实验。
代码片段和文件信息
%~~~~~~~~~~ DV-Hop算法 ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~
% BorderLength-----正方形区域的边长,单位:m
% NodeAmount-------网络节点的个数
% BeaconAmount---信标节点数
% Sxy--------------用于存储节点的序号,横坐标,纵坐标的矩阵
%Beacon----------信标节点坐标矩阵;BeaconAmount*BeaconAmount
%UN-------------未知节点坐标矩阵;2*UNAmount
% Distance------未知节点到信标节点距离矩阵;2*BeaconAmount
%h---------------节点间初始跳数矩阵
%X---------------节点估计坐标初始矩阵X=[xy]‘
% R------------------节点的通信距离,一般为10-100m
clearclose all;
BorderLength=100;
NodeAmount=100;
BeaconAmount=8;
UNAmount=NodeAmount-BeaconAmount;
R=50;
% D=zeros(NodeAmountNodeAmount);%未知节电到信标节点距离初始矩阵;BeaconAmount行NodeAmount列
h=zeros(NodeAmountNodeAmount);%初始跳数为0;BeaconAmount行NodeAmount列
X=zeros(2UNAmount);%节点估计坐标初始矩阵
%~~~~~~~~~在正方形区域内产生均匀分布的随机拓扑~~~~~~~~~~~~~~~~~~~~
C=BorderLength.*rand(2NodeAmount);
%带逻辑号的节点坐标
Sxy=[[1:NodeAmount];C];
Beacon=[Sxy(21:BeaconAmount);Sxy(31:BeaconAmount)];%信标节点坐标
UN=[Sxy(2(BeaconAmount+1):NodeAmount);Sxy(3(BeaconAmount+1):NodeAmount)];%未知节点坐标
%画出节点分布图
plot(Sxy(21:BeaconAmount)Sxy(31:BeaconAmount)‘r*‘Sxy(2(BeaconAmount+1):NodeAmount)Sxy(3(BeaconAmount+1):NodeAmount)‘k.‘)
%plot(Sxy(21:BeaconAmount)Sxy(31:BeaconAmount)‘r*‘)
%pause
%hold on
%plot(Sxy(2(BeaconAmount+1):NodeAmount)Sxy(3(BeaconAmount+1):NodeAmount)‘k.‘)
xlim([0BorderLength]);
ylim([0BorderLength]);
title(‘* 红色信标节点 . 黑色未知节点‘)
pause
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~初始化节点间距离、跳数矩阵~~~~~~~~~~~~~~~~~~~~~~
for i=1:NodeAmount
for j=1:NodeAmount
Dall(ij)=((Sxy(2i)-Sxy(2j))^2+(Sxy(3i)-Sxy(3j))^2)^0.5;%所有节点间相互距离
if (Dall(ij)<=R)&(Dall(ij)>0)
h(ij)=1;%初始跳数矩阵
elseif i==j
h(ij)=0;
else h(ij)=inf;
end
end
end
%~~~~~~~~~~~最短路经算法计算节点间跳数~~~~~~~~~~~~~~~~~~~~
for k=1:NodeAmount
for i=1:NodeAmount
for j=1:NodeAmount
if h(ik)+h(kj) h(ij)=h(ik)+h(kj);
end
end
end
end
h
%~~~~~~~~~~~~~求每个信标节点的校正值~~~~~~~~~~~~~~~~~~~~~~~~~~
h1=h(1:BeaconAmount1:BeaconAmount);
D1=Dall(1:BeaconAmount1:BeaconAmount);
for i=1:BeaconAmount
dhop(i1)=sum(D1(i:))/sum(h1(i:));%每个信标节点的平均每跳距离
end
D2=Dall(1:BeaconAmount(BeaconAmount+1):NodeAmount);%BeaconAmount行UNAmount列
for i=1:BeaconAmount
for j=1:UNAmount
if min(D2(:j))==D2(ij)
Dhop(1j)=dhop(i1);%未知节点从最近的信标获得校正值
end
%if min(D2(:j))==D2(ij)
% Dhop(1j)=D2(ij);%未知节点从最近的信标获得校正值
% end
end
end
Dhop
%~~~~~~~~~~~~~~~~用跳数估计距离~~~~~~~~~~~~~~~~~~~
hop1=h(1:BeaconAmount(BeaconAmount+1):NodeAmount)%未知节点到信标跳数,BeaconAmount行UNAmount列
for i=1:UNAmount
hop=Dhop(1i);%hop为从最近信标获得的校正值
Distance(:i)=hop*hop1(:i);%%Beacon行UN列;
end
% %~~~~~~~~~~~~~~~~~最小二乘法求未知点坐标~~~~~~~~~~~~~~~~~~~~~~~~
d=Distance;
for i=1:2
for j=1:(BeaconAmount-1)
a(ij)=Beacon(ij)-Beacon(iBeaconAmount);
end
end
A=-2*(a‘);
% d=d1‘;
for m=1:UNAmount
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3800 2018-05-08 18:33 DVHop.m
----------- --------- ---------- ----- ----
3800 1
- 上一篇:改进型的最大最小蚁群算法求解云计算虚拟机初始化放置问题
- 下一篇:直流电机 转矩控制
评论
共有 条评论