You are on page 1of 11

clc

close all
clear all
clear
Prop_delay_SUM = 0 ;
path_loss_SUM = 0 ;
packet_to_BS_SUM = 0;
packet_drop_SUM = 0;
dead_SUM = 0 ;
E_SUM = 0;
allive_SUM = 0;
packet_rcvd_AVG = 0;
P_loss_SUM = 0 ;
PACKETS_DROPD_SUM=0;
PKTS_TO_BS_SUM=0;
DEAD_SUM=0;
E_TOTAL_SUM=0;
Pro_delay_SUM = 0 ;
sink.x = .25;
sink.y = 1 ;
n = 8;
Eo = 0.5 ;
ETX=16.7*0.000000001;
ERX=36.1*0.000000001;
Emp=1.97*0.000000001;
EDA=5*0.000000001;
do = 0.1 ;
lambda = .125
speed = 299792458 ;
flag_first_dead=0;
flag_all_dead=0;
dead=0;
first_dead=0;
teenth_dead=0;
all_dead=0;
thr = 0.1 ;
rmax = 8000;
allive = n;

sink1.x=0.4;
sink1.y=0.9;
node = 8 ;
alliveA = node;
sink1.x=0.4;
sink1.y=0.9;
th=0.7;
P_loss_round = 0 ;
P_delay_round = 0 ;
P_loss = 0 ;
th_temp=98;
rng=0.8062;
% Network establishment
SA(1).xd=0.2;
SA(1).yd=1.2;
SA(2).xd=0.6;
SA(2).yd=1.1;
SA(3).xd=0.7;
SA(3).yd=0.8;
SA(4).xd=0.5;
SA(4).yd=0.6;
SA(5).xd=0.1;
SA(5).yd=0.8;
SA(6).xd=0.3;
SA(6).yd=0.5;
SA(7).xd=0.5;
SA(7).yd=0.3;
SA(8).xd=0.3;
SA(8).yd=0.1;
for i = 1:1:node
distanceA(i)=sqrt((SA(i).xd-(sink1.x) )^2 + (SA(i).yd-(sink1.y) )^2 );
end
for i=1:1:node
SA(i).type='N';
SA(i).E=Eo;
end
SA(node+1).xd=sink1.x;
SA(node+1).yd=sink1.y;
countCHs=0;
.
cluster=1;
dead=0;
allive=node;
packets_TO_BS=0;
packets_TO_CH=0;
Paskets_TO_BS_total=0;
pd=0;

d=0;
s=0;
S(1).xd=0.3;
S(1).yd=0.1;
S(1).P = 1;
S(2).xd=0.5;
S(2).yd=0.3;
S(2).P = 1;
S(3).xd=0.3;
S(3).yd=0.55;
S(3).P = 1;
S(4).xd= .5;
S(4).yd= .55;
S(4).P = 1;
S(7).xd= .37;
S(7).yd= .75;
S(7).P = 2;
S(8).xd= .45;
S(8).yd= .9;
S(8).P = 2;
S(5).xd= .7;
S(5).yd= .8;
S(5).P = 1;
S(6).xd= .1;
S(6).yd= .8;
S(6).P = 1;
for i = 1:1:n
S(i).E = Eo ;
S(i).id = i;
S(i).g = 0 ;
end
a = 1;
b = 1;
x0 = [];
x1 = [];
for i = 1:1:n
if S(i).P ==1
x0{a} = S(i).id;

a = a+1 ;
end
end
for i=1:1:n
end
pd1 = 0;
Packets_to_BS_total = 0;
total_delay_round = 0;

for r=1:1:rmax
path_loss = 0;
P_loss = 0;
PL = 0 ;
delay = 0;
dead = 0;
E =0 ;
E1 = 0 ;
for i=1:1:n
if (S(i).E<=0)
dead=dead+1;
if (dead==1)
if(flag_first_dead==0)
first_dead=r;
flag_first_dead=1;
end
end
if(dead==n)
if(flag_all_dead==0)
all_dead=r;
flag_all_dead=1 ;
end
end
end
if S(i).E>0
S(i).g = 0;
E = E+S(i).E ;
end
end
%E1 = E/(Eo*8) *100 ;
Prop_delay =0 ;

