LEACH算法改进 SEP算法源代码

1. SEP算法简介

% SEP: A Stable Election Protocol for clustered heterogeneous wireless sensor networks   

% This is the LEACH [1] code we have used.  

The same code can be used for FAIR if m=1           
 [1] W.R.Heinzelman, A.P.Chandrakasan and H.Balakrishnan,  "An application-specific protocol architecture for wireless  microsensor networks"IEEE Transactions on Wireless Communications, 1(4):660-670,2002

2.SEP算法matlab源代码

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Field Dimensions - x and y maximum (in meters)
xm=100;
ym=100;

%x and y Coordinates of the Sink
sink.x=0.5*xm;
sink.y=0.5*ym;

%Number of Nodes in the field
n=100

%Optimal Election Probability of a node
%to become cluster head
p=0.1;

%Energy Model (all values in Joules)
%Initial Energy 
Eo=0.5;
%Eelec=Etx=Erx
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000001;

%Values for Hetereogeneity
%Percentage of nodes than are advanced
m=0.1;
%\alpha
a=1;

%maximum number of rounds
rmax=9999

%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%

%Computation of do
do=sqrt(Efs/Emp);

%Creation of the random Sensor Network
figure(1);
for i=1:1:n
    S(i).xd=rand(1,1)*xm;
    XR(i)=S(i).xd;
    S(i).yd=rand(1,1)*ym;
    YR(i)=S(i).yd;
    S(i).G=0;
    %initially there are no cluster heads only nodes
    S(i).type='N';
   
    temp_rnd0=i;
    %Random Election of Normal Nodes
    if (temp_rnd0>=m*n+1) 
        S(i).E=Eo;
        S(i).ENERGY=0;
        plot(S(i).xd,S(i).yd,'o');
        hold on;
    end
    %Random Election of Advanced Nodes
    if (temp_rnd0<m*n+1)  
        S(i).E=Eo*(1+a)
        S(i).ENERGY=1;
        plot(S(i).xd,S(i).yd,'+');
        hold on;
    end
end

S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x');
    
        
%First Iteration
figure(1);

%counter for CHs
countCHs=0;
%counter for CHs per round
rcountCHs=0;
cluster=1;

countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;

for r=0:1:rmax
    r

  %Operation for epoch
  if(mod(r, round(1/p) )==0)
    for i=1:1:n
        S(i).G=0;
        S(i).cl=0;
    end
  end

hold off;

%Number of dead nodes
dead=0;
%Number of dead Advanced Nodes
dead_a=0;
%Number of dead Normal Nodes
dead_n=0;

%counter for bit transmitted to Bases Station and to Cluster Heads
packets_TO_BS=0;
packets_TO_CH=0;
%counter for bit transmitted to Bases Station and to Cluster Heads 
%per round
PACKETS_TO_CH(r+1)=0;
PACKETS_TO_BS(r+1)=0;

figure(1);

for i=1:1:n
    %checking if there is a dead node
    if (S(i).E<=0)
        plot(S(i).xd,S(i).yd,'red .');
        dead=dead+1;
        if(S(i).ENERGY==1)
            dead_a=dead_a+1;
        end
        if(S(i).ENERGY==0)
            dead_n=dead_n+1;
        end
        hold on;    
    end
    if S(i).E>0
        S(i).type='N';
        if (S(i).ENERGY==0)  
        plot(S(i).xd,S(i).yd,'o');
        end
        if (S(i).ENERGY==1)  
        plot(S(i).xd,S(i).yd,'+');
        end
        hold on;
    end
end
plot(S(n+1).xd,S(n+1).yd,'x');


STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
DEAD_N(r+1)=dead_n;
DEAD_A(r+1)=dead_a;

%When the first node dies
if (dead==1)
    if(flag_first_dead==0)
        first_dead=r
        flag_first_dead=1;
    end
end

countCHs=0;
cluster=1;
for i=1:1:n
   if(S(i).E>0)
   temp_rand=rand;     
   if ( (S(i).G)<=0)

 %Election of Cluster Heads
 if(temp_rand<= (p/(1-p*mod(r,round(1/p)))))
            countCHs=countCHs+1;
            packets_TO_BS=packets_TO_BS+1;
            PACKETS_TO_BS(r+1)=packets_TO_BS;
            
            S(i).type='C';
            S(i).G=round(1/p)-1;
            C(cluster).xd=S(i).xd;
            C(cluster).yd=S(i).yd;
            plot(S(i).xd,S(i).yd,'k*');
            
            distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
            C(cluster).distance=distance;
            C(cluster).id=i;
            X(cluster)=S(i).xd;
            Y(cluster)=S(i).yd;
            cluster=cluster+1;
            
            %Calculation of Energy dissipated
            distance;
            if (distance>do)
                S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance )); 
            end
            if (distance<=do)
                S(i).E=S(i).E- ( (ETX+EDA)*(4000)  + Efs*4000*( distance * distance )); 
            end
        end     
    
    end
  end 
end

STATISTICS(r+1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r+1)=cluster-1;

%Election of Associated Cluster Head for Normal Nodes
for i=1:1:n
   if ( S(i).type=='N' && S(i).E>0 )
     if(cluster-1>=1)
       min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
       min_dis_cluster=1;
       for c=1:1:cluster-1
           temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) );
           if ( temp<min_dis )
               min_dis=temp;
               min_dis_cluster=c;
           end
       end
       
       %Energy dissipated by associated Cluster Head
            min_dis;
            if (min_dis>do)
                S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis)); 
            end
            if (min_dis<=do)
                S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis)); 
            end
        %Energy dissipated
        if(min_dis>0)
          S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 ); 
         PACKETS_TO_CH(r+1)=n-dead-cluster+1; 
        end

       S(i).min_dis=min_dis;
       S(i).min_dis_cluster=min_dis_cluster;
           
   end
 end
