-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_DBSCAN.cpp
110 lines (98 loc) · 4.33 KB
/
test_DBSCAN.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
#include"DBSCAN.hpp"
#include"point.hpp"
#include"vertex.hpp"
#include<iostream>
#include<fstream>
#include<chrono>
void load(std::ifstream &is, point* points, int& n_points) {
int idx = 0;
while(is.peek() != EOF)
{
// read new points
for(int m = 0; m < point::get_dim(); m++)
{
is >> points[idx].coords[m];
}
idx++;
// consume \n :
is.get();
// std::cout << idx-1 << ": " << points[idx-1].coords[0] << " " << points[idx-1].coords[1] << std::endl;
}
is.close();
n_points = idx;
}
void generate_classified_output(DBSCAN& dbscan) {
std::ofstream os;
os.open("./output/test_DBSCAN_output.txt");
for(int i = 0; i < dbscan.n; i++)
{
for(int m = 0; m < point::get_dim(); m++)
{
os << dbscan.pa[i][m] << " ";
}
os << dbscan.clusters[i] <<"\n";
}
os.close();
std::cout << "output file generated \nformat : coord_1 coord_2 ... coord_d cluster\n";
}
int main(int argc, char const *argv[]) {
if ( (argc != 5 && argc != 8) || (argc == 8 && (std::atoi(argv[2]) != -1 && std::atoi(argv[2]) != -2) ) ) {
std::cout << "Usage: " << std::endl;
std::cout << argv[0] << " <file> <eps> <Minpts> <dimension> applies DBSCAN once with these arguments" << std::endl;
std::cout << argv[0] << " <file> -1 <MinPts> <start> <end> <step> <dimension> applies DBSCAN with adapted silhouette method on EPS" << std::endl;
std::cout << argv[0] << " <file> -2 <eps> <start> <end> <step> <dimension> applies DBSCAN with adapted silhouette method on MINPTS" << std::endl;
return 1;
}
// 2 first coordinates from the most known iris dataset example from TD05
std::ifstream is(argv[1]);
if(argc == 5)
point::d = std::atoi(argv[4]);
else
point::d = std::atoi(argv[7]);
point* points = new point[1000000];
int n_points;
load(is, points, n_points);
DBSCAN dbscan(n_points, points);
if(argc == 5)
{
double eps = std::atof(argv[2]);
int minpts = std::atoi(argv[3]);
auto time1 = std::chrono::high_resolution_clock::now();
dbscan.ComputeDBSCAN(eps, minpts);
auto time2 = std::chrono::high_resolution_clock::now();
std::cout << dbscan.n_clusters << " clusters found" << std::endl;
std::cout << "DBSCAN execution time: " << std::chrono::duration_cast<std::chrono::milliseconds>(time2-time1).count() << " ms" << std::endl;
generate_classified_output(dbscan);
}
else if(std::atoi(argv[2]) == -1)
{
double start = std::atof(argv[4]);
double step = std::atof(argv[6]);
double end = std::atof(argv[5]) + step;
int minpts = std::atoi(argv[3]);
std::cout << "silhouette X eps (P.S. eps too small -> no cluster -> s = awkward value)" << std::endl;
for(double eps = start; eps < end; eps += step){
dbscan.ResetDBSCAN();
auto time1 = std::chrono::high_resolution_clock::now();
dbscan.ComputeDBSCAN(eps, minpts);
auto time2 = std::chrono::high_resolution_clock::now();
std::cout<< "eps: " << eps << ", minpts: " << minpts << ", number of clusters: " << dbscan.n_clusters << ", s: "<<dbscan.silhouette()<< ", s_modified: "<<dbscan.non_convex_silhouette(minpts)<< ", execution time: " << std::chrono::duration_cast<std::chrono::milliseconds>(time2-time1).count() << " ms"<<std::endl;
}
}
else if(std::atoi(argv[2]) == -2)
{
double start = std::atof(argv[4]);
double step = std::atof(argv[6]);
double end = std::atof(argv[5]) + step;
double eps = std::atof(argv[3]);
std::cout << "silhouette X minpts (P.S. minpts too big -> no cluster -> s = awkward value)" << std::endl;
for(int minpts = start; minpts < end; minpts += step){
dbscan.ResetDBSCAN();
auto time1 = std::chrono::high_resolution_clock::now();
dbscan.ComputeDBSCAN(eps, minpts);
auto time2 = std::chrono::high_resolution_clock::now();
std::cout<< "eps: " << eps << ", minpts: " << minpts << ", number of clusters: " << dbscan.n_clusters << ", s: "<<dbscan.silhouette()<< ", s_modified: "<<dbscan.non_convex_silhouette(minpts)<< ", execution time: " << std::chrono::duration_cast<std::chrono::milliseconds>(time2-time1).count() << " ms"<<std::endl;
}
}
return 0;
}