• 大小: 6KB
    文件类型: .m
    金币: 2
    下载: 1 次
    发布日期: 2021-06-06
  • 语言: Matlab
  • 标签: leach  matlab  

资源简介

LEACH分簇算法matlab代码,手动输入方程中所需的参数即可运行

资源截图

代码片段和文件信息

function [STATISTICS1FD1TD1AD1]=leach(IniEngNetSizeNoOfNodeNoOfRoundcluster_head_percentage)


xm=NetSize;%设置网络大小
ym=NetSize;

sink.x=0.5*xm;%设置基站坐标
sink.y=1.35*ym;

n=NoOfNode;%设置网络节点数

p=cluster_head_percentage;%设置簇首概率

Eo=IniEng;%Initial energy节点初始能量
%Eelec=Etx=Erx
ETX=50*0.000000001;%节点传输消耗能量,每bit
ERX=50*0.000000001;%节点接收消耗能量,每bit
%Transmit Amplifier types
Efs=10*0.000000000001;%耗散能量,每bit
Emp=0.0013*0.000000000001;%放大信号能耗,每bit
%Data Aggregation Energy
EDA=5*0.000000001;%数据融合能耗,每bit

a=0;

rmax=NoOfRound;%总轮数

do=sqrt(Efs/Emp);
do

for i=1:1:n      %设置所有节点的X,Y坐标,初始能量,节点类型,簇首选举资格
    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).E=Eo*(1+rand*a);
    %initially there are no cluster heads only nodes
    S(i).type=‘N‘;
end

S(n+1).xd=sink.x;%设置基站的XY坐标
S(n+1).yd=sink.y;

countCHs=0;
cluster=1;
flag_first_dead=0;
flag_teenth_dead=0;
flag_all_dead=0;

dead=0;
first_dead=0;
teenth_dead=0;
all_dead=0;

allive=n;
%counter for bit transmitted to bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;

for r=0:1:rmax     
    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;
    for i=1:1:n
        if (S(i).E<=0)
            dead=dead+1;  
            if (dead==1)
                if(flag_first_dead==0)
                    first_dead=r;%首个死亡节点出现的轮数
                    flag_first_dead=1;
                end
            end   
            if(dead==0.1*n)
                if(flag_teenth_dead==0)
                    teenth_dead=r;%死亡节点数我x*10时的轮数(x=1--0.1*n)
                    flag_teenth_dead=1;
                end
            end
            if(dead==n)
                if(flag_all_dead==0)
                    all_dead=r;%所有节点全部死亡的轮数
                    flag_all_dead=1;
                end
            end
        end
        if S(i).E>0
            S(i).type=‘N‘;
        end
    end
    STATISTICS.DEAD(r+1)=dead;%第r+1轮时的死亡节点数
    STATISTICS.ALLIVE(r+1)=allive-dead;%第r+1轮时的存活节点数
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    TotalNetworkEnergy=0;
    for i=1:n         
        if S(i).E>0
            TotalNetworkEnergy=TotalNetworkEnergy+S(i).E;
        end
    end
    STATISTICS.TotalEnergy(r+1)=TotalNetworkEnergy;%第r+1轮时的网络总能量剩余
    STATISTICS.AvgEnergy(r+1)=TotalNetworkEnergy/n;%第r+1轮时的节点平均量剩余
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    
    countCHs=0;%初始簇头数为0
    cluster=1;
    for i=1:1:n
        if(S(i).E>0)
            temp_rand=rand;     
            if ( (S(i).G)<=0)    
                if(temp_rand<= (p/(1-p*mod(rround(1/p)))))
                    countCHs=countCHs+1;
                    packets_TO_BS=packets_TO_BS+1;
                    PACKETS_TO_BS(r+1)=packets_TO_BS;%第r+1轮CH传输给BS的总数据包数
    

评论

共有 条评论