无线传感器网络节能路由协议研究 仿真程序 毕设等

更新时间:2023-10-27 03:24:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

clear; clear;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%% w=4;

%Field Dimensions - x and y maximum (in meters)100*100的区域 xm = 100; ym = 100;

%x and y Coordinates of the Sink sink节点的坐标 sink.x =100; sink.y =200;

%Number of Nodes in the field 总共200个节点 n=50*w;

%Optimal Election Probability of a node %to become cluster head 簇节点选择概率 p=0.05;

packetLength = 4000;%数据包长度 ctrPacketLength = 100;%控制包长度 %Energy Model (all values in Joules) %Initial Energy 初始能量 Eo = 0.5;

?lec=Etx=Erx enode0 = 0;

ETX=50*0.000000001; Pnj/bit ERX=50*0.000000001; Pnj/bit

%Transmit Amplifier types

Efs=10*0.000000000001; pj/bit/m2 Emp=0.0013*0.000000000001;%0.0013pj/bit/m4 úta Aggregation Energy

EDA=5*0.000000001; %5nj/bit/signal

%Values for Hetereogeneity

%Percentage of nodes than are advanced %m=1; %\\alpha %a=1;

INFINITY = 999999999999999;

%maximum number of rounds 最大轮询次数 rmax=3000; node_1=0; node_2=0; node_3=0; node_1_x=0; node_2_x=0; node_3_x=0; node_1_z=0; node_2_z=0; node_3_z=0;

%%%%%%%%%%%%%%%%%%%%%%%%%

END

PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%

%Computation of do 一个距离的计算根据能量公式参考wendi的phd论文 do=sqrt(Efs/Emp);%取平方根

%Creation of the random Sensor Network

OF

%figure(1);

for i=1:1:n%n=200 200个节点数 设置节点位置及初始能量和状态

S(i).xd=rand(1,1)*xm;%坐标x,rand(1,1)产生一个0,1之间的随机数 s(i)表示的是数组 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; %每个节点的能量保持为0.05 该数组表示的是能量的存贮 % S(i).ENERGY=0; %初始消耗的能量???未知ing % end

%Random Election of Advanced Nodes priority(i) = randint(1,1,[1,3]); figure(1);

plot(S(i).xd,S(i).yd,'+'); hold on; end

node_1_x = node_1; node_2_x = node_2; node_3_x = node_3;

S(n+1).xd=sink.x; %基站位置x坐标 S(n+1).yd=sink.y; %基站位置y坐标 %First Iteration

%figure(1);

%counter for CHs total簇头个数 countCHs=0;

%counter for CHs per round 每轮的簇头个数 rcountCHs=0; cluster=1;

rcountCHs=rcountCHs+countCHs; flag_first_dead_old=0; flag_first_dead=0; node_p1 = 0; node_p2 = 0; node_p3 = 0; node_p1_x = 0; node_p2_x = 0; node_p3_x = 0;

for r=0:1:rmax %主循环,每次1轮 r;

ENOLD(r+1)=0; enode0=0; for i=1:1:n;

ENOLD(r+1)=S(i).E+ENOLD(r+1);%第r轮所有节点的总能量 end

EAOLD(r+1)=ENOLD(r+1)/n; %这个是第r轮的平均能量 %Operation for epoch

if(mod(r, round(1/p) )==0) 的整数倍轮数时 for i=1:1:n

S(i).G=0;%还未当选簇头的节点集合,因为是要判断前1/p轮中是否有节点时簇节点 S(i).cl=0;

%plot(S(i).xd,S(i).yd,'*');%这是绘制节点位置 end end

hold off;

%Number of dead nodes 每一轮开始的时候将该轮的统计参数清零 dead=0; dead_old=0;

%Number of dead Advanced Nodes Tad_a=0;

%Number of dead Normal Nodes dead_n=0; dead_n_old=0; node_p1 = 0; node_p2 = 0; node_p3 = 0;

%counter for bit transmitted to Bases Station and to Cluster Heads

%counter for bit transmitted to Bases Station and to Cluster Heads %per round

%figure(1);

for i=1:1:n

%checking if there is a dead node if S(i).E>0

S(i).type='N';%如果节点能量大于0,则类型为正常 end end

%countCHs=0; cluster=1; for i=1:1:n if(S(i).E>0)

temp_rand=rand;

if ( (S(i).G)<=0) %如果该节点在候选集合中 保证钱1/p轮没该节点不是簇节点 %Election of Cluster Heads

if( 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的距离,因为是簇节点所以直接到sink节点 C(cluster).distance = distance;

C(cluster).id = i;%表示第i个节点时簇节点 X(cluster)=S(i).xd; Y(cluster)=S(i).yd;

cluster=cluster+1; %广播自成为簇头 distanceBroad

50;%sqrt((S(i).xd)^2+(S(i).yd)^2);???????????????广播包的距离是50

%%%需要判断所剩的能量是否足以发送

if(S(i).E>=(

ETX

*

ctrPacketLength

+

Emp*

=

ctrPacketLength*( distanceBroad*distanceBroad*distanceBroad*distanceBroad )) || S(i).E>=( ETX * ctrPacketLength + Emp* ctrPacketLength*( distanceBroad*distanceBroad)))

if (distanceBroad > do)%大于极限距离就是4次方,小于则是2次方 发送控制比特 也就是广播包,广播给其他节点,不是给sink 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 else

S(i).E = 0; end if(S(i).E

>=

(

(ETX+EDA)*(packetLength)

+

Emp

*

packetLength*( distance*distance*distance*distance )) || S(i).E >= ( (ETX+EDA)*(packetLength) + Emp * packetLength*( distance*distance )))

êlculation of Energy dissipated 簇头自己发送数据包能量消耗 distance;%这是到sink节点的距离

if (distance > do)%发送数据到sink节点所剩的能量 S(i).E

=

S(i).E-

(

(ETX+EDA)*(packetLength)

+

Emp

*

packetLength*( distance*distance*distance*distance )); else S(i).E packetLength*( distance * distance )); end else

S(i).E = 0; end end end end end

STATISTICSOLD(r+1).CLUSTERHEADS = cluster-1;%统计第r轮簇头数目,r是从0开始的,所以加1;cluster最后要-1,是应为上面的循环多加了1 CLUSTERHSOLD(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 ) %普通节点

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; %加入最近的簇头

for c = 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 ); %判断到已有

=

S(i).E-

(

(ETX+EDA)*(packetLength)

+

Efs

*

簇节点的距离

if ( temp < min_dis ) min_dis = temp;

min_dis_cluster = c; %标记簇节点标号(节点所属的簇节点)通过循环找到到簇节点距离最小的簇节点 end

%接收簇头发来的广播的消耗

S(i).E = S(i).E - ETX * ctrPacketLength; end

energy_1 = ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis * min_dis * min_dis * min_dis) + ETX*(packetLength) + Emp*packetLength*( min_dis * min_dis * min_dis * min_dis);

energy_2 = ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis * min_dis ) + ETX*(packetLength) + Emp*packetLength*( min_dis * min_dis);

%Energy dissipated by associated Cluster Head普通节点发送数据包到簇头消耗,和加入消息

%min_dis; if(S(i).E

>=

energy_1+ETX*(ctrPacketLength)

||

S(i).E

>=

energy_1+ETX*(ctrPacketLength))

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); %接收簇头确认加入控制消息 else

S(i).E = 0; end

%Energy dissipated %簇头接收簇成员数据包消耗能量,接收加入消息和和确认加入消息

if(min_dis > 0)%这就是簇节点接收加入信息和数据的能量 if(S(i).E >= (ERX + EDA)*packetLength + ERX *ctrPacketLength) S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E - ( (ERX + EDA)*packetLength ); %接受簇成员发来的数据包 S(C(min_dis_cluster).id).E = *ctrPacketLength ; %接收加入消息

if (min_dis > do)%簇头向簇成员发送确认加入的消息 S(C(min_dis_cluster).id).E

=

S(C(min_dis_cluster).id).E

