-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
142 lines (129 loc) · 4.42 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
/*
* Copyright 2024 IntelliStream team (https://github.com/intellistream)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "birch.hpp"
#include "clustream.hpp"
#include "common.hpp"
#include "denstream.hpp"
#include "dstream.hpp"
#include "edmstream.hpp"
#include "evaluation.hpp"
#include "point.hpp"
#include "slkmeans.hpp"
#include <cassert>
#include <chrono>
#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <vector>
using namespace std;
using namespace std::chrono_literals;
void run(const string &name, const Dataset &dataset, Algorithm &algo) {
auto start = chrono::high_resolution_clock::now();
for (int i = 0; i < dataset.num_points; i += BATCH_SIZE) {
int end = min(i + BATCH_SIZE, dataset.num_points);
algo.cluster(vector<Point>(dataset.points.begin() + i,
dataset.points.begin() + end));
cout << "Progress: [" << (i + BATCH_SIZE) << " / " << dataset.num_points
<< "]\r";
}
cout << endl;
auto end = chrono::high_resolution_clock::now();
auto elapsed = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "Execution time: " << elapsed.count() << " ms" << endl;
auto centers = algo.output_centers();
cout << "Number of clusters: " << centers.size() << endl;
// save centers to file
ofstream out(name + ".centers");
for (const auto ¢er : centers) {
for (int i = 0; i < center.features.size(); i++) {
out << center.features[i] << " ";
}
out << endl;
}
out.close();
if (centers.size() > 0 && centers.size() <= 1000) {
auto purity = evaluate_purity(points_to_labels(dataset.points),
group_by_centers(dataset.points, centers),
dataset.num_true_clusters, centers.size());
cout << "Purity: " << purity << endl;
} else {
cout << "Purity: N/A, please check code correctness" << endl;
}
}
int main(int argc, char *argv[]) {
Dataset dataset;
{
cout << "Loading dataset ..." << endl;
int flags, opt;
int num_points = NUM_POINTS;
bool num_points_set = false;
while ((opt = getopt(argc, argv, "n:")) != -1) {
switch (opt) {
case 'n':
num_points = atoi(optarg);
num_points_set = true;
break;
default: /* '?' */
cerr << "Usage: " << argv[0] << " [-n num_points] /path/to/dataset"
<< endl;
exit(EXIT_FAILURE);
}
}
if (optind >= argc) {
cerr << "Usage: " << argv[0] << " [-n num_points] /path/to/dataset"
<< endl;
cout << "Using random generated dataset, results may vary." << endl;
dataset.gen(num_points, DIMENSIONS);
} else {
dataset.load(argv[optind]);
if (num_points_set) {
dataset.limit(num_points);
}
}
}
cout << dataset << endl;
// Benchmark BIRCH
cout << "==============================" << endl;
cout << "Running BIRCH ..." << endl;
BIRCH birch(dataset.dim);
run("birch", dataset, birch);
// Benchmark CluStream
cout << "==============================" << endl;
cout << "Running CluStream ..." << endl;
CluStream clustream(dataset.dim);
run("clustream", dataset, clustream);
// Benchmark EDMStream
cout << "==============================" << endl;
cout << "Running EDMStream ..." << endl;
EDMStream edm(dataset.dim);
run("edmstream", dataset, edm);
// Benchmark DStream
cout << "==============================" << endl;
cout << "Running DStream ..." << endl;
DStream dstream(dataset.dim);
run("dstream", dataset, dstream);
// Benchmark DenStream
cout << "==============================" << endl;
cout << "Running DenStream ..." << endl;
DenStream denstream(dataset.dim);
run("denstream", dataset, denstream);
// Benchmark SLKMeans
cout << "==============================" << endl;
cout << "Running SLKMeans ..." << endl;
SLKMeans slkmeans(dataset.dim, dataset.num_true_clusters);
run("slkmeans", dataset, slkmeans);
return 0;
}