资源简介
LEACH (Low2Energy Adaptive ClusteringHierarchy)算法是由MIT的Heinzelman等人提出的一种低功耗自适应分簇算法。其基本思想是以循环的方式随机选择簇头节点,将整个网络的能量负载均匀分配到网络中的每个传感器节点,从而达到降低网络能耗,提高网络生存周期的目的。
代码片段和文件信息
clear;
% profile clear;
% profile on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
w=8;
%Field Dimensions - x and y maximum (in meters)
xm = 200;
ym = 200;
maxBroad=sqrt(xm*xm+ym*ym)/4;
packetLength = 4000;%数据包长度
ctrPacketLength = 100;%控制包长度
%Initial Energy
Eo = 0.5;
%x and y Coordinates of the Sink
sink.x =100;
sink.y =250;
%Number of Nodes in the field
n=50*w;
%Optimal Election Probability of a node
%to become cluster head
T=0.1;
c3=0.1;
%R_c=(1-c*((d_max-d(s_iDS))/(d_max-d_min)))*R_c0;
R_c_0=90;
countCHs=0;%簇头个数
%Eelec=Etx=Erx
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
EDA=5*0.000000001;%Data Aggregation Energy
%Computation of do 一个距离的计算根据能量公式参考wendi的phd论文
do=sqrt(Efs/Emp);
%rcountCHs=rcountCHs+countCHs;
rmax=1000;
TD_MAX=140;
%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Creation of the random Sensor Network
%figure(1);
for i=1:1:n
S(i).xd=rand*xm;%坐标x,rand(11)产生一个0,1之间的随机数
XR(i)=S(i).xd;
S(i).yd=rand*ym;
YR(i)=S(i).yd;
S(i).CH=0; %初始化的时候,没有簇头产生的个数集合
S(i).type=‘N‘;%普通节点
S(i).E=Eo; %每个节点的能量保持为0.5
end
figure(1)
for i=1:1:n
plot(XRYR‘b*‘);
end
hold on;
BS_X=sink.x; %基站位置x坐标
BS_Y=sink.y; %基站位置y坐标
plot(BS_XBS_Y‘g*‘);
hold on;
flag_first_dead_old=0;%
flag_first_dead=0;
dead_old=0;
for r=0:1:rmax %主循环每次1轮
r
ENOLD(r+1)=0;
for i=1:1:n;
ENOLD(r+1)=S(i).E+ENOLD(r+1);%每轮节点总共剩余能量
end
EAOLD(r+1)=ENOLD(r+1)/n;%每轮节点平均剩余能量
hold off;
dead_old=0;
for i=1:1:n
%checking if there is a dead node
if (S(i).E<=0)
dead_old=dead_old+1;
end
if S(i).E>0
S(i).type=‘N‘;
end
end
% if (dead_old == n)%节点全部死亡退出循环
% break;
% end
DEAD(r+1)=dead_old;
ALIVE(r+1)=n-dead_old;
STATISTICSOLD(r+1).DEAD=dead_old;
% DEADOLD(r+1)=dead_old;
% DEAD_N(r+1)=dead_n;
% DEAD_A(r+1)=dead_a;
% When the first node dies
if (dead_old==1)
if(flag_first_dead_old==0)
first_dead_old=r;%记录第一个节点死亡的轮数
flag_first_dead_old=1;%死亡标志位
end
end
%countCHs=0;
j=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
%Election of Cluster Heads
if( temp_rand <=T);
distance=sqrt( (S(i).xd-BS_X )^2 + (S(i).yd-BS_Y )^2 );%到sink的距离
S(i).type = ‘C‘;%备选簇头
%S(i).mode =1;%“1“表示备选簇头为活跃状态
C(j).xd = S(i).xd;
C(j).yd = S(i).yd;
C(j).type = S(i).type ;
C(j).distance = distance;
d(j)= distance;
C(j).E=S(i).E;
C(j).id =i;
X(j)=S(i).xd;%保存节点坐标
Y(j)=S(i).yd;
countCHs =j;
j=j+1;
else
S(i).
评论
共有 条评论