-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
229 lines (208 loc) · 8.57 KB
/
main.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "scan.hpp"
#include <exception>
#include <string>
#include <iostream>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include "cluster_evaluator.hpp"
#include "intersection_naming.hpp"
/*
* Loads a file with clustering data for evaluation
*/
uint getClusters(std::string fclusters, hmap_uint_suint* clusters,
std::vector<std::string>* id_clusters, Graph* g) {
std::set<uint>* tmpset = NULL;
// as always, no cluster 0
id_clusters->push_back("");
std::ifstream cluster_file;
std::vector<std::string> tokens;
std::string line;
cluster_file.open(fclusters.c_str());
uint field_counter, cid = 1;
getline(cluster_file, line);
// To count the nodes who appear in multiple clusters
uint v_num_nodes = 0;
std::set<uint> nodes;
while (!cluster_file.eof()) {
// parse it!
tokens = stringTokenizer(line);
field_counter = 0;
if (tokens.size() < 3) {
std::cout << "Error! Too few elements in line.\n";
exit(1);
}
if (tokens[1] != "->") {
std::cout << "Error! Wrong format!.\n";
exit(1);
}
// Cluster id
id_clusters->push_back(tokens[0]);
cid = id_clusters->size()-1;
if (tmpset == NULL) tmpset = new std::set<uint>;
for (int i = 2; i < tokens.size(); ++i){
// Node ids
// FIXME O problema está aqui. Ele assume que os vertices são
// inteiros, o que não necessaiamente são. Precisamos montar
// um registro de quais os ids para cada nó. Graph já tem isso?
//tmpset->insert(atoi(tokens[i].c_str()));
uint node_id;
if (isNumber(tokens[i])) {
// May already be the node's id. Check
if (g->nodeLabelExists(tokens[i])) {
// Well, it IS a label. Who knew! Proceed as usual
node_id = g->getNodeLabelId(tokens[i]);
} else {
// Not a known label, so it is already a node id!
node_id = atoi(tokens[i].c_str());
}
} else {
node_id = g->getNodeLabelId(tokens[i]);
}
tmpset->insert(node_id);
if (nodes.find(node_id) != nodes.end()) {
++v_num_nodes;
} else {
nodes.insert(node_id);
}
}
clusters->insert(std::pair<uint, std::set<uint>* >(cid, tmpset));
tmpset = NULL;
getline(cluster_file, line);
}
cluster_file.close();
return v_num_nodes;
}
void getClustersByNode() {
}
void evaluate(char* fgraph, std::vector<std::string>* clusterings) {
// Loading the graph data
Graph gr(fgraph);
for (int i = 0; i < clusterings->size(); ++i) {
// Loading the cluster data
std::vector<std::string> id_clusters;
hmap_uint_suint clusters;
uint vnn = getClusters(clusterings->at(i), &clusters,
&id_clusters, &gr);
// Generating the clusters by node data
hmap_uint_suint cluster_label;
hmap_uint_suint::const_iterator it;
std::set<uint>::iterator sit;
for (it = clusters.begin(); it != clusters.end(); ++it) {
for (sit = it->second->begin(); sit != it->second->end();
++sit) {
if (cluster_label[*sit] == NULL)
cluster_label[*sit] = new std::set<uint>;
cluster_label[*sit]->insert(it->first);
}
}
// Adding cluster 0 to any node not in a cluster
hmap::iterator hit;
for (hit = gr.graph_map.begin(); hit != gr.graph_map.end(); ++hit) {
if (cluster_label[hit->first] == NULL) {
cluster_label[hit->first] = new std::set<uint>;
cluster_label[hit->first]->insert(0);
}
}
// Starting the cluster evaluator
ClusterEvaluator ce(&gr, &clusters, &cluster_label, &id_clusters,vnn);
std::cout << "------------------------------------" << std::endl;
std::cout << "--- Clusterings for: " << clusterings->at(i) <<
" ---" << std::endl;
std::cout << "------------------------------------" << std::endl;
for (int i = 1; i <= clusters.size(); ++i) {
std::cout << "Cluster size " << i << " = "<<
clusters[i]->size() << std::endl;
}
std::cout << "------------------------------------" << std::endl;
std::vector<double> sil = ce.getSilhouetteIndex();
for (int i = 1; i < sil.size(); ++i) {
std::cout << "Si for cluster " << i << ": " <<
sil[i] << std::endl;
}
std::cout << "------------------------------------" << std::endl;
float mod = ce.getModularity();
std::cout << "Modularity is: " << mod << std::endl;
std::cout << "------------------------------------" << std::endl;
float ent = ce.getGraphEntropy();
std::cout << "Entropy is: " << ent << std::endl;
std::cout << "------------------------------------" << std::endl;
// Coverage!
double cov = ce.getCoverage();
std::cout << "Coverage is: " << cov << std::endl;
std::cout << "------------------------------------" << std::endl;
// Single cluster editing!
std::vector<double> sce = ce.getSCE();
std::cout << "Single cluster editing:" << std::endl;
for (int i = 1; i < sce.size(); ++i) {
std::cout << "\t" << i << " = " << sce[i] << std::endl;
}
std::cout << "------------------------------------" << std::endl;
// Performance!
double perf = ce.getPerformance();
std::cout << "Performance is: " << perf << std::endl;
std::cout << "------------------------------------" << std::endl;
// Cleaning allocs
for (it = clusters.begin(); it != clusters.end(); ++it) {
delete it->second;
}
for (it = cluster_label.begin(); it != cluster_label.end(); ++it) {
delete it->second;
}
}
}
void runScan(float epsilon, int mi, uint simi_type, char* graph) {
Scan *s = new Scan(simi_type);
try {
s->loadGraph(graph);
} catch (std::string err) {
std::cout << err << std::endl;
}
//s->printGraph();
s->run(epsilon, mi);
s->writeAll(epsilon, mi);
delete s;
}
int main(int argc, char** argv){
if (argc < 2) {
std::cout << "Too few arguments: " << argc -1 << std::endl;
std::cout << "Possible uses:" << std::endl <<
"\t ./cluster scan <epsilon> <mi> <sim_function_#> <graph_file>"
<< std::endl;
std::cout << "\t ./cluster eval <graph_file> <cluster_file>"
<< std::endl;
} else {
std::string x(argv[1]);
if (x == "scan") {
// Run SCAN
if (argc != 6) {
std:: cout << "Bad command." << std::endl;
std::cout << "Should be:" << std::endl <<
"\t ./cluster scan <epsilon> <mi> "<<
"<sim_function_#> <graph_file>" << std::endl;
return 1;
}
runScan(atof(argv[2]), atoi(argv[3]), atoi(argv[4]),
argv[5]);
} else if (x == "eval") {
// Find the best epsilon using modularity
if (argc < 4) {
std:: cout << "Bad command." << std::endl;
std::cout << "Should be:" << std::endl <<
"\t ./cluster eval <graph_file> <cluster_files>" <<
std::endl;
return 1;
}
//std::cout << "Best Epsilon: " <<
std::vector<std::string> clustering_files;
for (int i = 3; i < argc; ++i) {
clustering_files.push_back(argv[i]);
}
evaluate(argv[2], &clustering_files);
// atof(argv[4]),atoi(argv[5]),atoi(argv[6]),
// argv[7]) << std::endl;
}
}
}