packet_to_BS_per_round = 0;
packet_to_BS = 0 ;
Dead(r+1)=dead;
Allive(r+1)=n-dead;
energy(r+1)=E;
cost_function = 0;
for i=1:1:length(x0)
if(S(i).E > 0 )
cost_function(i) = distance(i)/(S(i).E) ;
end
end
for i=1:1:length(x0)
[min_node,I] = min(cost_function);
node_num = I ;
node_sel(r) = node_num ;
end
if (S(node_num).E>thr )
S(node_num).g = 1 ; % forwarder
packet_to_BS = packet_to_BS+1 ;
distanceCH=sqrt( (S(node_num).xd-(sink.x) )^2 + (S(node_num).yd-(sink.y) )^2 );
S(node_num).E = S(node_num).E - ( (ETX+ERX+EDA)*(4000) +
Emp*3.38*4000*(distanceCH^3.38));
PL(node_num)=10*log(((4*pi*do)/lambda)^2+10*4*log(distanceCH/do))+4.1 ;
delay(node_num) = distanceCH/C ;
end
for i=1:1:length(x0)
if ( S(i).g == 0) && (S(i).E>thr && S(i).P ==1 )
temp = sqrt((S(i).xd - S(node_num).xd)^2 + (S(i).yd-S(node_num).yd)^2 ) ;
temp2 = sqrt((S(i).xd - sink.x)^2 + (S(i).yd-sink.y)^2 ) ;
if (temp <temp2)
S(i).E = S(i).E - ( (ETX)*(4000) + Emp*3.38*4000*(temp^3.38)); % 3.38 => human body
path loss exponent
packet_to_CH = packet_to_CH+1 ;
PL(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(temp/do))+4.1 ;
path_loss = path_loss + PL(i);
delay(i) = temp/C ;
Prop_delay = Prop_delay + delay(i) ;
else
S(i).E = S(i).E - ( (ETX)*(4000) + Emp*3.38*4000*(temp2^3.38)); % 3.38 => human body
path loss exponent
packet_to_BS = packet_to_BS+1;
PL(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(temp2/do))+4.1 ;

path_loss = path_loss + PL(i);


delay(i) = temp2/C ;
Prop_delay = Prop_delay + delay(i) ;
end
end
end
for i = 1:1:n
if (S(i).E > 0 && S(i).P == 2 )
d1 = sqrt((S(i).xd - sink.x)^2 + (S(i).yd-sink.y)^2 ) ;
S(i).E = S(i).E - ( (ETX)*(4000) + Emp*3.38*4000*(d1^3.38)); % 3.38 => human body path loss
exponent
packet_to_BS = packet_to_BS+1 ;
PL(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(d1/do))+4.1 ;
path_loss = path_loss+PL(i) ;
delay(i) = d1 /C ;
Prop_delay = Prop_delay + delay(i) ;
end
end
for i = 1:1:length(x0)
if (S(i).E < thr && S(i).E > 0 )
d = sqrt((S(i).xd - sink.x)^2 + (S(i).yd-sink.y)^2 ) ;
S(i).E = S(i).E - ( (ETX)*(4000) + Emp*3.38*4000*(d^3.38)); % 3.38 => human body path loss
exponent
packet_to_BS = packet_to_BS+1 ;
packet_to_BS = packet_to_BS+1 ;
PL(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(d/do))+4.1 ;
delay(i) = d/C ;
Prop_delay = Prop_delay + delay(i) ;
end
end
% SIMPLE Protocol
path_loss_round(r+1) = path_loss ;
Prop_delay_round(r+1) = Prop_delay ;
packet_to_BS_per_round(r+1)=packet_to_BS;
Packet_to_BS_total=Packet_to_BS_total+packet_to_BS;
packet_to_BS1(r+1)=Packet_to_BS_total;
pkts_rcvd1=0;
pkts_drpd1=0;
P_opt1=0.3;
for j=1:1: packet_to_BS_per_round(r+1)
pr=rand(1,1);

