/src/igraph/src/community/spinglass/clustertool.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | igraph library. |
3 | | Copyright (C) 2006-2012 Gabor Csardi <csardi.gabor@gmail.com> |
4 | | 334 Harvard street, Cambridge, MA 02139 USA |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 2 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | | 02110-1301 USA |
20 | | |
21 | | */ |
22 | | |
23 | | /* The original version of this file was written by Joerg Reichardt |
24 | | The original copyright notice follows here */ |
25 | | |
26 | | /*************************************************************************** |
27 | | main.cpp - description |
28 | | ------------------- |
29 | | begin : Tue Jul 13 11:26:47 CEST 2004 |
30 | | copyright : (C) 2004 by |
31 | | email : |
32 | | ***************************************************************************/ |
33 | | |
34 | | /*************************************************************************** |
35 | | * * |
36 | | * This program is free software; you can redistribute it and/or modify * |
37 | | * it under the terms of the GNU General Public License as published by * |
38 | | * the Free Software Foundation; either version 2 of the License, or * |
39 | | * (at your option) any later version. * |
40 | | * * |
41 | | ***************************************************************************/ |
42 | | |
43 | | #include "NetDataTypes.h" |
44 | | #include "NetRoutines.h" |
45 | | #include "pottsmodel_2.h" |
46 | | |
47 | | #include "igraph_community.h" |
48 | | #include "igraph_components.h" |
49 | | #include "igraph_error.h" |
50 | | #include "igraph_interface.h" |
51 | | #include "igraph_random.h" |
52 | | |
53 | | #include "core/interruption.h" |
54 | | #include "core/exceptions.h" |
55 | | |
56 | | static igraph_error_t igraph_i_community_spinglass_orig( |
57 | | const igraph_t *graph, |
58 | | const igraph_vector_t *weights, |
59 | | igraph_real_t *modularity, |
60 | | igraph_real_t *temperature, |
61 | | igraph_vector_int_t *membership, |
62 | | igraph_vector_int_t *csize, |
63 | | igraph_int_t spins, |
64 | | igraph_bool_t parupdate, |
65 | | igraph_real_t starttemp, |
66 | | igraph_real_t stoptemp, |
67 | | igraph_real_t coolfact, |
68 | | igraph_spincomm_update_t update_rule, |
69 | | igraph_real_t gamma); |
70 | | |
71 | | static igraph_error_t igraph_i_community_spinglass_negative( |
72 | | const igraph_t *graph, |
73 | | const igraph_vector_t *weights, |
74 | | igraph_real_t *modularity, |
75 | | igraph_real_t *temperature, |
76 | | igraph_vector_int_t *membership, |
77 | | igraph_vector_int_t *csize, |
78 | | igraph_int_t spins, |
79 | | igraph_bool_t parupdate, |
80 | | igraph_real_t starttemp, |
81 | | igraph_real_t stoptemp, |
82 | | igraph_real_t coolfact, |
83 | | igraph_spincomm_update_t update_rule, |
84 | | igraph_real_t gamma, |
85 | | igraph_real_t gamma_minus); |
86 | | |
87 | | /** |
88 | | * \function igraph_community_spinglass |
89 | | * \brief Community detection based on statistical mechanics. |
90 | | * |
91 | | * This function implements the community structure detection |
92 | | * algorithm proposed by Joerg Reichardt and Stefan Bornholdt. |
93 | | * The algorithm is described in their paper: Statistical Mechanics of |
94 | | * Community Detection, http://arxiv.org/abs/cond-mat/0603718 . |
95 | | * |
96 | | * </para><para> |
97 | | * From version 0.6, igraph also supports an extension to |
98 | | * the algorithm that allows negative edge weights. This is described |
99 | | * in V. A. Traag and Jeroen Bruggeman: Community detection in networks |
100 | | * with positive and negative links, http://arxiv.org/abs/0811.2329 . |
101 | | * |
102 | | * \param graph The input graph, it may be directed but the direction |
103 | | * of the edges is ignored by the algorithm. |
104 | | * \param weights The vector giving the edge weights, it may be \c NULL, |
105 | | * in which case all edges are weighted equally. The edge weights |
106 | | * must be positive unless using the \c IGRAPH_SPINCOMM_IMP_NEG |
107 | | * implementation. |
108 | | * \param modularity Pointer to a real number, if not \c NULL then the |
109 | | * modularity score of the solution will be stored here. This is the |
110 | | * gereralized modularity, taking into account the resolution parameter |
111 | | * \p gamma. See \ref igraph_modularity() for details. |
112 | | * \param temperature Pointer to a real number, if not \c NULL then |
113 | | * the temperature at the end of the algorithm will be stored |
114 | | * here. |
115 | | * \param membership Pointer to an initialized vector or \c NULL. If |
116 | | * not \c NULL then the result of the clustering will be stored |
117 | | * here. For each vertex, the number of its cluster is given, with the |
118 | | * first cluster numbered zero. The vector will be resized as |
119 | | * needed. |
120 | | * \param csize Pointer to an initialized vector or \c NULL. If not \c |
121 | | * NULL then the sizes of the clusters will stored here in cluster |
122 | | * number order. The vector will be resized as needed. |
123 | | * \param spins Integer giving the number of spins, i.e. the maximum |
124 | | * number of clusters. Even if the number of spins is high the number of |
125 | | * clusters in the result might be small. |
126 | | * \param parupdate A Boolean constant, whether to update all spins in |
127 | | * parallel. It is not implemented in the \c IGRAPH_SPINCOMM_INP_NEG |
128 | | * implementation. |
129 | | * \param starttemp Real number, the temperature at the start. A reasonable |
130 | | * default is 1.0. |
131 | | * \param stoptemp Real number, the algorithm stops at this temperature. A |
132 | | * reasonable default is 0.01. |
133 | | * \param coolfact Real number, the cooling factor for the simulated |
134 | | * annealing. A reasonable default is 0.99. |
135 | | * \param update_rule The type of the update rule. Possible values: \c |
136 | | * IGRAPH_SPINCOMM_UPDATE_SIMPLE and \c |
137 | | * IGRAPH_SPINCOMM_UPDATE_CONFIG. Basically this parameter defines |
138 | | * the null model based on which the actual clustering is done. If |
139 | | * this is \c IGRAPH_SPINCOMM_UPDATE_SIMPLE then the random graph |
140 | | * (i.e. G(n,p)), if it is \c IGRAPH_SPINCOMM_UPDATE then the |
141 | | * configuration model is used. The configuration means that the |
142 | | * baseline for the clustering is a random graph with the same |
143 | | * degree distribution as the input graph. |
144 | | * \param gamma Real number. The gamma parameter of the algorithm, |
145 | | * acting as a resolution parameter. Smaller values typically lead to |
146 | | * larger clusters, larger values typically lead to smaller clusters. |
147 | | * \param implementation Constant, chooses between the two |
148 | | * implementations of the spin-glass algorithm that are included |
149 | | * in igraph. \c IGRAPH_SPINCOMM_IMP_ORIG selects the original |
150 | | * implementation, this is faster, \c IGRAPH_SPINCOMM_INP_NEG selects |
151 | | * an implementation that allows negative edge weights. |
152 | | * \param gamma_minus Real number. Parameter for the \c IGRAPH_SPINCOMM_IMP_NEG |
153 | | * implementation. This acts as a resolution parameter for the negative part |
154 | | * of the network. Smaller values of \p gamma_minus leads to fewer negative |
155 | | * edges within clusters. If this argument is set to zero, the algorithm |
156 | | * reduces to a graph coloring algorithm when all edges have negative |
157 | | * weights, using the number of spins as the number of colors. |
158 | | * \return Error code. |
159 | | * |
160 | | * \sa \ref igraph_community_spinglass_single() for calculating the community |
161 | | * of a single vertex. |
162 | | * |
163 | | * Time complexity: TODO. |
164 | | * |
165 | | */ |
166 | | |
167 | | igraph_error_t igraph_community_spinglass(const igraph_t *graph, |
168 | | const igraph_vector_t *weights, |
169 | | igraph_real_t *modularity, |
170 | | igraph_real_t *temperature, |
171 | | igraph_vector_int_t *membership, |
172 | | igraph_vector_int_t *csize, |
173 | | igraph_int_t spins, |
174 | | igraph_bool_t parupdate, |
175 | | igraph_real_t starttemp, |
176 | | igraph_real_t stoptemp, |
177 | | igraph_real_t coolfact, |
178 | | igraph_spincomm_update_t update_rule, |
179 | | igraph_real_t gamma, |
180 | | igraph_spinglass_implementation_t implementation, |
181 | 500 | igraph_real_t gamma_minus) { |
182 | | |
183 | 500 | IGRAPH_HANDLE_EXCEPTIONS( |
184 | 0 | switch (implementation) { |
185 | 0 | case IGRAPH_SPINCOMM_IMP_ORIG: |
186 | 0 | return igraph_i_community_spinglass_orig(graph, weights, modularity, |
187 | 0 | temperature, membership, csize, |
188 | 0 | spins, parupdate, starttemp, |
189 | 0 | stoptemp, coolfact, update_rule, |
190 | 0 | gamma); |
191 | 0 | break; |
192 | 0 | case IGRAPH_SPINCOMM_IMP_NEG: |
193 | 0 | return igraph_i_community_spinglass_negative(graph, weights, modularity, |
194 | 0 | temperature, membership, csize, |
195 | 0 | spins, parupdate, starttemp, |
196 | 0 | stoptemp, coolfact, |
197 | 0 | update_rule, gamma, |
198 | 0 | gamma_minus); |
199 | 0 | break; |
200 | 0 | default: |
201 | 0 | IGRAPH_ERROR("Unknown implementation in spinglass community detection.", |
202 | 0 | IGRAPH_EINVAL); |
203 | 0 | } |
204 | 0 | ); |
205 | 0 | } |
206 | | |
207 | | static igraph_error_t igraph_i_community_spinglass_orig( |
208 | | const igraph_t *graph, |
209 | | const igraph_vector_t *weights, |
210 | | igraph_real_t *modularity, |
211 | | igraph_real_t *temperature, |
212 | | igraph_vector_int_t *membership, |
213 | | igraph_vector_int_t *csize, |
214 | | igraph_int_t spins, |
215 | | igraph_bool_t parupdate, |
216 | | igraph_real_t starttemp, |
217 | | igraph_real_t stoptemp, |
218 | | igraph_real_t coolfact, |
219 | | igraph_spincomm_update_t update_rule, |
220 | 500 | igraph_real_t gamma) { |
221 | | |
222 | 500 | igraph_int_t no_of_nodes = igraph_vcount(graph); |
223 | 500 | igraph_int_t changes, runs; |
224 | 500 | igraph_bool_t use_weights = false; |
225 | 500 | bool zeroT; |
226 | 500 | double kT, acc, prob; |
227 | | |
228 | | /* Check arguments */ |
229 | | |
230 | 500 | if (spins < 2) { |
231 | 0 | IGRAPH_ERROR("Number of spins must be at least 2.", IGRAPH_EINVAL); |
232 | 0 | } |
233 | 500 | if (update_rule != IGRAPH_SPINCOMM_UPDATE_SIMPLE && |
234 | 500 | update_rule != IGRAPH_SPINCOMM_UPDATE_CONFIG) { |
235 | 0 | IGRAPH_ERROR("Invalid update rule for spinglass community detection.", IGRAPH_EINVAL); |
236 | 0 | } |
237 | 500 | if (weights) { |
238 | 500 | if (igraph_vector_size(weights) != igraph_ecount(graph)) { |
239 | 0 | IGRAPH_ERROR("Invalid weight vector length.", IGRAPH_EINVAL); |
240 | 0 | } |
241 | 500 | use_weights = true; |
242 | 500 | if (igraph_vector_size(weights) > 0 && igraph_vector_min(weights) < 0) { |
243 | 0 | IGRAPH_ERROR( |
244 | 0 | "Weights must not be negative when using the original implementation of spinglass communities. " |
245 | 0 | "Select the implementation meant for negative weights.", |
246 | 0 | IGRAPH_EINVAL); |
247 | 0 | } |
248 | 500 | } |
249 | 500 | if (coolfact < 0 || coolfact >= 1.0) { |
250 | 0 | IGRAPH_ERROR("Cooling factor must be positive and strictly smaller than 1.", IGRAPH_EINVAL); |
251 | 0 | } |
252 | 500 | if (gamma < 0.0) { |
253 | 0 | IGRAPH_ERROR("Gamma value must not be negative.", IGRAPH_EINVAL); |
254 | 0 | } |
255 | 500 | if ( !(starttemp == 0 && stoptemp == 0) ) { |
256 | 500 | if (! (starttemp > 0 && stoptemp > 0)) { |
257 | 0 | IGRAPH_ERROR("Starting and stopping temperatures must be both positive or both zero.", |
258 | 0 | IGRAPH_EINVAL); |
259 | 0 | } |
260 | 500 | if (starttemp <= stoptemp) { |
261 | 0 | IGRAPH_ERROR("The starting temperature must be larger than the stopping temperature.", |
262 | 0 | IGRAPH_EINVAL); |
263 | 0 | } |
264 | 500 | } |
265 | | |
266 | | /* The spinglass algorithm does not handle the trivial cases of the |
267 | | null and singleton graphs, so we catch them here. */ |
268 | 500 | if (no_of_nodes < 2) { |
269 | 11 | if (membership) { |
270 | 11 | IGRAPH_CHECK(igraph_vector_int_resize(membership, no_of_nodes)); |
271 | 11 | igraph_vector_int_null(membership); |
272 | 11 | } |
273 | 11 | if (modularity) { |
274 | 11 | IGRAPH_CHECK(igraph_modularity(graph, membership, nullptr, 1, igraph_is_directed(graph), modularity)); |
275 | 11 | } |
276 | 11 | if (temperature) { |
277 | 0 | *temperature = stoptemp; |
278 | 0 | } |
279 | 11 | if (csize) { |
280 | | /* 0 clusters for 0 nodes, 1 cluster for 1 node */ |
281 | 0 | IGRAPH_CHECK(igraph_vector_int_resize(csize, no_of_nodes)); |
282 | 0 | igraph_vector_int_fill(csize, 1); |
283 | 0 | } |
284 | 11 | return IGRAPH_SUCCESS; |
285 | 11 | } |
286 | | |
287 | | /* Check whether we have a single component */ |
288 | 489 | igraph_bool_t conn; |
289 | 489 | IGRAPH_CHECK(igraph_is_connected(graph, &conn, IGRAPH_WEAK)); |
290 | 489 | if (!conn) { |
291 | 0 | IGRAPH_ERROR("Cannot work with unconnected graph.", IGRAPH_EINVAL); |
292 | 0 | } |
293 | | |
294 | 489 | network net; |
295 | | |
296 | | /* Transform the igraph_t */ |
297 | 489 | IGRAPH_CHECK(igraph_i_read_network_spinglass(graph, weights, |
298 | 489 | &net, use_weights)); |
299 | | |
300 | 489 | prob = 2.0 * net.sum_weights / double(net.node_list.Size()) |
301 | 489 | / double(net.node_list.Size() - 1); |
302 | | |
303 | 489 | PottsModel pm(&net, spins, update_rule); |
304 | | |
305 | 489 | if ((stoptemp == 0.0) && (starttemp == 0.0)) { |
306 | 0 | zeroT = true; |
307 | 489 | } else { |
308 | 489 | zeroT = false; |
309 | 489 | } |
310 | 489 | if (!zeroT) { |
311 | 489 | kT = pm.FindStartTemp(gamma, prob, starttemp); |
312 | 489 | } else { |
313 | 0 | kT = stoptemp; |
314 | 0 | } |
315 | | /* assign random initial configuration */ |
316 | 489 | pm.assign_initial_conf(-1); |
317 | 489 | runs = 0; |
318 | 489 | changes = 1; |
319 | | |
320 | 188k | while (changes > 0 && (kT / stoptemp > 1.0 || (zeroT && runs < 150))) { |
321 | | |
322 | 188k | IGRAPH_ALLOW_INTERRUPTION(); |
323 | | |
324 | 188k | runs++; |
325 | 188k | if (!zeroT) { |
326 | 188k | kT *= coolfact; |
327 | 188k | if (parupdate) { |
328 | 0 | changes = pm.HeatBathParallelLookup(gamma, prob, kT, 50); |
329 | 188k | } else { |
330 | 188k | acc = pm.HeatBathLookup(gamma, prob, kT, 50); |
331 | 188k | if (acc < (1.0 - 1.0 / double(spins)) * 0.01) { |
332 | 263 | changes = 0; |
333 | 187k | } else { |
334 | 187k | changes = 1; |
335 | 187k | } |
336 | 188k | } |
337 | 188k | } else { |
338 | 0 | if (parupdate) { |
339 | 0 | changes = pm.HeatBathParallelLookupZeroTemp(gamma, prob, 50); |
340 | 0 | } else { |
341 | 0 | acc = pm.HeatBathLookupZeroTemp(gamma, prob, 50); |
342 | | /* less than 1 percent acceptance ratio */ |
343 | 0 | if (acc < (1.0 - 1.0 / double(spins)) * 0.01) { |
344 | 0 | changes = 0; |
345 | 0 | } else { |
346 | 0 | changes = 1; |
347 | 0 | } |
348 | 0 | } |
349 | 0 | } |
350 | 188k | } /* while loop */ |
351 | | |
352 | 489 | pm.WriteClusters(modularity, temperature, csize, membership, kT, gamma); |
353 | | |
354 | 489 | return IGRAPH_SUCCESS; |
355 | 489 | } |
356 | | |
357 | | /** |
358 | | * \function igraph_community_spinglass_single |
359 | | * \brief Community of a single node based on statistical mechanics. |
360 | | * |
361 | | * This function implements the community structure detection |
362 | | * algorithm proposed by Joerg Reichardt and Stefan Bornholdt. It is |
363 | | * described in their paper: Statistical Mechanics of |
364 | | * Community Detection, http://arxiv.org/abs/cond-mat/0603718 . |
365 | | * |
366 | | * </para><para> |
367 | | * This function calculates the community of a single vertex without |
368 | | * calculating all the communities in the graph. |
369 | | * |
370 | | * \param graph The input graph, it may be directed but the direction |
371 | | * of the edges is not used in the algorithm. |
372 | | * \param weights Pointer to a vector with the weights of the edges. |
373 | | * Alternatively \c NULL can be supplied to have the same weight |
374 | | * for every edge. |
375 | | * \param vertex The vertex ID of the vertex of which ths community is |
376 | | * calculated. |
377 | | * \param community Pointer to an initialized vector, the result, the |
378 | | * IDs of the vertices in the community of the input vertex will be |
379 | | * stored here. The vector will be resized as needed. |
380 | | * \param cohesion Pointer to a real variable, if not \c NULL the |
381 | | * cohesion index of the community will be stored here. |
382 | | * \param adhesion Pointer to a real variable, if not \c NULL the |
383 | | * adhesion index of the community will be stored here. |
384 | | * \param inner_links Pointer to a real, if not \c NULL the |
385 | | * number of edges within the community (or the sum of their weights) |
386 | | * is stored here. |
387 | | * \param outer_links Pointer to a real, if not \c NULL the |
388 | | * number of edges between the community and the rest of the graph |
389 | | * (or the sum of their weights) will be stored here. |
390 | | * \param spins The number of spins to use, this can be higher than |
391 | | * the actual number of clusters in the network, in which case some |
392 | | * clusters will contain zero vertices. |
393 | | * \param update_rule The type of the update rule. Possible values: \c |
394 | | * IGRAPH_SPINCOMM_UPDATE_SIMPLE and \c |
395 | | * IGRAPH_SPINCOMM_UPDATE_CONFIG. Basically this parameter defined |
396 | | * the null model based on which the actual clustering is done. If |
397 | | * this is \c IGRAPH_SPINCOMM_UPDATE_SIMPLE then the random graph |
398 | | * (ie. G(n,p)), if it is \c IGRAPH_SPINCOMM_UPDATE then the |
399 | | * configuration model is used. The configuration means that the |
400 | | * baseline for the clustering is a random graph with the same |
401 | | * degree distribution as the input graph. |
402 | | * \param gamma Real number. The gamma parameter of the |
403 | | * algorithm. This defined the weight of the missing and existing |
404 | | * links in the quality function for the clustering. The default |
405 | | * value in the original code was 1.0, which is equal weight to |
406 | | * missing and existing edges. Smaller values make the existing |
407 | | * links contibute more to the energy function which is minimized |
408 | | * in the algorithm. Bigger values make the missing links more |
409 | | * important. (If my understanding is correct.) |
410 | | * \return Error code. |
411 | | * |
412 | | * \sa igraph_community_spinglass() for the traditional version of the |
413 | | * algorithm. |
414 | | * |
415 | | * Time complexity: TODO. |
416 | | */ |
417 | | |
418 | | igraph_error_t igraph_community_spinglass_single(const igraph_t *graph, |
419 | | const igraph_vector_t *weights, |
420 | | igraph_int_t vertex, |
421 | | igraph_vector_int_t *community, |
422 | | igraph_real_t *cohesion, |
423 | | igraph_real_t *adhesion, |
424 | | igraph_real_t *inner_links, |
425 | | igraph_real_t *outer_links, |
426 | | igraph_int_t spins, |
427 | | igraph_spincomm_update_t update_rule, |
428 | 0 | igraph_real_t gamma) { |
429 | 0 | IGRAPH_HANDLE_EXCEPTIONS( |
430 | 0 | igraph_bool_t use_weights = false; |
431 | 0 | char startnode[SPINGLASS_MAX_NAME_LEN]; |
432 | | |
433 | | /* Check arguments */ |
434 | |
|
435 | 0 | if (spins < 2) { |
436 | 0 | IGRAPH_ERROR("Number of spins must be at least 2", IGRAPH_EINVAL); |
437 | 0 | } |
438 | 0 | if (update_rule != IGRAPH_SPINCOMM_UPDATE_SIMPLE && |
439 | 0 | update_rule != IGRAPH_SPINCOMM_UPDATE_CONFIG) { |
440 | 0 | IGRAPH_ERROR("Invalid update rule", IGRAPH_EINVAL); |
441 | 0 | } |
442 | 0 | if (weights) { |
443 | 0 | if (igraph_vector_size(weights) != igraph_ecount(graph)) { |
444 | 0 | IGRAPH_ERROR("Invalid weight vector length", IGRAPH_EINVAL); |
445 | 0 | } |
446 | 0 | use_weights = 1; |
447 | 0 | } |
448 | 0 | if (gamma < 0.0) { |
449 | 0 | IGRAPH_ERROR("Invalid gamme value", IGRAPH_EINVAL); |
450 | 0 | } |
451 | 0 | if (vertex < 0 || vertex > igraph_vcount(graph)) { |
452 | 0 | IGRAPH_ERROR("Invalid vertex ID", IGRAPH_EINVAL); |
453 | 0 | } |
454 | | |
455 | | /* Check whether we have a single component */ |
456 | 0 | igraph_bool_t conn; |
457 | 0 | IGRAPH_CHECK(igraph_is_connected(graph, &conn, IGRAPH_WEAK)); |
458 | 0 | if (!conn) { |
459 | 0 | IGRAPH_ERROR("Cannot work with unconnected graph", IGRAPH_EINVAL); |
460 | 0 | } |
461 | |
|
462 | 0 | network net; |
463 | | |
464 | | /* Transform the igraph_t */ |
465 | 0 | IGRAPH_CHECK(igraph_i_read_network_spinglass(graph, weights, |
466 | 0 | &net, use_weights)); |
467 | |
|
468 | 0 | PottsModel pm(&net, spins, update_rule); |
469 | | |
470 | | /* to be expected, if we want to find the community around a particular node*/ |
471 | | /* the initial conf is needed, because otherwise, |
472 | | the degree of the nodes is not in the weight property, stupid!!! */ |
473 | 0 | pm.assign_initial_conf(-1); |
474 | 0 | snprintf(startnode, sizeof(startnode) / sizeof(startnode[0]), "%" IGRAPH_PRId "", vertex + 1); |
475 | 0 | pm.FindCommunityFromStart(gamma, startnode, community, |
476 | 0 | cohesion, adhesion, inner_links, outer_links); |
477 | 0 | ); |
478 | |
|
479 | 0 | return IGRAPH_SUCCESS; |
480 | 0 | } |
481 | | |
482 | | static igraph_error_t igraph_i_community_spinglass_negative( |
483 | | const igraph_t *graph, |
484 | | const igraph_vector_t *weights, |
485 | | igraph_real_t *modularity, |
486 | | igraph_real_t *temperature, |
487 | | igraph_vector_int_t *membership, |
488 | | igraph_vector_int_t *csize, |
489 | | igraph_int_t spins, |
490 | | igraph_bool_t parupdate, |
491 | | igraph_real_t starttemp, |
492 | | igraph_real_t stoptemp, |
493 | | igraph_real_t coolfact, |
494 | | igraph_spincomm_update_t update_rule, |
495 | | igraph_real_t gamma, |
496 | 0 | igraph_real_t gamma_minus) { |
497 | |
|
498 | 0 | igraph_int_t no_of_nodes = igraph_vcount(graph); |
499 | 0 | igraph_int_t runs; |
500 | 0 | igraph_bool_t use_weights = false; |
501 | 0 | bool zeroT; |
502 | 0 | double kT, acc; |
503 | 0 | igraph_real_t d_n; |
504 | 0 | igraph_real_t d_p; |
505 | | |
506 | | /* Check arguments */ |
507 | |
|
508 | 0 | if (parupdate) { |
509 | 0 | IGRAPH_ERROR("Parallel spin update not implemented with negative weights.", |
510 | 0 | IGRAPH_UNIMPLEMENTED); |
511 | 0 | } |
512 | | |
513 | 0 | if (spins < 2) { |
514 | 0 | IGRAPH_ERROR("Number of spins must be at least 2.", IGRAPH_EINVAL); |
515 | 0 | } |
516 | 0 | if (update_rule != IGRAPH_SPINCOMM_UPDATE_SIMPLE && |
517 | 0 | update_rule != IGRAPH_SPINCOMM_UPDATE_CONFIG) { |
518 | 0 | IGRAPH_ERROR("Invalid update rule for spinglass community detection.", IGRAPH_EINVAL); |
519 | 0 | } |
520 | 0 | if (weights) { |
521 | 0 | if (igraph_vector_size(weights) != igraph_ecount(graph)) { |
522 | 0 | IGRAPH_ERROR("Invalid weight vector length.", IGRAPH_EINVAL); |
523 | 0 | } |
524 | 0 | use_weights = true; |
525 | 0 | } |
526 | 0 | if (coolfact < 0 || coolfact >= 1.0) { |
527 | 0 | IGRAPH_ERROR("Cooling factor must be positive and strictly smaller than 1.", IGRAPH_EINVAL); |
528 | 0 | } |
529 | 0 | if (gamma < 0.0) { |
530 | 0 | IGRAPH_ERROR("Gamma value must not be negative.", IGRAPH_EINVAL); |
531 | 0 | } |
532 | 0 | if ( !(starttemp == 0 && stoptemp == 0) ) { |
533 | 0 | if (! (starttemp > 0 && stoptemp > 0)) { |
534 | 0 | IGRAPH_ERROR("Starting and stopping temperatures must be both positive or both zero.", |
535 | 0 | IGRAPH_EINVAL); |
536 | 0 | } |
537 | 0 | if (starttemp <= stoptemp) { |
538 | 0 | IGRAPH_ERROR("The starting temperature must be larger than the stopping temperature.", |
539 | 0 | IGRAPH_EINVAL); |
540 | 0 | } |
541 | 0 | } |
542 | | |
543 | | /* The spinglass algorithm does not handle the trivial cases of the |
544 | | null and singleton graphs, so we catch them here. */ |
545 | 0 | if (no_of_nodes < 2) { |
546 | 0 | if (membership) { |
547 | 0 | IGRAPH_CHECK(igraph_vector_int_resize(membership, no_of_nodes)); |
548 | 0 | igraph_vector_int_null(membership); |
549 | 0 | } |
550 | 0 | if (modularity) { |
551 | 0 | IGRAPH_CHECK(igraph_modularity(graph, membership, nullptr, 1, igraph_is_directed(graph), modularity)); |
552 | 0 | } |
553 | 0 | if (temperature) { |
554 | 0 | *temperature = stoptemp; |
555 | 0 | } |
556 | 0 | if (csize) { |
557 | | /* 0 clusters for 0 nodes, 1 cluster for 1 node */ |
558 | 0 | IGRAPH_CHECK(igraph_vector_int_resize(csize, no_of_nodes)); |
559 | 0 | igraph_vector_int_fill(csize, 1); |
560 | 0 | } |
561 | 0 | return IGRAPH_SUCCESS; |
562 | 0 | } |
563 | | |
564 | | /* Check whether we have a single component */ |
565 | 0 | igraph_bool_t conn; |
566 | 0 | IGRAPH_CHECK(igraph_is_connected(graph, &conn, IGRAPH_WEAK)); |
567 | 0 | if (!conn) { |
568 | 0 | IGRAPH_ERROR("Cannot work with unconnected graph.", IGRAPH_EINVAL); |
569 | 0 | } |
570 | | |
571 | 0 | if (weights && igraph_vector_size(weights) > 0) { |
572 | 0 | igraph_vector_minmax(weights, &d_n, &d_p); |
573 | 0 | } else { |
574 | 0 | d_n = d_p = 1; |
575 | 0 | } |
576 | |
|
577 | 0 | if (d_n > 0) { |
578 | 0 | d_n = 0; |
579 | 0 | } |
580 | 0 | if (d_p < 0) { |
581 | 0 | d_p = 0; |
582 | 0 | } |
583 | 0 | d_n = -d_n; |
584 | |
|
585 | 0 | network net; |
586 | | |
587 | | /* Transform the igraph_t */ |
588 | 0 | IGRAPH_CHECK(igraph_i_read_network_spinglass(graph, weights, |
589 | 0 | &net, use_weights)); |
590 | | |
591 | 0 | bool directed = igraph_is_directed(graph); |
592 | |
|
593 | 0 | PottsModelN pm(&net, spins, directed); |
594 | |
|
595 | 0 | if ((stoptemp == 0.0) && (starttemp == 0.0)) { |
596 | 0 | zeroT = true; |
597 | 0 | } else { |
598 | 0 | zeroT = false; |
599 | 0 | } |
600 | | |
601 | | //Begin at a high enough temperature |
602 | 0 | kT = pm.FindStartTemp(gamma, gamma_minus, starttemp); |
603 | | |
604 | | /* assign random initial configuration */ |
605 | 0 | pm.assign_initial_conf(true); |
606 | |
|
607 | 0 | runs = 0; |
608 | 0 | while (kT / stoptemp > 1.0 || (zeroT && runs < 150)) { |
609 | 0 | IGRAPH_ALLOW_INTERRUPTION(); |
610 | | |
611 | 0 | runs++; |
612 | 0 | kT = kT * coolfact; |
613 | 0 | acc = pm.HeatBathLookup(gamma, gamma_minus, kT, 50); |
614 | 0 | if (acc < (1.0 - 1.0 / double(spins)) * 0.001) { |
615 | 0 | break; |
616 | 0 | } |
617 | 0 | } /* while loop */ |
618 | | |
619 | | /* These are needed, otherwise 'modularity' is not calculated */ |
620 | 0 | igraph_matrix_t adhesion, normalized_adhesion; |
621 | 0 | igraph_real_t polarization; |
622 | 0 | IGRAPH_MATRIX_INIT_FINALLY(&adhesion, 0, 0); |
623 | 0 | IGRAPH_MATRIX_INIT_FINALLY(&normalized_adhesion, 0, 0); |
624 | 0 | pm.WriteClusters(modularity, temperature, csize, membership, |
625 | 0 | &adhesion, &normalized_adhesion, &polarization, |
626 | 0 | kT, d_p, d_n); |
627 | 0 | igraph_matrix_destroy(&normalized_adhesion); |
628 | 0 | igraph_matrix_destroy(&adhesion); |
629 | 0 | IGRAPH_FINALLY_CLEAN(2); |
630 | |
|
631 | 0 | return IGRAPH_SUCCESS; |
632 | 0 | } |