无线传感器网络路由优化中的能量均衡LEACH改进算法(Matlab代码实现)

    目录

💥1 概述

📚2 运行结果

🎉3 参考文献

👨‍💻4 Matlab代码

💥1 概述

由于簇的规模和簇头选择对WSN总能耗影响较大:一方面,当簇的规模较小时,易导致WSN能量消耗不合理;另一方面,当簇的规模较大时,簇头转发数据量太大、负担较重,易造成能耗增大,使普通成员节点在单位之间可发送的数据量急速降低。

基于能耗均衡的LEACH改进算法在执行时循环进行簇的重构,仍然和传统 LEACH 一样按轮(round)进行,每轮依然分成簇的建立阶段和传输数据的稳定阶段。该算法的目标是在每一轮中产生可以实现整个WSN更低能耗的最优数目的簇头,同时,在簇头的选举时充分考虑节点的剩余能量引入簇头选择新阈值,从而使能量消耗更加均衡。

📚2 运行结果

主函数部分代码:

clc;
clear;%清除內存变量
​
xm=100;%x轴范围
ym=100;%y轴范围
​
sink.x=0.5*xm;%基站x轴
sink.y=0.5*ym;%基站y轴
​
n=100;%节点总数
​
p=0.1;%簇头概率
​
E0=0.02;%初始能量
ETX=50*0.000000000001;%传输能量,每bit,10e-12
ERX=50*0.000000000001;%接收能量,每bit
Efs=10*0.000000000001;%耗散能量,每bit
EDA=5*0.000000000001;%融合能耗,每bit
​
cc=0.6;%融合率
​
rmax=1000;%总轮数
​
CM=32;%控制信息大小
DM=4000;%数据信息大小
​
figure(1);%显示图片
%% 为每个节点随机分配坐标,并设置初始能量为E0,节点类型为普通,并绘制基站
for i=1:1:n
    S(i).xd=rand(1,1)*xm;
    S(i).yd=rand(1,1)*ym;
    S(i).G=0;%
每一周期結束此变量为
0
    S(i).E=E0;%设置初始能能量E0
    S(i).type='N';%节点类型为普通
​
    plot(S(i).xd,S(i).yd,'o');
    hold on;%保持所画的图像
end
​
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x');%绘制基站节点
​
flag_first_dead=0;%第一个死亡节点的标识变量
%% 开始每轮循环
for r=1:1:rmax
  r=r+1;%
显示轮数  %% 变量初始化
    %
 如果轮数正好是一個周期的整数倍,则设置S(i).G为
0
    if(mod(r,round(1/p))==0)
       for i=1:1:n
           S(i).G=0;
       end
    end
    
     hold off;%每轮图片重新绘制
     cluster=0;%初始簇头数为0
     dead=0;%初始死亡节点数为0
     %%%%%%%%%绘图
     %figure(1);
    %% 记录死亡节点
     for i=1:1:n
         %
 将能量小于等于
0
的节点绘制成紅色,并将死亡节点数增加
1
         if(S(i).E<=0)
             %%%%%%%%%绘图
%            plot(S(i).xd,S(i).yd,'red .');
            dead=dead+1;
            
            if(dead==1)
                if(flag_first_dead==0)
                    first_dead=r %第一个节点的死亡轮数
                    save ltest, first_dead;
                    flag_first_dead=1;
                end
            end
​
 %        hold on;
         %绘制其他节点,其他节点正常标识
         else
             S(i).type='N';
  %%%%%%%%%绘图
  %           plot(S(i).xd,S(i).yd,'o');
             hold on;
         end      
     end
%%%%%%%%%绘图
  %   plot(S(n+1).xd,S(n+1).yd,'x');%绘制基站
​
     Dead(r+1)=dead; %每轮记录当前的死亡节点数
     save ltest, Dead(r+1);%将此数据存入ltest文件
     
     %% 按概率选取簇头,选出簇头后标识簇头,将簇头标识(位置,距离,ID)都记录下来
     for i=1:1:n
         if(S(i).E>0)
           if(S(i).G<=0)
            temp_rand=rand;%