if(pr>=P_opt1)
lnk_status_flag1='good';
pkts_rcvd1=pkts_rcvd1+1;
else
lnk_status_flag1='bad';
pkts_drpd1=pkts_drpd1+1;
end
end
pd1 = pd1+pkts_drpd1;
packet_drop(r+1) =pd1;
deadA=0;
E_total=0;
for i=1:1:node
if (SA(i).E<=0)
deadA=deadA+1;
end
if(SA(i).E>0)
SA(i).type='N';
E_total=E_total+SA(i).E;
end
end
E_TOTAL(r+1)=E_total;
DEADA(r+1)=deadA;
ALLIVEA(r+1)=alliveA-deadA;
packets_TO_BS_per_round=0;
packets_TO_BSA=0;
delay_ATT = 0 ;
total_delay = 0;
for i=1:1:node
ct=90+(rand*10); % ct is between 90 to 100
if (SA(i).E>0 ) % && ct<th_temp
cv=rand;
if (cv>th)
distA=sqrt( (SA(i).xd-(SA(node+1).xd) )^2 + (SA(i).yd-(SA(node+1).yd) )^2 );
SA(i).E=SA(i).E- ( (ETX)*(4000) + Emp*3.38*4000*(distA^3.38));
if(distA > 0)
PL_ATT(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(distA/do))+4.1;
P_loss = P_loss + PL_ATT(i);
delay_ATT(i) = distA/C ;
total_delay = total_delay+delay_ATT(i) ;
end
%S(i).E=S(i).E- ( (ETX)*(4000) + Emp*4000*(dist*dist));
d=i;

packets_TO_BSA=packets_TO_BSA+1;
end
if(cv<=th)
min_dis=Inf;
min_dis_cluster=0;
for c=1:1:node
if i~= c
tempA=min(min_dis,sqrt( (SA(i).xd-SA(c).xd)^2 + (SA(i).yd-SA(c).yd)^2 ) );
if ( tempA<min_dis )
min_dis=tempA;
min_dis_cluster=c;
end
end
end
disA=sqrt( (SA(i).xd-(SA(node+1).xd) )^2 + (SA(i).yd-(SA(node+1).yd) )^2 );
if (min_dis<disA)
disA=sqrt( (SA(i).xd-(SA(min_dis_cluster).xd) )^2 + (SA(i).yd-(SA(min_dis_cluster).yd) )
^2 );
SA(i).E=SA(i).E- ( (ETX)*(4000) + Emp*3.38*4000*(disA^3.38));
PL_ATT(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(disA/do))+4.1 ;
P_loss = P_loss + PL_ATT(i);
delay_ATT(i) = disA/C ;
total_delay = total_delay+delay_ATT(i) ;
s=i;
end
if(min_dis>disA)
disA=sqrt( (SA(i).xd-(SA(n+1).xd) )^2 + (SA(i).yd-(SA(n+1).yd) )^2 );
SA(i).E=SA(i).E- ( (ETX)*(4000) + Emp*3.38*4000*(disA^3.38));
PL_ATT(i)=10*log(((4*pi*do)/lambda)^2+10*4*log(disA/do))+4.1 ;
P_loss = P_loss + PL_ATT(i);
delay_ATT(i) = disA/C ;
total_delay = total_delay+delay_ATT(i) ;

packets_TO_BSA=packets_TO_BSA+1;
end
end
end
end
if (SA(4).E>0)
SA(4).E=SA(4).E- ( (ERX+EDA)*(4000) );
end
if (SA(5).E>0)

SA(5).E=SA(5).E- ( (ERX+EDA)*(4000) );
end
if (SA(7).E>0)
SA(7).E=SA(7).E- ( (ERX+EDA)*(4000) );
end
P_loss_round(r+1) = P_loss ;
total_delay_round(r+1)= total_delay ;
%Prop_delay_round(r+1) = Prop_delay ;
packets_TO_BS_per_round(r+1)=packets_TO_BSA;
Paskets_TO_BS_total=Paskets_TO_BS_total+packets_TO_BSA;
PKTS_TO_BS(r+1)=Paskets_TO_BS_total;
%% Random uniformed model of packet drops starts from here
pkts_rcvd=0;
pkts_drpd=0;
P_opt=0.3;
%Optimal probability to determine link status
for j=1:1:packets_TO_BS_per_round(r+1)
p_r=rand(1,1);
if(p_r>=P_opt)
lnk_status_flag='good';
pkts_rcvd=pkts_rcvd+1;
else
lnk_status_flag='bad';
pkts_drpd=pkts_drpd+1;
end
end
pd=pd+pkts_drpd;
PACKETS_DROPPED(r+1)= pd;
end
Prop_delay_SUM = Prop_delay_round+Prop_delay_SUM ;
path_loss_SUM = path_loss_round+ path_loss_SUM ;
packet_to_BS_SUM=packet_to_BS1+packet_to_BS_SUM;
packet_drop_SUM=packet_drop+packet_drop_SUM;
dead_SUM=Dead+dead_SUM;
E_SUM=E_SUM+energy;
allive_SUM = allive_SUM +Allive ;
Pro_delay_SUM = total_delay_round+Pro_delay_SUM ;
P_loss_SUM = P_loss_round+ P_loss_SUM ;
PKTS_TO_BS_SUM=PKTS_TO_BS+PKTS_TO_BS_SUM;
PACKETS_DROPD_SUM=PACKETS_DROPPED+PACKETS_DROPD_SUM;
DEAD_SUM=DEADA+DEAD_SUM;
E_TOTAL_SUM=E_TOTAL_SUM+E_TOTAL;
path_loss_AVG = path_loss_SUM/5 ;
Pro_delay_AVG = Pro_delay_SUM/5 ;
packet_to_BS_AVG = packet_to_BS_SUM/5;
packet_drop_AVG = packet_drop_SUM/5;
packet_rcvd_AVG = packet_to_BS_AVG-packet_drop_AVG;

