-
Notifications
You must be signed in to change notification settings - Fork 18
/
Demo.cpp
88 lines (68 loc) · 2.85 KB
/
Demo.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
#include <chrono>
#include "SNNDPC.hpp"
using namespace std::chrono;
int main(int argc, char* argv[]) {
// Parameter
// --------------------------------------------------------------------------------
const auto pathData = SOLUTION_DIR"data/S2.tsv";
const int k = 35;
const int n = 5000; // Number of data points
const int d = 2; // Dimension
const int nc = 15; // Number of centroids
// Read dataset
// --------------------------------------------------------------------------------
int label[n];
float data[n * d];
const auto fileData = fopen(pathData, "r");
for (int i = 0; i < n; i++)
fscanf(fileData, "%f %f %d\n", &data[i * d], &data[i * d + 1], &label[i]);
fclose(fileData);
// Export ground truth
// --------------------------------------------------------------------------------
const auto pathGroundTruth = SOLUTION_DIR"temp/GroundTruth.txt";
const auto fileGroundTruth = fopen(pathGroundTruth, "w");
for (int i: label)
fprintf(fileGroundTruth, "%d\n", i);
fclose(fileGroundTruth);
// Normalize
// --------------------------------------------------------------------------------
float least[d], most[d];
const auto infinity = std::numeric_limits<float>::infinity();
std::fill(least, least + d, infinity);
std::fill(most, most + d, -infinity);
for (int i = 0; i < n; i++)
for (int j = 0; j < d; j++) {
least[j] = std::min(least[j], data[i * d + j]);
most[j] = std::max(most[j], data[i * d + j]);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < d; j++)
data[i * d + j] = (data[i * d + j] - least[j]) / (most[j] - least[j]);
// Do the magic
// --------------------------------------------------------------------------------
const auto time = high_resolution_clock::now();
const auto [centroid, assignment] = SNNDPC(k, n, d, nc, data);
printf("Time Cost = %lldms\n", duration_cast<milliseconds>(high_resolution_clock::now() - time).count());
// Export centroid
// --------------------------------------------------------------------------------
const auto fileCentroid = fopen(SOLUTION_DIR"temp/Centroid.txt", "w");
for (int i = 0; i < nc; i++)
fprintf(fileCentroid, "%d\n", centroid[i]);
fclose(fileCentroid);
// Export assignment
// --------------------------------------------------------------------------------
const auto pathAssignment = SOLUTION_DIR"temp/Assignment.txt";
const auto fileAssignment = fopen(pathAssignment, "w");
for (int i = 0; i < n; i++)
fprintf(fileAssignment, "%d\n", assignment[i]);
fclose(fileAssignment);
// Evaluate
// --------------------------------------------------------------------------------
char command[1024];
sprintf(command, "python %s %s %s", SOLUTION_DIR"EvaluateAssignment.py", pathGroundTruth, pathAssignment);
system(command);
// Clean up
// --------------------------------------------------------------------------------
delete[] centroid;
delete[] assignment;
}