固定簇半径的分簇协议HEED matlab代码

Posted MatlabQQ1575304183

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了固定簇半径的分簇协议HEED matlab代码相关的知识,希望对你有一定的参考价值。

文章目录

一、理论基础

二、分簇过程

三、MATLAB程序实现

%% 清空环境变量
clear;
clc;
% 传感器节点区域100×100
xm = 100;
ym = 100;
% 基站坐标
sink.x = 0.5*xm;
sink.y = 0.5*ym;
% 节点总数100
NodeNums = 100;
% 节点成为簇头的初始概率和概率阈值
Cprob = 0.05;
pmin = 0.04;
packetLength = 4000;      % 数据包长度
ctrPacketLength = 100;    % 控制包长度
% 初始能量(最大能量) 0.3J
Eo = 0.3;
% 发送能耗RTX = 接收能耗ERX = 50nJ/bit
ETX=50*10^(-9);
ERX=50*10^(-9);
% 发射放大器类型
Efs=10*10^(-12);
Emp=0.0013*10^(-12);
% 数据聚合能量
EDA=5*10^(-9);
% 最大轮数
rmax = 5;
% the time of round
Tr=20;
distanceBroad = sqrt(Tr^2+Tr^2);
% 临界距离
do = sqrt(Efs/Emp);
% 节点类型
NON_CH = 0;         % 普通节点
TENT_CH = 1;        % 临时簇头
FINAL_CH = 2;       % 最终簇头

figure(1);
%% 传感器节点的随机分布
for i = 1:NodeNums
    Node(i).xd = rand(1,1)*xm;
    Node(i).yd = rand(1,1)*ym;
    Node(i).RE = Eo;                  % 节点能量
    Node(i).NumNbr = 0;               % 邻居节点数
    Node(i).AMRP = Node(i).NumNbr;    % 平均可达能级
    Node(i).CH = 0;                   % 簇头
    Node(i).role = NON_CH;            % 节点角色
    plot(Node(i).xd, Node(i).yd, 'o', sink.x, sink.y, '*r');
    hold on;
    title 'Wireless Sensor Network';
    xlabel 'X-coordinates';
    ylabel 'Y-coordinates';
    legend('节点', '基站');
end
hold off;
%% 计算每个节点的邻居节点集
for i = 1:NodeNums
    count = 0;  % 计数器:用于统计每个节点的邻居节点总数
    for j = 1:NodeNums
        if j ~= i
            dist = sqrt((Node(i).xd-Node(j).xd).^2 + (Node(i).yd-Node(j).yd).^2);   % 节点i与节点j距离的平方
            if dist < distanceBroad  % 如果此距离小于广播半径,则可认为j是i的邻居节点
                count = count +1;
                Node(i).Nbr(count) = j; % j是i的一个邻居节点
            end
        end
        if j == NodeNums % 说明循环到了最后一个节点
            Node(i).NumNbr = count;     % 节点i的邻居节点个数count
        end
    end
end

