LEACH分簇算法

几个参考链接:
https://www.cnblogs.com/lemonzhang/p/9254435.html
https://blog.csdn.net/qq_24133491/article/details/79057079
https://blog.csdn.net/wuchuanpingstone/article/details/7064050

一份可运行的代码:

clear; 
e=1; 
 
xm = 100; 
ym = 100; 
sink.j =0.5 * xm;  
sink.k = ym*1.5; 
sink.x =0.5 * xm;  
sink.y = ym*1.5; 
n = 100 ; 
  
p=0.1;  
packetLength = 4000;   
ctrPacketLength = 100;  
%Energk Model (all valueT in JouleT) 
%Initial Energk  
Einit =0.5; 
Eo=0.5; 
%Eelec=ETX=ERX 
ETX=100*0.000000001; 
ERX=100*0.000000001; 
Efs=10*0.000000000001;  
Emp=0.0013*0.000000000001;  
EDA=200*0.000000001; 
 
Emin=0.0001; 
det=0.5; 
q0=EDA; 
c0=1;
 
m=0.1; 
a=1; 
% INFINITk = 999999999999999; 
rmax=1000; 
% END OF PARAMETERT  
do=sqrt(Efs/Emp); 
 
 
%Eo=0.5;        
for Eo=0.5:0.25:2 
 
     
packets_TO_BS=0; 
packets_TO_CH=0; 
for i=1:(rmax+1) 
PACKETS_TO_CH(i)=0; 
end 
 
 
for i=1:(rmax+1) 
PACKETS_TO_BS(i)=0; 
end 
Et=0;
 
 
 
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; 
     
     
     
    
     
  S(i).type='N';  
   temp_rnd0 = i; 
    
     
       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 

     if (temp_rnd00)         
        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'); 
 
if (dead == n)  
   break; 
end 
 
STATISTICS(r+1).DEAD=dead; 
DEAD(r+1)=dead; 
DEAD_N(r+1)=dead_n; 
DEAD_A(r+1)=dead_a; 
if (dead==0.1*n) 
    if(flag_first_dead1==0) 
        first_dead1(e)=r; 
        flag_first_dead1=1; 
    end 
end 
countCHs=0; 
cluster=1; 
 
for i=1:1:n 
    if S(i).E<=0 
       PACKETS_TO_BS(r+1)=packets_TO_BS; 
    end 
   if(S(i).E>0) 
     temp_rand=rand;      
     if ( (S(i).G)<=0)   
          if (S(i).ENERGY==0 &&(temp_rand <= (pnrm/(1-pnrm*mod(r,round(1/pnrm)))))) 
            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,'ro'); 
            hold on; 
    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; 
            
            distanceBroad = sqrt (xm*xm+ym*ym); 
            if (distanceBroad > do)   
                S(i).E = S(i).E- ( ETX * ctrPacketLength + Emp* ctrPacketLength*( distanceBroad*distanceBroad*distanceBroad*distanceBroad ));%%���͵�·�������Emp����Efs 
            else 
                S(i).E = S(i).E- ( ETX * ctrPacketLength + Efs * ctrPacketLength*( distanceBroad*distanceBroad));  
            end 
            %Calculation of Energy dissipated  
            distance; 
            if (distance > do) 
                 S(i).E = S(i).E- ( (ETX)*(packetLength) + Emp * packetLength*( distance*distance*distance*distance ));  
            else 
                 S(i).E = S(i).E- ( (ETX)*(packetLength) + Efs * packetLength*( distance * distance ));  
            end 
          end 
     if( ( S(i).ENERGY==1 && ( temp_rand <= ( padv / ( 1 - padv * mod(r,round(1/padv)) )) ) )  ) 
         
            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,'ro'); 
            hold on; 
                
             distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );   %sink 
             C(cluster).distance=distance; 
             C(cluster).id=i; 
             X(cluster)=S(i).xd; 
             Y(cluster)=S(i).yd; 
             cluster=cluster+1; 
             
              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 
            %Calculation of Energy dissipated   
             distance; 
            if (distance>do) 
                 S(i).E=S(i).E- ( (ETX)*(4000) + Emp*4000*( distance*distance*distance*distance ));  
             else 
                 S(i).E=S(i).E- ( (ETX)*(4000)  + Efs*4000*( distance * distance ));  
             end 
             end 
       
     end      
         
     end 
   end  
 
