-
Notifications
You must be signed in to change notification settings - Fork 0
/
bhatta.cpp
196 lines (148 loc) · 4.71 KB
/
bhatta.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
#include <iostream>
#include <vector>
#include <numeric>
#include <math.h>
#include <map>
#include <set>
#include <fstream>
#include <string>
#include <sstream>
#include <assert.h>
#include <algorithm>
#include <cstring>
#include <sys/time.h>
#ifdef PARALLEL
#include <omp.h>
#endif
using namespace std;
typedef map<string, int> Histogram;
float mean(Histogram hist);
float bhatta(Histogram hist1, Histogram hist2);
pair<vector<Histogram>, vector<string>> reads_file(char* file_name);
void write_results_to_file(char* filename, string version, int n_threads, long double elapsed_time);
long double timedifference_msec(struct timeval start, struct timeval end);
int main(int argc, char *argv[])
{
timeval begin_time;
timeval end_time;
// default is 1
int n_threads = 1;
gettimeofday(&begin_time, 0);
if (argc < 2)
{
cerr << "usage: <program> <instance_name>" << endl;
return EXIT_FAILURE;
}
char *filename = argv[1];
std::pair<vector<Histogram>, vector<string>> dataset = reads_file(filename);
vector<Histogram> histograms = dataset.first;
vector<string> classification = dataset.second;
float** scores = (float**) malloc(histograms.size() * sizeof(float*));
for (int i=0; i<histograms.size(); i++)
scores[i] = (float*) malloc(histograms.size() * sizeof(float));
#ifdef PARALLEL
n_threads = N_THREADS;
#pragma omp parallel for num_threads(N_THREADS)
#endif
for (int i=0; i<histograms.size(); i++)
{
for (int j=0; j<histograms.size(); j++)
scores[i][j] = (bhatta(histograms[i], histograms[j]));
}
#ifdef DEBUG
for (int i=0; i<histograms.size(); i++)
{
for (int j=0; j<histograms.size(); j++)
cout << scores[i][j] << " ";
cout << endl;
}
#endif
#ifdef DEBUG
cout << "Done with " << filename << endl;
#endif
gettimeofday(&end_time, 0);
long int elapsed_msec = timedifference_msec(begin_time, end_time);
#ifdef DEBUG
cout << "Elapsed time: " << elapsed_msec << "ms" << endl;
#endif
#ifdef RESULTSFILE
write_results_to_file(filename, argv[0], n_threads, elapsed_msec);
#endif
return 0;
}
float mean(Histogram hist)
{
float acc = 0;
for (auto &pair : hist)
acc += pair.second;
return acc/hist.size();
}
float bhatta(Histogram hist1, Histogram hist2)
{
float h1_mean = mean(hist1);
float h2_mean = mean(hist2);
float score = 0;
// evaluate only keys present on both histograms
for (auto &pair : hist1)
if (hist2.find(pair.first) != hist2.end())
score += sqrt(hist1.at(pair.first) * hist2.at(pair.first));
score = sqrt( 1 - ( 1 / sqrt(h1_mean * h2_mean * hist1.size() * hist2.size()) ) * score );
return score;
}
pair<vector<Histogram>, vector<string>> reads_file(char* file_name)
{
vector<Histogram> instance;
vector<string> classification;
string line;
ifstream file(file_name);
if (file.is_open())
{
#ifdef DEBUG
cout << "Started reading file" << endl;
#endif
while (getline(file, line))
{
Histogram hist;
string entry;
istringstream iss(line);
// Ignoring date entry for now
getline(iss, entry, ',');
getline(iss, entry, ',');
classification.push_back(entry);
while(!getline(iss, entry, ',').eof())
{
int split_pos = entry.find(':');
string key = entry.substr(0, split_pos);
string str_value = entry.substr(split_pos + 1);
int value = stoi(str_value);
hist.insert(std::pair<string,int>(key, value));
}
instance.push_back(hist);
}
file.close();
}
#ifdef DEBUG
cout << "Finished reading file" << endl;
#endif
return pair<vector<Histogram>, vector<string>>(instance, classification);
}
void write_results_to_file(char* filename, string version, int n_threads, long double elapsed_time)
{
ofstream results_file;
results_file.open(version + "_results.txt", std::ios_base::app);
if (results_file.is_open())
{
results_file << "FILE=" << filename << "\t";
results_file << "N_THREADS=" << n_threads << "\t";
results_file << "T=" << elapsed_time << endl;
#ifdef DEBUG
cout << "Results written to file" << endl;
#endif
}
results_file.close();
}
long double timedifference_msec(struct timeval start, struct timeval end)
{
return (end.tv_sec * 1000 + end.tv_usec / 1000)
- (start.tv_sec * 1000 + start.tv_usec / 1000);
}