Source code for multi hop LEACH protocol in wireless sensor networks
11 ビュー (過去 30 日間)
古いコメントを表示
Source code for multi hop LEACH protocol in wireless sensor networks
1 件のコメント
Walter Roberson
2021 年 11 月 28 日
The paper https://iopscience.iop.org/article/10.1088/1757-899X/1010/1/012014/pdf indicates that they ran tl-leach simulations in MATLAB. You could write to the authors and ask if they will make their code available.
回答 (2 件)
Eng.Miasser
2023 年 1 月 17 日
編集済み: Walter Roberson
2023 年 8 月 8 日
clc;
clear;
close all ;
warning off all;
tic;
%% create sensor nodes , Set Parameters and create Energy model
%% initial parameters
n = 100 ; %Number of Nodes in the field
[Area,Model]=setParameters(n); % set Parameter sensors and network
%configuration Sensors
CreateRandomSen(Model,Area); %create a random scenario
load locations %load sensor location
Sensors= ConfigureSensors(Model , n , X , Y );
ploter(Sensors, Model); %Plot sensors
% parameter initialization
countCHs=0 ; %counter for CHs
flag_first_dead=0; %flag_first_dead
deadNum =0 ; %Number of dead nodes
initEnergy=0; %Initial Energy
for i=1:n
initEnergy = Sensors(i).E+initEnergy;
end
SRP =zeros(1,Model , rmax); %number of sent routing packets
RRP = zeros(1 , Model , rmax); %number of receive routing packets
SDP = zeros(1, Model , rmax); %number of sent data packets
RDP = zeros(1,Model ,rmax); %number of receive data packets
Sum_DEAD =zeros(1,Model.rmax);
CLUSTERHS = zeros(1, Model.rmax);
AllSensorEnergy = zeros(1 , Model.rmax);
%start simulation
globe srp rrp sdp rdp
srp=0 ; %counter number of sent routing packets
rrp=0; %counter number of receive routing packets
sdp=0; %counter number of sent data packets
rdp=0; % counter number of receive data packets
%sink broadcast start message to all nodes
Sender= n+1 ; %sink
Receiver= 1:n ; %All nodes
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Hello' , Receiver);
%All sensor send location information to sink
Sensors=disToSink(Sensors , Model);
%Sender = 1:n ; %All nodes
%Receiver = n+1; %Sink
%Sensors = SendReceivePackets(Sensors , Model , Sender , 'Hello' ,Receiver);
%Save metrics
SRP(1) = srp;
RRP(1) = rrp;
SDP(1) = sdp;
RDP(1) = rdp;
%Main loop program
for r=1:1:Model.rmax
% Initialization
%This section operate for each epoch
member=[]; %Member of each cluster in per period
coutCHs=0; %Number of CH in per period
%counter for bit transmitted to bases station and cluster Heads
srp=0 ; %counter number of sent routing packets
rrp=0; %counter number of receive routing packets
sdp=0; %counter number of sent data packets to sink
rdp=0; % counter number of receive data packets by sink
%initialization per round
SRP(r+1) = srp;
RRP(r+1) = rrp;
SDP(r+1) = sdp;
RDP(r+1) = rdp;
pause(0.001) %pause simulation
hold off; %clear figure
Sensors=resetSensors(Sensors, Model);
% allow to sensor to become cluster-head LEACH algorithm
AroundClear =10 ;
if(mod(r, AroundClear) == 0)
for i=1:1:n
Sensors(i).G=0;
end
end
%plot Sensors
deadNum=ploter(Sensors, Model);
% save r'th period when the first node dies
if(deadNum >= 1)
if(flag_first_dead == 0)
first_dead = r ;
flag_first_dead =1 ;
end
end
% cluster head election
% Selection Candidate Cluster Head Based on LEACH set-up Phase
[TotalCH , Sensors] = SelectCH(Sensors , Model , r);
% Broadcasting CHs to All Sensors that are in Radio Rage CH
for i=1:length(TotalCH)
Sender=TotalCH(i).id ;
SenderRR= Model.RR ;
Receiver = findReceiver(Sensors , Model , Sender , SenderRR);
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Hello' , Receiver);
end
% Sensors join to nearest CH
Sensors = JoinToNearestCH(Sensors , Model , TotalCH);
% End of cluster head election phase
% plot network status in end of set-up phase
for i=1:n
if(Sensors(i).type == 'N' && Sensors(i).dis2ch<Sensors(i).dis2sink && Sensors(i).E>0)
XL = [Sensors(i).xd , Sensors(Sensors(i).MCH).xd];
YL = [Sensors(i).yd , Sensors(Sensors(i).MCH).yd];
hold on
line(XL , YL)
end
end
% Steady-State phase
NumPacket = Model.NumPacket ;
for i = 1 :1 : 1 % NumPacket
%Plotter
deadNum = ploter(Sensors , Model);
% All sensors send data packet to CH
for j = 1 :length(TotalCH)
Receiver = TotalCH(j).id ;
Sender = findSender(Sensors , Model , Receiver);
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Data' , Receiver);
end
end
% Send data packet from CH to sink after Data aggregation
for i = 1: length(TotalCH)
Receiver= n+1 ; %sink
Sender = TotalCH(i).id ; %CH
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Data' , Receiver);
end
% send data packet directly from other nodes (that are not in each cluster ) to sink
for i = 1 : n
if(Sensors(i).MCH == Sensors(n+1).id)
Receiver = n+1 ; % sink
Sender = Sensors(i).id ; % other nodes
Sensors = SendReceivePackets(Sensors , Model , Sender , 'Data' , Receiver);
end
end
% Statistics
Sum_DEAD(r+1) = deadNum ;
SRP(r+1) = srp;
RRP(r+1) = rrp;
SDP(r+1) = sdp;
RDP(r+1) = rdp;
CLUSTERHS(r+1) = countCHs ;
alive = 0 ;
SensorEnergy = 0;
for i=1:n
if Sensors(i).E>0
alive= alive + 1 ;
SensorEnergy = SensorEnergy + Sensors(i).E ;
end
end
aliveSensors(r) = alive; % OK
SumEnergyAllSensor(r+1) = SensorEnergy ;
AvgEnergyAllSensor(r+1) = SensorEnergy / alive ;
ConsumEnergy(r+1) = ( initEnergy-SumEnergyAllSensor(r+1))/n ;
En=0 ;
for i =1:n
if Sensors(i).E>0
En = En+ Sensors(i).E-AvgEnergyAllSensor(r+1))^2 ;
end
end
Enheraf(r+1) = En/ alive ;
title(sprintf( 'Round=%d , Dead nodes = %d' , r+1 , deadNum));
%dead
if(n==deadNum)
lastPeriod = r ;
break;
end
end % for r=0:1:rmax
% end % for r=0:1:rmax
disp('End of simulation');
toc;
disp('create report ')
filename= sprintf('leach%d.mat' , n);
% save report
save(filename);
3 件のコメント
hassan
2023 年 11 月 2 日
Give the error: undefined parameter or function at [Area,Model]=setParameters(n)
Walter Roberson
2023 年 11 月 2 日
編集済み: Walter Roberson
2024 年 9 月 27 日
vahid ghasemi
2024 年 9 月 26 日
how to run:
1- download the implementation of Amin (mamnum amin, kheili sadast ama shaiad kar rah andazam bashe! ki che midune refigh) from: https://www.mathworks.com/matlabcentral/fileexchange/69778-wireless-sensor-networks-simulation. and extract it somwhere.
2-in the same directory of Amin's leach, run the above code (thanks Walter, but i'm not sure of its soundness due to primitive following errors).
3-change "zeros(1 , Model , rmax)" to "zeros(1 , Model.rmax)"
4-change "globe srp rrp sdp rdp" to "global srp rrp sdp rdp"
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!