leach分簇算法仿真(基于matlab)leach分簇算法仿真(基于matlab)2007-12-0721:46%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%SEP:AStableElectionProtocolforclustered%%heterogeneouswirelesssensornetworks%%%%(c)GeorgiosSmaragdakis%%WINGgroup,ComputerScienceDepartment,BostonUniversity%%%%Youcanfindfulldocumentationandrelatedinformationat:%%:%%gsmaragd@cs.bu.edu%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ThisistheLEACH[1]codewehaveused.%%ThesamecodecanbeusedforFAIRifm=1%%%%[1]W.R.Heinzelman,A.P.ChandrakasanandH.Balakrishnan,%%"Anapplication-specificprotocolarchitectureforwireless%%microsensornetworks"%%IEEETransactionsonWirelessCommunications,1(4):660-670,2002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%clear;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%PARAMETERS%%%%%%%%%%%%%%%%%%%%%%%%%%%%%FieldDimensions-xandymaximum(inmeters)xm=200;ym=200;%xandyCoordinatesoftheSinksink.x=0.5*xm;sink.y=ym+50;%NumberofNodesinthefieldn=200%OptimalElectionProbabilityofanode%tobecomeclusterheadp=0.05;packetLength=4000;%数据包长度ctrPacketLength=100;%控制包长度%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=1;%\alphaa=1;INFINITY=999999999999999;%maximumnumberofroundsrmax=2000%%%%%%%%%%%%%%%%%%%%%%%%%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;%RandomElectionofNormalNodes%if(temp_rnd0>=m*n+1)S(i).E=Eo;S(i).ENERGY=0;%plot(S(i).xd,S(i).yd,'o');%holdon;%end%RandomElectionofAdvancedNodes%if(temp_rnd0<m*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:rmax%主循环,每次1轮r%Operationforepochif(mod(r,round(1/p))==0)fori=1:1:nS(i).G=0;S(i).cl=0;endendholdoff;%Numberofdeadnodesdead=0;%NumberofdeadAdvancedNodesdead_a=0;%NumberofdeadNormalNodesdead_n=0;%counterforbittransmittedtoBasesStationandtoClusterHeadspackets_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;endifS(i).E>0S(i).type='N';endend%plot(S(n+1).xd,S(n+1).yd,'x');if(dead==n)%节点全部死亡退出循环break;endSTATISTICS(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).E>0)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);%到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));%广播自成为簇头elseS(i).E=S(i).E-(ETX*ctrPacketLength+Efs*ctrPacketLength*(distanceBroad*distanceBroad));end%CalculationofEnergydissipated簇头自己发送数据包能量消耗distance;if(distance>do)S(i).E=S(i).E-((ETX+EDA)*(packetLength)+Emp*packetLength*(distance*distance*distance*distance));elseS(i).E=S(i).E-((ETX+EDA)*(packetLength)+Efs*packetLength*(distance*distance));endpackets_TO_BS=packets_TO_BS+1;PACKETS_TO_BS(r+1)=packets_TO_BS;endendendendSTATISTICS(r+1).CLUSTERHEADS=cluster-1;%统计第r轮簇头数目,r是从0开始的,所以加1;cluster最后要-1,是应为上面的循环多加了1CLUSTERHS(r+1)=cluster-1;%ElectionofAssociatedClusterHeadforNormalNodesfori=1:1:nif(S(i).type=='N'&&S(i).E>0)%普通节点%min_dis=sqrt((S(i).xd-S(n+1).xd)^2+(S(i).yd-S(n+1).yd)^2);%默认距离是到sink的距离min_dis=INFINITY;if(cluster-1>=1)%如果有簇头存在min_dis_cluster=1;%加入最近的簇头forc=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-ETX*ctrPacketLength;end%EnergydissipatedbyassociatedClusterHead普通节点发送数据包到簇头消耗,和加入消息min_dis;if(min_dis>do)S(i).E=S(i).E-(ETX*(ctrPacketLength)