1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
clc;
clear all;
close all;
%% Parameters
% Area
A1 = 100;
N=50;% Number of nodes
data = rand(N,2); % Randomly generated n no. of nodes
x = data(:,1)*100;
y = data(:,2)*100;%1000
% Define x and y cordinate of basestation
BSx = A1/2;
BSy = A1+10;
% define the weighting factors
a1=0.3;
a2=0.4;
a3=0.3;
% number of nodes per cluster
N_cluster=15;
%Energy Model (all values in Joules)
%Initial Energy
Eo=0.5;
%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;
% parameters to determine the RSSI values
d_ref=100;
n_pathloss=2.5;
RSSI_0=-75;
% RSSI threshold
T0=-50;
% number of malicious nodes
malicious=0;
% number of normal nodes
normal=0;
% number of CH
countCHs=0;
% number of clusters
cluster=1;
%%
plot(x,y,'*');
hold on
plot(BSx,BSy,'--rs','LineWidth',2,...
'MarkerEdgeColor','k',...
'MarkerFaceColor','g',...
'MarkerSize',10)
grid on
node=struct('id',{},'x',{},'y',{},'weight',{},'energy',{},'dist',{}, 'type',{});
%{'N','M','CH'}
for i=1:N
node(i).id=i;
node(i).x=x(i);
node(i).y=y(i);
%initially there are no cluster heads only nodes
node(i).type='N';
node(i).energy=Eo;
%text(x(i),y(i),num2str(i));
end
node(N+1).x=BSx;
node(N+1).y=BSy;
%Compute of the distance
for i=1:1:N
node(i).dist=sum(sqrt((node(i).x-(node(N+1).x))^2 + (node(i).y -(node(N+1).y))^2));
%a(i)=node(i).dist;
%disp(node(i).dist);
% ch1=ceil(rand(1)*N)
% text(node(ch1).x,node(ch1).y,['ch1'],'Color' ,'r');
end
for i=1:1:N
RSSI=RSSI_0-10*n_pathloss*log(node(i).dist/d_ref)
if RSSI<T0
% figure(2)
node(i).type='M';
malicious=malicious+1;
plot(node(i).x,node(i).y,'g*');
hold on;
end
if RSSI>T0
plot(node(i).x,node(i).y,'b*');
hold on;
node(i).energy =node(i).energy - ( (ETX+EDA)*(4000) + Efs*4000*( node(i).dist * node(i).dist ));
node(i).weight= a1*(N/N_cluster)+a2*node(i).energy+a3*node(i).dist;
a(i)=node(i).weight;
disp(node(i).weight);
A=sort(a,'descend')
a=1;
ach = 1;
node_weight(a) = node(i).weight;
a = a+1;
Maximum_weight = max(node_weight);
if node(i).weight == max(node_weight)
node(i).id=i;
node(i).type='CH';
node(i).weight=max(node_weight);
countCHs=countCHs+1;
C(cluster).xd=node(i).x;
C(cluster).yd=node(i).y;
% text(node(i).x,node(i).y,num2str(i));%,'color','r');
distance=sqrt( (node(i).x-(node(N+1).x) )^2 + (node(i).y-(node(N+1).y) )^2 );
C(cluster).distance=distance
C(cluster).id=i
X(cluster)=node(i).x
Y(cluster)=node(i).y
cluster=cluster+1;
end
plot(node(i).x,node(i).y,'ro');
text(node(i).x,node(i).y,['CH ' num2str(node(i).id)]);
ach = ach+1;
end
pointer = zeros(countCHs, 1);
pp= zeros(N, 2);
for i=1:countCHs
pointer(i, 1)=C(i).id
end
[nl nc]=size(pointer);
for i=1:nc%[m n]=size(pointer), m=nbr de ligne
Group=['cluster' num2str(i)]
end
for i=1:N
pp(i,1)=node(i).x;
pp(i,2)=node(i).y;
end
for i=1:size(pointer)
%I = randfeatures(pointer,Group,'SubsetSize',15,'Classifier','knn');
idx = nearestneighbour(pointer, pp)
end
% D = pdist(pp,'euclidean')
end
% end |
Partager