-

S(C(min_dis_cluster).id).E

-

ERX

( 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 else

S(i).E = 0; 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;%i 节点所属的簇节点号

for i=1:1:n if(S(i).E_x>0) temp_rand=rand;

if ( (S(i).G_x)<=0) %如果该节点在候选集合中 保证钱1/p轮没该节点不是簇节点 if( temp_rand <= ((p/(1-p*mod(r,round(1/p))))*(0.99*(dis_max-dis_bs(i))/(dis_max) + 0.01*(S(i).E_x)/0.5)))

countCHs_x = countCHs_x+1;%簇节点数加一

S(i).type_x = 'C';%该节点的类型变为簇节点 S(i).G_x = round(1/p)-1; %簇头节点的位置 C_x(cluster_x).xd = S(i).xd; C_x(cluster_x).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的距离,因为是簇节点所以直接到sink节点

C_x(cluster_x).distance = distance;

C_x(cluster_x).id = i;%表示第i个节点时簇节点 X_x(cluster_x)=S(i).xd; Y_x(cluster_x)=S(i).yd; cluster_x=cluster_x+1; %广播自成为簇头 distanceBroad

50;%sqrt((S(i).xd)^2+(S(i).yd)^2);???????????????广播包的距离是50

%%%需要判断所剩的能量是否足以发送

if(S(i).E_x

>=

ETX

*

ctrPacketLength

+

Emp*

=

ctrPacketLength*( distanceBroad*distanceBroad*distanceBroad*distanceBroad) || S(i).E_x >=

ETX * ctrPacketLength + Emp* ctrPacketLength*( distanceBroad*distanceBroad))

if (distanceBroad > do)%大于极限距离就是4次方,小于则是2次方 发送控制比特 也就是广播包,广播给其他节点,不是给sink

S(i).E_x = S(i).E_x- ( ETX * ctrPacketLength + Emp* ctrPacketLength*( distanceBroad*distanceBroad*distanceBroad*distanceBroad ));%广播自成为簇头 else

S(i).E_x = S(i).E_x- ( ETX * ctrPacketLength + Efs * ctrPacketLength*( distanceBroad*distanceBroad)); end else

S(i).E_x =0; end

êlculation of Energy dissipated 簇头自己发送数据包能量消耗 distance;%这是到sink节点的距离 if(S(i).E_x

>=

(ETX+EDA)*(packetLength)

+

Emp

*

packetLength*( distance*distance*distance*distance ) || S(i).E_x >= (ETX+EDA)*(packetLength) + Efs * packetLength*( distance * distance ))

if (distance > do)%发送数据到sink节点所剩的能量

S(i).E_x = S(i).E_x- ( (ETX+EDA)*(packetLength) + Emp * packetLength*( distance*distance*distance*distance )); else

S(i).E_x = S(i).E_x- ( (ETX+EDA)*(packetLength) + Efs * packetLength*( distance * distance )); end else

S(i).E_x = 0; end end

end end end

STATISTICSOLD_x(r+1).CLUSTERHEADS = cluster_x-1;%统计第r轮簇头数目,r是从0开始的,所以加1;cluster最后要-1,是应为上面的循环多加了1 CLUSTERHSOLD_x(r+1)= cluster_x-1;

%Election of Associated Cluster Head for Normal Nodes for i=1:1:n

if ( S(i).type_x=='N' && S(i).E_x>0 ) %普通节点

min_dis_x = sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 );%默认距离是到sink的距离

%min_dis_x = INFINITY;

if(cluster_x -1 >= 1)%如果有簇头存在 min_dis_cluster = 1; %加入最近的簇头

for c = 1:1:cluster_x - 1 %簇头数量一共是cluster - 1

%temp = min(min_dis_x,sqrt( (S(i).xd - C_x(c).xd)^2 + (S(i).yd - C_x(c).yd)^2 ) ); temp = sqrt( (S(i).xd - C_x(c).xd)^2 + (S(i).yd - C_x(c).yd)^2 ); %判断到已有簇节点的距离

