-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_d.m
252 lines (218 loc) · 6.96 KB
/
main_d.m
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
clear;
load('test.mat');
Rmax = 30;
figure(1);
for r= 1:1:rmax
r
% Compute Neigbor desity & neighbor cost
figure(1);
hold off;
dead = 0;
for i=1:1:n
%checking if there is a dead node
if (S(i).RE<=0)
plot(S(i).xd,S(i).yd,'red +');
dead=dead+1;
S(i).state='DEAD';
S(i).type='DEAD';
S(i).RE = 0;
hold on;
else
S(i).type='N';
S(i).state='Initial_State';
plot(S(i).xd,S(i).yd,'o');
hold on;
end
end
plot(S(n+1).xd,S(n+1).yd,'x');
text(S(n+1).xd,S(n+1).yd,' BS','Color','b','FontWeight','b');
for i= 1:1:n
number_neighbor_i = 0;
sigma_neigh_cost = 0;
for j= 1:1:n
disJtoI = sqrt((S(i).xd-S(j).xd)^2 + (S(i).yd-S(j).yd)^2);
if (disJtoI <= Rmax && disJtoI > 0)
number_neighbor_i = number_neighbor_i + 1;
sigma_neigh_cost = sigma_neigh_cost + (S(i).xd-S(j).xd)^2 + (S(i).yd-S(j).yd)^2;
end
end
S(i).neigh_des = number_neighbor_i/n;
S(i).neigh_cost = (sqrt(sigma_neigh_cost/number_neighbor_i))/Rmax;
end
% CH %
for i= 1:1:n
if (S(i).RE >0 )
S(i).distoBS = sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2);
% compute Td
Energy_level = S(i).RE/S(i).Initial_energy;
S(i).Fuzzy_fitness1 = evalfis([Energy_level S(i).distoBS], fis1);
S(i).Fuzzy_fitness2 = evalfis([S(i).neigh_des S(i).neigh_cost], fis2);
% random alpha from [0.9 1]
alpha = rand(1,1) / 10 + 0.9;
S(i).Td = alpha * (1 - S(i).Fuzzy_fitness1) * Tc;
%%S(i).rad = evalfis([S(i).RE S(i).distoBS S(i).Fuzzy_fitness1], fis1);
%Initial candidates
S(i).candidate = [];
end
end
% Start Bau CH
% Bau CH
number_normal_node = 100;
cluster = 0;
while number_normal_node > 0
for i= 1:1:n
CH_selection = 0;
min_Td = Tc;
for j= 1:1:n
% Chon thang co Td nho nhat ma chua phai la CH va khong phai la
% worker cua thang khac (nam trong ban kinh cua 1 CH nao do)
if (S(j).RE > 0 && S(j).Td < min_Td && isequal(S(j).type,'N') && length(S(j).candidate) == 0)
min_Td = S(j).Td;
CH_selection = j;
end
end
if (i == CH_selection)
S(i).type = 'CH';
rad = evalfis([S(i).Fuzzy_fitness1 S(i).Fuzzy_fitness2], fis3);
S(i).rad = rad;
S(i).number_worker = 0;
cluster = cluster +1;
C(cluster).xd=S(i).xd;
C(cluster).yd=S(i).yd;
plot(S(i).xd,S(i).yd,'k*');
% compute node j received from i
for t= 1:1:n
if ((isequal(S(t).type,'N') || isequal(S(t).type,'W'))&& (S(t).RE >0))
disJToI = sqrt( (S(i).xd-S(t).xd)^2 + (S(i).yd-S(t).yd)^2 );
if (disJToI <= S(i).rad)
k = length(S(t).candidate) + 1;
S(t).type = 'W';
S(t).candidate(k) = i;
end
end
end
end
end
number_normal_node = 0;
for i= 1:1:n
if (isequal(S(i).type,'N') && (S(i).RE >0))
number_normal_node = number_normal_node + 1;
end
end
end
% Chon CH cho worker
for j= 1:1:n
if (isequal(S(j).type,'W') && (S(j).RE >0) && length(S(j).candidate) > 0)
candidate = S(j).candidate;
CH_i = candidate(1);
dist_Sj_CH_i = sqrt((S(j).xd-S(CH_i).xd)^2 + (S(j).yd-S(CH_i).yd)^2 );
CH_cost_1 = dist_Sj_CH_i * S(CH_i).distoBS/S(CH_i).RE;
CH = CH_i;
for i= 2:1:length(candidate)
CH_i = candidate(i);
dist_Sj_CH_i = sqrt((S(j).xd-S(CH_i).xd)^2 + (S(j).yd-S(CH_i).yd)^2 );
CH_cost = dist_Sj_CH_i * S(CH_i).distoBS/S(CH_i).RE;
if (CH_cost < CH_cost_1)
CH = CH_i;
CH_cost_1 = CH_cost;
end
end
S(j).CH = CH;
S(CH).number_worker = S(CH).number_worker + 1;
end
end
for i=1:1:n
if (isequal(S(i).type,'CH') && S(i).RE > 0)
S(i).L = S(i).distoBS/Rmax;
S(i).PN = [];
S(i).parent = n+1;
end
end
% --- End Bau CH
% Routing
for i=1:1:n
if (isequal(S(i).type,'CH') && S(i).RE > 0)
S(i).L = S(i).distoBS/Rmax;
end
end
for i=1:1:n
if (isequal(S(i).type,'CH') && S(i).RE > 0)
range = k*Rmax;
for j=1:1:n
disJToI = sqrt((S(j).xd-S(i).xd)^2 + (S(j).yd-S(i).yd)^2 );
if (disJToI <= range && isequal(S(j).type,'CH') && S(j).RE > 0 && S(j).L < S(i).L)
k = length(S(i).PN) + 1;
S(i).PN(k) = j;
end
end
end
end
for i=1:1:n
if (isequal(S(i).type,'CH') && S(i).RE > 0 && length(S(i).PN) > 0)
PN = S(i).PN;
PN_j = PN(1);
parent = PN_j;
disJToI = sqrt((S(i).xd-S(PN_j).xd)^2 + (S(i).yd-S(PN_j).yd)^2 );
if (disJToI < d0)
Eij = ETX + Efs*(disJToI^2);
end
if (disJToI >= d0)
Eij = ETX + Emp*(disJToI^4);
end
TEij = (Eij + ETX)*S(i).number_worker*bit;
phi = (pi - (pi/2*S(PN_j).RE/S(PN_j).Initial_energy));
Wij = exp(1/sin(phi));
Cost_1 = Wij*TEij;
for j= 2:1:length(PN)
PN_j = PN(j);
disJToI = sqrt((S(i).xd-S(PN_j).xd)^2 + (S(i).yd-S(PN_j).yd)^2 );
if (disJToI < d0)
Eij = ETX + Efs*(disJToI^2);
end
if (disJToI >= d0)
Eij = ETX + Emp*(disJToI^4);
end
TEij = (Eij + ETX)*S(i).number_worker*bit;
phi = (pi - (pi/2*S(PN_j).RE/S(PN_j).Initial_energy));
Wij = exp(1/sin(phi));
Costij = Wij*TEij;
if (Costij < Cost_1)
parent = PN_j;
Cost_1 = Costij;
end
end
S(i).parent = parent;
S(i).Cost_parent = Cost_1;
end
end
% --- End Routing
%reduce energy
for i=1:1:n
if (isequal(S(i).type,'W') && (S(i).RE >0))
CH = S(i).CH;
disToCH = sqrt((S(i).xd-S(CH).xd)^2 + (S(i).yd-S(CH).yd)^2 );
if (disToCH < d0)
S(i).RE = S(i).RE - bit*(ETX + Efs*(disToCH^2));
end
if (disToCH >= d0)
S(i).RE = S(i).RE - bit*(ETX + Emp*(disToCH^4));
end
end
if (isequal(S(i).type,'CH') && (S(i).RE >0))
% NL nhan tu worker
S(i).RE = S(i).RE - S(i).number_worker*bit*ETX;
%Nl nhan tu CH khac
parent = S(i).parent;
% parent cua node i se bi mat di S(i).Cost_parent
S(parent).RE = S(parent).RE - S(i).Cost_parent;
% node i se mat them nl phat toi parent cua no
disToParent = sqrt((S(i).xd-S(parent).xd)^2 + (S(i).yd-S(parent).yd)^2 );
if (disToParent < d0)
S(i).RE = S(i).RE - S(i).number_worker*bit*(ETX + Efs*(disToParent^2));
end
if (disToParent >= d0)
S(i).RE = S(i).RE - S(i).number_worker*bit*(ETX + Emp*(disToParent^4));
end
end
end
end