hold off; 
STATISTICS(r+1).CLUSTERHEADS = cluster-1;
CLUSTERHS(r+1)= cluster-1; 
 
for i=1:1:n  
   if ( S(i).type=='N' && S(i).E>0 ) 
    % min_dis = INFINITY;  
     if(cluster -1 >= 1) 
         min_dis_cluster = 1;  
          min_dis = sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
    
         for c = 1:1:cluster-1     %cluster - 1
             temp = min(min_dis,sqrt( (S(i).xd - C(c).xd)^2 + (S(i).yd - C(c).yd)^2 ) ); 
            % temp = 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 
            S(i).E = S(i).E - ERX * ctrPacketLength; 
         end 
 
         min_dis;  
         if (min_dis > do) 
          S(i).E = S(i).E-( ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis * min_dis * min_dis * min_dis));  
         S(i).E = S(i).E - ( ETX*(packetLength) + Emp*packetLength*( min_dis * min_dis * min_dis * min_dis));
        else 
          S(i).E=S(i).E - ( ETX*(ctrPacketLength) + Efs*ctrPacketLength*( min_dis * min_dis));  
          S(i).E = S(i).E - ( ETX*(packetLength) + Efs*packetLength*( min_dis * min_dis));  
       end 
         S(i).E = S(i).E - ERX*(ctrPacketLength); 
  
         if(min_dis > 0) 
            S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( (ERX + EDA)*packetLength ); 
            S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ERX *ctrPacketLength ; 
            if (min_dis > do)
              S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis * min_dis * min_dis * min_dis)); 
            else 
                S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( ETX*(ctrPacketLength) + Efs * ctrPacketLength*( min_dis * min_dis)); 
            end 
                
         end 
                
         S(i).min_dis = min_dis; 
         S(i).min_dis_cluster = min_dis_cluster;  
          
     else 
         min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 ); 
            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 
            packets_TO_BS=packets_TO_BS+1; 
            PACKETS_TO_BS(r+1)=packets_TO_BS; 
             
     end 
end 
end 
 
 
if r==0                    
   PACKETS_TO_CH(1) = n - dead - cluster + 1 ; 
end 
if r>=1 
  PACKETS_TO_CH(r+1) = n - dead - cluster + 1 +PACKETS_TO_CH(r) ; 
end 
hold on; 
countCHs; 
rcountCHs = rcountCHs + countCHs; 
Ey1(e)=0; 
for i=1:n 
Ey1(e)=S(i).E+Ey1(e);  
end 
 
[vx,vy]=voronoi(X,Y); 
plot(X,Y,'ro',vx,vy,'b-'); 
 hold on; 
voronoi(X,Y); 
axis([0 xm 0 ym]); 
end %for rmax
e=e+1; 
 
 
x=1:1:r; 
y=1:1:r; 
z=1:1:r; 
w=1:1:r; 
ec=1:1:r; 
for i=1:r; 
    x(i)=i; 
    y(i)= n - STATISTICS(i).DEAD; 
    z(i)=CLUSTERHS(i); 
    w(i)=PACKETS_TO_BS(i); 
    ec(i)=Ec(i); 
end 
figure(3) 
plot(x,y,'g'); 
xlabel('time/round'); 
ylabel('node_left/number'); 
%title(''); 
hold on; 
 
 
%figure(4) 
%Eo=0.5:0.25:2;plot(Eo,Ey1,'-r');legend('LEACH');xlabel('��ʼ����/J');ylabel('ʣ������/J');title('��ʼ������ʣ��������ϵ'); 
 
 
figure(5) 
plot(x,w,':b'); 
legend('LEACH'); 
xlabel('time/round'); 
ylabel('data/packet'); 
%title('LEACH����վ�����ݰ�������'); 
 
figure(6) 
plot(x,ec,':b'); 
legend('LEACH'); 
xlabel('time/round'); 
ylabel('consumption/J'); 
%title('LEACH������������'); 
 
 
end%%%%%%%% (for Eo=0.5:0.25:2) 
 
figure(7) 
Eo=0.5:0.25:2; 
plot(Eo,Ey1,'-r'); 
xlabel('initial energy/J'); 
ylabel('energy_left/J'); 
%title('��ʼ������ʣ�������Ĺ�ϵ') 
 

你可能感兴趣的:(LEACH分簇算法)