• 大小: 2KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-01
  • 语言: 其他
  • 标签: leach  WSN  

资源简介

leach算法做了新的改进,对WSN的路由研究有帮助

资源截图

代码片段和文件信息

%只考虑簇首能量和簇首多跳
clear;
r=75;
w1=0.3w2=0.1w3=0.6
xm = 100;
ym = 100;
sink.x=50;
sink.y=175;
n=100
T=zeros(1n);
p=0.05;
packetLength =4000;%数据包长度  
ctrPacketLength = 100;%控制包长度
Eo = 1;
ETX=50*0.000000001;
ERX=50*0.000000001;
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
EDA=5*0.000000001;
INFINITY = 999999999999999;
rmax=6000
do=sqrt(Efs/Emp);
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
for i=1:1:n
    S(i).xd=rand(11)*xm;%坐标
    XR(i)=S(i).xd;
    S(i).yd=rand(11)*ym;
    YR(i)=S(i).yd;
    S(i).G=0;
    S(i).type=‘N‘;%普通节点
    S(i).E=Eo;
    S(i).ENERGY=0;
    S(i).d=sqrt((S(i).xd-(S(n+1).xd))^2 + (S(i).yd-(S(n+1).yd))^2);
    for j=1:1:10
        if (j-1)*r            S(i).TABLEID=j;
        end
    end
end
countCHs=0;
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0; 
for r=0:1:rmax %主循环每次1轮
  r
  if(mod(r round(1/p))==0)
     for i=1:1:n
        S(i).G=0;
        S(i).cl=0;
     end
  end
dead=0;
packets_TO_BS=0;
packets_TO_CH=0;
PACKETS_TO_CH(r+1)=0;
PACKETS_TO_BS(r+1)=0;
for i=1:1:n
     if (S(i).E<=0)
       dead=dead+1;
     end
     if (S(i).E>0)
        S(i).type=‘N‘;
     end
end
if (dead == n)%节点全部死亡退出循环
   break;
end
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
if (dead==1)
    if(flag_first_dead==0)
        first_dead=r
        flag_first_dead=1;
    end
end
for c=1:1:5
    xiao(c)=INFINITY;
    for i=1:1:n
    if(S(i).E>0)
       distance=sqrt((S(i).xd-(S(n+1).xd))^2 + (S(i).yd-(S(n+1).yd))^2);%到sink的距离
      W(i)=w1*(Eo-S(i).E)+w2*T(i)+w3*distance;
     if ((S(i).G)<=0) %如果该节点在候选集合中
     if W(i)        xiao(c)=W(i);
        C(c).id = i;
        W(i)=INFINITY;
        S(i).type = ‘C‘;
        S(i).G=1;
        C(c).xd = S(i).xd;
        C(c).yd = S(i).yd;
        C(c).distance = distance;
        C(c).id = i;
        X(c)=S(i).xd;
        Y(c)=S(i).yd;
     distanceBroad = sqrt(xm*xm+ym*ym);
            if (distanceBroad >=do)
                S(i).E = S(i).E-(ETX*ctrPacketLength + Emp*ctrPacketLength*(distanceBroad*distanceBroad*distanceBroad*distanceBroad));%广播自成为簇头
            else
                S(i).E = S(i).E-(ETX*ctrPacketLength + Efs*ctrPacketLength*(distanceBroad*distanceBroad)); 
            end
            packets_TO_BS = packets_TO_BS+1;
            PACKETS_TO_BS(r+1) = packets_TO_BS;
        end        
    end
end
i=i+1;
end
T(C(c).id)=T(C(c).id)+1;
end
for c = 1:1:cluster-1
    if S(C(c).id).TABLEID~=1
        for k=S(C(c).id).TABLEID:(-1):1
            for j=1:1:cluster-1
                if S(C(c).id).TABLEID==k-1
                   S(C(c).id).E=S(C(c).id).E-(ERX + EDA)*packetLength;
               end
           end
       end
   else
       S(i).E = S(i).E-((ETX+EDA)*packetLength+ Efs*packetLength*(distance*distance)); 
   end
end
for i=1:1:n
   if (S(i).type==‘N‘ &S(i).E>0) %普通节点
     min_dis=INFINITY; 
         min_dis_cluster = 1;
  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       5122  2010-12-03 14:08  c.m

----------- ---------  ---------- -----  ----

                 5122                    1


评论

共有 条评论