%add by me,2014
%簇间多跳;选择超级簇头作为簇头的中继;
%其中必须在d盘新建文件夹:实验结果,文件名有二个,leachmf_300_number.txt
%wst_leach_number,其中初始能量为0.6J.二个文件分别统计存活的节点数;以及总的能量之和。
%在LEACH的基础上做的多跳实现;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Field Dimensions - x and y maximum (in meters)
xm=100;
ym=100;
%x and y Coordinates of the Sink
sink.x=0.5*xm;%基站x轴
sink.y=0.5*ym;%基站y轴
%Number of Nodes in the field
n=100;
%包的大小
packetLength = 4000;%数据包长度
ctrPacketLength = 32;%控制包长度
%Optimal Election Probability of a node
%to become cluster head
p=0.05;
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.02;
%Eelec=Etx=Erx
ETX=50*0.000000000001;
ERX=50*0.000000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000000001;
fid=fopen('D://实验结果//wst_leach_number.txt','w');
fid1=fopen('D://实验结果//wst_leach_energy.txt','w');
%融合率
cc=0.6;
%Values for Hetereogeneity
%Percentage of nodes than are advanced
m=0.1;
%\alpha
a=1;
%maximum number of rounds
rmax=3000;
%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Computation of do
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network
figure(1);
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';
%Random Election of Normal Nodes
S(i).E=Eo;
plot(S(i).xd,S(i).yd,'o');
hold on;
%Random Election of Advanced Nodes
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x');
%First Iteration
%figure(1);
%counter for CHs
countCHs=0;
%counter for CHs per round
rcountCHs=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;
end
end
hold off;
title('图2 数据融合树的节点初始分布图'); %写出此图形代表标题
%Number of dead nodes
dead=0;
figure(2);
for i=1:1:n
%checking if there is a dead node
S(i).Degree = 0;
if (S(i).E<=0)
plot(S(i).xd,S(i).yd,'red .');
dead=dead+1;
hold on;
end
if (S(i).E > 0)
S(i).type='N';
plot(S(i).xd,S(i).yd,'o');
hold on;
end
%每个节点都广播广播距离为do
S(i).E = S(i).E - (ETX * ctrPacketLength + Efs * ctrPacketLength * (do * do ));
%计算出每节点的度
for j=1:1:n
if(S(j).E > 0)
if(sqrt((S(i).xd-(S(j).xd) )^2 + (S(i).yd-(S(j).yd) )^2) < 20)
S(i).Degree = S(i).Degree + 1;
end
end
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
%When the first node dies
if (dead == 1)
if(flag_first_dead == 0)
first_dead = r
flag_first_dead = 1;
end
end
countCHs=0;
cluster=1;
energybegin = 0;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if(mod(r+1, round(1/p) )==0)
temp_rand = 0;
end
distance = sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
if ( (S(i).G)<=0)
%Election of Cluster Heads
if(temp_rand <= (p / (1-p*mod(r,round(1/p))) ))
S(i).type='C';
S(i).G=round(1/p)-1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
%初始状态节点未加入树
C(cluster).state = 0;
plot(S(i).xd,S(i).yd,'k*');
C(cluster).distance=distance;
C(cluster).id=i;
X(cluster)=S(i).xd;
Y(cluster)=S(i).yd;
cluster=cluster+1;
energybegin = energybegin + S(i).E;
%广播自成为簇头
distanceBroad = sqrt(xm*xm+ym*ym);
if (distanceBroad >=do)
S(i).E = S(i).E-(ETX * ctrPacketLength + Emp * ctrPacketLength * (distanceBroad*distanceBroad*distanceBroad*distanceBroad));%广播自成为簇头
else
S(i).E = S(i).E-(ETX * ctrPacketLength + Efs * ctrPacketLength * (distanceBroad*distanceBroad));
end
end
end
end
end
STATISTICS(r+1).CLUSTERHEADS=cluster-1;
CLUSTERHS(r+1)=cluster-1;
%Election of Associated Cluster Head for Normal Nodes
for i=1:1:n
if ( S(i).type=='N' && S(i).E>0 )
if(cluster-1>=1)
min_dis = 1000;
min_dis_cluster=1;
for c=1:1:cluster-1
temp=min(min_dis,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
%普通节点发送数据包到簇头消耗,和加入消息
min_dis;
if (min_dis > do)
S(i).E = S(i).E - (ETX * ctrPacketLength + Emp * ctrPacketLength*( min_dis * min_dis * min_dis * min_dis)); %向簇头发送加入控制消息
S(i).E = S(i).E - (ETX * packetLength + Emp * packetLength*( min_dis * min_dis * min_dis * min_dis)); %向簇头数据包
else
S(i).E = S(i).E -(ETX*(ctrPacketLength) + Efs*ctrPacketLength*( min_dis * min_dis)); %向簇头发送加入控制消息
S(i).E = S(i).E -(ETX*(packetLength) + Efs*packetLength*( min_dis * min_dis)); %向簇头数据包
end
S(i).E = S(i).E - ETX *(ctrPacketLength); %接收簇头确认加入控制消息
%Energy dissipated %簇头接收簇成员数据包消耗能量,接收加入消息和和确认加入消息
if(min_dis > 0)
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - (ERX + EDA) * packetLength ; %接受簇成员发来的数据包
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ERX * ctrPacketLength ; %接收加入消息
if (min_dis > do)%簇头向簇成员发送确认加入的消息
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( ETX * (ctrPacketLength) + Emp * ctrPacketLength*( min_dis * min_dis * min_dis * min_dis));
else
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( ETX * (ctrPacketLength) + Efs * ctrPacketLength*( min_dis * min_dis));
end
PACKETS_TO_CH(r+1) = n - dead - cluster + 1; %所有的非死亡的普通节点都发送数据包
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
line([S(i).xd S(C(min_dis_cluster).id).xd],[S(i).yd S(C(min_dis_cluster).id).yd]);
else
distance1 = sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 );
if (distance1 > do)
S(i).E = S(i).E - Emp * packetLength *( distance1 * distance1 * distance1 * distance1);
else
S(i).E = S(i).E - Efs * packetLength *( distance1 * distance1);
end
end
end
end
hold on;
for c=1:1:cluster
MIN(c) = c;
end
CH_Num(r+1)=cluster; %每轮的簇头数
%统计簇头节点的个数
STATISTICS(r+1).CLUSTERHEADS = cluster-1;
CLUSTERHS(r+1) = cluster-1;
%将所有簇头节点按照距离排列
temp10 = 0;
for c=1:1:cluster-2
for c1=c+1:1:cluster-1
if( C(MIN(c)).distance > C(MIN(c1)).distance)
temp10 = MIN(c1);
MIN(c1) = MIN(c);
MIN(c) = temp10;
end
end
end
for c=1:1:cluster - 1
NH(c) = 0;
end
C(cluster).xd=S(n + 1).xd;
C(cluster).yd=S(n + 1).yd;
%初始状态节点未加入树
C(cluster).state = 1;
C(cluster).distance = 0;
tree = 0;
while(tree ~= cluster - 1)
max = 0;
评论4