%% 分簇阶段
for r = 1:rmax
%     % 死亡节点总数
%     dead = 0;
%     % 计算节点死亡数目
%     for i = 1:NodeNums
%         if Node(i).RE <= 0  % 统计节点死亡数目
%             dead = dead + 1;
%         end
%     end
%     if dead == NodeNums  % 节点全部死亡退出循环
%         break;
%     end
    s = 0;
    % 第一步:初始化
    for i = 1:NodeNums
        Node(i).RE = Eo;
        Node(i).CHprob = max(Cprob*(Node(i).RE/Eo), pmin);
        Node(i).AMRP = Node(i).NumNbr;    % 平均可达能级
        Node(i).CH = 0;
        Node(i).role = NON_CH;                     % 节点角色
        Node(i).stop = 1;
        s = s + Node(i).stop;
    end
    % 第二步:迭代,直至节点概率为1或节点死亡(能量耗尽)
    while s ~= 0
        s = 0;
        for i = 1:NodeNums
            Node(i).CHprob = max(Cprob*(Node(i).RE/Eo), pmin);
        end
        for i = 1:NodeNums
            if Node(i).RE > 0 && Node(i).CHprob <= 1
                flag0 = 0;
                flag1 = 0;
                for j = 1:Node(i).NumNbr
                    neighbor = Node(i).Nbr(j);
                    if Node(neighbor).role == TENT_CH
                        flag0 = 1;
                        if Node(i).AMRP > Node(neighbor).AMRP
                            flag1 = 1;
                        end
                    end
                end
                % 自身是临时簇头
                if Node(i).role == TENT_CH
                    if flag0 == 1 && flag1 == 0  % 周围有临时簇头且自身的AMRP值最小
                        if Node(i).CHprob == 1      % 概率为1
                            Node(i).CH = -1;
                            Node(i).RE = Node(i).RE-(ETX*ctrPacketLength+Efs*ctrPacketLength*distanceBroad^2);
                            % 邻居节点接收消息
                            for j = 1:Node(i).NumNbr
                                neighbor = Node(i).Nbr(j);
                                Node(neighbor).RE = Node(neighbor).RE-ERX*ctrPacketLength;
                            end
                        end
                    end
                end
                % 邻居节点无临时簇头
                if flag0 == 0
                    if rand <= Node(i).CHprob
                        Node(i).role = TENT_CH;
                        Node(i).RE = Node(i).RE-(ETX*ctrPacketLength+Efs*ctrPacketLength*distanceBroad^2);
                        % 邻居节点接收消息
                        for j = 1:Node(i).NumNbr
                            neighbor = Node(i).Nbr(j);
                            Node(neighbor).RE = Node(neighbor).RE-ERX*ctrPacketLength;
                        end
                    end
                end
                % 备选簇头状态
                if Node(i).CHprob < 1
                    Node(i).role = TENT_CH;
                    Node(i).RE = Node(i).RE-(ETX*ctrPacketLength+Efs*ctrPacketLength*distanceBroad^2);
                    x = Node(i).AMRP;
                    for j = 1:Node(i).NumNbr
                        neighbor = Node(i).Nbr(j);
                        Node(neighbor).RE = Node(neighbor).RE-ERX*ctrPacketLength;
                        if Node(neighbor).role ~= NON_CH    % 簇头节点
                            if Node(i).AMRP > Node(neighbor).AMRP   % AMRP值更小
                                Node(i).role = NON_CH;      % 改为普通节点
                                Node(i).CH = neighbor;       % 加入该候选簇头
                                Node(i).AMRP = Node(neighbor).AMRP;
                            end
                        end
                    end
                    Node(i).AMRP = x;
                else    % 最终簇头状态
                    Node(i).role = FINAL_CH;
                    Node(i).CH = -1;
                    Node(i).RE = Node(i).RE-(ETX*ctrPacketLength+Efs*ctrPacketLength*distanceBroad^2);
                    for j = 1:Node(i).NumNbr
                        neighbor = Node(i).Nbr(j);
                        Node(neighbor).RE = Node(neighbor).RE-ERX*ctrPacketLength;
                    end
                end
            else
                Node(i).stop = 0;
            end
            s = s+Node(i).stop;
            Node(i).CHprob = min(Node(i).CHprob * 2, 1);
        end
    end
    %% 第三,处理孤儿节点
    for i = 1:NodeNums
        if Node(i).RE > 0
            if Node(i).role ~= FINAL_CH
                flag = 0;
                for j = 1:Node(i).NumNbr
                    neighbor = Node(i).Nbr(j);
                    if Node(neighbor).role ~= NON_CH    % 簇头节点
                        flag = 1;
                        y = Node(i).AMRP;
                        if Node(i).AMRP > Node(neighbor).AMRP   % AMRP值更小
                            Node(i).role = NON_CH;      % 改为普通节点
                            Node(i).CH = neighbor;       % 加入该候选簇头
                            Node(i).AMRP = Node(neighbor).AMRP;
                        end
                    end
                end
                if flag == 0
                    Node(i).CH = -1;
                    Node(i).role = FINAL_CH;
                else
                    Node(i).AMRP = y;
                end
            end
        end
    end
    figure(r+1)
    %% 画出分簇图
    for i = 1:NodeNums
        if Node(i).role ~= NON_CH
            plot(Node(i).xd, Node(i).yd, '*'); % 簇头节点以*标记
            hold on
            text(Node(i).xd, Node(i).yd, num2str(i));
        else
            plot(Node(i).xd, Node(i).yd, 'o');   %普通节点以o标记
            text(Node(i).xd, Node(i).yd, num2str(i));
            hold on
            plot([Node(Node(i).CH).xd; Node(i).xd], [Node(Node(i).CH).yd; Node(i).yd]);  % 将节点与簇头连起来,即加入簇头集合
            hold on
        end
    end
end

其中一个分簇如图所示:
在这里插入图片描述

HEED分簇图

四、参考文献

[1] Younis O , Fahmy S . HEED: A Hybrid, Energy-Efficient, Distributed Clustering Approach for Ad Hoc Sensor Networks[J]. IEEE Transactions on Mobile Computing, 2004, 3(4):366-379.

以上是关于固定簇半径的分簇协议HEED matlab代码的主要内容,如果未能解决你的问题,请参考以下文章

LEACH协议基于matlab最佳簇半径的无线传感器网络分簇路由算法含Matlab源码 2087期

LEACH协议基于matlab最佳簇半径的无线传感器网络分簇路由算法含Matlab源码 2087期

能量均衡的无线传感器网络非均匀分簇路由协议

能量均衡的无线传感器网络非均匀分簇路由协议

matlab实现LEACH协议路由分簇算法

matlab实现LEACH协议路由分簇算法