-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_functions.cpp
158 lines (120 loc) · 3.9 KB
/
file_functions.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
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <string>
#include <vector>
#include <string>
#include "file_functions.h"
using namespace std;
int num_of_clusters;
int global_k;
int global_L;
void read_file(const char *file_name, int &dim) {
vector<double> point;
ifstream file(file_name);
string str, id;
int num_points, int_id = 0;
char chr;
bool read_id = false;
file >> str;
if (str == "dimension:") {
file >> dim;
} else {
read_id = true;
id = str;
}
point.resize(dim);
while (1) {
if (!read_id) {
file >> id;
} else {
read_id = false;
}
if (file.eof()) {
break;
}
file >> num_points;
Curve curve(id, int_id++, dim);
for (int i = 0; i < num_points; i++) {
for(int j = 0; j < dim; ++j) {
file >> chr >> point[j];
}
file >> chr;
if (i < num_points - 1) {
file >> chr;
}
if (!curve.is_empty() && curve.get_last_point() == point) { // remove duplicates
continue;
}
curve.insert_point(point);
}
input_curves.push_back(curve);
}
file.close();
}
void read_configuration_file(const char *file_name) {
ifstream file(file_name);
string str;
while(1) {
if (file.eof()) {
break;
}
file >> str;
if (str == "number_of_clusters:") {
file >> num_of_clusters;
} else if (str == "number_of_grid_curves:") {
file >> global_k;
} else if (str == "number_of_hash_tables:") {
file >> global_L;
}
}
file.close();
}
void print_file(const char *file_name, const char *metric, const vector<double> &silhouette_cluster, double time, const vector<const Curve*> ¢roids, const vector<vector<int> > &clusters, int dim, const char *complete) {
ofstream out_file(file_name);
out_file << "I" << method_init << "A" << method_assign << "U" << method_update << "\n";
out_file << "Metric: " << metric << "\n";
for (int i = 0; i < (int)clusters.size(); ++i) {
out_file << "CLUSTER-" << i << " {size: " << (int)clusters[i].size() << ", centroid: ";
if (method_update == 2) {
out_file << centroids[i]->get_id() << "}" << "\n";
} else {
out_file << "[";
for (int j = 0; j < centroids[i]->get_length(); ++j) {
out_file << "(";
for (int k = 0; k < dim; ++k) {
out_file << centroids[i]->get_coord_point(k, j);
if (k < dim - 1) {
out_file << ", ";
}
}
if (j == centroids[i]->get_length() - 1) {
out_file << ")";
} else {
out_file << "), ";
}
}
out_file << "]}\n";
}
}
out_file << "clustering_time: " << time << "\n";
out_file << "Silhouette: [";
double avg_s = 0;
for (int i = 0; i < (int)silhouette_cluster.size(); ++i) {
avg_s += silhouette_cluster[i];
out_file << silhouette_cluster[i] << ", ";
}
avg_s /= (int)silhouette_cluster.size();
out_file << avg_s << "]" << "\n";
if (strlen(complete)) {
for (int i = 0; i < (int)clusters.size(); ++i) {
out_file << "CLUSTER-" << i << " {";
for (int j = 0; j < (int)clusters[i].size() - 1; ++j) {
out_file << clusters[i][j] << ", ";
}
out_file << clusters[i][(int)clusters[i].size() - 1] << "}\n";
}
}
out_file.close();
}