dead_AVG = dead_SUM/5;
E_AVG = E_SUM/5;
Allive_AVG = allive_SUM/5 ;
Prop_delay_AVG = Prop_delay_SUM/5 ;
P_loss_AVG = P_loss_SUM/5 ;
PKTS_TO_BS_AVG=PKTS_TO_BS_SUM/5;
PACKETS_DROPD_AVG=PACKETS_DROPD_SUM/5;
PACKETS_RCVD_AVG=PKTS_TO_BS_AVG-PACKETS_DROPD_AVG;
DEAD_AVG=DEAD_SUM/5;
E_TOTAL_AVG=E_TOTAL_SUM/5;
r=0:rmax;
figure(1)
plot(r,dead_AVG,'r-',r,DEAD_AVG,'b-','linewidth',2);
legend('SIMPLE','ATTEMPT');
xlabel('Rounds (r)','FontSize',xylabel,'FontName','Arial');
ylabel('No. of dead nodes','FontSize',xylabel,'FontName','Arial');
axis([0 8000 0 12]);
grid on
figure(4)
plot(r,packet_rcvd_AVG,'r-',r,PACKETS_RCVD_AVG,'b-.','linewidth',2);
legend('SIMPLE','ATTEMPT');
xlabel('Rounds (r)','FontSize',xylabel,'FontName','Arial');
ylabel('Packets received at sink','FontSize',xylabel,'FontName','Arial');
grid on
figure(5)
plot(r,E_AVG,'r-',r,E_TOTAL_AVG,'b-.','linewidth',2);
legend('SIMPLE','ATTEMPT');
xlabel('Rounds (r)','FontSize',xylabel,'FontName','Arial');
ylabel('Residual energy(J)','FontSize',xylabel,'FontName','Arial');
grid on
figure(6)
plot(r,path_loss_AVG,'r',r,P_loss_AVG,'b');
xlabel('Rounds (r)','FontSize',xylabel,'FontName','Arial')
ylabel('Path Loss(dB)','FontSize',xylabel,'FontName','Arial')
legend1=legend('SIMPLE','ATTEMPT');
set(legend1,'FontSize',legendsize)
r=0:rmax;
figure(2)
plot(r,STATISTICS.DEAD,'r',r,DEADA,'b');
xlabel('Rounds','FontSize',xylabel,'FontName','Arial')
ylabel('Dead','FontSize',xylabel,'FontName','Arial')

legend1=legend('Proposed','ATTEMPT');
set(legend1,'FontSize',legendsize)
figure(3)
plot(r,STATISTICS.ALLIVE,'r',r,ALLIVEA,'b');
xlabel('rounds','FontSize',xylabel,'FontName','Arial')
ylabel('Percent of Allive Nodes','FontSize',xylabel,'FontName','Arial')
legend1=legend('Proposed','ATTEMPT');
set(legend1,'FontSize',legendsize)
figure(4)
plot(r,STATISTICS.ENERGY,'r',r,E_TOTAL,'b');
xlabel('Rounds','FontSize',xylabel,'FontName','Arial')
ylabel('Remaining Energy','FontSize',xylabel,'FontName','Arial')
legend1=legend('Proposed','ATTEMPT');
set(legend1,'FontSize',legendsize)