-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_clusters.m
112 lines (85 loc) · 3.01 KB
/
create_clusters.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
function clusters = create_clusters(ID, D, params)
% USAGE
% clusters = create_clusters( ID, D, a, w_1, w_2 )
%
% INPUTS
% ID - [m x m] Interest Distance matrix
% D - [m x m] Physical Distance matrix
% a - probability for device to form own cluster
% w_1 - weight for ID in cluster creation
% w_2 - weight for D in cluster creation
%
% OUTPUTS
% clusters - [1 x c] cell array with corresponding clusters
IDD = params.wa_1.*(1./ID) + params.wa_2.*D;
% remove Inf in diagonal so that it doesn't connect to itself
IDD(isinf(IDD)) = 0;
% myIDD will be filled with IDD values as the devices enter into clusters
% so that the denominator in the equation is correctly calculated
% It will only contain nodes that have already been put in a cluster
myIDD = zeros(size(IDD));
m = size(IDD,1);
% put first device in it's own cluster
clusters = {};
clusters{1} = 1;
% repeat for all other devices
for ii = 2:m
% fill myIDD to get the right denominator as discussed above
% will add corresponding entries in IDD of the node with every other
% node that already exists in the clusters
for jj = 1:size(clusters,2)
myIDD = fill_myIDD(IDD,myIDD,ii,clusters{jj});
end
prob = calculate_probabilities(IDD, myIDD, params.a, ii, clusters);
% round to remove decimal points after the 4th digit
sum_prob=round((sum(prob)*10^4)/10^4);
if sum_prob ~= 1.0
double(sum(prob))
end
if sum_prob ~= 1
error('Probabilities don''t sum up to one');
end
cluster_number = find_matching_cluster(prob);
% if it is in it's own cluster (last cluster), create a new entry
% else append it to the other nodes of the cluster
if cluster_number < size(prob,2)
clusters{cluster_number} = [clusters{cluster_number}, ii];
else
clusters{cluster_number} = ii;
end
end
end
function prob = calculate_probabilities(IDD, myIDD, a, node, clusters)
prob = zeros(1,size(clusters,2)+1);
denominator = sum(myIDD, 2) + a;
for jj = 1:size(clusters,2)
numerator = calculate_numerator(IDD,node,clusters{jj});
prob(jj) = numerator/denominator(node);
end
% probability it forms it's own cluster
prob(size(clusters,2)+1) = a/denominator(node);
end
function numerator = calculate_numerator(IDD, node, cluster)
numerator = 0;
for ii = 1:size(cluster,2)
numerator = numerator + IDD(node,cluster(ii));
end
end
function myIDD = fill_myIDD(IDD, myIDD, node,cluster)
% This function fills the myIDD table with the nodes as they enter
% into the clusters
for ii = 1:size(cluster,2)
myIDD(node,cluster(ii)) = IDD(node,cluster(ii));
myIDD(cluster(ii),node) = IDD(node,cluster(ii));
end
end
function cluster = find_matching_cluster(prob)
cumsum_prob = cumsum(prob);
ball = rand;
for jj = 1:(size(cumsum_prob,2))
if ball < cumsum_prob(jj)
cluster = jj;
break;
end
end
end