end
hold on;

countCHs;
rcountCHs=rcountCHs+countCHs;



%Code for Voronoi Cells
%Unfortynately if there is a small
%number of cells, Matlab's voronoi
%procedure has some problems

%[vx,vy]=voronoi(X,Y);
%plot(X,Y,'r*',vx,vy,'b-');
% hold on;
% voronoi(X,Y);
% axis([0 xm 0 ym]);

end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   STATISTICS    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                                     %
%  DEAD  : a rmax x 1 array of number of dead nodes/round 
%  DEAD_A : a rmax x 1 array of number of dead Advanced nodes/round
%  DEAD_N : a rmax x 1 array of number of dead Normal nodes/round
%  CLUSTERHS : a rmax x 1 array of number of Cluster Heads/round
%  PACKETS_TO_BS : a rmax x 1 array of number packets send to Base Station/round
%  PACKETS_TO_CH : a rmax x 1 array of number of packets send to ClusterHeads/round
%  first_dead: the round where the first node died                   
%                                                                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





3.总结

SEP算法是基于二级异构的网络,即网络中存在高级节点和普通节点,算法为不同初始能量的节点分配不同的轮转周期来实现延长网络稳定周期的目的,但SEP算法的簇头选举只是基于节点的初始能量,未考虑节点的剩余能量。DEEC算法将二级异构网络扩展到多级异构网络,在SEP算法的基础上根据节点的剩余能量水平和网络的异构性来决定簇头的选举,既能充分利用网络的异构性又能适应节点能量的变化。但DEEC使用估算方案求解网络平均剩余能量,求解的前提是网络能耗均匀,这与实际并不相符,从而削弱了DEEC的实用性。Aderohumu等人提出了E-SEP算法,该算法将网络中的节点根据能量分为了三类,增加了中间节点,相比于SEP算法,该算法进一步改善了网络的稳定性,但是E-SEP仍未考虑到节点与基站的距离。Faisal等人提出了Z-SEP算法,Z-SEP算法将网络中的能量异构节点按照能量的不同部署在不同的区域,簇头只在高级节点部署的区域中选举,这种分区部署策略一定程度上节省了能耗,延长了网络生命周期。自适应高效动态聚类路由算法EDFCM,与DEEC算法相似,也是通过估算网络平均剩余能量,对下一轮理想状态下的平均消耗能量进行预测,将预测值与历史能耗作为计算概率的参考因素。

 

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
LEACH算法是一种无线传感器网络中常用的聚簇路由协议,其主要思想是将网络中的传感器节点分为若干个簇(cluster),每个簇由一个簇首(cluster head)负责进行数据的聚合和传输,从而实现对能量的有效利用和延长网络的寿命。 LEACH算法的主要实现过程如下: 1. 随机选择若干个节点作为簇首,每个节点以一定的概率P选择成为簇首,概率P与节点的剩余能量成反比,即剩余能量越小的节点,成为簇首的概率更大。 2. 其他节点选择距离自己最近的簇首加入所在的簇中,每个节点以一定的概率P选择加入簇中,概率P与节点到簇首的距离成反比,即距离越近的节点,加入簇的概率更大。 3. 簇首节点负责收集簇中所有节点的数据并进行聚合,然后将聚合后的数据传输给下一级节点,最终传输到基站。 4. 在每个轮次中,重新选择簇首和节点加入簇的过程,以保证网络中各个簇的均衡。 下面是LEACH算法源代码实现(Python版): ```python import random class Node: def __init__(self, id, x, y, energy): self.id = id self.x = x self.y = y self.energy = energy self.cluster_head = False self.cluster = None class LEACH: def __init__(self, n, m, e, r, p): self.n = n #节点数量 self.m = m #簇首数量 self.e = e #节点能量 self.r = r #通信半径 self.p = p #簇首选择概率 self.nodes = [] #节点列表 self.heads = [] #簇首列表 #初始化节点 def init_nodes(self): for i in range(self.n): x = random.uniform(0, 100) y = random.uniform(0, 100) node = Node(i, x, y, self.e) self.nodes.append(node) #计算节点之间的距离 def distance(self, node1, node2): return ((node1.x - node2.x) ** 2 + (node1.y - node2.y) ** 2) ** 0.5 #选择簇首 def select_heads(self): for node in self.nodes: if random.random() < self.p: node.cluster_head = True node.cluster = [] self.heads.append(node) #节点加入簇 def join_cluster(self): for node in self.nodes: if not node.cluster_head: min_dis = float('inf') for head in self.heads: dis = self.distance(node, head) if dis < min_dis: min_dis = dis node.cluster = head.cluster node.cluster.append(node) #簇首聚合数据 def aggregate_data(self): for head in self.heads: data = [] for node in head.cluster: data.append(node.energy) avg_energy = sum(data) / len(data) head.energy -= 0.01 * avg_energy #消耗能量 if head.energy <= 0: #簇首能量耗尽 self.heads.remove(head) for node in head.cluster: node.cluster_head = False #运行LEACH算法 def run(self, rounds): self.init_nodes() for i in range(rounds): self.heads = [] self.select_heads() self.join_cluster() self.aggregate_data() print('Round %d: %d clusters, %d cluster heads' % (i+1, len(self.heads), sum([node.cluster_head for node in self.nodes]))) #测试LEACH算法 leach = LEACH(100, 5, 1, 10, 0.1) leach.run(10) ``` 注意:这里只是一个简单的LEACH算法实现,实际应用中需要根据具体情况进行参数调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr Leach

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值