You are on page 1of 23

Number of nodes in the space n=200

Initial energy of sensor E0=0.1

Field Dimensions (in meters) xm=100, ym=100

x- and y- Coordinates of the Sink sinkx=1.5xm, sinky=0.5ym

Optimal Election Probability of a node to become cluster head p = 0.2

ETX=5010-9 ERX=5010-9

Eelec=ETX=ERX

Transmit Amplifier types

Efs=1010-12 Emp=0.001310-12

Data Aggregation Energy Values for Heterogeneity

EDA=510-9

Percentage of nodes than are advanced

m=0.5

=1

Maximum number of rounds (iterations you want to run)

rmax=100
n=200;%Enter the number of nodes in the space (in the field)
%Energy Model (all values in Joules)
Eo=0.1;%Initial Energy the initial energy of sensor nJ
xm=100;ym=100;%Field Dimensions - x and y maximum (in meters)
x,y value for area plot
sink.x=1.5*xm;sink.y=0.5*ym;%x and y Coordinates of the Sink
p=0.2;%Optimal Election Probability of a node to become CH
ETX=50e-9;ERX=50e-9;%Eelec=Etx=Erx
Efs=10e-12;Emp=0.0013e-12;%Transmit Amplifier types
EDA=53-9;%Data Aggregation Energy Values for Heterogeneity
m=0.5;%Percentage of nodes than are advanced
a=1;%\alpha
rmax=100;%maximum number of rounds (iterations);

Computation of d0:


0 =

Assume x- and y- Coordinates of the nodes are Randomly Distributed:



=

=

= 1,2, . , =

Initially there are no cluster heads only nodes

= 0

=

do=sqrt(Efs/Emp);%Computation of do
%Creation of the random Sensor Network
figure(10);
for i=1:1:n
S(i).xd=rand(1,1)*xm;
S(i).yd=rand(1,1)*ym;
S(i).G=0;
S(i).type='N';%initially there are no cluster heads only nodes
Random Election of Normal Nodes:
0 + 1
= {
0 (1 + ) < + 1

0 + 1
={
1 < + 1
+1
=
+1
=

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-r');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,'o','MarkerSize',12,...
'MarkerFaceColor','r');
100

90

80

70

60

50

40

30
Sink

20

10

0
0 50 100 150

+ Advanced Node O Normal Node

For the First Iteration:


countCHs = 0 rcountCHs = 0 Number of CHs, and CHs per round
rcountCHs = rcountCHs + countCHs
flag of first dead node = 0

figure(1);
%First Iteration
countCHs=0;%counter for CHs
rcountCHs=0;%counter for CHs per round
cluster=1;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
For rmax of rounds:
Operation for epoch


= 0 , = 0 (, (1/) ) = 0

= 1,2, . , =

= 1,2, . , max =

Initially there are no dead nodes, dead advanced nodes, and dead normal
nodes, therefore their counters are reset to zero
Counters for bit transmitted to Bases Station and to Cluster Heads are
reset to zero
Counters per round for bit transmitted to Bases Station and to Cluster
Heads are reset to zero
for r=0:1:rmax
%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;
dead=0;%Number of dead nodes
dead_a=0;%Number of dead Advanced Nodes
dead_n=0;%Number of dead Normal Nodes
packets_TO_BS=0;%counter for bit transmitted to Bases Station
packets_TO_CH=0;%and to Cluster Heads
%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);
Checking if there is a dead node

0 &

= 0

0 &

= 1

Counters of dead nodes, dead advanced nodes, and dead normal nodes,
will start counting

> 0

> 0 &

= 0 , =

> 0 &

= 1

When the first node dies: flag of first dead node = 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,'^','LineWidth',1,...
'MarkerEdgeColor','k', 'MarkerFaceColor','y',
'MarkerSize',8);
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','LineWidth',1,
'MarkerEdgeColor','k',...
'MarkerFaceColor','g', 'MarkerSize',8);
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'+','LineWidth',3,
'MarkerEdgeColor','k',...
'MarkerFaceColor','r', 'MarkerSize',8);
end
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x','LineWidth',1,
'MarkerEdgeColor','k',...
'MarkerFaceColor','r', 'MarkerSize',8);
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;DEAD_N(r+1)=dead_n;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

Counter for Cluster Heads are reset to zero starting with cluster number 1

> 0

1
(, ( ))

Counters for bit transmitted to Bases Station and to Cluster Heads start
counting

= , = (1/) 1

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))))) %Election of CHs
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;
Calculating the cluster head coordinates:

=

=

Calculating the distance between the cluster head and the sink:

2 2
= ( ) + ( )


=
Calculation of Energy dissipated

