【WSN通信】A星改进LEACH多跳传输协议附matlab代码

1 简介

OHILEACH(Optimized heuristic ILEACH)是一种新技术,它使用基于 AI 的启发式搜索来优化网络寿命。​

2 部分代码

close all;clear;clc;%-------------------------------%Number of Nodes in the fieldn=200;%n=input('Enter the number of nodes in the space : ');%Energy Model (all values in Joules)%Initial EnergyEo=0.1;%Eo=input('Enter the initial energy of sensor nJ : ');%Field Dimensions - x and y maximum (in meters)% xm=input('Enter x value for area plot : ');% ym=input('Enter y value for area plot : ');xm=100;ym=100;%x and y Coordinates of the Sinksink.x=1.5*xm;sink.y=0.5*ym;%Optimal Election Probability of a node%to become cluster headp=0.2;%Eelec=Etx=ErxETX=50*0.000000001;ERX=50*0.000000001;%Transmit Amplifier typesEfs=10*0.000000000001;Emp=0.0013*0.000000000001;%Data Aggregation EnergyEDA=5*0.000000001;%Values for Hetereogeneity%Percentage of nodes than are advancedm=0.5;%\alphaa=1;%maximum number of rounds%rmax=input('enter the number of iterations you want to run : ');rmax=200;%------------------%Computation of dodo=sqrt(Efs/Emp);%Creation of the random Sensor Networkfigure(1);hold off;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-r');        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;    endendS(n+1).xd=sink.x;S(n+1).yd=sink.y;plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');figure(1);% figure(1)%  plot(o1,o2,'^','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','y', 'MarkerSize',12);%    hold on%First Iteration%counter for CHscountCHs=0;%counter for CHs per roundrcountCHs=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,'^','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','y', 'MarkerSize',8);            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','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','g', 'MarkerSize',8);            end            if (S(i).ENERGY==1)                plot(S(i).xd,S(i).yd,'+','LineWidth',3, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);            end            hold on;        end    end    plot(S(n+1).xd,S(n+1).yd,'x','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);            STATISTICS(r+1).DEAD=dead;    DEAD(r+1)=dead;    DEAD_N(r+1)=dead_n;    DEAD_A(r+1)=dead_a;    %          plot(S(n+1).xd,S(n+1).yd,'o', 'MarkerSize', 12, 'MarkerFaceColor', 'r');    %          plot(S(n+1).xd,S(n+1).yd,'x','LineWidth',1, 'MarkerEdgeColor','k', 'MarkerFaceColor','r', 'MarkerSize',8);    %When the first node dies    if (dead==1)        if(flag_first_dead==0)            first_dead=r;            flag_first_dead=1;        end    end    for i=1:1:n        S(i).mem=-5;        S(i).neigh=5;    end    countCHs=0;    cluster=1;    for i=1:1:n        if(S(i).E>0)            temp_rand=rand;            if ( (S(i).G)<=0)                form = p / (1 + a*m);                %form = form *(S(i).E/0.1);                %v=sqrt((n-dead)*p - 1);                %form=form*sqrt(S(i).neigh);                if S(i).ENERGY == 1                    form = form * (1 + a);                end                if r+1 - S(i).mem < 1/p                    form = S(i).E/0.1 * sqrt(S(i).neigh);                end                %Election of Cluster Heads                if(temp_rand <= (form/(1-form*mod(r,round(1/form)))))                    countCHs=countCHs+1;                    packets_TO_BS=packets_TO_BS+1;                    PACKETS_TO_BS(r+1)=packets_TO_BS;                                        S(i).type='C';                    S(i).mem=r+1;                    S(i).G=round(1/form)-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% figure(2);% for r=0:1:24%     ylabel('Average Energy of Each Node');%     xlabel('Round Number');%     plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');%     hold on;% end% figure(3);% for r=0:1:49%     ylabel('Average Energy of Each Node');%     xlabel('Round Number');%     plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');%     hold on;% end% figure(4);% for r=0:1:74%     ylabel('Average Energy of Each Node');%     xlabel('Round Number');%     plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');%     hold on;% endfigure(2);for r=0:1:rmax-1    ylabel('Average Energy of Each Node');    xlabel('Round Number');    plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');    hold on;end% figure(6);% for r=0:1:24%     ylabel('Number of Dead Nodes');%     xlabel('Round Number');%     plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');%     hold on;% end% figure(7);% for r=0:1:49%         ylabel('Number of Dead Nodes');%     xlabel('Round Number');%     plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');%     hold on;% end% figure(8);% for r=0:1:74%         ylabel('Number of Dead Nodes');%     xlabel('Round Number');%     plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');%     hold on;% endfigure(3);for r=0:1:rmax-1    ylabel('Number of Dead Nodes');    xlabel('Round Number');    plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');    hold on;endfid=fopen('ILEACH.txt','wt');for r=0:1:rmax-1    fprintf( fid, '%f ',STATISTICS(r+1).AVG);endfprintf(fid ,'\n');for r=0:1:rmax-1    fprintf( fid, '%f ',STATISTICS(r+1).DEAD);endfprintf(fid ,'\n');

3 仿真结果

4 参考文献

[1]张华忠, 刘志杰, 于鹏程. WSN中负载平衡的LEACH通信协议研究[J]. 计算机工程与设计, 2007(18):85-88.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

matlab科研助手

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

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

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

打赏作者

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

抵扣说明:

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

余额充值