您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > wsn中leach算法改进程序
%FieldDimensions-xandymaximum(inmeters)xm=300;ym=300;%xandyCoordinatesoftheSinksink.x=0.5*xm;sink.y=0.5*ym;%NumberofNodesinthefieldn=900;%OptimalElectionProbabilityofanode%tobecomeclusterheadp=0.1;%EnergyModel(allvaluesinJoules)%InitialEnergyEo=0.5;%Eelec=Etx=ErxETX=50*0.000000001;ERX=50*0.000000001;%TransmitAmplifiertypesEfs=10*0.000000000001;Emp=0.0013*0.000000000001;%DataAggregationEnergyEDA=5*0.000000001;%ValuesforHetereogeneity%Percentageofnodesthanareadvancedm=0.1;%\alphaa=1;cc=10;CM=32;DM=4000;%maximumnumberofroundsrmax=100;%%%%%%%%%%%%%%%%%%%%%%%%%ENDOFPARAMETERS%%%%%%%%%%%%%%%%%%%%%%%%%Computationofdodo=sqrt(Efs/Emp);%CreationoftherandomSensorNetworkfigure(1);fori=1:1:nS(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;%initiallytherearenoclusterheadsonlynodesS(i).type='N';temp_rnd0=i;%RandomElectionofNormalNodesif(temp_rnd0=m*n+1)S(i).E=Eo;S(i).ENERGY=0;%plot(S(i).xd,S(i).yd,'o');%holdon;end%RandomElectionofAdvancedNodesif(temp_rnd0m*n+1)S(i).E=Eo*(1+a);S(i).ENERGY=1;%plot(S(i).xd,S(i).yd,'+');%holdon;endendS(n+1).xd=sink.x;S(n+1).yd=sink.y;%plot(S(n+1).xd,S(n+1).yd,'x');%FirstIterationfigure(1);%counterforCHscountCHs=0;%counterforCHsperroundrcountCHs=0;cluster=1;countCHs;rcountCHs=rcountCHs+countCHs;flag_first_dead=0;forr=0:1:rmaxr+1%Operationforepochif(mod(r,round(1/p))==0)fori=1:1:nS(i).G=0;%S(i).cl=0;endendholdoff;EJ(r+1)=0;%Numberofdeadnodesdead=0;%NumberofdeadAdvancedNodesdead_a=0;%NumberofdeadNormalNodesdead_n=0;%counterforbittransmittedtoBasesStationandtoClusterHeads%packets_TO_BS=0;%packets_TO_CH=0;%counterforbittransmittedtoBasesStationandtoClusterHeads%perroundPACKETS_TO_CH(r+1)=0;PACKETS_TO_BS(r+1)=0;figure(1);fori=1:1:n%checkingifthereisadeadnodeif(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;endif(S(i).ENERGY==0)dead_n=dead_n+1;endholdon;elseEJ(r+1)=EJ(r+1)+S(i).E;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%holdon;endend%plot(S(n+1).xd,S(n+1).yd,'x');%nl=n-dead;%p=do*sqrt(xm*ym/(2*pi*nl))/(2/3*xm*ym-(sink.x+sink.y)*sqrt(xm*ym)+sink.x^2+sink.y^2)p=1/(cc+1);STATISTICS(r+1).DEAD=dead;DEAD(r+1)=dead;DEAD_N(r+1)=dead_n;DEAD_A(r+1)=dead_a;%Whenthefirstnodediesif(dead==1)if(flag_first_dead==0)first_dead=rflag_first_dead=1;endendcountCHs=0;cluster=1;fori=1:1:nif(S(i).E0)temp_rand=rand;if((S(i).G)=0)%ElectionofClusterHeadsif(temp_rand=(p/(1-p*mod(r,round(1/p)))))countCHs=countCHs+1;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;packets_TO_BS(cluster)=1;cluster=cluster+1;%CalculationofEnergydissipatedendendendendSTATISTICS(r+1).CLUSTERHEADS=cluster-1;CLUSTERHS(r+1)=cluster-1;%ElectionofAssociatedClusterHeadforNormalNodesfori=1:1:nif(S(i).type=='N'&&S(i).E0)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;forc=1:1:cluster-1temp=sqrt((S(i).xd-C(c).xd)^2+(S(i).yd-C(c).yd)^2);if(tempmin_dis)min_dis=temp;min_dis_cluster=c;endendpackets_TO_BS(min_dis_cluster)=packets_TO_BS(min_dis_cluster)+1;%line([S(i).xd,C(min_dis_cluster).xd],[S(i).yd,C(min_dis_cluster).yd]);%EnergydissipatedbyassociatedClusterHeadmin_dis;Er1=ERX*CM*(cluster+1);if(min_disdo)Et1=ETX*(CM+DM)+Emp*(CM+DM)*min_dis*min_dis*min_dis*min_dis;endif(min_dis=do)Et1=ETX*(CM+DM)+Efs*(CM+DM)*min_dis*min_dis;endS(i).E=S(i).E-Er1-Et1;EJ(r+1)=EJ(r+1)-Er1-Et1;S(i).min_dis=min_dis;S(i).min_dis_cluster=min_dis_cluster;endendend%a=zeros(cluster-1,1)forc=1:1:cluster-1CEr1=ERX*(CM+DM)*(packets_TO_BS(c)-1);distemp=0;forpinkx=0:xm:xmforpinky=0:ym:ymdispink=sqrt((X(c)-pinkx)^2+(Y(c)-pinky)^2);if(dispinkdistemp)distemp=dispink;endendenddistbroadcast(c)=distemp;if(distbroadcast(c)do)CEt1=ETX*CM+Emp*CM*distbroadcast(c)*distbroadcast(c)*distbroadcast(c)*distbroadcast(c);endif(distbroadcast(c)=do)CEt1=ETX*CM+Efs*CM*distbroadcast(c)*distbroadcast(c);endS(C(c).id).E=S(C(c).id).E-CEr1-CEt1;if(S(C(c).id).E=0)%plot(S(i).xd,S(i).yd,'red.');dead=dead+1;if(S(i).ENERGY==1)dead_a=dead_a+1;endif(S(i).ENERGY==0)dead_n=dead_n+1;endholdon;elseEJ(r+1)=EJ(r+1)-CEr1-CEt1;endendSTATISTICS(r+1).DEAD=dead;DEAD(r+1)=dead;DEAD_N(r+1)=dead_n;DEAD_A(r+1)=dead_a;Et=0;Et=Et+S(C(c).id).E;%EJ(r+1)=EJ(r+1)-CEr1-CEt1;%PACKETS_TO_BS(r+1)=packets_TO_BS(c);holdon;%E=EJ(r+1)/nl;%b=zeros(,1)%fori=1:1:cluster-1%if(a(i)E)%b(i)=a(i);%en%endCH=1;while(cluster-120)%contiuemakecluster%selectsecclusterheadsE=Et/(cluster-1);forc=1:1:cluster-1if(S(C(c).id).EE)C(c).type='D';D(CH).xd=X(c);D(CH).yd=Y(c);distance=sqrt((X(c)-S(n+1).xd)^2+(Y(c).yd-S(n+1).yd)^2);D(CH).distance=distance;D(CH).id=C(c).id;X(CH)=C(cluster).xd;Y(CH)=C(cluster).yd;packets_TO_BS(CH)=1;CH=CH+1;endendSTATISTICS(r+1).CLUSTERHEADS=CH-1;CLUSTERHS(r+1)=CH-1;forc=1:1:cluster-1if(C(c).type=='C'&&S(C(c).id).E0)if(CH-1=1)%min_dis=sqrt((C(c).xd-S(
本文标题:wsn中leach算法改进程序
链接地址:https://www.777doc.com/doc-5258408 .html