-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHGMeans.cpp
354 lines (281 loc) · 11.2 KB
/
HGMeans.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Authors: Daniel Gribel and Thibaut Vidal
* Contact: dgribel@inf.puc-rio.br
*/
#include "HGMeans.h"
void SaveHeader(ofstream& writer_output, stringstream& filename_output, PbData pb_data) {
writer_output.open (filename_output.str().c_str());
// Write the header
string header = string("DATASET ") +
"NB_CLUSTERS " +
"SIZE_POPULATION " +
"MAX_IT " +
"OBJECTIVE " +
"TIME";
if(pb_data.GetParam().eval && pb_data.GetNbClasses() == pb_data.GetM()) {
header = header + " CRAND " + "NMI " + "CI";
}
for (int i = 0; i < pb_data.GetN(); ++i) {
header = header + " X" + to_string(i+1);
}
writer_output << header << " " << "\n";
writer_output.close();
}
void SaveOutput(ofstream& writer_output, stringstream& filename_output, GeneticOperations* genetic, double elapsedSecs) {
Param param = genetic->GetParam();
int m = genetic->GetPbData().GetM();
string instance = genetic->GetPbData().GetInstanceName();
Solution* solution = genetic->GetBestSolution();
writer_output.open (filename_output.str().c_str(), ofstream::out | ofstream::app);
writer_output << instance << " ";
writer_output << m << " ";
writer_output << param.size_population << " ";
writer_output << param.max_it << " ";
writer_output << fixed << setprecision(10) << solution->GetCost() << " ";
writer_output << fixed << setprecision(4) << elapsedSecs;
// Attach clustering indexes to output
if(param.eval && genetic->GetPbData().GetNbClasses() == m) {
writer_output << " ";
writer_output << fixed << setprecision(4) << solution->GetCRand() << " ";
writer_output << fixed << setprecision(4) << solution->GetNmi() << " ";
writer_output << fixed << setprecision(4) << solution->GetCentroidIndex();
}
for (int i = 0; i < genetic->GetPbData().GetN(); ++i) {
writer_output << " " << solution->GetAssignment()[i] + 1;
}
writer_output << "\n";
writer_output.close();
}
void PrintResult(GeneticOperations* genetic, double cpu_time) {
Solution* best_solution = genetic->GetBestSolution();
PbData pb_data = genetic->GetPbData();
Param param = genetic->GetParam();
std::cout << std::fixed;
cout << "-- Optimization finished." << endl;
cout << " Solution objective: " << setprecision(4) << best_solution->GetCost() << endl
<< " CPU time (s): " << setprecision(2) << cpu_time << endl;
if(param.eval && pb_data.GetNbClasses() == pb_data.GetM()) {
cout << " Clustering performance: "
<< setprecision(4) << best_solution->GetCRand() << " (C-Rand), "
<< setprecision(4) << best_solution->GetNmi() << " (NMI), "
<< setprecision(4) << best_solution->GetCentroidIndex() << " (CI)" << endl;
}
cout << endl;
}
string FilenameOutput(PbData pb_data, bool save) {
stringstream filename_output;
string out_folder = "hgm_out/";
mode_t mode = 0733; // UNIX style permissions
int result = 0;
if(save) {
#if defined(_WIN32)
result = _mkdir(out_folder.c_str()); // can be used on Windows
#else
result = mkdir(out_folder.c_str(), mode); // can be used on non-Windows
#endif
// if (result != 0) {
// cout << "No output directory created at /hg-means. It already exists, or writing permissions should be checked." << endl;
// }
}
filename_output << out_folder << pb_data.GetInstanceName() << '_' <<
setw(3) << setfill('0') << pb_data.GetM() << "_" <<
pb_data.GetParam().size_population << '_' <<
pb_data.GetParam().max_it << ".out";
return filename_output.str();
}
void RunFile(int seed, PbData pb_data, const Dataset* x, bool save) {
srand(seed);
ofstream writer_output;
stringstream filename_output;
filename_output << FilenameOutput(pb_data, save);
// Clean file content if it already exists
if(save) {
SaveHeader(writer_output, filename_output, pb_data);
}
for(int i = 0; i < pb_data.GetParam().nb_runs; i++) {
clock_t begin = clock();
// Create GeneticOperations instance
GeneticOperations* genetic = new GeneticOperations(pb_data);
cout << "-- Starting optimization: " << pb_data.GetInstanceName() << " dataset | m = " << pb_data.GetM() << " clusters" << endl;
// Run HG-Means algorithm
genetic->HGMeans(x);
// Measure cpu time in seconds
double cpu_time = double(clock() - begin) / CLOCKS_PER_SEC;
if (pb_data.GetParam().eval && pb_data.GetM() == pb_data.GetNbClasses()) {
genetic->GetBestSolution()->ComputeExternalMetrics();
}
PrintResult(genetic, cpu_time);
if(save) {
// Save output results
SaveOutput(writer_output, filename_output, genetic, cpu_time);
}
delete genetic;
}
}
std::vector<int> Run(int seed, PbData pb_data, const Dataset* x, bool save) {
srand(seed);
ofstream writer_output;
stringstream filename_output;
filename_output << FilenameOutput(pb_data, save);
// Clean file content if it already exists
if(save) {
SaveHeader(writer_output, filename_output, pb_data);
}
std::vector<int> outcome(x->n);
double best_obj = MAX_FLOAT;
for(int i = 0; i < pb_data.GetParam().nb_runs; i++) {
clock_t begin = clock();
// Create GeneticOperations instance
GeneticOperations* genetic = new GeneticOperations(pb_data);
cout << "-- Starting optimization: " << pb_data.GetInstanceName() << " dataset | m = " << pb_data.GetM() << " clusters" << endl;
// Run HG-Means algorithm
genetic->HGMeans(x);
// Measure cpu time in seconds
double cpu_time = double(clock() - begin) / CLOCKS_PER_SEC;
if (pb_data.GetParam().eval && pb_data.GetM() == pb_data.GetNbClasses()) {
genetic->GetBestSolution()->ComputeExternalMetrics();
}
PrintResult(genetic, cpu_time);
if(save) {
// Save output results
SaveOutput(writer_output, filename_output, genetic, cpu_time);
}
Solution* solution = genetic->GetBestSolution();
double obj = solution->GetCost();
if(obj < best_obj) {
best_obj = obj;
for(int i = 0; i < x->n; i++) {
outcome[i] = solution->GetAssignment()[i];
}
}
delete genetic;
}
return outcome;
}
Param LoadParam(InputValidator command) {
Param param;
param.size_population = command.GetPiMin();
param.max_it = command.GetMaxIt();
param.w = 2;
param.mutation = 1;
param.nb_runs = command.GetNbIt();
param.eval = true;
param.max_population = 2*param.size_population;
param.no_improvement_it = param.max_it/10;
if(param.no_improvement_it < 1) {
param.no_improvement_it = 1;
}
return param;
}
const Dataset* LoadData(string data_path) {
ifstream input(data_path.c_str());
// Read number of points and dimensionality of data
int n, d;
input >> n;
input >> d;
// Allocate storage
const Dataset *x = new Dataset(n, d);
// Read the data values directly into the dataset
for (int i = 0; i < n * d; ++i) {
input >> x->data[i];
}
return x;
}
const Dataset* LoadData(std::vector< std::vector<double> > dataset) {
int n = dataset.size();
int d = dataset[0].size();
// Allocate storage
const Dataset *x = new Dataset(n, d);
// Read the data values directly into the dataset
for (int i = 0; i < n; ++i) {
for (int j = 0; j < d; ++j) {
x->data[d*(i % n) + j] = dataset[i][j];
}
}
return x;
}
HGMeans::HGMeans() {
}
HGMeans::~HGMeans() {
}
void HGMeans::GoFile(char* filename, int size_population, int max_it, int nb_it, const std::vector<int>& m, bool save) {
string filenme_str(filename);
InputValidator command(filenme_str, size_population, max_it, nb_it, m);
if(command.Validate(true)) {
Param param = LoadParam(command);
const Dataset* x = LoadData(command.GetDatasetPath());
if (x == NULL) {
cerr << "Please load a dataset first" << endl;
return;
}
PbData pb_data(command.GetLabelPath(), command.GetDatasetName(), x->data, x->n, x->d, param);
for(int i = 0; i < command.GetNbClusters().size(); i++) {
if(command.GetNbClusters()[i] < pb_data.GetN()) { // m <= n
pb_data.SetM(command.GetNbClusters()[i]);
RunFile(16007, pb_data, x, save);
} else {
cerr << "The number of clusters must be less than " << x->n << " (number of data points)" << endl;
}
}
pb_data.DeleteGroundTruth();
delete x;
}
}
std::vector< std::vector<int> > HGMeans::Go(std::vector< std::vector<double> > dataset, std::vector<int> y, int size_population, int max_it, int nb_it, const std::vector<int>& m, bool save) {
string filenme_str("temp");
InputValidator command(filenme_str, size_population, max_it, nb_it, m);
std::vector< std::vector<int> > results;
if(command.Validate(false)) {
Param param = LoadParam(command);
const Dataset* x = LoadData(dataset);
if (x == NULL) {
cerr << "Please load a dataset first" << endl;
return { {NULL} };
}
PbData pb_data(y, command.GetDatasetName(), x->data, x->n, x->d, param);
for(int i = 0; i < command.GetNbClusters().size(); i++) {
if(command.GetNbClusters()[i] < pb_data.GetN()) { // m <= n
pb_data.SetM(command.GetNbClusters()[i]);
std::vector<int> res = Run(16007, pb_data, x, save);
results.push_back(res);
} else {
cerr << "The number of clusters must be less than " << x->n << " (number of data points)" << endl;
}
}
// std::cout << "The X:" << "\n";
// for (int i = 0; i < x->n; ++i) {
// for (int j = 0; j < x->d; ++j) {
// std::cout << x->data[x->d*(i % x->n) + j] << " ";
// }
// std::cout << "\n";
// }
// std::cout << "The Y:" << "\n";
// for (int i = 0; i < x->n; ++i) {
// std::cout << pb_data.GetTruthAssignment()[i] << " ";
// }
// std::cout << "\n";
pb_data.DeleteGroundTruth();
delete x;
}
return results;
}
int main(int argc, char** argv) {
std::vector<int> nb_clusters;
string w = argv[argc - 1];
bool save = false;
int lim_clusters = argc;
if(w == "w") {
save = true;
lim_clusters -= 1;
}
if(argc >= 6) {
for(int i = 5; i < lim_clusters; i++) {
nb_clusters.push_back(atoi(argv[i]));
}
HGMeans hgmeans;
hgmeans.GoFile(argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), nb_clusters, save);
} else {
cerr << "Insufficient number of parameters provided. Please use the following input format:" <<
endl << "./hgmeans DATASET_PATH PI_MIN N2 NB_IT [M]" << endl;
}
return 0;
}