Line data Source code
1 : //----------------------------------------------------------------------------
2 : // LAGraph/src/test/test_peer_pressure.c: test cases for Peer Pressure
3 : // ----------------------------------------------------------------------------
4 :
5 : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
6 : // SPDX-License-Identifier: BSD-2-Clause
7 : //
8 : // For additional details (including references to third party source code and
9 : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
10 : // Contributors.txt for a full list of contributors. Created, in part, with
11 : // funding and support from the U.S. Government (see Acknowledgments.txt file).
12 : // DM22-0790
13 :
14 : // Contributed by Cameron Quilici, Texas A&M University
15 :
16 : //-----------------------------------------------------------------------------
17 :
18 : #include <acutest.h>
19 : #include <stdio.h>
20 :
21 : #include "LG_Xtest.h"
22 : #include <LAGraphX.h>
23 : #include <LAGraph_test.h>
24 :
25 : char msg[LAGRAPH_MSG_LEN];
26 :
27 : LAGraph_Graph G = NULL;
28 : GrB_Matrix A = NULL;
29 : #define LEN 512
30 : char filename[LEN + 1];
31 :
32 : typedef struct
33 : {
34 : const char *name;
35 : } matrix_info;
36 :
37 : const matrix_info files[] = {
38 : {"A.mtx"}, {"jagmesh7.mtx"}, {"west0067.mtx"}, // unsymmetric
39 : {"bcsstk13.mtx"}, {"karate.mtx"}, {"mcl.mtx"}, {""},
40 : };
41 :
42 : const int nfiles = 6;
43 :
44 : const double coverage[] = {
45 : 1.000000, 0.653359, 0.181507, 0.048510, 0.243590, 0.833333,
46 : // Start config 2
47 : 1.000000, 0.644804, 0.123288, 0.695750, 1.000000, 0.722222};
48 :
49 : const double performance[] = {
50 : 0.714286, 0.989642, 0.841701, 0.977048, 0.887701, 0.866667,
51 : // Start config 2
52 : 0.714286, 0.992349, 0.914518, 0.934843, 0.139037, 0.777778};
53 :
54 : const double modularity[] = {
55 : 0.000000, 0.641262, 0.043324, 0.042696, 0.158120, 0.500000,
56 : // Start config 2
57 : 0.000000, 0.634677, 0.078228, 0.596324, 0.000000, 0.351852};
58 :
59 : //****************************************************************************
60 1 : void test_peer_pressure(void)
61 : {
62 : #if LAGRAPH_SUITESPARSE
63 1 : LAGraph_Init(msg);
64 :
65 1 : for (int k = 0;; k++)
66 6 : {
67 : // load the matrix as A
68 7 : const char *aname = files[k].name;
69 7 : if (strlen(aname) == 0)
70 1 : break;
71 6 : printf("\n================================== %s:\n", aname);
72 6 : TEST_CASE(aname);
73 6 : snprintf(filename, LEN, LG_DATA_DIR "%s", aname);
74 6 : FILE *f = fopen(filename, "r");
75 6 : TEST_CHECK(f != NULL);
76 6 : OK(LAGraph_MMRead(&A, f, msg));
77 : // GxB_print (A, 5) ;
78 6 : fclose (f) ;
79 :
80 : // construct a directed graph G with adjacency matrix A
81 6 : OK(LAGraph_New(&G, &A, LAGraph_ADJACENCY_DIRECTED, msg));
82 6 : TEST_CHECK(A == NULL);
83 :
84 : // compute AT
85 6 : OK(LAGraph_Cached_AT(G, msg));
86 : // Needed to compute quality metrics
87 6 : OK(LAGraph_Cached_IsSymmetricStructure(G, msg));
88 :
89 6 : GrB_Vector c = NULL;
90 :
91 : // compute clustering
92 : double cov, perf, mod;
93 6 : OK(LAGr_PeerPressureClustering(&c, true, false, 0.0001, 50, G, msg));
94 6 : OK(LAGr_PartitionQuality(&cov, &perf, c, G, msg));
95 6 : OK(LAGr_Modularity(&mod, (double)1, c, G, msg));
96 6 : GrB_free (&c) ;
97 :
98 6 : bool ok_cov = false, ok_perf = false, ok_mod = false;
99 6 : printf("\nConfiguration 1:\n");
100 6 : printf("coverage: %g %g\n", cov, coverage[k]);
101 6 : printf("perf: %g %g\n", perf, performance[k]);
102 6 : printf("modularity: %g %g\n", mod, modularity[k]);
103 6 : ok_cov = (fabs(cov - coverage[k]) < 1e-4) ? true : ok_cov;
104 6 : ok_perf = (fabs(perf - performance[k]) < 1e-4) ? true : ok_perf;
105 6 : ok_mod = (fabs(mod - modularity[k]) < 1e-4) ? true : ok_mod;
106 :
107 6 : TEST_CHECK(ok_cov);
108 6 : TEST_CHECK(ok_perf);
109 6 : TEST_CHECK(ok_mod);
110 :
111 6 : OK(LAGr_PeerPressureClustering(&c, false, true, 0.0001, 50, G, msg));
112 6 : OK(LAGr_PartitionQuality(&cov, &perf, c, G, msg));
113 6 : OK(LAGr_Modularity(&mod, (double)1, c, G, msg));
114 :
115 6 : ok_cov = false, ok_perf = false, ok_mod = false;
116 6 : printf("\nConfiguration 2:\n");
117 6 : printf("coverage: %g %g\n", cov, coverage[k + nfiles]);
118 6 : printf("perf: %g %g\n", perf, performance[k + nfiles]);
119 6 : printf("modularity: %g %g\n", mod, modularity[k + nfiles]);
120 6 : ok_cov = (fabs(cov - coverage[k + nfiles]) < 1e-4) ? true : ok_cov;
121 6 : ok_perf =
122 6 : (fabs(perf - performance[k + nfiles]) < 1e-4) ? true : ok_perf;
123 6 : ok_mod = (fabs(mod - modularity[k + nfiles]) < 1e-4) ? true : ok_mod;
124 :
125 6 : TEST_CHECK(ok_cov);
126 6 : TEST_CHECK(ok_perf);
127 6 : TEST_CHECK(ok_mod);
128 :
129 6 : OK(GrB_free(&c));
130 :
131 6 : OK(LAGraph_Delete(&G, msg));
132 : }
133 :
134 1 : LAGraph_Finalize(msg);
135 : #endif
136 1 : }
137 :
138 : //------------------------------------------------------------------------------
139 : // test_errors
140 : //------------------------------------------------------------------------------
141 :
142 1 : void test_errors(void)
143 : {
144 : #if LAGRAPH_SUITESPARSE
145 1 : LAGraph_Init(msg);
146 :
147 1 : snprintf(filename, LEN, LG_DATA_DIR "%s", "karate.mtx");
148 1 : FILE *f = fopen(filename, "r");
149 1 : TEST_CHECK(f != NULL);
150 1 : OK(LAGraph_MMRead(&A, f, msg));
151 1 : TEST_MSG("Loading of adjacency matrix failed");
152 1 : fclose (f) ;
153 :
154 : // construct an undirected graph G with adjacency matrix A
155 1 : OK(LAGraph_New(&G, &A, LAGraph_ADJACENCY_UNDIRECTED, msg));
156 1 : TEST_CHECK(A == NULL);
157 :
158 1 : GrB_Vector c = NULL;
159 1 : bool normalize = false, make_undirected = false;
160 1 : double thresh = 1e-5;
161 1 : int max_iter = 100;
162 :
163 : // c is NULL
164 1 : GrB_Info result = LAGr_PeerPressureClustering(
165 : NULL, normalize, make_undirected, thresh, max_iter, G, msg);
166 1 : printf("\nresult: %d %s\n", result, msg);
167 1 : TEST_CHECK(result == GrB_NULL_POINTER);
168 :
169 : // G has no AT
170 1 : G->AT = NULL;
171 1 : make_undirected = true;
172 1 : G->kind = LAGraph_ADJACENCY_DIRECTED;
173 1 : G->is_symmetric_structure = LAGraph_FALSE;
174 1 : result = LAGr_PeerPressureClustering(&c, normalize, make_undirected, thresh,
175 : max_iter, G, msg);
176 1 : printf("\nresult: %d %s\n", result, msg);
177 1 : TEST_CHECK(result == LAGRAPH_NOT_CACHED);
178 1 : GrB_free (&c) ;
179 :
180 1 : G->kind = LAGraph_ADJACENCY_UNDIRECTED;
181 1 : G->is_symmetric_structure = LAGraph_FALSE;
182 1 : result = LAGr_PeerPressureClustering(&c, normalize, make_undirected, thresh,
183 : max_iter, G, msg);
184 1 : printf("\nresult: %d %s\n", result, msg);
185 1 : TEST_CHECK(result == LAGRAPH_NOT_CACHED);
186 1 : GrB_free (&c) ;
187 :
188 1 : OK(LAGraph_Delete(&G, msg));
189 1 : LAGraph_Finalize(msg);
190 : #endif
191 1 : }
192 :
193 : //****************************************************************************
194 :
195 : TEST_LIST = {{"peer_pressure", test_peer_pressure},
196 : {"peer_pressure_errors", test_errors},
197 : {NULL, NULL}};
|