Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Nicola Sparnacci
17 giugno 2017
Indice
1 Protocolli flat 2
1.1 Creazione della mappa . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Collegamento dei nodi . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Flooding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Gossiping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Protocolli gerarchici 4
2.1 Ricerca del sink e clustering . . . . . . . . . . . . . . . . . . . 4
2.2 LEACH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Risultati 6
A Codici 19
1
1 Protocolli flat
Nelle reti di sensori wireless, i protocolli di tipo flat utilizzano i nodi allo
stesso modo uguale per tutti, senza avere dei nodi come compiti particolari.
Questi protocolli sono la pi semplice soluzione utilizzabile per far comuni-
care dei nodi in una rete complessa. In seguito, viene discusso come stato
strutturato il codice MatLAB per simulare e confrontare i due protocolli
principali appartenenti a questa categoria, ovvero il Flooding e il Gossiping.
2
Loutput della funzione fornisce la matrice delle adiacenze A, la matri-
ce delle distanze d e la matrice grado D. Soprattutto le prime due sono
fondamentali nella scrittura del codice di simulazione dei protocolli.
1.3 Flooding
Il protocollo Flooding il pi semplice protocollo per WSN, in quanto ogni
nodo che riceve il pacchetto da trasmettere lo passa in broadcast a tutti i nodi
ad esso collegato. Per evitare che un nodo trasmetta pi volte -inutilmente-
il pacchetto, ogni dispositivo provvisto di una piccola memoria che gli evita
di effettuare una ritrasmissione.
Chiaramente, questo protocollo caratterizzato da un grande dispendio
di energia. Inoltre, i percorsi coperti dal pacchetto sono molto ridondanti
rispetto a quello che si potrebbe fare con altri protocolli e c il problema
dellimplosione della rete.
Nella scrittura del codice si scelto di utilizzare diverse liste e nel seguente
elenco puntato vengono descritte ad una ad una per spiegare le motivazioni:
1. memorizzare i nodi che hanno gi trasmesso il pacchetto durante il
round di trasmissione;
2. una lista per tenere traccia dei nodi che hanno ricevuto il pacchetto;
3. una lista delle connessioni del nodo in esame, eliminando dagli eventuali
collegamenti i nodi appartenenti alla prima lista o quelli scarichi.
Per tenere traccia dellandamento delle energie di ogni nodo, invece, si
scelto di andare a creare una matrice con numero di colonne pari al numero
di nodi della mappa e righe pari al numero di round di simulazione.
1.4 Gossiping
Il Gossiping levoluzione del Flooding: dalla trasmissione broadcast si pas-
sa ad una trasmissione P2P con uno dei nodi vicini, scelto in modo casuale.
Questo, come si vedr in simulazione, permette di utilizzare in modo decisa-
mente migliore le risorse energetiche dei sensori e di migliorare allaumentare
dei collegamenti tra i nodi. In questo modo il problema dellimplosione della
rete viene eliminato, ma si ha comunque una ridondanza nella trasmissione.
Considerando il minor numero di trasmissioni che un nodo deve fare, la
scrittura del codice per simulare il Gossiping molto pi semplice del Floo-
ding. Infatti, non si rivelato necessario lutilizzo di liste in quando si pu
lavorare singolarmente sul nodo attivo considerando che si pu immagina-
re che ad un certo istante sia lunico a trasmettere. Analizzando un nodo,
si cercano i collegamenti possibili con i vicini e si sceglie una destinazione
in modo casuale. Questa operazione si ripete finch non viene raggiunta la
destinazione o finch nella scelta casuale non si incontra un nodo scarico,
perdendo il pacchetto.
3
2 Protocolli gerarchici
Un approccio differente si ha per protocolli gerarchici, dove i nodi non svolgo-
no tutti lo stesso compito. Questa soluzione permette di gestire reti capillari,
composte da gruppi (cluster) di nodi semplici che trasmettono ad un cluster
head, ovvero un nodo scelto per collezionare tutti i dati degli altri, per poi
trasmettere il tutto in forma compressa al sink, unit centrale della rete.
2.2 LEACH
Limplementazione MatLAB del protocollo LEACH prevede un rinnova-
mento dei cluster heads ad ogni round di trasmissione. Questa selezione
viene fatta casualmente, prendendo un nodo in modo casuale allinterno del
cluster ed andando a creare i collegamenti dei nodi semplici al neo eletto clu-
ster head e, a sua volta, dai cluster heads al sink. La scelta casuale stata
fatta utilizzando una variabile aleatoria uniformemente distribuita in modo
da rendere il pi possibile equiprobabile lelezione di un nodo allinterno del
cluster.
Una volta instaurati i nuovi collegamenti, si procede alla trasmissione dei
pacchetti contenenti le misure dei nodi semplici. Ad ogni round di trasmis-
sione si analizzano uno ad uno i cluster heads e guardando i collegamenti
si ottengono le liste di nodi che hanno trasmesso (eliminando da tale lista
eventuali nodi scarichi). Ripetendo il processo per tutti i cluster heads si
ottiene la lista di tutti i nodi semplici che hanno trasmesso e quindi pos-
4
sibile andare a simulare la scarica della batteria. Infine, si va a simulare la
trasmissione dei pacchetti compressi dei cluster heads al sink.
Si ricordi che allutente permesso scegliere i pesi delle trasmissioni:
5
3 Risultati
In questultimo paragrafo vengono riportati alcuni risultati per effettuare dei
confronti sui protocolli di routing implementati. I grafici riportati in segui-
to, mostrano la mappa dei nodi e dei diversi collegamenti in base allutilizzo
dellapproccio flat o a cluster; poi si ha una coppia di grafici che raffigurano
gli andamenti dellenergie medie dei nodi e del numero di batterie non sca-
riche ad ogni round di trasmissione; infine lultimo set di grafici rappresenta
landamento dellenergia di ciascun nodo per i diversi protocolli testati.
6
impostati i costi delle trasmissioni tra nodi semplici e cluster heads e clu-
ster heads e sink, come riportato in Figura 1. Il confronto fra lautonomia
energetica del Gossiping col LEACH, quindi, pu essere migliore o peggiore
a seconda del costo. Nelle simulazioni comparative nei vari scenari, il costo
di tutte le trasmissioni stato mantenuto costante per cercare di mantenere
le condizioni quanto pi simili possibili.
7
Energia residua con costheads =0.001
100
90
80
70
Energia residua
60
50
40
30
20
10
0
200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
90
80
70
Energia residua
60
50
40
30
20
10
0
200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
90
80
70
Energia residua
60
50
40
30
20
10
0
200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
8
Mappa dei nodi collegati per Flooding e Gossiping
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
(a) Mappa dei 20 nodi
LEACH
60
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
Gossiping
15 LEACH
10
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
(b) Energie medie e numero di nodi vivi
9
Flooding Gossiping LEACH
N
Ricevuti Persi Ricevuti Persi Ricevuti Persi
20 62 (3.1%) 1824 (91.2%) 545 (27.3%) 1348 (67.4%) 35708 (94.0%) 2292 (6.0%)
40 33 (1.7%) 1908 (95.4%) 691 (34.5%) 1255 (62.7%) 77522 (99.4%) 478 (0.6%)
60 22 (1.1%) 1946 (97.3%) 638 (31.9%) 1330 (66.5%) 117730 (99.8%) 270 (0.2%)
80 17 (0.9%) 1955 (97.8%) 1152 (57.6%) 820 (41.0%) 158000 (100.0%) 0 (0.0%)
10
Mappa dei nodi collegati per Flooding e Gossiping
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
(a) Mappa dei 40 nodi
LEACH
60
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
Gossiping
30 LEACH
20
10
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
(b) Energie medie e numero di nodi vivi
11
Figura 5: Andamento delle energie dei 40 nodi
12
Mappa dei nodi collegati per Flooding e Gossiping
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
(a) Mappa dei 60 nodi
LEACH
60
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
50
Gossiping
LEACH
40
30
20
10
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
(b) Energie medie e numero di nodi vivi
13
Figura 7: Andamento delle energie dei 60 nodi
14
Mappa dei nodi collegati per Flooding e Gossiping
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
(a) Mappa dei 80 nodi
LEACH
60
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
Gossiping
60 LEACH
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
(b) Energie medie e numero di nodi vivi
15
Figura 9: Andamento delle energie dei 80 nodi
16
Mappa dei nodi collegati per Flooding e Gossiping
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
6
4
2
0
-2
-4
-6
-6 -4 -2 0 2 4 6
(a) Mappa dei 40 nodi
LEACH
60
40
20
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
Gossiping
30 LEACH
20
10
0
0 200 400 600 800 1000 1200 1400 1600 1800 2000
#Round
(b) Energie medie e numero di nodi vivi
Figura 10: Mappe, energie medie e nodi vivi con 40 sensori con costi realistici
17
Figura 11: Andamento delle energie dei 40 nodi con costi realistici
18
A Codici
19
50 warning ( E l e v a t a d e n s i t a d i n o d i : p o t r e b b e s e r v i r e molto
tempo p e r l a c o l l o c a z i o n e d e i n o d i . C o n ti n u a re ? [Y/N ] )
51 c h o i c e= ;
52 %C o n t r o l l o c a r a t t e r e immesso
53 w h i l e (~ strcmp ( c h o i c e , N ) && ~strcmp ( c h o i c e , n ) && ~
strcmp ( c h o i c e , Y ) && ~strcmp ( c h o i c e , y ) )
54 c h o i c e=i n p u t ( Y: C o n ti n u a re , N: I n t e r r o m p e r e \n , s ) ;
55 end
56 i f ( strcmp ( c h o i c e , N ) | | strcmp ( c h o i c e , n ) )
57 e r r o r ( R i p e t e r e aumentando l a r e a o diminuendo i l
numero d i n o d i . )
58 end
59 end
60
61 f o r k=1:N
62 % c a l c o l o c o o r d i n a t e random
63 x i ( k )=l rand ( 1 ) l / 2 ;
64 y i ( k )=h rand ( 1 )h / 2 ;
65
66 % quando ho piu p u n ti , c o n t r o l l o che s i a n o d i s t a n z i a t i
almeno d i
67 % d i s t a n c e , c a l c o l a n d o l a d i s t a n z a e u c l i d e a . Se l a
distanza e i n f e r i o r e
68 % a l l i m i t e v o l u to , vengono r i c a l c o l a t e l e p o s i z i o n i e
viene e f f e t tu a t o
69 % nuovamente i l c o n t r o l l o
70 i f k~=1
71 j =1;
72 w h i l e j <k
73 c a l c _ d i s t =s q r t ( ( x i ( k )x i ( j ) ) ^2 + ( y i ( k )y i ( j ) ) ^2) ;
74 i f c a l c _ d i s t <r
75 x i ( k )=l rand ( 1 ) l / 2 ;
76 y i ( k )=h rand ( 1 )h / 2 ;
77 n_sovrap ( k )=n_sovrap ( k ) +1;
78 i f n_sovrap ( k )==1e6
79 e r r o r ( Dopo 1 0 . 0 0 0 . 0 0 0 d i t e n t a t i v i , non
sono r i u s c i t o a p o s i z i o n a r e i l nodo %i
corretamente . , k)
80 end
81 %dopo un cambio r i c o n t r o l l o t u t t i i p u n t i
82 j =1;
83 else
84 j=j +1;
85 end
86 end
87 end
88 % cal co l o percentuale progresso cal co lo
89 p e r c=f l o o r ( k/N 1 0 0 ) ;
90 s=num2str ( p e r c ) ;
91 f p r i n t f ( [ s t r s %%\n ] )
92 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
93 end
94 t i t o l o =[ D i s p o s i z i o n e d i , num2str (N) , n o d i a d i s t a n z a mutua
, num2str ( r ) , . ] ;
95
96 % Rete ad a n e l l o d i r a g g i o r
97 case ring
98 i f ( l h < p i r ^2)
99 e r r o r ( Area d e l l a n e l l o m a g g i o re d e l l a r e a d i s p o n i b i l e .
)
100 end
101
20
102 f o r k=1:N
103 x i ( k )=r c o s ( ( k1) 2 p i /N) ;
104 y i ( k )=r s i n ( ( k1) 2 p i /N) ;
105
106 % cal co l o percentuale progresso cal co lo
107 p e r c=f l o o r ( k/N 1 0 0 ) ;
108 s=num2str ( p e r c ) ;
109 f p r i n t f ( [ s t r s %%\n ] )
110 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
111 end
112
113 t i t o l o =[ D i s p o s i z i o n e d i , num2str (N) , su a n e l l o d i r a g g i o ,
num2str ( r ) , . ] ;
114
115 end
116
117 % Grafico
118 i f strcmp ( graph , p l o t )
119 m_axis=max( l , h ) / 2 ;
120 p l o t ( x i , y i , ob ) ,
121 title ( titolo ) ,
122 a x i s ([ m_axis m_axis m_axis m_axis ] ) ,
123 x t i c k s ([ l /2 0 l / 2 ] ) ,
124 y t i c k s ([ h /2 0 h / 2 ] )
125 end
126
127 f p r i n t f ( \b , S o v r a p p o s i z i o n i r i s o l t e : %i \n , sum ( n_sovrap ) )
128 end
21
30
31 w h i l e f l a g ==0
32
33 f o r k=1:N
34 f o r j =1:N
35 i f ( k~=j )
36 d ( k , j )=s q r t ( ( x i ( k )x i ( j ) ) ^2 + ( y i ( k )y i ( j ) ) ^2) ;
37 else
38 A( k , k ) =0;
39 end
40 end
41 % media e a g g i u n g o i c o l l e g a m e n t i piu c o r t i a l l e a d i a c e n z e
42 mean_d=sum ( d ( k , : ) / (N1) ) ;
43 c = f i n d ( d ( k , : )<th mean_d & d ( k , : ) ~=0) ;
44 A( k , c ) =1;
45 A( c , k ) =1;
46
47 % c al co lo percentuale progresso c al col o
48 p e r c=f l o o r ( k /N 1 0 0 ) ;
49 s=num2str ( p e r c ) ;
50 f p r i n t f ( [ s t r s %%\n ] )
51 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
52 end
53
54 %m a t r i c e g ra d o
55 D=d i a g ( sum (A) ) ;
56
57 %m a t r i c e l a p l a c i a n a e a u t o v a l o r i
58 L=DA;
59 e=e i g (L ) ;
60 i f l e n g t h ( f i n d ( abs ( e )<1e 5) )==1
61 f p r i n t f ( \b , Grafo c o n n e s s o . \ n\n )
62 f l a g =1;
63 else
64 f p r i n t f ( \b , Grafo non c o n n e s o : aumento i l numero d i
collegamenti .\n )
65 th=th + 0 . 1 ;
66 s t r= C o l l e g a m e n to n o d i . . . ;
67 end
68 end
69 end
22
13 %e j i . I n o l t r e , p e r c o n v e n z i o n e s i pone a 0 i l c o l l e g a m e n t o i i , q u i n d i
la
14 %d i a g o n a l e p r i n c i p a l e e composta da s o l i 0 .
15 %
16 % In p u t :
17 % A, m a t r i c e d e l l e a d i a c e n z e ;
18 % xi , yi , coord i n ate d ei nodi ;
19 % C o l o rL i n e , e l a s t r i n g a che d e f i n i s c e l o s t i l e d e l l a l i n e a d e l
grafico
20 % ( g u a r d a r e l e p o s s i b i l i t a con h e l p p l o t n e l l a command window ) ;
21 % s t r , e l a s t r i n g a u t i l i z z a t a p e r m o s t r a r e i l completamento d e l l a
funzione
22
23
24 % Per o g n i c o p p i a , c o n t r o l l o s e e 1 e d i s e g n o i l c o l l e g a m e n t o
25 N=l e n g t h (A) ;
26 f o r r =1:N
27 f o r c =1:N
28 i f (A( r , c )==1)
29 p l ot ( [ xi ( r ) xi ( c ) ] , [ yi ( r ) , yi ( c ) ] , ColorLine )
30 end
31 end
32 % ca lc ol o percentuale progresso ca lc ol o
33 p e r c=f l o o r ( r /N 1 0 0 ) ;
34 s=num2str ( p e r c ) ;
35 f p r i n t f ( [ s t r s %%\n ] )
36 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
37 end
Codice 4: Flooding
1 f u n c t i o n [ e n e rg y , a l i v e , o f f ] = f l o o d i n g ( s o u r c e s , d e s t i n a t i o n s
, f u l l c h a r g e , A, d , c o s t )
2 %
3 % N i c o l a S p a r n a c c i 31/5/2017 v e r s i o n e 1 . 1
4 %
5 %f u n c t i o n [ e n e rg y , a l i v e , o f f ] = f l o o d i n g ( s o u r c e s , d e s t i n a t i o n s ,
f u l l c h a r g e , A, d , c o s t )
6 %
7 % Funzione che p e rm e tte d i t e s t a r e i l p r o t o c o l l o F l o o d i n g :
8 % Ingressi :
9 % s o u r c e s , v e t t o r e c o n t e n e n t e i n o d i da c u i deve p a r t i r e i l
messaggio ;
10 % d e s t i n a t i o n s , v e t t o r e c o n te n e te i d e s t i n a t a r i d e i messaggi ;
11 % f u l l c h a r g e , c a r i c a massima d e l l e b a t t e r i e d e i s e n s o r i w i r e l e s s ;
12 % A, m a t r i c e a d i a c e n z e ;
13 % d , m a t r i c e d e l l e d i s t a n z e mutue d e i n o d i ;
14 % c o s t , c o e f f i c i e n t e p e r m o d i f i c a r e i l p e s o d e l l e t r a t t e da
percorrere
15 % ( m o l t i p l i c a l a d i s t a n z a , q u i n d i s e d12=1 mettendo c o s t = 0 . 1 , l
energia
16 % calera di 0.1) ;
17 %
18 % Uscite :
19 % e n e rg y , m a t r i c e c o r r i s p o n d e n t e a l l andamento d e l l e n e r g i a d u ra n te
le
20 % t r a s m i s s i o n i d i p a c c h e t t i : l e c o l o n n e r a p p r e s e n t a n o i nodi , mentre
la
21 % i esima r i g a r a p p r e s e n t a l i +1esima t r a s m i s s i o n e ( s i s a l v a n e l l a
riga 1
22 % l i s t a n t e i n i z i a l e 0 , q u i n d i e tu tto t r a s l a t o d i 1) ;
23 % o f f , l i s t a d ei nodi s c a r i c h i ;
23
24 % a l i v e , andamento d e l l a somma d e i n o d i v i v i p e r round .
25
26 % round
27 n_tx=l e n g t h ( s o u r c e s ) ;
28 % numero d i n o d i
29 N=l e n g t h (A) ;
30 %i n i z i a l i z z o l e v a r i a b i l i
31 e n e rg y = z e r o s ( n_tx+1 ,N) ;
32 e n e rg y ( 1 , : )=f u l l c h a r g e o n e s ( 1 ,N) ;
33
34 %c o n t e g g i o p a c c h e t t i
35 c o r r e c t s =0;
36 f a i l s =0;
37 sames =0;
38
39 % v a r i a b i l i per nodi s c a r i c h i
40 o f f=z e r o s ( 1 ,N) ;
41 o f f _ l i s t=z e r o s ( 1 ,N) ;
42 a l i v e=z e r o s ( 1 , l e n g t h ( s o u r c e s ) ) ;
43
44 s t r= T r a s m i s s i o n e p a c c h e t t i con p r o t o c o l l o F l o o d i n g . . . ;
45
46 f o r tx =1: n_tx
47 % porto l en ergi a d e l l a tras m i s s i on e precedente a q u el l o at tu al e
48 e n e rg y ( tx + 1 , : )=e n e rg y ( tx , : ) ;
49 % s e l a s o r g e n t e e l a d e s t i n a z i o n e non sono l a s t e s s a c o s a ,
analizzo la
50 % s o r g e n t e e l a a g g i u n g o t r a i n o d i che hanno g i a t r a s m e s s o
51 % ( used_nodes ) e metto n e l l a l i s t a F i n o d i d i c u i ho g i a
analizzato
52 % anche i n o d i . Tra i n o d i u s a t i non metto q u e l l i s c a r i c h i p e rc h e
non
53 % p o s s o n o a n d a re a t r a s m e t t e r e a n c o ra
54 i f s o u r c e s ( tx )~= d e s t i n a t i o n s ( tx ) && i s e m p ty ( f i n d ( o f f _ l i s t==s o u r c e s
( tx ) , 1 ) )
55 a c t u a l =s o u r c e s ( tx ) ;
56 F=a c t u a l ;
57 conn=f i n d (A( a c t u a l , : ) ) ;
58 i n d e x _ o f f= ismember ( conn , o f f _ l i s t ) ;
59 conn ( i n d e x _ o f f ) = [ ] ;
60 used_nodes =[F conn ] ;
61 t =1;
62
63 % per t u t t i i nodi c o l l e g a t i a l l a sorgente , a n a l i z z o i
collegamenti
64 % e metto i n o d i che ho i n c o n t r a t o e che q u i n d i hanno r i c e v u t o
e
65 % t r a s m e s s o n e l l a l i s t a d i q u e l l i g i a u s a t i . Se i l nodo e l a
66 % s o r g e n t e non c i guardo p e rc h e l ho g i a f a t t o s o p r a
67 w h i l e t <= l e n g t h ( used_nodes )
68 i f used_nodes ( t )~=s o u r c e s ( tx )
69
70 % s e i n F non t r o v o i l nodo che s t o a n a l i z z a n d o a d e s s o
e se
71 % i l nodo non e l a d e s t i n a z i o n e ( che non deve
trasmettere
72 % q u i n d i va e s c l u s o ) a n a l i z z o i c o l l e g a m e n t i e
a g g i u n g o ad
73 % F i l nodo a t t u a l e .
74 i f i s e m p ty ( f i n d (F==used_nodes ( t ) , 1 ) ) && used_nodes ( t )
~=d e s t i n a t i o n s ( tx )
75 a c t u a l =used_nodes ( t ) ;
24
76 F=[F a c t u a l ] ;
77
78 % c e r c o l e a d i a c e n z e d e l nodo a t t u a l e e t o l g o i
nodi
79 % che hanno g i a t r a s m e s s o t r a l e c o n n e s s i o n i e
quelli
80 % s c a r i c h i p e rc h e non p o s s o n o piu t r a s m e t t e r e
81 conn=f i n d (A( a c t u a l , : ) ) ;
82 index_F= ismember ( conn , F) ;
83 conn ( index_F ) = [ ] ;
84 i n d e x _ o f f= ismember ( conn , o f f _ l i s t ) ;
85 conn ( i n d e x _ o f f ) = [ ] ;
86 % aggiungo a l l a l i s t a d ei nodi t o c c a t i
87 used_nodes=u n i q u e ( [ used_nodes conn ] , s t a b l e ) ;
88 end
89 end
90 % s e t u t t i t r a s m e t t o n o e s c o d a l c i c l o p e rc h e s i c u r a m e n t e
91 % raggiungo l a d es ti n azi on e
92 i f l e n g t h ( used_nodes ) == N
93 b re a k
94 end
95 t=t +1;
96 end
97
98 %c a l c o l o s e l a d e s t i n a z i o n e e s t a t a r a g g i u n t a
99 i f ~ i s e m p ty ( f i n d ( used_nodes==d e s t i n a t i o n s ( tx ) , 1 ) )
100 c o r r e c t s =c o r r e c t s +1;
101 %d i s p ( TX ok )
102 else
103 f a i l s = f a i l s +1;
104 %d i s p ( TX non ok )
105 end
106
107 % dai nodi u t i l i z z a t i t o l g o l e n e r g i a ( escludendo l a
destinazione )
108 used_nodes=used_nodes ( used_nodes~=d e s t i n a t i o n s ( tx ) ) ;
109 f o r k=1: l e n g t h ( used_nodes )
110 e n e rg y ( tx +1 , used_nodes ( k ) )=e n e rg y ( tx +1 , used_nodes ( k ) )c o s t
sum ( d ( : , used_nodes ( k ) ) ) ;
111 % se s c a r i c h i lo segnalo
112 i f e n e rg y ( tx +1 , used_nodes ( k ) ) <= 0
113 e n e rg y ( tx +1 , used_nodes ( k ) ) = 0 ;
114 o f f ( used_nodes ( k ) ) =1;
115 o f f _ l i s t ( used_nodes ( k ) )=used_nodes ( k ) ;
116 end
117 end
118 e l s e i f s o u r c e s ( tx )==d e s t i n a t i o n s ( tx )
119 sames=sames +1;
120 else
121 f a i l s = f a i l s +1;
122 end
123
124 %somma d e i n o d i v i v i
125 a l i v e ( tx )=Nsum ( o f f ) ;
126
127 % ca lc ol o percentuale progresso ca lc ol o
128 p e r c=f l o o r ( tx / l e n g t h ( s o u r c e s ) 1 0 0 ) ;
129 s=num2str ( p e r c ) ;
130 f p r i n t f ( [ s t r s %%\n ] )
131 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
132 end
133 f p r i n t f ( P a c c h e t t i r i c e v u t i : %i (%.1 f%%) , P a c c h e t t i p e r s i : %i (%.1 f%%)
25
, S t e s s a s o r g e n t e : %i (%.1 f%%)\n\n , c o r r e c t s , c o r r e c t s / l e n g t h (
s o u r c e s ) 1 0 0 , f a i l s , f a i l s / l e n g t h ( s o u r c e s ) 1 0 0 , sames , sames / l e n g t h (
s ou rces ) 100) ;
134 end
Codice 5: Gossiping
1 f u n c t i o n [ e n e rg y , a l i v e , o f f ] = g o s s i p i n g ( s o u r c e s , d e s t i n a t i o n s ,
f u l l c h a r g e , A, d , c o s t )
2 %
3 % N i c o l a S p a r n a c c i 21/5/2017 v e r s i o n e 2 . 1
4 %
5 %f u n c t i o n [ e n e rg y , a l i v e , o f f ] = g o s s i p i n g ( s o u r c e s , d e s t i n a t i o n s ,
f u l l c h a r g e , A, d , c o s t )
6 %
7 % Funzione che p e rm e tte d i t e s t a r e i l p r o t o c o l l o G o s s i p i n g :
8 % Ingressi :
9 % s o u r c e s , v e t t o r e c o n t e n e n t e i n o d i da c u i deve p a r t i r e i l
messaggio ;
10 % d e s t i n a t i o n s , v e t t o r e c o n te n e te i d e s t i n a t a r i d e i messaggi ;
11 % f u l l c h a r g e , c a r i c a massima d e l l e b a t t e r i e d e i s e n s o r i w i r e l e s s ;
12 % A, m a t r i c e a d i a c e n z e ;
13 % d , m a t r i c e d e l l e d i s t a n z e mutue d e i n o d i ;
14 % c o s t , c o e f f i c i e n t e p e r m o d i f i c a r e i l p e s o d e l l e t r a t t e da
percorrere
15 % ( m o l t i p l i c a l a d i s t a n z a , q u i n d i s e d12=1 mettendo c o s t = 0 . 1 , l
energia
16 % calera di 0.1) ;
17 %
18 % Uscite :
19 % e n e rg y , m a t r i c e c o r r i s p o n d e n t e a l l andamento d e l l e n e r g i a d u ra n te
le
20 % t r a s m i s s i o n i d i p a c c h e t t i : l e c o l o n n e r a p p r e s e n t a n o i nodi , mentre
la
21 % i esima r i g a r a p p r e s e n t a l i +1esima t r a s m i s s i o n e ( s i s a l v a n e l l a
riga 1
22 % l i s t a n t e i n i z i a l e 0 , q u i n d i e tu tto t r a s l a t o d i 1) ;
23 % a l i v e , andamento d e l l a somma d e i n o d i v i v i p e r round ;
24 % o f f , l i s t a d i nodi s c a r i c h i ;
25 %
26
27
28 % inizializzo variabili
29 N=l e n g t h (A( 1 , : ) ) ;
30 tx_number = z e r o s ( 1 ,N) ;
31 e n e rg y = z e r o s ( l e n g t h ( s o u r c e s ) +1 ,N) ;
32 e n e rg y ( 1 , : )=f u l l c h a r g e o n e s ( 1 ,N) ;
33 s t r= T r a s m i s s i o n e p a c c h e t t i con p r o t o c o l l o G o s s i p i n g . . . ;
34
35 % t r a c c i a nodi s c a r i c h i
36 o f f=z e r o s ( 1 ,N) ;
37 % somma d e i n o d i v i v i
38 a l i v e=z e r o s ( 1 , l e n g t h ( s o u r c e s ) ) ;
39 % conteggio pacchetti c o r r e t t i / pers i / s tes s a sorgente
40 c o r r e c t =0;
41 f a i l s =0;
42 same =0;
43 t =0;
44
45 %c o n t r o l l o che v e t t o r i s o r g e n t e e d e s t i n a z i o n e s i a n o d e l l a s t e s s a
m i s u ra
46 i f l e n g t h ( s o u r c e s ) ~= l e n g t h ( d e s t i n a t i o n s )
26
47 e r r o r ( F o r n i r e s t e s s o numero d i s o r g e n t i e d e s t i n a z i o n i . )
48 end
49
50 % p e r o g n i c o p p i a s o r g e n t e d e s t i n a z i o n e c e r c o i l percorso
51 f o r tx =1: l e n g t h ( s o u r c e s )
52 e n e rg y ( tx + 1 , : )=e n e rg y ( tx , : ) ;
53 s r c=s o u r c e s ( tx ) ;
54 d s t=d e s t i n a t i o n s ( tx ) ;
55
56 % memorizzo l a s o r g e n t e n e l p e r c o r s o
57 %p a th s ( 1 , tx )=s r c ;
58 path=s r c ;
59 node=s r c ;
60 k =2;
61
62 w h i l e ( node ~= d s t )
63 %c o n t r o l l o i n e x t hop d a l l a m a t r i c e d i a d i a c e n z e prendendo g l i
i n d i c i con
64 %f i n d
65 con=f i n d (A( node , : ) ==1) ;
66
67 %s c e l g o c a s u a l m e n te un c o l l e g a m e n t o
68 i f e n e rg y ( tx +1 , node )<=0
69 b re a k
70 e l s e i f i s e m p ty ( con )
71 b re a k
72 else
73 i f ( l e n g t h ( con )==1)
74 next_hop=con ;
75 else
76 next_hop =0;
77 w h i l e ( next_hop == 0 )
78 rand_hop = r a n d i ( max( con ) ) ;
79 i f ~ i s e m p ty ( f i n d ( con == rand_hop , 1 ) )
80 next_hop = rand_hop ;
81 end
82 end
83 end
84 % trasmissione
85 tx_number ( node )=tx_number ( node ) +1;
86 e n e rg y ( tx +1 , node )=e n e rg y ( tx +1 , node )d ( node , next_hop ) c o s t ;
87
88 % se energi a negativa , l a azzero e tol go i col l egam enti al
nodo
89 % p e rc h e non r i e s c e piu a c o m u n i c a re p e rc h e s p e n t o
90 i f ( e n e rg y ( tx +1 , node )<=0)
91 e n e rg y ( tx +1 , node ) =0;
92 A( node , : ) =0;
93 A ( : , node ) =0;
94 t=t +1;
95 o f f ( t ) =1;
96 b re a k
97 end
98 end
99 % r i c e z i o n e : i l nexthop d i v e n t a i l nodo i n esame
100 node=next_hop ;
101 p a th s =[ path next_hop ] ;
102 k=k +1;
103 end
104 % conteggio pacchetti
105 %last_node_ind e x = p a th s ( end ) ;
106 i f ( s o u r c e s ( tx )==d e s t i n a t i o n s ( tx ) )
27
107 same=same +1;
108 e l s e i f ( p a th s ( end )==d s t )
109 c o r r e c t=c o r r e c t +1;
110 else
111 f a i l s = f a i l s +1;
112 end
113
114 %n o d i v i v i
115 a l i v e ( tx )=Nsum ( o f f ) ;
116
117 % ca lc ol o percentuale progresso ca lc ol o
118 p e r c=f l o o r ( tx / l e n g t h ( s o u r c e s ) 1 0 0 ) ;
119 s=num2str ( p e r c ) ;
120 f p r i n t f ( [ s t r s %%\n ] )
121 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
122 end
123
124 % stampa r i s u l t a t i
125 f p r i n t f ( P a c c h e t t i r i c e v u t i : %i (%.1 f%%) , P a c c h e t t i p e r s i : %i (%.1 f%%)
, S t e s s a s o r g e n t e : %i (%.1 f%%)\n\n , c o r r e c t , c o r r e c t / l e n g t h ( s o u r c e s
) 1 0 0 , f a i l s , f a i l s / l e n g t h ( s o u r c e s ) 1 0 0 , same , same / l e n g t h ( s o u r c e s )
100) ;
126
127 end
28
30 mean_d=sum ( d ( s i n k , : ) / (N1) ) ;
31 c l u s t e r _ h e a d s = f i n d ( d ( s i n k , : )<mean_d ) ;
32
33 % d i c o c h i ho c o l l e g a t o e p a s s o l a l i s t a d e i c l u s t e r heads ( t o g l i e n d o
il
34 % sink ) in cluster_heads
35 c o n n e c t e d ( c l u s t e r _ h e a d s ) =1;
36 c l u s t e r _ h e a d s =c l u s t e r _ h e a d s ( c l u s t e r _ h e a d s ~=s i n k ) ;
37
38 %a g g i u n g o i c o l l e g a m n e t i a l l a m a t r i c e d e i c l u s t e r heads
39 Aheads ( s i n k , c l u s t e r _ h e a d s ) =1;
40 Aheads ( c l u s t e r _ h e a d s , s i n k ) =1;
41
42 %% c r e a z i o n e c o l l e g a m e n t i
43 % vengono c r e a t i i c o l l e g a m e n t i piu i d o n e i p e r f o rm a re i c l u s t e r
44
45 %c r e o l a m a t r i c e d i s t a n z e d e i s o l i c l u s t e r head
46 Anodes=z e r o s (N) ;
47
48 % c r e o un m a t r i c e c o n t e n e n t e s o l o l e d i s t a n z e d e i c l u s t e r heads d a i
nodi
49 % dove c e r c h e r o q u e l l o a d i s t a n z a minima p e r c i a s c u n nodo ( mettendo
ad
50 % i n f i n i t o g l i 0 p e r l a r i c e r c a d e l minimo )
51 h e a d _ d i s ta n c e=z e r o s (N) ;
52 h e a d _ d i s ta n c e ( c l u s t e r _ h e a d s , : ) =d ( c l u s t e r _ h e a d s , : ) ;
53 h e a d _ d i s ta n c e ( h e a d _ d i s ta n c e == 0 )=i n f ;
54
55 % c o l l e g o n o d i l i b e r i : f i n c h e non sono t u t t i c o n n e s s i , c o n t i n u o a
cercare i
56 % c o l l e g a m e n t i m i g l i o r i basandomi s u l l e l i m i n a z i o n e d i c l u s t e r heads
vuoti
57 % ( u n i c i componenti d e l c l u s t e r ) e s u l l a r i c e r c a d e l nodo a d i s t a n z a
minima
58 % da un c l u s t e r heads da a s s o c i a r e
59 w h i l e ( sum ( c o n n e c t e d )~=N)
60 f o r k=1:N
61 i f ( c o n n e c t e d ( k )~=1 && k~=s i n k )
62 %c e r c o i l c o l l e g a m e n t o a d i s t a n z a minima
63 new_conn = f i n d ( h e a d _ d i s ta n c e ( : , k )==min ( h e a d _ d i s ta n c e ( : , k )
));
64 %f p r i n t f ( Nodo %i s a ra c o l l e g a t o a %i \n , k , new_conn )
65 % quando l o t r o v o l o a g g i u n g o a l l a m a t r i c e a d i a c e n z e d e i
nodi
66 Anodes ( k , new_conn ) =1;
67 Anodes ( new_conn , k ) =1;
68 %a g g i u n g o q u e s t i n o d i a q u e l l i c o n n e s s i
69 c o n n e c t e d ( k ) =1;
70 %drawnow
71 %p l o t ( [ x i ( k ) x i ( new_conn ) ] , [ y i ( k ) y i ( new_conn ) ] , b )
72 end
73 end
74 % e l i m i n o c l u s t e r heads v u o t i : s e n e l l a m a t r i c e d e i n o d i c e una
75 % c o l o n n a vuota i n c o r r i s p o n d e n z a d i un c l u s t e r head , s i g n i f i c a
che non
76 % ha n o d i c o l l e g a t i
77 j =1;
78 w h i l e j<=l e n g t h ( c l u s t e r _ h e a d s )
79 i f sum ( Anodes ( : , c l u s t e r _ h e a d s ( j ) ) )==0 && j~=s i n k
80 % l o t o l g o d a l l a m a t r i c e d e i c l u s t e r heads
81 Aheads ( s i n k , c l u s t e r _ h e a d s ( j ) ) =0;
82 Aheads ( c l u s t e r _ h e a d s ( j ) , s i n k ) =0;
29
83 h e a d _ d i s ta n c e ( c l u s t e r _ h e a d s ( j ) , : )=i n f ;
84 % l o s e g n a l o come d i s c o n n e s s o
85 c o n n e c t e d ( c l u s t e r _ h e a d s ( j ) ) =0;
86 c l u s t e r _ h e a d s =c l u s t e r _ h e a d s ( c l u s t e r _ h e a d s ~=c l u s t e r _ h e a d s ( j
));
87 end
88 j=j +1;
89 end
90 end
91
92 % metto i n o d i n e i c l u s t e r
93 c l u s t e r=z e r o s (N, l e n g t h ( c l u s t e r _ h e a d s ) ) ;
94 max_ind=z e r o s ( 1 , l e n g t h ( c l u s t e r _ h e a d s ) ) ;
95 f o r k =1: l e n g t h ( c l u s t e r _ h e a d s )
96 c l i = [ c l u s t e r _ h e a d s ( k ) ; f i n d ( Anodes ( : , c l u s t e r _ h e a d s ( k ) )==1) ] ;
97 c l u s t e r ( : , k ) = [ c l i ; z e r o s (Nl e n g t h ( c l i ) , 1 ) ] ;
98 max_ind( k )=f i n d ( c l u s t e r ( : , k ) ~=0 ,1 , l a s t ) ;
99 end
100
101 % m a t r i c e a d i a c e n z e c o m p l e ta
102 A=Aheads+Anodes ;
103 %m a t r i c e g ra d o
104 D=d i a g ( sum (A) ) ;
105 %m a t r i c e l a p l a c i a n a e a u t o v a l o r i
106 L=DA;
107 e=e i g (L) ;
108 % control l o connessione
109 i f l e n g t h ( f i n d ( abs ( e )<1e 5) )==1
110 f p r i n t f ( \b , Grafo c o n n e s s o . \ n )
111 else
112 f p r i n t f ( \b , Grafo non c o n n e s o . \ n )
113 end
114 end
30
25 y_min=y i ( 1 ) ;
26 d_min=s q r t ( ( x_bx_min ) ^2 + (y_by_min ) ^2) ;
27 i_min =1;
28
29 % ricerca migliore
30 f o r a =2:N
31 d_new=s q r t ( ( x_bx i ( a ) ) ^2 + (y_by i ( a ) ) ^2) ;
32 i f (d_new < d_min )
33 x_min=x i ( a ) ;
34 y_min=y i ( a ) ;
35 d_min=d_new ;
36 i_min=a ;
37 end
38 end
39 f p r i n t f ( I l nodo p i u v i c i n o a l b a r i c e n t r o e i l %i , con c o o r d i n a t e
(%.2 f , % . 2 f ) . \ n , i_min , x_min , y_min )
40 end
Codice 8: LEACH
1 f u n c t i o n [ e n e rg y , a l i v e , o f f ] = LEACH( n , f u l l c h a r g e , cost_nodes ,
cost_heads , c l u s t e r _ h e a d s , c l u s t e r , max_ind , d , s i n k )
2 %
3 % N i c o l a S p a r n a c c i 28/5/2017 v e r s i o n e 2
4 %
5 %f u n c t i o n [ e n e rg y , a l i v e , o f f ] = LEACH( n , f u l l c h a r g e , cost_nodes ,
cost_heads , c l u s t e r _ h e a d s , c l u s t e r , max_ind , d , s i n k )
6 %
7 %Funzione che p e rm e tte d i s i m u l a r e i l comportamento d e l p r o t o c o l l o
LEACH
8 % (Low Energy Adaptive C l u s t e r i n g H i e r a r c h y ) p e r una t o p o l o g i a d i n o d i
9 % s u d d i v i s a i n c l u s t e r che comunicano i d a t i r a c c o l t i a t t r a v e r s o i
cluster
10 % heads a l s i n k . I l r i s u l t a t o d i q u e s t a f u n z i o n e e l andamento d e l l
energia
11 % d u ra n te t u t t e l e f a s i d i t r a s m i s s i o n e e l i d e n t i f i c a t i v o d e i n o d i
12 % s c a r i c h i dopo a v e r e s e g u i t o t u t t e l e t r a s m i s s i o n i .
13 %
14 % In p u t :
15 % n , numero d i round d i t r a s m i s s i o n e ;
16 % f u l l c h a r g e , c a r i c a massima d e i n o d i ;
17 % cost_nodes , c o e f f i c i e n t e m o l t i p l i c a t i v o che p e s a i l c o s t o d i
trasmissione
18 % d e l l i n v i o d e i d a t i da n o d i a c l u s t e r heads ;
19 % cost_heads , a n a l o g a m e n te a l p r e c e d e n t e ma r i g u a r d a n t e l a
trasmissione
20 % t r a c l u s t e r heads e s i n k ;
21 % c l u s t e r _ h e a d s , l i s t a d i c l u s t e r heads ;
22 % c l u s t e r , m a t r i c e c o n t e n e n t e i n o d i s u d d i v i s i i n c l u s t e r ( una
colonna
23 % c o r r i s p o n d e ad un c l u s t e r ) ;
24 % max_ind , numero d i c o l l e g a m e n t i p e r o g n i c l u s t e r ;
25 % d , matrice d e l l e d i s t a n z e tra i nodi ;
26 % sink , i d e n t i f i c a t i v o del sink ;
27 %
28 % Output :
29 % e n e rg y , andamento d e l l e n e r g i a r e s i d u a d e i n o d i dove o g n i c o l o n n a
30 % r a p p r e s e n t a l andamento d e l s i n g o l o nodo ;
31 % o f f , l i s t a d ei nodi s c a r i c h i .
32 % a l i v e , andamento d e l l a somma d e i n o d i v i v i p e r round
33
34
31
35 % inizializzazione variabili :
36 N=sum ( max_ind) +1;
37 % energia :
38 e n e rg y=z e r o s ( n+1 , N) ;
39 e n e rg y ( 1 , : )=f u l l c h a r g e ;
40 % nodi s c a r i c h i
41 o f f=z e r o s ( 1 ,N) ;
42 % somma d e i n o d i v i v i
43 a l i v e=z e r o s ( 1 , n ) ;
44 % v a r i a b i l i conteggio pacchetti
45 c o r r e c t =0;
46 f a i l s =0;
47
48 % ad o g n i t r a s m i s s i o n e vengono e l e t t i d e i c l u s t e r head , q u i n d i vengono
49 % azzerate l e m atri ci di adiacenza e r i c r e o i col l egam enti
50 s t r= T r a s m i s s i o n e p a c c h e t t i con p r o t o c o l l o LEACH . . . ;
51 f o r tx =1: n
52 e n e rg y ( tx + 1 , : )=e n e rg y ( tx , : ) ;
53 i f tx > 1
54 Aheads=z e r o s (N) ;
55 Anodes=z e r o s (N) ;
56 f o r k=1: l e n g t h ( c l u s t e r _ h e a d s )
57 %e l e g g o c a s u a l m e n te i l nuovo c l u s t e r head p e r c i a s c u n
cluster
58 c l u s t e r _ h e a d s ( k )=c l u s t e r ( r a n d i ( max_ind( k ) , 1 ) , k ) ;
59 %cambio i c o l l e g a m e n t i c o l s i n k
60 Aheads ( c l u s t e r _ h e a d s ( k ) , s i n k ) =1;
61 Aheads ( s i n k , c l u s t e r _ h e a d s ( k ) ) =1;
62 %c o l l e g o i n o d i d e l c l u s t e r a l c l u s t e r head
63 f o r j =1: max_ind( k )
64 i f c l u s t e r ( j , k ) ~= c l u s t e r _ h e a d s ( k )
65 Anodes ( c l u s t e r _ h e a d s ( k ) , c l u s t e r ( j , k ) ) =1;
66 Anodes ( c l u s t e r ( j , k ) , c l u s t e r _ h e a d s ( k ) ) =1;
67 end
68 end
69 end
70 end
71
72 % t r a s m i s s i o n e d a i n o d i a c l u s t e r heads
73 f o r k=1: l e n g t h ( c l u s t e r _ h e a d s )
74 % per ogni c l u s t e r f a c c i o tra s m e tte r e i l pacchetto a i nodi
verso i l
75 % l o r o c l u s t e r head : i n q u e s t a f a s e devo e s c l u d e r e i c l u s t e r
head e
76 % i n o d i g i a s c a r i c h i p e r s t u d i a r e i l consumo d i e n e r g i a
77 f o r j =1: max_ind( k )
78 i f c l u s t e r ( j , k )~=c l u s t e r _ h e a d s ( k ) && o f f ( c l u s t e r ( j , k ) )~=1
79 e n e rg y ( tx +1 , c l u s t e r ( j , k ) )=e n e rg y ( tx +1 , c l u s t e r ( j , k ) )
c o s t_ n o d e s d ( c l u s t e r _ h e a d s ( k ) , c l u s t e r ( j , k ) ) ;
80 % s e n o d i s c a r i c h i metto a e n e r g i a z e r o
81 i f e n e rg y ( tx +1 , c l u s t e r ( j , k ) ) <= 0 && o f f ( c l u s t e r ( j , k ) )
~=1
82 e n e rg y ( tx +1 , c l u s t e r ( j , k ) ) =0;
83 o f f ( c l u s t e r ( j , k ) ) =1;
84 end
85 end
86 end
87 % e n e r g i a t r a s m i s s i o n e CH a s i n k
88 i f e n e rg y ( tx , c l u s t e r _ h e a d s ( k ) ) <= 0
89 % s e i l c l u s t e r head e s c a r i c o non r i e s c e a i n v i a r e i
pacchetti
90 % che g l i sono s t a t i c o n s e g n a t i d a i n o d i
32
91 f a i l s = f a i l s +max_ind( k ) ;
92 else
93 % e n e r g i a persa per tra s m e tte re a l s i n k : se i l c l u s t e r
head non
94 % e s c a r i c o a l l o r a t u t t i i p a c c h e t t i ( p a r i a i nodi d el
cluster )
95 % vengono i n v i a t i a l s i n k c o r r e t t a m e n t e
96 e n e rg y ( tx +1 , c l u s t e r _ h e a d s ( k ) )=e n e rg y ( tx +1 , c l u s t e r _ h e a d s ( k )
)c o s t_ h e a d s d ( c l u s t e r _ h e a d s ( k ) , s i n k ) ;
97 c o r r e c t=c o r r e c t+max_ind( k ) ;
98 % s e c l u s t e r head s c a r i c h i e n e r g i a a z e r o
99 i f e n e rg y ( tx +1 , c l u s t e r _ h e a d s ( k ) ) <= 0 && o f f ( c l u s t e r _ h e a d s
( k ) )~=1
100 e n e rg y ( tx +1 , c l u s t e r _ h e a d s ( k ) ) =0;
101 o f f ( c l u s t e r _ h e a d s ( k ) ) =1;
102 end
103 end
104 end
105 %n o d i v i v i
106 a l i v e ( tx )=Nsum ( o f f ) ;
107 i f a l i v e ( tx )==1
108 a l i v e ( tx ) =0;
109 end
110
111 % ca lc ol o percentuale progresso ca lc ol o
112 p e r c=f l o o r ( tx /n 1 0 0 ) ;
113 s=num2str ( p e r c ) ;
114 f p r i n t f ( [ s t r s %%\n ] )
115 s t r=repmat ( \b , 1 , l e n g t h ( s ) +2) ;
116 end
117 t o t=c o r r e c t+ f a i l s ;
118 f p r i n t f ( P a c c h e t t i r i c e v u t i : %i (%.1 f%%) , P a c c h e t t i p e r s i : %i (%.1 f%%)
\n\n , c o r r e c t , c o r r e c t / t o t 1 0 0 , f a i l s , f a i l s / t o t 1 0 0 ) ;
119 end
33
22 % cost = 0. 01;
23 % c o s t_ n o d e s= 0 . 0 1 ;
24 % c o s t_ h e a d s= 0 . 0 3 ;
25 %
26 % Dove s i vede come i l F l o o d i n g s c a r i c h i i n o d i molto piu v e l o c e m e n t e
27 % r i s p e t t o g l i a l t r i due e come i l LEACH r i e s c a ad a v e r e m o l t i piu
dati
28 % c o l l e z i o n a t i n e l l o s t e s s o numero d i round n o n o s t a n t e l a c a r i c a d e i
nodi
29 % s i a paragonabile al Gossiping .
30
31 clear
32 close all
33 %% D a ti i n i n p u t
34 % D a ti p e r l a c r e a z i o n e d e l l a mappa :
35 % N, numero d i n o d i da d i s p o r r e n e l l a mappa ;
36 % l , l a t o d e l r e t t a n g o l o su c u i d i s t r u b u i r e i n o d i ;
37 % h , al tezza del rettangolo ;
38 % r , d i s t a n z a minima da c i a s c u n nodo .
39 N=i n p u t ( I n s e r i r e i l numero d i n o d i : ) ;
40 l=i n p u t ( I n s e r i r e l a t o d e l r e t t a n g o l o : ) ;
41 h=i n p u t ( I n s e r i r e a l t e z z a d e l r e t t a n g o l o : ) ;
42 r=i n p u t ( I n s e r i r e l a d i s t a n z a minima t r a n o d i : ) ;
43
44 % D a ti p e r l a s i m u l a z i o n e :
45 % n_tx , numero d i round d i t r a s m i s s i o n e ;
46 % s o u r c e s , a r r a y c o n t e n e n t e t u t t i i n o d i da c u i p a r t e i l m e s s a g g i o ;
47 % d e s t i n a t i o n s , a r r a y c o n t e n e n t e l e d e s t i n a z i o n i d e i m e s s a g g i da
source ;
48 % f u l l c h a r g e , massima c a r i c a d e l l e b a t t e r i e d e i n o d i ;
49 % c o s t , c o e f f i c i e n t e m o l t i p l i c a t i v o che aumenta / d i m i u s c e i l c o s t o
della
50 % trasmissione ;
51 tx_number=i n p u t ( I n s e r i r e numero d i round d i t r a s m i s s i o n e : ) ;
52 s o u r c e s =[ r a n d i (N, 1 , tx_number 1) 1 ] ;
53 d e s t i n a t i o n s =[ r a n d i (N, 1 , tx_number 1) 1 ] ;
54 f u l l c h a r g e = i n p u t ( I n s e r i r e c a r i c a massima d e l l e b a t t e r i e : ) ;
55 cost = input ( I n s e r i r e costo dei col l egam enti Gossiping / Flooding : ) ;
56
57 % In p u t LEACH:
58 % c o s t_ n o d e s e i l c o e f f i c i e n t e che m o l t i p l i c a l a d i s t a n z a p e r
r e n d e r e piu
59 % o meno e n e r g e t i c a m e n t e d i s p e n d i o s a l a t r a s m i s s i o n e t r a nodo e
c l u s t e r head ;
60 % c o s t_ h e a d s e a n a l o g o a l p r e c e d e n t e ma r i g u a r d a l a t r a s m i s s i o n e
tra
61 % c l u s t e r heads e s i n k ;
62 c o s t_ n o d e s=i n p u t ( I n s e r i r e c o s t o d e i c o l l e g a m e n t i nodi c l u s t e r head :
);
63 c o s t_ h e a d s=i n p u t ( I n s e r i r e c o s t o d e i c o l l e g a m e n t i c l u s t e r headss i n k :
) ;
64
65 tic
66
67 %% C r e a z i o n e Mappa
68 % s i puo s c e g l i e r e d i u t i l i z z a r e una t o p o l o g i a random , che
distribuisce i
69 % n o d i i n modo c a s u a l e n e l r e t t a n g o l o , o l a t o p o l o g i a ad a n e l l o r i n g
,
70 % dove r d i v e n t a i l r a g g i o d e l l a n e l l o .
71 [ x i , y i ]= p l a c e n o d e s (N, l , h , random , no p l o t , r ) ;
72
34
73 %% C r e a z i o n e c o l l e g a m e n t i
74 % q u e s t a f u n z i o n e da i n u s c i t a l a m a t r i c e A d e l l e a d i a c e n z e , l a
matrice D
75 % d e l g ra d o e l a d d e l l e d i s t a n z e . S i c e r c a d i c r e a r e i l minor numero
di
76 % c o l l e g a m e n t i e s e con q u e s t a c o n f i g u r a z i o n e non s i ha una r e t e
connessa ,
77 % s i aumenta i l numero d i c o l l e g a m e n t i i n modo a u to m a ti c o .
78 [ A, D, d]= c o n n e c t n o d e s ( x i , y i ) ;
79
80 %% FLOODING
81 [ energy_flooding , al i v e_ f l ood i n g ] = f l ood i n g ( sources , d es ti n ati on s ,
f u l l c h a r g e , A, d , c o s t ) ;
82
83 %% GOSSIPING
84 [ energy_gossiping , al i v e_ gos s i p i n g ] = gos s i p i n g ( sources , d es ti n ati on s ,
f u l l c h a r g e , A, d , c o s t ) ;
85
86 %% LEACH
87
88 % Ricerca sink
89 [ s i n k , x_sink , y_sink , x_b , y_b ] = c e n t e r n o d e ( x i , y i ) ;
90 f p r i n t f ( S i n k : %i \n , s i n k )
91
92 % creazione cluster
93 [ c l u s t e r _ h e a d s , c l u s t e r , max_ind , Aheads , Anodes ] = c l u s t e r i n g ( s i n k , d )
;
94
95 % p r o t o c o l l o LEACH
96 [ energy_LEACH , alive_LEACH ] = LEACH( tx_number , f u l l c h a r g e , cost_nodes ,
cost_heads , c l u s t e r _ h e a d s , c l u s t e r , max_ind , d , s i n k ) ;
97
98 %% G r a f i c i d e l l e e n e r g i e r e s i d u e
99 % Grafico del l energi a Flooding
100 f i g u r e ( u n i t s , n o r m a l i z e d , o u t e r p o s i t i o n , [ 0 0 1 1 ] , Name ,
Andamento d e l l e e n e r g i e r e s i d u e ) ,
101 s u b p l o t ( 3 , 1 , 1 ) , h o l d on ,
102 t i t l e ( Andamento e n e r g i a r e s i d u a d e i n o d i con F l o o d i n g ) ,
103 x l a b e l ( #Round ) ,
104 y l ab el ( Energia res i d u a ) ,
105 axis ( [ 1 length ( sources ) 0 f ul l ch ar ge ] )
106 f o r k =1:N
107 p l o t ( 1 : l e n g t h ( s o u r c e s ) +1 , e n e r g y _ f l o o d i n g ( : , k ) )
108 end
109
110 % Grafico del l energi a Gossiping
111 s u b p l o t ( 3 , 1 , 2 ) , h o l d on ,
112 t i t l e ( Andamento e n e r g i a r e s i d u a d e i n o d i con G o s s i p i n g ) ,
113 x l a b e l ( #Round ) ,
114 y l ab el ( Energia res i d u a ) ,
115 axis ( [ 1 length ( sources ) 0 f ul l ch ar ge ] )
116 f o r k =1:N
117 p l o t ( 1 : l e n g t h ( s o u r c e s ) +1 , e n e r g y _ g o s s i p i n g ( : , k ) )
118 end
119
120 % G r a f i c o d e l l e n e r g i a LEACH
121 s u b p l o t ( 3 , 1 , 3 ) , h o l d on ,
122 t i t l e ( Andamento e n e r g i a r e s i d u a d e i n o d i con LEACH ) ,
123 x l a b e l ( #Round ) ,
124 y l ab el ( Energia res i d u a ) ,
125 axis ( [ 1 length ( sources ) 0 f ul l ch ar ge ] )
126 f o r k =1:N
35
127 p l o t ( 1 : l e n g t h ( s o u r c e s ) +1 , energy_LEACH ( : , k ) )
128 end
129
130 % G r a f i c o e n e r g i e medie
131 E _ f l o o d i n g = sum ( e n e r g y _ f l o o d i n g , 2 ) /N;
132 E _ g o s s i p i n g = sum ( e n e r g y _ g o s s i p i n g , 2 ) /N;
133 E_LEACH = sum ( energy_LEACH , 2 ) /N;
134 f i g u r e ( Name , E n e r g i e medie e n o d i c a r i c h i ) ,
135 subplot (2 ,1 ,1) ,
136 p l o t ( 0 : tx_number , E_flooding , r , 0 : tx_number , E_gossiping , b , 0 :
tx_number , E_LEACH , k )
137 t i t l e ( E n e r g i e medie n e i n o d i ) ,
138 x l a b e l ( #Round ) ,
139 y l ab el ( Energia res i d u a )
140 l e g e n d ( F l o o d i n g , G o s s i p i n g , LEACH )
141
142 % Graf i co d ei nodi v i v i
143 subplot (2 ,1 ,2)
144 plot ( 1: length ( sources ) , alive_flooding , r , 1: length ( sources ) ,
a l i v e _ g o s s i p i n g , b , 1 : l e n g t h ( s o u r c e s ) , alive_LEACH , k )
145 a x i s ( [ 0 tx_number 0 N] ) ,
146 t i t l e ( Numero d i n o d i v i v i a l l aumentare d e i ro u n d s ) ,
147 x l a b e l ( #Round ) ,
148 y l a b e l ( Numero d i n o d i c a r i c h i )
149 l e g e n d ( F l o o d i n g , G o s s i p i n g , LEACH )
150
151
152
153 %% G r a f i c i d e l l e mappe d e i n o d i
154 % Mappa F l o o d i n g e G o s s i p i n g
155 f i g u r e ( Name , Mappe d e i n o d i ) ,
156 s u b p l o t ( 2 , 1 , 1 ) , h o l d on ,
157 p l o t ( x i , y i , ob ) ,
158 a x i s ([ l /2 l /2 h /2 h / 2 ] ) ,
159 t i t l e ( Mappa d e i n o d i c o l l e g a t i p e r F l o o d i n g e G o s s i p i n g )
160 p l o t l i n k (A, x i , y i , r , P l o t c o l l e g a m e n t i u t i l i z z a t i F l o o d i n g e
Gossiping . . . )
161
162 % Mappa LEACH
163 s u b p l o t ( 2 , 1 , 2 ) , h o l d on
164 p l o t ( x i , y i , ob , x_sink , y_sink , r ) ,
165 a x i s ([ l /2 l /2 h /2 h / 2 ] ) ,
166 t i t l e ( Mappa d e i n o d i s u d d i v i s i i n c l u s t e r s )
167 p l o t l i n k ( Aheads , x i , y i , r , P l o t d e i c o l l e g a m e n t i d e i c l u s t e r heads . . .
)
168 p l o t l i n k ( Anodes , x i , y i , b , P l o t d e i c o l l e g a m e n t i d e i n o d i . . . )
169
170 f p r i n t f ( Tempo d i s i m u l a z i o n e : %.4 f s . \ n , t o c )
36