📄 eventmodel2d.m
字号:
clear all;
close all;
inf = 1e4; %infinite distance
R = 100; %side of the square region.
N = 400; %number of sensors deployed in the region.
B = 512; %packet size in bits
et = 50e-9 * B; % energy spent in transmission electronics
er = 50e-9 * B; % energy spent in reception
ed = 100e-12 * B; % energy spent in the transmit amplifier
Eo = 3e-3; % intial energy of each sensor node
lifetime = 0; % network lifetime
p = 2; %path loss exponent
X_max = R/2;
Y_max = R/2;
r_sense = 10;
r_radio = 2*r_sense;
X_sink = 0;
Y_sink = 0;
node_data = zeros(9,N); % 1&2 - node co-ordinates, 3- node energy, 4&5 - index and distance to next relay node, 6 - indication that it is within the commn. range of sink. 7 - event sense indicator, 8 - packet ready to transmit, 9-packet generated.
node_data(4,:) = inf;
node_data(5,:) = inf;
node_data(3,:) = Eo;
node_connect = zeros(20,2,N);
%%%%%%%%% Uniform distribution of all nodes in the given square area %%%%%%%%%
for n = 1:N
x = ceil(R*rand(1,1)-(R/2));
y = ceil(R*rand(1,1)-(R/2));
if (x < -X_max | x > X_max | y < -Y_max | y > Y_max | (x == 0 & y == 0))
n = n-1;
else
node_data(1,n) = x;
node_data(2,n) = y;
end
end
hold on
for n = 1:N
plot(node_data(1,n),node_data(2,n),'.');
end
plot(X_sink,Y_sink,'r*');
hold off
%%%%%%%%% Procedure for finding the nearest neighbour of each node %%%%%%%%%
%%%%%%%%% Finding the nodes within the radio range of a particular node %%%%%%%%%%
for i = 1:N
k = 1;
if((node_data(1,i)^2 + node_data(2,i)^2)^0.5 < r_radio)
node_connect(k,2,i) = (node_data(1,i)^2 + node_data(2,i)^2)^0.5;
node_connect(k,1,i) = 0;
k = k+1;
end
for j = 1:N
if(((node_data(1,j) - node_data(1,i))^2 + (node_data(2,j) - node_data(2,i))^2)^0.5 <= r_radio & j ~= i)
node_connect(k,2,i) = (((node_data(1,j) - node_data(1,i))^2 + (node_data(2,j) - node_data(2,i))^2)^0.5);
node_connect(k,1,i) = j;
k = k+1;
end
end
end
k = 0;
%%%%%%%%% Finding those nodes for whom sink is within communication range and sorting them in ascending order %%%%%%%%%
for i = 1:N
if((node_data(1,i)^2 + node_data(2,i)^2)^0.5 < r_radio)
k = k+1;
sink_neighbors(k,1) = i;
sink_neighbors(k,2) = (node_data(1,i)^2 + node_data(2,i)^2)^0.5;
%k = k+1;
node_data(4,i) = 0;
node_data(5,i) = sink_neighbors(k,2);
node_data(6,i) = 1;
end
end
[row col] = size(sink_neighbors);
count = row;
for i = 1:(row-1)
%temp = sink_neighbors(1,:);
for j = (i+1):row
if(sink_neighbors(i,2) > sink_neighbors(j,2))
temp = sink_neighbors(i,:);
sink_neighbors(i,:) = sink_neighbors(j,:);
sink_neighbors(j,:) = temp;
temp = [];
end
end
end
%%%%%%%%% Sorting the nodes in the vicinity of each node in ascending order %%%%%%%%%
[L b h] = size(node_connect);
for i = 1:N
for j = 1:(L-1)
for k = (j+1) : L
if(node_connect(j,2,i) ~= 0 & node_connect(k,2,i) ~= 0)
if(node_connect(j,2,i) > node_connect(k,2,i))
temp = node_connect(j,:,i);
node_connect(j,:,i) = node_connect(k,:,i);
node_connect(k,:,i) = temp;
temp = [];
end
end
end
end
end
node_connect = node_connect(1:25,:,:); % choosing the nearest 25 neighbors of each node
for i = 1:N
sink_distance_i = (node_data(1,i)^2 + node_data(2,i)^2)^0.5; % distance of ith node to sink
for j = 1:25
temp = node_connect(j,:,i);
% sink_distance_neighbor_j = (node_data(1,temp(1,1))^2 + node_data(2,temp(1,1))^2)^0.5; % distance of the node that is jth in the list of connected nodes corr. to ith node.(see node_connect array corr. to ith node).
if(temp(1,2) ~= 0 & temp(1,1) ~= 0)
sink_distance_neighbor_j = (node_data(1,temp(1,1))^2 + node_data(2,temp(1,1))^2)^0.5; % distance of the node that is jth in the list of connected nodes corr. to ith node.(see node_connect array corr. to ith node).
if(node_data(6,i) ~= 1)
if(sink_distance_neighbor_j < sink_distance_i)
node_data(4,i) = temp(1,1);
node_data(5,i) = temp(1,2);
break;
end
else
break; % ith node is connected to sink
end
elseif(temp(1,1) ~= 0)
node_data(4,i) = temp(1,1);
node_data(5,i) = temp(1,2);
else
node_data(4,i) = node_connect(1,1,i);
node_data(5,i) = node_connect(1,2,i);
end
end
if(node_data(5,i) == inf)
node_data(4,i) = node_connect(1,1,i);
node_data(5,i) = node_connect(1,2,i);
end
end
% network_alive = 1 %indicator that network is still alive
%
% while (network_alive == 1)
%
% X_event = ceil(R*rand(1,1) - (R/2));
% Y_event = ceil(R*rand(1,1) - (R/2));
%
% for i = 1:N
%
% if (((X_event - node_data(1,i))^2 + (Y_event - node_data(2,i))^2)^0.5 <= r_sense)
%
% node_data(6,i) = 1;
% node_data(8,i) = 1;
% node_data(7,i) = node_data(7,i) + 1;
%
% end %if an event occurs within the sensing range of a node, it sets its event sense indicator
%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -