How to sort out the dead nodes in a WSN and exclude them from the rest of the operations and calculations in the MATLAB code?

3 次查看(过去 30 天)
Hello,
I am trying to build a Wireless Sensor Network (WSN). I started with the code below, but I have some problems with it:
  • The value of the variable, operating_nodes, in the code below keeps decreasing once the control reaches the line (operating_nodes = operating_nodes - 1;) and does not continue to the next line until the while loop condition is false or a certain error occurs, although that I have assigned the initialisation (operating_nodes = n;) and the process on the variable correctly inside the while loop.
  • I used the method described in the MATLAB Answer [1] below to improve the performance of the WSN. Steps: 1 Dividing the whole area into 9 sub-regions, 2 Creating a matrix of the distances (dists(i,f)) between every single node and the 9 centers of the sub-regions, 3 Electing the nearest node to every center of the sub-regions as a cluster head node by finding the minimum distance in the matrix in the one column. Now, I want to elect the second 9 nearest nodes to the centers of the sub-regions (after the first 9 nearest nodes die). So, I am trying to delete every dead node from the matrix of the distances using (dists(i,:) = [ ];) then using the same method that I used in Step 3. I noticed that the size of the matrix, dists(i,f) decreases to a certain size, then the matrix produce the following error: Matrix index is out of range for deletion. How can I sort the dead nodes out and exclude them from the rest of the operations and calculations in the code?
  • When running the code below, the plot shows that only one cluster head died (red dot in the middle of the node 'blue circle') drawn by the function (plot(SN(i).x,SN(i).y, 'red .');) and the whole of the rest nodes become cluster head nodes (green asterisk in the middle of the node 'blue circle').
MATLAB Code:
close all;
clear;
clc;
% Parameters %
n = 50;
rnd = 0;
xm = 100;
ym = 100;
x = 0;
y = 0;
sinkx = xm / 2;
sinky = ym;
Eo = 1;
Eelec = 50 * 10 ^ (-9);
ETx = 50 * 10 ^ (-9);
Eamp = 100 * 10 ^ (-12);
EDA = 5 * 10 ^ (-9);
k = 4000;
operating_nodes = n;
% Centers of the Sub-Regions %
NrGrid = 3; % 9 Sub-Regions
[XC, YC] = ndgrid(xm/(NrGrid*2):xm/NrGrid:xm, ym/(NrGrid*2):ym/NrGrid:ym);
hold on;
% Creating the Sensor Nodes %
for i = 1:1:n
SN(i).x = rand(1,1) * xm;
SN(i).y = rand(1,1) * ym;
SN(i).role = 0; % 0 means normal node, 1 means cluster head node
SN(i).E = Eo;
SN(i).dtch = 0;
SN(i).dts = 0;
plot(x,y,xm,ym,SN(i).x,SN(i).y,'ob',sinkx,sinky,'Xr');
end
% Distances Between the Sensor Nodes and the 9 centers of the Sub-Regions %
for i = 1:1:n
for f = 1:1:numel(YC)
dists(i,f) = sqrt((SN(i).x - XC(f)) .^ 2 + (SN(i).y - YC(f)) .^ 2);
end
end
while operating_nodes > 0
CLheads = 0;
% finding the dead nodes and excluding them %
for i = 1:1:n
if SN(i).E > 0
SN(i).role = 0;
end
if SN(i).E <= 0
dists(i,:) = []; % Removing the row that contains the dead node in the distances array
operating_nodes = operating_nodes - 1
plot(SN(i).x,SN(i).y, 'red .');
end
end
size(dists);
% Array of the closest nodes to the 9 centers of the Sub-Regions %
for f = 1:1:numel(YC)
[mindist, minidx] = min(dists(:,f));
CCH(f) = minidx;
end
% Electing the Cluster Head Nodes %
for i = 1:1:n
if SN(i).E > 0
if ismember(i, CCH(:))
SN(i).role = 1; % assign the current node as a cluster head
SN(i).dts = sqrt((sinkx - SN(i).x) ^ 2 + (sinky - SN(i).y) ^ 2); % distance between the current cluster head and the sink
CLheads = CLheads + 1;
CL(CLheads).x = SN(i).x;
CL(CLheads).y = SN(i).y;
plot(SN(i).x,SN(i).y, 'g*');
end
end
end
% Calculating the Distance Between the Normal Nodes and the Closest Cluster Head Node %
for i = 1:1:n
if SN(i).role == 0 && SN(i).E > 0
for m = 1:1:CLheads
d(m) = sqrt((CL(m).x - SN(i).x) ^ 2 + (CL(m).y - SN(i).y) ^ 2);
end
d = d(1:CLheads);
[M,I] = min(d(:));
[Row, Col] = ind2sub(size(d),I);
SN(i).dtch = d(Col);
end
end
% Energy Dissipation %
for i = 1:1:n
if SN(i).E > 0
% Normal Node %
if SN(i).role == 0
ETx = Eelec * k + Eamp * k * SN(i).dtch ^ 2;
SN(i).E = SN(i).E - ETx;
end
% Cluster Head Node %
if SN(i).role == 1
ETx = (Eelec + EDA) * k + Eamp * k * SN(i).dts ^ 2;
SN(i).E = SN(i).E - ETx;
end
end
end
pause(0.1);
rnd = rnd + 1;
end

回答(1 个)

Walter Roberson
Walter Roberson 2021-1-21
dists(i,:) = []; % Removing the row that contains the dead node in the distances array
dists was created as a symmetric distance matrix. When you remove a node from consideration, you need to remove column i as well as row i
operating_nodes = operating_nodes - 1
plot(SN(i).x,SN(i).y, 'red .');
SN indexing refers to original coordinates, original node numbers, which does not change when you remove entries from dist . So when you remove entries from dist, the indexing gets out of synchronization with dist, so you should not be using the same indexing SN(i) and dist(i,:) . You need a parallel vector that translates between dist row/column indexing and SN indexing.
didx = 1:n;
%...
dists(i,:) = [];
dists(:,i) = [];
nidx = didx(i);
didx(i) = [];
plot(SN(nidx).x, SN(nidx).y, 'r.');

类别

Help CenterFile Exchange 中查找有关 WSNs 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by