-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_dis_hyd.m
191 lines (186 loc) · 8.82 KB
/
demo_dis_hyd.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
clear;
%Example demonstration of the hybrid initialization (random & NNDSVD initialization) for disjoint community detection
%====================
%Load the network datasets
%file = load('data\washington.mat');
%edges = file.washington.adj;
%adj = Compute_adjmatrix(edges); %Adjacent matrix
%att = file.washington.content; %Network attribute matrix
%gnd = file.washington.groundtruth(:,2); %Ground-truth of community membership
%==========
file = load('data\texas.mat');
edges = file.texas.adj;
adj = Compute_adjmatrix(edges); %Adjacent matrix
att = file.texas.content; %Adjacent matrix
gnd = file.texas.groundtruth(:,2); %Adjacent matrix
%==========
%file = load('data\cora.mat');
%adj = file.G; %Adjacent matrix
%att = file.content; %Network attribute matrix
%gnd = file.labels; %Ground-truth of community membership
%==========
%file = load('data\cite.mat');
%adj = file.G; %Adjacent matrix
%att = file.content; %Network attribute matrix
%gnd = file.labels; %Ground-truth of community membership
%==========
%Get the network parameters
num_nodes = size(adj, 1); %Number of nodes
num_atts = size(att, 2); %Number of node attributes
num_topo_clus = max(gnd); %Number of topology clusters
num_att_clus = num_topo_clus; %Number of attribtue clusters
%====================
max_iter = 1e4; %Maximum number of optimization iteratons
min_error = 1e-5; %Minimum relative error to determine the convergence of optimization
%==========
num_runs = 10; %Number of independent runs of DHCD
num_ch_runs = 10; %Number of independent runs of channel selection
sample_rate = 0.10; %Sampling rate of Ch-NMI
%==========
lambd = 0;
%====================
%Remove self-connectd edges
for i=1:num_nodes
adj(i, i) = 0;
end
%==========
adj = sparse(adj);
att = sparse(att);
%p = parpool;
%====================
params = [0.1:0.1:0.9, 1:1:10];
[~, num_params] = size(params); %Number of parameter settings
for l=1:num_params
%==============================
%T-A Channel, i.e., DHCD T-A
alpha = params(l);
fprintf('Alpha: %f\n', alpha);
%====================
[topo_mem_init, ~] = NNDSVD(adj, num_topo_clus, 0); %Initialize the topology cluster membership matrix, i.e., X
[~, att_desc_init] = NNDSVD(att, num_att_clus, 0); %Initialize the attribute description matrix, i.e., Z
att_desc_init = att_desc_init';
trans_TA_init = rand(num_topo_clus, num_att_clus); %Initialize the T-A transition matrix, i.e., U
[topo_mem_TA,att_desc_TA,trans_TA,obj] = DHCD_TA(adj,att,topo_mem_init,att_desc_init,trans_TA_init,alpha,lambd,max_iter,min_error);
fprintf('T-A Obj. %8.4f\n', obj);
%==========
[~, labels] = max(topo_mem_TA, [], 2); %Extract the community detection result from X
chNMI = get_Ch_NMI(labels, gnd, sample_rate, num_ch_runs); %Derive the corresponding Ch-NMI
fprintf('Ch-NMI %f\n', chNMI);
%==========
%Evaluate the performance of current community detection result
NMI_TA = compute_NMI(gnd, labels);
res = bestMap(gnd, labels);
AC_TA = length(find(gnd == res))/length(gnd);
fprintf('T-A Ch. NMI: %8.4f; AC: %8.4f\n', NMI_TA, AC_TA);
fid = fopen('DHCD_T-A(dis_hyd).txt', 'at');
fprintf(fid, 'T-A Ch. Alpha: %8.4f; Obj: %8.4f; Ch-NMI: %8.4f; NMI: %8.4f; AC: %8.4f\n', [alpha, obj, chNMI, NMI_TA, AC_TA]);
fclose(fid);
%====================
%Independently run the DHCD T-A algorihtm multiple times
for t=2:num_runs
trans_TA_init = rand(num_topo_clus, num_att_clus); %Initialize the T-A transition matrix, i.e., U
[cur_topo_mem_TA,cur_att_desc_TA,cur_trans_TA,cur_obj] = DHCD_TA(adj,att,topo_mem_init,att_desc_init,trans_TA_init,alpha,lambd,max_iter,min_error);
fprintf('T-A Obj. %8.4f\n', cur_obj);
%==========
[~, labels] = max(cur_topo_mem_TA, [], 2); %Extract the community detection result from X
chNMI = get_Ch_NMI(labels, gnd, sample_rate, num_ch_runs); %Derive the corresponding Ch-NMI
%==========
%Evaluate the performance of current community detection result
fprintf('Ch-NMI %f\n', chNMI);
NMI_TA = compute_NMI(gnd, labels);
res = bestMap(gnd, labels);
AC_TA = length(find(gnd == res))/length(gnd);
fprintf('T-A Ch. NMI: %8.4f; AC: %8.4f\n', NMI_TA, AC_TA);
%==========
fid = fopen('DHCD_T-A(dis_hyd).txt', 'at');
fprintf(fid, 'T-A Ch. Alpha: %8.4f; Obj: %8.4f; Ch-NMI: %8.4f; NMI: %8.4f; AC: %8.4f\n', [alpha, cur_obj, chNMI, NMI_TA, AC_TA]);
fclose(fid);
%====================
%Update the best community detection result based on the value of objective function
if cur_obj<obj
topo_mem_TA = cur_topo_mem_TA;
att_desc_TA = cur_att_desc_TA;
trans_TA = cur_trans_TA;
obj = cur_obj;
end
end
%====================
%Evalute the performance of the best community detection result
[~, labels] = max(topo_mem_TA, [], 2);
NMI_TA = compute_NMI(gnd, labels);
res = bestMap(gnd, labels);
AC_TA = length(find(gnd == res))/length(gnd);
fprintf('(Obj. Min.)T-A Ch. NMI: %8.4f; AC: %8.4f\n', NMI_TA, AC_TA);
fprintf('====================\n');
%===========
fid = fopen('DHCD_T-A(dis_hyd).txt', 'at');
fprintf(fid, 'Obj. Min. T-A Ch. Alpha: %8.4f; Obj: %8.4f; NMI: %8.4f; AC: %8.4f\n', [alpha, obj, NMI_TA, AC_TA]);
fprintf(fid, '====================\n');
fclose(fid);
%==============================
%A-T Channel, i.e., DHCD A-T
beta = alpha;
fprintf('Beta: %f\n', beta);
%====================
%Initialize the attribute cluster membership matrix & attribute description matrix, i.e., Y & Z
[att_mem_init, att_desc_init] = NNDSVD(att, num_att_clus, 0);
att_desc_init = att_desc_init';
trans_AT_init = rand(num_att_clus, num_topo_clus); %Initialize the A-T transition matrix, i.e., V
[att_desc_AT,att_mem_AT,trans_AT,obj] = DHCD_AT(adj,att,att_mem_init,att_desc_init,trans_AT_init,beta,lambd,max_iter,min_error);
fprintf('A-T Obj. %8.4f\n', obj);
%==========
[~, labels] = max(att_mem_AT, [], 2); %Extract the community detection result from Y
chNMI = get_Ch_NMI(labels, gnd, sample_rate, num_ch_runs); %Derive the corresponding Ch-NMI
fprintf('Ch-NMI %f\n', chNMI);
%==========
%Evaluate the performance of current community detection result
NMI_AT = compute_NMI(gnd, labels);
res = bestMap(gnd, labels);
AC_AT = length(find(gnd == res))/length(gnd);
fprintf('A-T Ch. NMI: %8.4f; AC: %8.4f\n', NMI_AT, AC_AT);
fid = fopen('DHCD_A-T(dis_hyd).txt', 'at');
fprintf(fid, 'A-T Ch. Beta: %8.4f; Obj: %8.4f; Ch-NMI: %8.4f; NMI: %8.4f; AC: %8.4f\n', [beta, obj, chNMI, NMI_AT, AC_AT]);
fclose(fid);
%====================
%Independently run the DHCD A-T algorihtm multiple times
for t=2:num_runs
trans_AT_init = rand(num_att_clus, num_topo_clus); %Initialize the A-T transition matrix, i.e., V
[cur_att_desc_AT,cur_att_mem_AT,cur_trans_AT,cur_obj] = DHCD_AT(adj,att,att_mem_init,att_desc_init,trans_AT_init,beta,lambd,max_iter,min_error);
fprintf('A-T Obj. %8.4f\n', cur_obj);
%==========
[~, labels] = max(cur_att_mem_AT, [], 2); %Extract the community detection result from Y
chNMI = get_Ch_NMI(labels, gnd, sample_rate, num_ch_runs); %Derive the corresponding Ch-NMI
fprintf('Ch-NMI %f\n', chNMI);
%==========
%Evaluate the performance of current community detection result
NMI_AT = compute_NMI(gnd, labels);
res = bestMap(gnd, labels);
AC_AT = length(find(gnd == res))/length(gnd);
fprintf('A-T Ch. NMI: %8.4f; AC: %8.4f\n', NMI_AT, AC_AT);
fid = fopen('DHCD_A-T(dis_hyd).txt', 'at');
fprintf(fid, 'A-T Ch. Beta: %8.4f; Obj: %8.4f; Ch-NMI: %8.4f; NMI: %8.4f; AC: %8.4f\n', [beta, cur_obj, chNMI, NMI_AT, AC_AT]);
fclose(fid);
%===================
%Update the best community detection result based on the value of objective function
if cur_obj<obj
att_mem_AT = cur_att_mem_AT;
att_desc_AT = cur_att_desc_AT;
trans_AT = cur_trans_AT;
obj = cur_obj;
end
end
%======================
%Evalute the performance of the best community detection result
[~, labels] = max(att_mem_AT, [], 2);
NMI_AT = compute_NMI(gnd, labels);
res = bestMap(gnd, labels);
AC_AT = length(find(gnd == res))/length(gnd);
fprintf('(Obj. Min.)A-T Ch. NMI: %8.4f; AC: %8.4f\n', NMI_AT, AC_AT);
fprintf('====================\n');
%==========
fid = fopen('DHCD_A-T(dis_hyd).txt', 'at');
fprintf(fid, 'Obj. Min. A-T Ch. Beta: %8.4f; Obj: %8.4f; NMI: %8.4f; AC: %8.4f\n', [beta, obj, NMI_AT, AC_AT]);
fprintf(fid, '=====================\n');
fclose(fid);
end
%delete(p);