Can anyone help in getting multihop transmission matlab code?

6 visualizaciones (últimos 30 días)
Krishna Kumar
Krishna Kumar el 20 de Abr. de 2022
Comentada: Krishna Kumar el 21 de Abr. de 2022
I have a project that needs multihop transmission of data. I have to do is the node of outer zone should transmit data to the cluster head of middle zone and then from cluster head of the middle zone to the sink node. And the nodes of middle zone should send data to the cluster head that is nearest to the sink node. and the inner zone's node should send data directly to the sink node. I need help to do this code and it is based on multihop and taken help form SEP method.
In the figure, there are 3 types of nodes i.e., Normal nodes of energy Eo=1,Intermediate nodes of Energy=1.5 and Advance nodes of Energy=2.
The nodes position is random.
Cluster head should be taken as the nodes which have maxm energy.
If anyone can help please help me.
Thanks in advance
clear;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 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=20;
%\alpha
a=1;
b=a/2;
ma=0.2; %Percentage of nodes that are advanced
mi=0.3; %Percentage of nodes that are intermediate
mn=0.5; %Percentage of nodes that are normal
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.01;
En=n*Eo*(1-ma-mi); %initial energy of Normal nodes
Ea=Eo*n*ma*(1+a); %initial energy of advanced nodes
Ei=Eo*n*mi*(1+b); %initial energy of intermediate nodes
%Total Energy of En, Ei, Ea
Etotal=n*Eo*(1+mi*b+ma*a);
%Optimal Election Probability of a node to become cluster head
p=0.1;
pnrm=p/(1+ma*a+mi*b); %Probability of Normal nodes
pint=p*(1+b)/(1+ma*a+mi*b); %Probability of Intermediate nodes
padv=p*(1+a)/(1+ma*a+mi*b); %Probability of Advanced nodes
%Zonal Distance
r3=0; %Distance of Sink to Last zone
r2=0; %Distance of Sink to Middle zone
r1=0; %Distance of Sink to First zone
%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;
%maximum number of rounds
rmax=2000;
first_dead=0;
%%%%%%%%%%%%%%%%%%%%%%%%% 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;
S(i).CZ=0;
%initially there are no cluster heads only nodes
S(i).type='N';
temp_rnd0=i;
%Random Election of Normal Nodes
if (temp_rnd0>=mn*n+1)
S(i).E=Eo;
S(i).ENERGY=0;
hold on;
end
%Random Election of Intermediate Nodes
if (temp_rnd0>=ma*n+1) && (temp_rnd0<mn*n+1)
S(i).E=Ei;
S(i).ENERGY=0.5;
hold on;
end
%Random Election of Advanced Nodes
if (temp_rnd0<ma*n+1)
S(i).E=Ea;
S(i).ENERGY=1;
hold on;
end
end
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
maxN = 10000;
dis = zeros(1,maxN);
max_dis=0;
for i=1:1:n
sum1=0;
dis(i)=sqrt((S(i).xd-(S(n+1).xd))^2 + (S(i).yd-(S(n+1).yd))^2);
if(dis(i)<do) %checking the range of nodes within 'do'
number(i)=dis(i);
end
if(dis(i)>max_dis)
max_dis=dis(i);
end
sum1=number(i)+sum1;
%avg distance of ith node to the other nodes
dis_avg(i)=sum1/number(i); %dis_avg is equal to d(i)
end
%First Iteration
figure(1);
%counter for CHs
countCHs=0;
%counter for CHs per round
rcountCHs=0;
cluster=1;
countCHs;
rcountCHs=rcountCHs+countCHs;
flag_first_dead=0;
for r=0:1:rmax
r
%Operation for heterogeneous epoch
if(mod(r, round(1/pnrm) )==0)
for i=1:1:n
S(i).G=0;
S(i).cl=0;
end
end
if(mod(r, round(1/pint) )==0)
for i=1:1:n
if(S(i).ENERGY==0.5)
S(i).G=0;
S(i).cl=0;
end
end
end
%Operations for sub-epochs
if(mod(r, round(1/padv) )==0)
for i=1:1:n
if(S(i).ENERGY==1)
S(i).G=0;
S(i).cl=0;
end
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;
%Number of dead Intermediate Nodes
dead_i=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);
nrg_i=0;
nrg_a=0;
nrg_n=0;
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.5)
dead_i=dead_i+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,'blue o');
end
if (S(i).ENERGY==0.5)
plot(S(i).xd,S(i).yd,'green ^');
end
if (S(i).ENERGY==1)
plot(S(i).xd,S(i).yd,'magenta +');
end
pi=3.14;
r3=max_dis+5;
r2=r3/2;
r1=r2/2;
angle=0:0.001:2*pi;
xp=r1*cos(angle);
yp=r1*sin(angle);
xp1=r2*cos(angle);
yp1=r2*sin(angle);
xp2=r3*cos(angle);
yp2=r3*sin(angle);
plot(S(n+1).xd+xp,S(n+1).yd+yp,S(n+1).xd+xp1,S(n+1).yd+yp1,S(n+1).xd+xp2,S(n+1).yd+yp2,'g','Linewidth',1.5);
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'black x');
STATISTICS(r+1).DEAD=dead;
DEAD(r+1)=dead;
DEAD_N(r+1)=dead_n;
DEAD_A(r+1)=dead_a;
DEAD_I(r+1)=dead_i;
%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;
% Threshold Value %
tn=(pnrm/(1-pnrm*r*mod(1,1/pnrm))); %Threshold of Normal nodes
ti=(pint/(1-pint*r*mod(1,1/pint))); %Threshold of Intermediate nodes
ta=(padv/(1-padv*r*mod(1,1/padv))); %Threshold of Advanced nodes
for i=1:n
if(S(i).E>0)
temp_rand=rand;
if ((S(i).G)<=0)
%Election of Cluster Heads for normal nodes
if((S(i).ENERGY==0 && (temp_rand<=(pnrm/(1-pnrm*mod(r,round(1/pnrm)))))))
S(i).type='C';
S(i).G=100;
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;
if(distance>r1)
S(i).CZ=1;
end
if(dis(i)<=r2)
countCHs=countCHs+1;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
end
elseif(dis(i)>r2 && S(i).CZ==1)
countCHs=countCHs+1;
packets_TO_CH2=packets_TO_CH2+1;
packets_TO_BS2=packets_TO_CH2+packets_TO_BS2;
packets_TO_BS=packets_TO_BS+1;
PACKETS_TO_BS(r+1)=packets_TO_BS;
%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
%Election of Cluster Heads for Intermediate nodes
if( ( S(i).ENERGY==0.5 && ( temp_rand <= ( pint / ( 1 - pint * mod(r,round(1/pint)))))))
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=100;
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
%Election of Cluster Heads for Advanced nodes
if( ( S(i).ENERGY==1 && ( temp_rand <= ( padv / ( 1 - padv * mod(r,round(1/padv)))))))
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=100;
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
end
if nrg_n>0
enrgy_n(i)=nrg_n;
end
if nrg_i>0
enrgy_i(i)=nrg_i;
end
if nrg_a>0
enrgy_a(i)=nrg_a;
end
sum_n=0;
for i=1:first_dead
sum_n=nrg_n(i) + sum_n;
end
En_avg=sum_n/(mn*n); %Average energy of normal nodes
sum_i=0;
for i=1:first_dead
sum_i=nrg_i(i-1) + sum_i;
end
Ei_avg=sum_i/(mi*n); %Average energy of Intermediate nodes
sum_a=0;
for i=1:first_dead
sum_a=nrg_a(i-1) + sum_a;
end
Ea_avg=sum_a/(ma*n); %Average energy of Advanced nodes
Etotal_avg=(Ei_avg+En_avg+Ea_avg)/3; %Total avg energy of all the nodes per round
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
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
end
hold on;
countCHs;
rcountCHs=rcountCHs+countCHs;
  3 comentarios

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Fluid Dynamics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by