%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% SEP: 一种适用于集群百分比异构无线传感器网络的稳定选举协议 %
%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
%
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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;
sink.y=0.5*ym;
%Number of Nodes in the field
n=100
%Optimal Election Probability of a node
%to become cluster head
p=0.1;
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.5;
%Eelec=Etx=Erx
ETX=50*0.000000001;
ERX=50*0.000000001;
%Transmit Amplifier types
Efs=10*0.000000000001;
Emp=0.0013*0.000000000001;
%Data Aggregation Energy
EDA=5*0.000000001;
%Values for Hetereogeneity
%Percentage of nodes than are advanced
m=0.1;
%\alpha
a=1;
%maximum number of rounds
rmax=50;
%%%%%%%%%%%%%%%%%%%%%%%%% 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';
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');
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;
end
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);title('第一次迭代');
%counter for CHs
countCHs=0;
%counter for CHs per round
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
allive=n;
for r=0:1:rmax
r
%
total=0;
for i=1:1:n
total=total+S(i).E;
end
STATISTICS.TOTAL(r+1)=total;
%
%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,'red .');
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');
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+');
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
STATISTICS.DEAD(r+1)=dead;
STATISTICS.ALLIVE(r+1)=allive-dead;
STATISTICS.DEAD(r+1)=dead;
STATISTICS.DEAD_N(r+1)=dead_n;
STATISTICS.DEAD_A(r+1)=dead_a;
%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;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ( (S(i).G)<=0)
%Election of Cluster Heads
if(temp_rand<= (p/(1-p*mod(r,round(1/p)))))
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
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;
cluster=cluster+1;
%Calculation of Energy dissipated
distance;
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance ));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance ));
end
end
end
end
end
STATISTICS.CLUSTERHEADS(r+1)=cluster-1;
STATISTICS.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=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );
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
end
%Energy dissipated by associated Cluster Head
min_dis;
if (min_dis>do)
S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis));
end
%Energy dissipated
if(min_dis>0)
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 );
PACKETS_TO_CH(r+1)=n-dead-cluster+1;
end
S(i).min_dis=min_dis;
S(i).min_dis_cluster=min_dis_cluster;
end
end
STATISTICS.PACKETS_TO_BS(r+1)=packets_TO_BS;
end
hold on;
countCHs;
rcountCHs=rcountCHs+countCHs;
%Code for Voronoi Cells
%Unfortynately if there is a small
%number of cells, Matlab's voronoi
%procedure has some problems
[vx,vy]=voronoi(X,Y);
plot(X,Y,'r*',vx,vy,'b-');
hold on;
voronoi(X,Y);
axis([0 xm 0 ym]);
end
r=0:rmax;
figure(2);
plot(r,STATISTICS.ALLIVE,'-r');title('STATISTICS.ALLIVE');
figure(3);
plot(r,STATISTICS.PACKETS_TO_BS,'-r');title('STATISTICS.PACKETS_TO_BS');
figure(4);
plot(r,STATISTICS.DEAD,'-r');title('STATISTICS.DEAD');
figure(5);
plot(r,STATISTICS.TOTAL,'-r');title('STATISTICS.TOTAL');
figure(6);
subplot(2,2,1);
plot(r,STATISTICS.ALLIVE,'-r');
xlabel('no. of rounds');
ylabel('活着的节点');
subplot(2,2,2);
plot(r,STATISTICS.PACKETS_TO_BS,'-r');
xlabel('no. of rounds');
ylabel('packets to bs');
subplot(2,2,3);
plot(r,STATISTICS.DEAD,'-r');
xlabel('no. of rounds');
ylabel('死亡节点');
subplot(2,2,4);
plot(r,STATISTICS.TOTAL,'-r');
xlabel('no. of rounds');
ylabel('总的能量');
%fprintf('ROUND NO.(when first node dead)%d\t',first_dead);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STATISTICS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% DEAD : a rmax x 1 array of number of dead nodes/round
% DEAD_A : a rmax x 1 array of number of dead Advanced nodes/round
% DEAD_N : a rmax x 1 array of number of dead Normal nodes/round
% CLUSTERHS : a rmax x 1 arra
Matlab领域
- 粉丝: 3w+
- 资源: 3577
最新资源
- 【企业文化分析模型-1】克拉克洪—斯托特柏克构架.docx
- 【企业文化分析模型-7】郑伯埙的VOCS量表.docx
- 【企业文化分析模型-3】弗恩斯·特朗皮纳斯的组织文化模型.docx
- 【企业文化分析模型-2】霍夫斯泰德的组织文化模型.docx
- 【范文】企业文化管理制度2.docx
- 【学习】如何创建学习型组织2.ppt
- 《华为的企业文化》-27页.ppt
- 【案例分析】惠普—康柏企业文化整合方案-29页 英文.ppt
- 【案例分析】沃尔玛的企业文化.ppt
- 【培训课件】华夏基石—企业文化落地与传播-68页.ppt
- 【培训课件】联想核心价值观培训(2006年)-75页.ppt
- 惠普-文化尽职调查研究-29页 英文版本.PPT
- 【培训课件】企業文化---培訓教材.ppt
- 前端技术实现圣诞树与飘雪花特效
- 【培训课件】中兴通讯-企业文化建设方案-42页.ppt
- 《白沙企业文化大纲》-21页.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