取一个随机数            if(temp_rand<=((p/(1-p*mod(r,round(1/p))))*(S(i).E/E0)))%如果随机数小于等于
            %if(temp_rand<=(p/(1-p*mod(r,round(1/p)))))%如果随机数小于等于
            S(i).type='C';%此节点为此轮簇头
            S(i).G=round(1/p)-1;%S(i).G设置为大于0,此周期不能再被选择为簇头
            cluster=cluster+1;%簇头数加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; %此簇头的节点id
​
            packet_To_BS(cluster)=1;%发送到基站的数据包数为1,每个簇头到基站都有一个数据包
            end
           end
          end
         end

🎉3 参考文献

[1]董嘉依,曹佳棋.基于延长WSN生命周期的CHL-LEACH算法[J].中国科技信息,2021(17):71-73.

部分理论引用网络文献,若有侵权联系博主删除。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Inputs: [AorV] Either A or V where A is a NxN adjacency matrix, where A(I,J) is nonzero if and only if an edge connects point I to point J NOTE: Works for both symmetric and asymmetric A V is a Nx2 (or Nx3) matrix of x,y,(z) coordinates [xyCorE] Either xy or C or E (or E3) where xy is a Nx2 (or Nx3) matrix of x,y,(z) coordinates (equivalent to V) NOTE: only valid with A as the first input C is a NxN cost (perhaps distance) matrix, where C(I,J) contains the value of the cost to move from point I to point J NOTE: only valid with A as the first input E is a Px2 matrix containing a list of edge connections NOTE: only valid with V as the first input E3 is a Px3 matrix containing a list of edge connections in the first two columns and edge weights in the third column NOTE: only valid with V as the first input [SID] (optional) 1xL vector of starting points. If unspecified, the algorithm will calculate the minimal path from all N points to the finish point(s) (automatically sets SID = 1:N) [FID] (optional) 1xM vector of finish points. If unspecified, the algorithm will calculate the minimal path from the starting point(s) to all N points (automatically sets FID = 1:N) Outputs: [costs] is an LxM matrix of minimum cost values for the minimal paths [paths] is an LxM cell containing the shortest path arrays [showWaitbar] (optional) a scalar logical that initializes a waitbar if nonzero Note: If the inputs are [A,xy] or [V,E], the cost is assumed to be (and is calculated as) the point to point Euclidean distance If the inputs are [A,C] or [V,E3], the cost is obtained from either the C matrix or from the edge weights in the 3rd column of E3 Example: % Calculate the (all pairs) shortest distances and paths using [A,C] inputs n = 7; A = zeros(n); xy = 10*rand(n,2) tri = delaunay(xy(:,1),xy(:,2)); I = tri(:); J = tri(:,[2 3 1]); J = J(:); IJ = I + n*(J-1); A(IJ) = 1 a = (1:n); b = a(ones(n,1),:); C = round(reshape(sqrt(sum((xy(b,:) - xy(b',:)).^2,2)),n,n)) [costs,paths] = dijkstra(A,C) Example: % Calculate the shortest distance and path from point 3 to 5 n = 15; A = zeros(n); xy = 10*rand(n,2) tri = delaunay(xy(:,1),xy(:,2)); I = tri(:); J = tri(:,[2 3 1]); J = J(:); IJ = I + n*(J-1); A(IJ) = 1 [cost,path] = dijkstra(A,xy,3,5) gplot(A,xy,'b.:'); hold on; plot(xy(path,1),xy(path,2),'ro-','LineWidth',2) for k = 1:n, text(xy(k,1),xy(k,2),[' ' num2str(k)],'Color','k'); end Example: % Calculate the shortest distances and paths from the 3rd point to all the rest n = 7; V = 10*rand(n,2) I = delaunay(V(:,1),V(:,2)); J = I(:,[2 3 1]); E = [I(:) J(:)] [costs,paths] = dijkstra(V,E,3) Example: % Calculate the shortest distance and path from points [1 3 4] to [2 3 5 7] n = 7; V = 10*rand(n,2) I = delaunay(V(:,1),V(:,2)); J = I(:,[2 3 1]); E = [I(:) J(:)] [costs,paths] = dijkstra(V,E,[1 3 4],[2 3 5 7]) Revision Notes: (3/13/15) Previously, this code ignored edges that have a cost of zero, potentially producing an incorrect result when such a condition exists. I have solved this issue by using NaNs in the table rather than a sparse matrix of zeros.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值