-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_filtre.cc
135 lines (102 loc) · 3.47 KB
/
experiment_filtre.cc
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
#include <iostream>
#include <fstream>
#include <chrono>
#include <map>
#include "./SuperSopa/superSopa.hh"
using namespace std;
//retorna el moment actual
auto moment () {
return chrono::steady_clock::now();
}
void llegir_fitxer (vector<string>& v, const string& path) {
ifstream fp_in;
string a;
fp_in.open(path);
while (fp_in >> a) {
v.push_back(a);
}
fp_in.close();
}
void afegir_prefix (BloomFilterDictionary& d, string s) {
int n = s.size();
string aux = "";
for (int i = 0; i < n-1; ++i) {
aux.push_back(s[i]);
if (not d.comprovar(aux)) {
d.afegir(aux);
}
}
}
void mitjana (vector<pair<double, int>>& execucions, double& temps, int& errors) {
int n = execucions.size();
double sumaT = 0;
double sumaE = 0;
for (int i = 0; i < n; ++i) {
sumaT += execucions[i].first;
sumaE += execucions[i].second;
}
temps = sumaT/n;
errors = sumaE/n;
}
int main () {
ifstream fp_in;
ofstream fp_out;
superSopa super_sopa;
string pathSopes = "sopes.txt";
string pathResultat = "./resultats/resultatFiltre.txt";
string pathDiccionari = "./diccionaris/quijote-vocabulary-6.txt";
//llegir diccionari
vector<string> diccionari;
llegir_fitxer(diccionari, pathDiccionari);
BloomFilterDictionary bloom_filter(diccionari.size());
BloomFilterDictionary prefixos(diccionari.size()*2);
TrieDictionary trie;
sort(diccionari.begin(), diccionari.end());
int meitat = diccionari.size()/2;
for (int i = meitat; i < diccionari.size(); ++i) trie.afegir(diccionari[i]);
for (int j = meitat - 1; j >= 0; --j) trie.afegir(diccionari[j]);
auto begin = moment();
for (int i = 0; i < diccionari.size(); ++i) {
afegir_prefix(prefixos, diccionari[i]);
bloom_filter.afegir(diccionari[i]);
}
auto end = moment();
double ta = chrono::duration_cast<chrono::microseconds>(end - begin).count();
//EXPERIMENT COMPROVAR
fp_in.open(pathSopes);
fp_out.open(pathResultat);
fp_out << ta << endl;
char s;
int n;
for (int nSopes = 0; nSopes < 100; ++nSopes) {
//llegir sopa
fp_in >> n;
Sopa sopa = Sopa(n, vector<char>(n, '#'));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
fp_in >> s;
sopa[i][j] = s;
}
}
//resoldre-la 10 cops
vector<pair<double, int>> execucions; //temps i errors de cada execució
int trobadesFiltre = 0, trobadesVector = 0;
for (int cops = 0; cops < 10; ++cops) {
auto begin = moment();
trobadesFiltre = super_sopa.resoldre(bloom_filter, prefixos, sopa);
auto end = moment();
trie.iniciarResultat();
super_sopa.resoldre(trie, sopa);
double t = chrono::duration_cast<chrono::microseconds>(end - begin).count();
int e = trobadesFiltre - trie.paraulesTotals();
execucions.push_back({t, e});
}
double t;
int e;
mitjana(execucions, t, e);
//nombre de la sopa, mida, temps, paraules vector, paraules filtre, falsos positius
fp_out << nSopes+1 << ' ' << n << ' ' << t << ' ' << trie.paraulesTotals() << ' ' << trobadesFiltre << ' ' << e << endl;
}
fp_in.close();
fp_out.close();
}