-
Notifications
You must be signed in to change notification settings - Fork 4
/
Leach_SelectCluster.m
37 lines (32 loc) · 1.23 KB
/
Leach_SelectCluster.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
function Network=Leach_SelectCluster(Model,Network)
Temp=num2cell(zeros(1,length(Network)));
[Network.IsCluster]=Temp{:};
AliveIndex=([Network(1:end-1).Energy]>0);
Alive=find(AliveIndex);
Input=[[Network(1:end-1).X]',[Network(1:end-1).Y]'];
Input=Input(Alive,:);
K=min([length(Alive),Model.NCluster]);
Kmeans=kmeans(Input,K);
Temp=num2cell(Kmeans);
[Network(Alive).ClusterIndex]=Temp{:};
for i=1:K
Index=find(AliveIndex .* [Network.ClusterIndex]==i);
IndexG=find([Network(Index).G]<=0);
if(length(IndexG)~=0)
Selected=randsample(IndexG,1);
else
Selected=randsample(Index,1);
end
Network(Selected).IsCluster=1;
Network(Selected).G=round(1/Model.P)-1;
if (Network(Selected).Distance>Model.BaseDistance)
Network(Selected).Energy=Network(Selected).Energy ...
-( ( (Model.Energy.Send_Bit +Model.Energy.Aggregation_Bit)*(4000) ...
+ Model.Energy.Ampli_Upper*4000*( Network(Selected).Distance^2)));
else
Network(Selected).Energy=Network(Selected).Energy -...
(( (Model.Energy.Send_Bit +Model.Energy.Aggregation_Bit)*(4000) ...
+ Model.Energy.Ampli_Under*4000*( Network(Selected).Distance^2)));
end
end
end