|
| 1 | + |
| 2 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | +% % |
| 4 | +% This is the LEACH [1] code we have used. % |
| 5 | +% The same code can be used for FAIR if m=1 % |
| 6 | +% % |
| 7 | +% [1] W.R.Heinzelman, A.P.Chandrakasan and H.Balakrishnan, % |
| 8 | +% "An application-specific protocol architecture for wireless % |
| 9 | +% microsensor networks" % |
| 10 | +% IEEE Transactions on Wireless Communications, 1(4):660-670,2002 % |
| 11 | +% www.forum.wsnlab.ir % |
| 12 | + |
| 13 | +% % |
| 14 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 15 | + |
| 16 | +clear; |
| 17 | + |
| 18 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 19 | + |
| 20 | +%Field Dimensions - x and y maximum (in meters) |
| 21 | +xm=100; |
| 22 | +ym=100; |
| 23 | + |
| 24 | +%x and y Coordinates of the Sink |
| 25 | +sink.x=1.5*xm; |
| 26 | +sink.y=0.5*ym; |
| 27 | + |
| 28 | +%Number of Nodes in the field |
| 29 | +n=200 |
| 30 | + |
| 31 | +%Optimal Election Probability of a node |
| 32 | +%to become cluster head |
| 33 | +p=0.2; |
| 34 | + |
| 35 | +%Energy Model (all values in Joules) |
| 36 | +%Initial Energy |
| 37 | +Eo=0.1; |
| 38 | +%Eelec=Etx=Erx |
| 39 | +ETX=50*0.000000001; |
| 40 | +ERX=50*0.000000001; |
| 41 | +%Transmit Amplifier types |
| 42 | +Efs=10*0.000000000001; |
| 43 | +Emp=0.0013*0.000000000001; |
| 44 | +%Data Aggregation Energy |
| 45 | +EDA=5*0.000000001; |
| 46 | + |
| 47 | +%Values for Hetereogeneity |
| 48 | +%Percentage of nodes than are advanced |
| 49 | +m=0.0; |
| 50 | +%\alpha |
| 51 | +a=1; |
| 52 | + |
| 53 | +%maximum number of rounds |
| 54 | +rmax=100 |
| 55 | + |
| 56 | +%%%%%%%%%%%%%%%%%%%%%%%%% END OF PARAMETERS %%%%%%%%%%%%%%%%%%%%%%%% |
| 57 | + |
| 58 | +%Computation of do |
| 59 | +do=sqrt(Efs/Emp); |
| 60 | + |
| 61 | +%Creation of the random Sensor Network |
| 62 | +figure(1); |
| 63 | +hold off; |
| 64 | +for i=1:1:n |
| 65 | + S(i).xd=rand(1,1)*xm; |
| 66 | + XR(i)=S(i).xd; |
| 67 | + S(i).yd=rand(1,1)*ym; |
| 68 | + YR(i)=S(i).yd; |
| 69 | + S(i).G=0; |
| 70 | + %initially there are no cluster heads only nodes |
| 71 | + S(i).type='N'; |
| 72 | + |
| 73 | + temp_rnd0=i; |
| 74 | + %Random Election of Normal Nodes |
| 75 | + if (temp_rnd0>=m*n+1) |
| 76 | + S(i).E=Eo; |
| 77 | + S(i).ENERGY=0; |
| 78 | + % plot(S(i).xd,S(i).yd,'o'); |
| 79 | + hold on; |
| 80 | + end |
| 81 | + %Random Election of Advanced Nodes |
| 82 | + if (temp_rnd0<m*n+1) |
| 83 | + S(i).E=Eo*(1+a) |
| 84 | + S(i).ENERGY=1; |
| 85 | + % plot(S(i).xd,S(i).yd,'+'); |
| 86 | + hold on; |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +S(n+1).xd=sink.x; |
| 91 | +S(n+1).yd=sink.y; |
| 92 | +%plot(S(n+1).xd,S(n+1).yd,'x'); |
| 93 | + |
| 94 | + |
| 95 | +%First Iteration |
| 96 | +figure(1); |
| 97 | + |
| 98 | +%counter for CHs |
| 99 | +countCHs=0; |
| 100 | +%counter for CHs per round |
| 101 | +rcountCHs=0; |
| 102 | +cluster=1; |
| 103 | + |
| 104 | +countCHs; |
| 105 | +rcountCHs=rcountCHs+countCHs; |
| 106 | +flag_first_dead=0; |
| 107 | + |
| 108 | +for r=0:1:rmax |
| 109 | + r |
| 110 | + |
| 111 | + %Operation for epoch |
| 112 | + if(mod(r, round(1/p) )==0) |
| 113 | + for i=1:1:n |
| 114 | + S(i).G=0; |
| 115 | + S(i).cl=0; |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | +hold off; |
| 120 | + |
| 121 | +%Number of dead nodes |
| 122 | +dead=0; |
| 123 | +%Number of dead Advanced Nodes |
| 124 | +dead_a=0; |
| 125 | +%Number of dead Normal Nodes |
| 126 | +dead_n=0; |
| 127 | + |
| 128 | +%counter for bit transmitted to Bases Station and to Cluster Heads |
| 129 | +packets_TO_BS=0; |
| 130 | +packets_TO_CH=0; |
| 131 | +%counter for bit transmitted to Bases Station and to Cluster Heads |
| 132 | +%per round |
| 133 | +PACKETS_TO_CH(r+1)=0; |
| 134 | +PACKETS_TO_BS(r+1)=0; |
| 135 | + |
| 136 | +figure(1); |
| 137 | + |
| 138 | +for i=1:1:n |
| 139 | + %checking if there is a dead node |
| 140 | + if (S(i).E<=0) |
| 141 | +% plot(S(i).xd,S(i).yd,'red .'); |
| 142 | + dead=dead+1; |
| 143 | + if(S(i).ENERGY==1) |
| 144 | + dead_a=dead_a+1; |
| 145 | + end |
| 146 | + if(S(i).ENERGY==0) |
| 147 | + dead_n=dead_n+1; |
| 148 | + end |
| 149 | + hold on; |
| 150 | + end |
| 151 | + if S(i).E>0 |
| 152 | + S(i).type='N'; |
| 153 | + if (S(i).ENERGY==0) |
| 154 | + % plot(S(i).xd,S(i).yd,'o'); |
| 155 | + end |
| 156 | + if (S(i).ENERGY==1) |
| 157 | + plot(S(i).xd,S(i).yd,'+'); |
| 158 | + end |
| 159 | + hold on; |
| 160 | + end |
| 161 | +end |
| 162 | +%plot(S(n+1).xd,S(n+1).yd,'x'); |
| 163 | + |
| 164 | + |
| 165 | +STATISTICS(r+1).DEAD=dead; |
| 166 | +DEAD(r+1)=dead; |
| 167 | +DEAD_N(r+1)=dead_n; |
| 168 | +DEAD_A(r+1)=dead_a; |
| 169 | + |
| 170 | +%When the first node dies |
| 171 | +if (dead==1) |
| 172 | + if(flag_first_dead==0) |
| 173 | + first_dead=r |
| 174 | + flag_first_dead=1; |
| 175 | + end |
| 176 | +end |
| 177 | + |
| 178 | +countCHs=0; |
| 179 | +cluster=1; |
| 180 | +for i=1:1:n |
| 181 | + if(S(i).E>0) |
| 182 | + temp_rand=rand; |
| 183 | + if ( (S(i).G)<=0) |
| 184 | + |
| 185 | + %Election of Cluster Heads |
| 186 | + if(temp_rand<= (p/(1-p*mod(r,round(1/p))))) |
| 187 | + countCHs=countCHs+1; |
| 188 | + packets_TO_BS=packets_TO_BS+1; |
| 189 | + PACKETS_TO_BS(r+1)=packets_TO_BS; |
| 190 | + |
| 191 | + S(i).type='C'; |
| 192 | + S(i).G=round(1/p)-1; |
| 193 | + C(cluster).xd=S(i).xd; |
| 194 | + C(cluster).yd=S(i).yd; |
| 195 | + % plot(S(i).xd,S(i).yd,'k*'); |
| 196 | + |
| 197 | + distance=sqrt( (S(i).xd-(S(n+1).xd) )^2 + (S(i).yd-(S(n+1).yd) )^2 ); |
| 198 | + C(cluster).distance=distance; |
| 199 | + C(cluster).id=i; |
| 200 | + X(cluster)=S(i).xd; |
| 201 | + Y(cluster)=S(i).yd; |
| 202 | + cluster=cluster+1; |
| 203 | + |
| 204 | + %Calculation of Energy dissipated |
| 205 | + distance; |
| 206 | + if (distance>do) |
| 207 | + S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance )); |
| 208 | + S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance )); |
| 209 | + end |
| 210 | + if (distance<=do) |
| 211 | + S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance )); |
| 212 | + S(i).E=S(i).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance )); |
| 213 | + end |
| 214 | + end |
| 215 | + |
| 216 | + end |
| 217 | + end |
| 218 | +end |
| 219 | + |
| 220 | +STATISTICS(r+1).CLUSTERHEADS=cluster-1; |
| 221 | +CLUSTERHS(r+1)=cluster-1; |
| 222 | + |
| 223 | +%Election of Associated Cluster Head for Normal Nodes |
| 224 | +for i=1:1:n |
| 225 | + if ( S(i).type=='N' && S(i).E>0 ) |
| 226 | + if(cluster-1>=1) |
| 227 | + min_dis=sqrt( (S(i).xd-S(n+1).xd)^2 + (S(i).yd-S(n+1).yd)^2 ); |
| 228 | + min_dis_cluster=1; |
| 229 | + for c=1:1:cluster-1 |
| 230 | + temp=min(min_dis,sqrt( (S(i).xd-C(c).xd)^2 + (S(i).yd-C(c).yd)^2 ) ); |
| 231 | + if ( temp<min_dis ) |
| 232 | + min_dis=temp; |
| 233 | + min_dis_cluster=c; |
| 234 | + end |
| 235 | + end |
| 236 | + |
| 237 | + %Energy dissipated by associated Cluster Head |
| 238 | + min_dis; |
| 239 | + if (min_dis>do) |
| 240 | + S(i).E=S(i).E- ( ETX*(4000) + Emp*4000*( min_dis * min_dis * min_dis * min_dis)); |
| 241 | + end |
| 242 | + if (min_dis<=do) |
| 243 | + S(i).E=S(i).E- ( ETX*(4000) + Efs*4000*( min_dis * min_dis)); |
| 244 | + end |
| 245 | + %Energy dissipated |
| 246 | + if(min_dis>0) |
| 247 | + distance=sqrt( (S(C(min_dis_cluster).id).xd-(S(n+1).xd) )^2 + (S(C(min_dis_cluster).id).yd-(S(n+1).yd) )^2 ); |
| 248 | + S(C(min_dis_cluster).id).E = S(C(min_dis_cluster).id).E- ( (ERX + EDA)*4000 ); |
| 249 | +if (distance>do) |
| 250 | + S(C(min_dis_cluster).id).E=S(C(min_dis_cluster).id).E- ( (ETX+EDA)*(4000) + Emp*4000*( distance*distance*distance*distance )); |
| 251 | + end |
| 252 | + if (distance<=do) |
| 253 | + S(C(min_dis_cluster).id).E=S(C(min_dis_cluster).id).E- ( (ETX+EDA)*(4000) + Efs*4000*( distance * distance )); |
| 254 | + end |
| 255 | + PACKETS_TO_CH(r+1)=n-dead-cluster+1; |
| 256 | + end |
| 257 | + |
| 258 | + S(i).min_dis=min_dis; |
| 259 | + S(i).min_dis_cluster=min_dis_cluster; |
| 260 | + |
| 261 | + end |
| 262 | + end |
| 263 | +end |
| 264 | +hold on; |
| 265 | + |
| 266 | +countCHs; |
| 267 | +rcountCHs=rcountCHs+countCHs; |
| 268 | +sum=0; |
| 269 | +for i=1:1:n |
| 270 | +if(S(i).E>0) |
| 271 | + sum=sum+S(i).E; |
| 272 | +end |
| 273 | +end |
| 274 | +avg=sum/n; |
| 275 | +STATISTICS(r+1).AVG=avg; |
| 276 | +sum; |
| 277 | + |
| 278 | + |
| 279 | +%Code for Voronoi Cells |
| 280 | +%Unfortynately if there is a small |
| 281 | +%number of cells, Matlab's voronoi |
| 282 | +%procedure has some problems |
| 283 | + |
| 284 | +%[vx,vy]=voronoi(X,Y); |
| 285 | +%plot(X,Y,'r*',vx,vy,'b-'); |
| 286 | +% hold on; |
| 287 | +% voronoi(X,Y); |
| 288 | +% axis([0 xm 0 ym]); |
| 289 | + |
| 290 | +end |
| 291 | +figure(2); |
| 292 | +for r=0:1:24 |
| 293 | + ylabel('Average Energy of Each Node'); |
| 294 | + xlabel('Round Number'); |
| 295 | + plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red'); |
| 296 | + hold on; |
| 297 | +end |
| 298 | +figure(3); |
| 299 | +for r=0:1:49 |
| 300 | + ylabel('Average Energy of Each Node'); |
| 301 | + xlabel('Round Number'); |
| 302 | + plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red'); |
| 303 | + hold on; |
| 304 | +end |
| 305 | +figure(4); |
| 306 | +for r=0:1:74 |
| 307 | + ylabel('Average Energy of Each Node'); |
| 308 | + xlabel('Round Number'); |
| 309 | + plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red'); |
| 310 | + hold on; |
| 311 | +end |
| 312 | +figure(5); |
| 313 | +for r=0:1:99 |
| 314 | + ylabel('Average Energy of Each Node'); |
| 315 | + xlabel('Round Number'); |
| 316 | + plot([r r+1],[STATISTICS(r+1).AVG STATISTICS(r+2).AVG],'red'); |
| 317 | + hold on; |
| 318 | +end |
| 319 | +figure(6); |
| 320 | +for r=0:1:24 |
| 321 | + ylabel('Number of Dead Nodes'); |
| 322 | + xlabel('Round Number'); |
| 323 | + plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red'); |
| 324 | + hold on; |
| 325 | +end |
| 326 | +figure(7); |
| 327 | +for r=0:1:49 |
| 328 | + ylabel('Number of Dead Nodes'); |
| 329 | + xlabel('Round Number'); |
| 330 | + plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red'); |
| 331 | + hold on; |
| 332 | +end |
| 333 | +figure(8); |
| 334 | +for r=0:1:74 |
| 335 | + ylabel('Number of Dead Nodes'); |
| 336 | + xlabel('Round Number'); |
| 337 | + plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red'); |
| 338 | + hold on; |
| 339 | +end |
| 340 | +figure(9); |
| 341 | +for r=0:1:99 |
| 342 | + ylabel('Number of Dead Nodes'); |
| 343 | + xlabel('Round Number'); |
| 344 | + plot([r r+1],[STATISTICS(r+1).DEAD STATISTICS(r+2).DEAD],'red'); |
| 345 | + hold on; |
| 346 | +end |
| 347 | + |
| 348 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% STATISTICS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 349 | +% % |
| 350 | +% DEAD : a rmax x 1 array of number of dead nodes/round % |
| 351 | +% DEAD_A : a rmax x 1 array of number of dead Advanced nodes/round % |
| 352 | +% DEAD_N : a rmax x 1 array of number of dead Normal nodes/round % |
| 353 | +% CLUSTERHS : a rmax x 1 array of number of Cluster Heads/round % |
| 354 | +% PACKETS_TO_BS : a rmax x 1 array of number packets send to Base Station/round % |
| 355 | +% PACKETS_TO_CH : a rmax x 1 array of number of packets send to ClusterHeads/round % |
| 356 | +% first_dead: the round where the first node died % |
| 357 | +% % |
| 358 | +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 359 | + |
| 360 | + |
| 361 | + |
| 362 | + |
| 363 | + |
| 364 | + |
0 commit comments