+1
4000( + + (4 )) > 0
={
4000( + + (2 )) 0

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
if (distance>do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*(distance^4));
end
if (distance<=do)
S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance^2));
end
Energy_disp(r+1) = S(i).E;
end
end
end
end
Counting for cluster heads in each cluster
After selecting a cluster head the associated normal nodes will be elected
according to the distance

= , > 0 (Check if the node is normal and not dead)

Also (if cluster-1>=1) to start with the node is next to the cluster head

Calculating the minimum distance:


2 2
min = ( ) + ( )
Applying this equation with the nodes in cluster to detect the nearest nodes
2 2
cluster = ( ) + ( )
= (min , cluster )
< min
min = & =

= 1,2, . , 1 =

Energy dissipated by associated Cluster Head



4000( + + (4 )) min > 0
= {
4000( + + (2 )) min 0

Energy dissipated by associated Cluster Head


2
= ( )2 + ( )
= 4000( + + )

=

4000( + + (4 )) > 0
= {
4000( + + (2 )) 0

Counters per round for bit transmitted to Bases Station =number of


nodes-number of dead nodes-number of clusters+1

= min ,
= cluster

Counter for Cluster Heads per rounds (current) = Counter for Cluster Heads per
rounds (pervious) + Counter for Cluster Head

Calculating the average value of of all nodes

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=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
if (min_dis>do)
S(i).E=S(i).E-(ETX*(4000) + Emp*4000*(min_dis^4));
end
if (min_dis<=do)
S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*(min_dis^2));
end
%Energy dissipated
if(min_dis>0)
distance=sqrt((S(C(min_dis_cluster).id).xd-(S(n+1).xd))^2 +...
(S(C(min_dis_cluster).id).yd-(S(n+1).yd) )^2 );
S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E-((ERX +
EDA)*4000 );
if (distance>do)
S(C(min_dis_cluster).id).E=S(C(min_dis_cluster).id).E-...
((ETX+EDA)*(4000)+Emp*4000*(distance^4));
end
if (distance<=do)
S(C(min_dis_cluster).id).E=S(C(min_dis_cluster).id).E-...
((ETX+EDA)*(4000)+Efs*4000*(distance^2));
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;
end
end
end
hold on;
rcountCHs=rcountCHs+countCHs;
sum=0;
for i=1:1:n
if(S(i).E>0)
sum=sum+S(i).E;
end
end
avg=sum/n;
STATISTICS(r+1).AVG=avg;
warning('OFF');%Code for Voronoi Cells
[vx,vy]=voronoi(X(:),Y(:));
plot(X,Y,'g+',vx,vy,'m-');hold on;
voronoi(X,Y);axis([10 xm 0 ym]);
title(['Iteration #' num2str(r) '/' num2str(rmax)])
end
Sink

+ Normal Node Normal Node Cluster Head

First Iteration cluster head election


Dead Node
LEACH STATSTICS
Results of counters
figure(2);
for r=0:1:24
ylabel('Average Energy of Each Node');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');
hold on;
end
figure(3);
for r=0:1:49
ylabel('Average Energy of Each Node');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');
hold on;
end
figure(4);
for r=0:1:74
ylabel('Average Energy of Each Node');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');
hold on;
end
figure(5);
for r=0:1:99
ylabel('Average Energy of Each Node');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red');
hold on;
end
figure(6);
for r=0:1:24
ylabel('Number of Dead Nodes');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');
hold on;
end
figure(7);
for r=0:1:49
ylabel('Number of Dead Nodes');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');
hold on;
end
figure(8);
for r=0:1:74
ylabel('Number of Dead Nodes');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');
hold on;
end
figure(9);
for r=0:1:99
ylabel('Number of Dead Nodes');
xlabel('Round Number');
plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red');
hold on;
end

0.15

0.145

0.14
Average Energy of Each Node

0.135

0.13

0.125

0.12

0.115

0.11
0 5 10 15 20 25
Round Number
0.16

0.15

0.14
Average Energy of Each Node

0.13

0.12

0.11

0.1

0.09

0.08
0 5 10 15 20 25 30 35 40 45 50
Round Number

0.16

0.14
Average Energy of Each Node

0.12

0.1

0.08

0.06

0.04
0 10 20 30 40 50 60 70 80
Round Number
0.16

0.14
Average Energy of Each Node

0.12

0.1

0.08

0.06

0.04

0.02
0 10 20 30 40 50 60 70 80 90 100
Round Number

0.8

0.6

0.4
Number of Dead Nodes

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 5 10 15 20 25
Round Number
14

12

10
Number of Dead Nodes

0
0 5 10 15 20 25 30 35 40 45 50
Round Number

45

40

35
Number of Dead Nodes

30

25

20

15

10

0
0 10 20 30 40 50 60 70 80
Round Number
80

70

60
Number of Dead Nodes

50

40

30

20

10

0
0 10 20 30 40 50 60 70 80 90 100
Round Number

You might also like