clc;
clear all;
close all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Field Dimensions - x and y maximum (in meters)
xm =30;
ym =30;
%x and y Coordinates of the Sink
sink.x=15;
sink.y=15;
%Number of Nodes in the field
n =24;
%Optimal Election Probability of a node to become cluster head
p=0.05;
packetLength =4000; %bits
%Energy Model (all values in Joules)
Eo = 0.5;%Initial Energy
%Eelec=Etx=Erx
ETX=50*0.000000001;%transmission energy in nJ/bits
ERX=50*0.000000001;%receiving energy in nJ/bits
%Transmit Amplifier types
Efs=10*0.000000000001;%radio propagation through free space in pJ/bits
Emp=0.0013*0.000000000001;%radio propagation through multipath in pJ/bits
%Data Aggregation Energy
EDA=5*0.000000001;%in nJ/bits/symbol
%Values for Hetereogeneity
%m fraction of nodes has alpha times more energy than normal nodes.
%m=1; %Percentage of nodes than are advanced
m=0;
%\alpha
a=1;
%maximum number of rounds
rmax=1999
%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%
%Computation of do
do=sqrt(Efs/Emp);
%Creation of the random Sensor Network
figure(1);
data=xlsread('X24.xls');
inputX=data(1:24);
data=xlsread('Y24.xls');
inputY=data(1:24);
j=floor(m*n);
for i=1:1:j
XR(i)=inputX(1,i);
S(i).xd=XR(i);
YR(i)=inputY(1,i);
S(i).yd=YR(i);
S(i).G=0; %G is set of nodes that have not been selected as a cluster head in last 1/P rounds
%initially there are no cluster heads only nodes
S(i).type='N';
S(i).E=Eo*(1+a);
plot(S(i).xd,S(i).yd,'+');
hold on;
end;
for i=(j+1):1:100
XR(i)=inputX(1,i);
S(i).xd=XR(i);
YR(i)=inputY(1,i);
S(i).yd=YR(i);
S(i).G=0; %G is set of nodes that have not been selected as a cluster head in last 1/P rounds
%initially there are no cluster heads only nodes
S(i).type='N';
S(i).E=Eo;
plot(S(i).xd,S(i).yd,'o');
hold on;
end
S(n+1).xd=sink.x; %sink is a n+1 node, x-axis postion of a node
S(n+1).yd=sink.y; %sink is a n+1 node, y-axis postion of a node
plot(S(n+1).xd,S(n+1).yd,'x'); %location of sink
h=xlabel('X Coordinates in Meters------>');
h=ylabel('Y Coordinates in Meters--------->');
%hold on;
%counter for CHs per round
countCHs=0;
%counter for CHs
rcountCHs=0;
cluster=1;
%countCHs;
rcountCHs=rcountCHs+countCHs; %total CH= CH in current round+CH upto previous round
for r=0:1:rmax
%Operation for epoch (reference point from which time is measured)
if(mod(r, round(1/p))==0)
for i=1:1:n
S(i).G=0;
end
end
%Number of dead nodes
dead=0;
%counter for bit transmitted to Base 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;
for i=1:1:n
%checking if there is a dead node
if (S(i).E<=0)
dead=dead+1;
end
if (S(i).E>0)
S(i).type='N';
end
end
if (dead == n)
break;
end
STATISTICS(r+1).DEAD=dead;
%figure(1);
%Election of Cluster Heads
countCHs=0;
cluster=1;
for i=1:1:n
if(S(i).E>0)
temp_rand=rand;
if ((S(i).G)==0)
%if(temp_rand <=(p/(1-p*mod(r,round(1/p)))))
if(temp_rand <=((p/(1-p*mod(r,round(1/p))))*((S(i).E)/Eo)));
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;
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;
%plot(S(i).xd,S(i).yd,'k*');
%Calculation of Energy dissipated
distance;
if(distance>=do)
S(i).E = S(i).E-((ETX+EDA)*packetLength+ Emp*packetLength*(distance*distance*distance*distance ));
else
S(i).E = S(i).E-((ETX+EDA)*packetLength+ Efs*packetLength*(distance*distance));
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)
min_dis = sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 ); %distance between node & sink
if(cluster-1>=1)
min_dis_cluster = 1;
for c = 1:1:cluster-1
temp = sqrt((S(i).xd - C(c).xd)^2 + (S(i).yd - C(c).yd)^2); %distance between node & cluster head
if (temp<min_dis)
min_dis = temp;
min_dis_cluster = c;
end
end
%Energy dissipated by associated Cluster
min_dis;
if (min_dis > do)
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*(packetLength) + Efs*packetLength*( 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)*packetLength );
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
end
%hold on;
countCHs;
rcountCHs = rcountCHs + countCHs;
end
x=1:1:r;
y=1:1:r;
z=1:1:r;
v=1:1:r;
figure(2);
for i=1:1:r;
x(i)=i;
y(i) = n - STATISTICS(i).DEAD;
z(i)=CLUSTERHS(i);
v(i)=PACKETS_TO_CH(i);
end
%plot(x,y,'r',x,z,'b');
plot(x,y,'r');
%plot(x,v,'g');
grid on;
hold on;
leach.rar_Leach_The Network
版权申诉
166 浏览量
2022-07-14
16:21:29
上传
评论
收藏 2KB RAR 举报
alvarocfc
- 粉丝: 112
- 资源: 1万+