if ( temp < min_dis_x ) min_dis_x = temp;

min_dis_cluster = c; %标记簇节点标号(节点所属的簇节点)通过循环找到到簇节点距离最小的簇节点 end

%接收簇头发来的广播的消耗

S(i).E_x = S(i).E_x - ETX * ctrPacketLength; end

%Energy dissipated by associated Cluster Head普通节点发送数据包到簇头消耗,和加入消息

min_dis_x;

energy_1x = ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis_x * min_dis_x * min_dis_x * min_dis_x) + ETX*(packetLength) + Emp*packetLength*( min_dis_x * min_dis_x * min_dis_x * min_dis_x);

energy_2x = ETX*(ctrPacketLength) + Efs*ctrPacketLength*( min_dis_x * min_dis_x) + ETX*(packetLength) + ETX*(packetLength) + Efs*packetLength*( min_dis_x * min_dis_x); if(S(i).E_x >= energy_1x + ETX*(ctrPacketLength) || S(i).E_x >= energy_2x + ETX*(ctrPacketLength))

if (min_dis_x > do)%每个节点需要发送一个回应加入群的包和传输数据包的能量

S(i).E_x

=

S(i).E_x

-

(

ETX*(ctrPacketLength)

+

Emp

*

ctrPacketLength*( min_dis_x * min_dis_x * min_dis_x * min_dis_x)); %向簇头发送加入控制消息 S(i).E_x

=

S(i).E_x

-

(

ETX*(packetLength)

+

Emp*packetLength*( min_dis_x * min_dis_x * min_dis_x * min_dis_x)); %向簇头数据包 else S(i).E_x

=

S(i).E_x

-

(

ETX*(ctrPacketLength)

+

Efs*ctrPacketLength*( min_dis_x * min_dis_x)); %向簇头发送加入控制消息

S(i).E_x = S(i).E_x - ( ETX*(packetLength) + Efs*packetLength*( min_dis_x * min_dis_x)); %向簇头数据包 end

S(i).E_x = S(i).E_x - ETX*(ctrPacketLength); %接收簇头确认加入控制消息 else

S(i).E_x = 0; end

energy_3x = (ERX + EDA)*packetLength + ERX *ctrPacketLength + ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis_x * min_dis_x * min_dis_x * min_dis_x);

energy_4x = (ERX + EDA)*packetLength + ERX *ctrPacketLength +

ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis_x * min_dis_x);

if(S(C_x(min_dis_cluster).id).E_x >= energy_3x || S(C_x(min_dis_cluster).id).E_x >= energy_4x)

%Energy dissipated %簇头接收簇成员数据包消耗能量,接收加入消息和和确认加入消息

if(min_dis_x > 0)%这就是簇节点接收加入信息和数据的能量

S(C_x(min_dis_cluster).id).E_x = S(C_x(min_dis_cluster).id).E_x - ( (ERX + EDA)*packetLength ); %接受簇成员发来的数据包

S(C_x(min_dis_cluster).id).E_x = S(C_x(min_dis_cluster).id).E_x - ERX *ctrPacketLength ; %接收加入消息

if (min_dis_x > do)%簇头向簇成员发送确认加入的消息

S(C_x(min_dis_cluster).id).E_x = S(C_x(min_dis_cluster).id).E_x - ( ETX*(ctrPacketLength) + Emp * ctrPacketLength*( min_dis_x * min_dis_x * min_dis_x * min_dis_x));

else

S(C_x(min_dis_cluster).id).E_x = S(C_x(min_dis_cluster).id).E_x - ( ETX*(ctrPacketLength) + Efs * ctrPacketLength*( min_dis_x * min_dis_x)); end

PACKETS_TO_CH(r+1) = n - dead_x - cluster_x + 1; %所有的非死亡的普通节点都发送数据包 end else

S(C_x(min_dis_cluster).id).E_x = 0; end

S(i).min_dis_x = min_dis_x;

S(i).min_dis_cluster = min_dis_cluster;%i 节点所属的簇节点号 % end else

本文来源:https://www.bwwdw.com/article/cfn2.html

Top