-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMVMB.cpp
More file actions
443 lines (426 loc) · 9.85 KB
/
MVMB.cpp
File metadata and controls
443 lines (426 loc) · 9.85 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "MVMB.hpp"
void mvmb::setMV(vector<MV> p_mv)
{
mv = p_mv;
}
vector<MV> mvmb::getMV()
{
return mv;
}
void mvmb::powSet(int i, vector<char> chr, vector<string> &tempstr_vector)
{
int j = 0;
bool pre = false;
string tempstr = "";
while (i > 0)
{
if (i & 1)
{
tempstr += chr[j];
pre = true;
}
i >>= 1;
j++;
}
tempstr_vector.push_back(tempstr);
}
vector<int> mvmb::sort_MV(vector<MV> p_mv)
{
static const int k = 30; // MV_NUMBER;
int max_benefit = 0;
int tag = 0;
int tag_temp = 0;
int len = 0;
int tags[k];
int tags_falg = 1;
int total = 1;
for (auto a : p_mv)
{
a.setBenefit((p_mv[0].getCost() - a.getCost()) * pow(2, a.getDims()));
if (a.getBenefit() > max_benefit)
{
max_benefit = a.getBenefit();
tag = tag_temp;
p_mv[tag_temp].setBenefit(max_benefit);
}
tag_temp++;
}
vector<string> tempstr_vector;
char temp_chr_arr[p_mv[tag].getDims()];
for (int i = 0; i < p_mv[tag].getDims(); ++i)
{
temp_chr_arr[i] = p_mv[tag].getChr()[i];
}
len = 1 << sizeof(temp_chr_arr) / sizeof(char);
for (int i = 0; i < len; i++)
{
powSet(i, p_mv[tag].getChr(), tempstr_vector);
}
for (int i = 0; i < tempstr_vector.size(); i++)
{
mv_map.insert(map<string, int>::value_type(tempstr_vector[i], p_mv[tag].getCost()));
}
tags[0] = tag;
tag_temp = 0;
for (int i = 1; i < k; ++i)
{
max_benefit = 0;
int max_cost = 0;
vector<string> temp_vector_for_map;
tag_temp = 0;
for (auto a : p_mv)
{
char temp_chr[a.getDims()];
int sum = 0;
int temp_len = 0;
vector<string> temp_vector;
for (int j = 0; j < a.getDims(); ++j)
{
temp_chr[j] = a.getChr()[j];
}
temp_len = 1 << sizeof(temp_chr) / sizeof(char);
for (int m = 0; m < temp_len; m++)
{
powSet(m, a.getChr(), temp_vector);
}
for (int n = 0; n < temp_vector.size(); n++)
{
if (mv_map.count(temp_vector[n]) == 0)
{
sum++;
}
}
map<string, int>::iterator iter;
iter = mv_map.find(temp_vector[temp_vector.size() - 1]);
if (iter != mv_map.end())
{
a.setBenefit((iter->second - a.getCost()) * pow(2, sum));
}
else
{
a.setBenefit((p_mv[0].getCost() - a.getCost()) * pow(2, sum));
p_mv[tag_temp].setBenefit((p_mv[0].getCost() - a.getCost()) * pow(2, sum));
}
if (i >= 29)
{
if (a.getBenefit() > max_benefit)
{
}
}
if (a.getBenefit() > max_benefit)
{
max_benefit = a.getBenefit();
tags[tags_falg] = tag_temp;
temp_vector_for_map = temp_vector;
max_cost = a.getCost();
p_mv[tag_temp].setBenefit(max_benefit);
}
tag_temp++;
}
for (int index = 0; index < temp_vector_for_map.size(); index++)
{
if (mv_map.count(temp_vector_for_map[index]) == 0)
{
mv_map.insert(map<string, int>::value_type(temp_vector_for_map[index], max_cost));
}
}
tags_falg++;
}
cout << tags_falg << endl;
cout << "------------" << endl;
vector<int> res;
for (int i = 0; i < tags_falg; ++i)
{
p_mv[tags[i]].display();
res.push_back(tags[i]);
}
return res;
}
vector<int> mvmb::sort_MV2(vector<MV> p_mv)
{
int k = 2;
int max_benefit = 0;
int tag = 0;
int tag_temp = 0;
int len = 0;
int tags[2];
int tags_falg = 1;
int total = 1;
map<int, int> chosed_map;
for (auto a : p_mv)
{
a.setBenefit((p_mv[0].getCost() - a.getCost()) * pow(2, a.getDims()));
if (a.getBenefit() > max_benefit)
{
max_benefit = a.getBenefit();
tag = tag_temp;
p_mv[tag_temp].setBenefit(max_benefit);
}
tag_temp++;
}
vector<string> tempstr_vector;
vector<char> temp_chr_arr(p_mv[tag].getDims());
for (int i = 0; i < p_mv[tag].getDims(); ++i)
{
temp_chr_arr[i] = p_mv[tag].getChr()[i];
}
len = 1 << sizeof(temp_chr_arr) / sizeof(char);
for (int i = 0; i < len; i++)
{
powSet(i, p_mv[tag].getChr(), tempstr_vector);
}
for (int i = 0; i < tempstr_vector.size(); i++)
{
mv_map.insert(map<string, int>::value_type(tempstr_vector[i], p_mv[tag].getCost()));
}
chosed_map.insert(map<int, int>::value_type(tag, 1));
tags[0] = tag;
for (int i = 1; i < k; ++i)
{
max_benefit = 0;
int max_cost = 0;
vector<string> temp_powerset_vector_for_map;
tag_temp = 0;
for (int w = 0; w < p_mv.size(); ++w)
{
if (chosed_map.count(w) != 0)
{
continue;
}
vector<char> temp_chr(p_mv[w].getDims());
int sum = 0;
int temp_len = 0;
vector<string> temp_powset_vector;
for (int j = 0; j < p_mv[w].getDims(); ++j)
{
temp_chr[j] = p_mv[w].getChr()[j];
}
temp_len = 1 << sizeof(temp_chr) / sizeof(char);
for (int m = 0; m < temp_len; m++)
{
powSet(m, p_mv[w].getChr(), temp_powset_vector);
}
for (int n = 0; n < temp_powset_vector.size(); n++)
{
if (mv_map.count(temp_powset_vector[n]) == 0)
{
sum++;
}
}
map<string, int>::iterator iter;
iter = mv_map.find(temp_powset_vector[temp_powset_vector.size() - 1]);
if (iter != mv_map.end())
{
p_mv[w].setBenefit((iter->second - p_mv[w].getCost()) * pow(2, sum));
}
else
{
p_mv[w].setBenefit((p_mv[0].getCost() - p_mv[w].getCost()) * pow(2, sum));
}
if (p_mv[w].getBenefit() > max_benefit)
{
max_benefit = p_mv[w].getBenefit();
tags[tags_falg] = w;
tag_temp = w;
temp_powerset_vector_for_map = temp_powset_vector;
max_cost = p_mv[w].getCost();
p_mv[w].setBenefit(max_benefit);
}
}
for (int index = 0; index < temp_powerset_vector_for_map.size(); index++)
{
if (mv_map.count(temp_powerset_vector_for_map[index]) == 0)
{
mv_map.insert(map<string, int>::value_type(temp_powerset_vector_for_map[index], max_cost));
}
}
chosed_map.insert(map<int, int>::value_type(tag_temp, 1));
tags_falg++;
}
cout << tags_falg << endl;
cout << "------------" << endl;
vector<int> res;
for (int i = 0; i < tags_falg; ++i)
{
p_mv[tags[i]].display();
res.push_back(tags[i]);
}
return res;
}
equivalence_class_num_type mvmb::get_the_number_of_equivalence_classes() const
{
cout << "cache_manager_MVMB::get_the_number_of_equivalence_classes()const not need to accomplish,if error,quit." << endl;
exit(0);
return 0;
}
data_cell_num_type mvmb::get_the_number_of_data_units() const
{
return MVMB_Container.size();
}
bool mvmb::query_in_cache(queryItem &queryItem)
{
bool flag = false;
if (MVMB_Container.empty())
{
return false;
}
for (auto iter = MVMB_Container.begin(); iter != MVMB_Container.end(); ++iter)
{
if (iter->getDims() == queryItem.getDims())
{
queryItem.setMeasure(iter->getMeasure());
flag = true;
break;
}
}
if (flag)
{
return true;
}
else
{
return false;
}
}
void mvmb::refresh_cache(const queryItem &queryItem_1)
{
queryItem temp_queryItem = queryItem_1;
string str = temp_queryItem.get_Dims();
if (MVMB_map.count(str) == 0)
{
MVMB_Container.push_front(queryItem_1);
MVMB_map.insert(map<string, int>::value_type(str, 1));
}
else
{
}
while (get_the_number_of_data_units() > data_cells_limit_size)
{
cache_replacement_policy();
}
}
void mvmb::cache_replacement_policy()
{
MVMB_Container.pop_back();
}
vector<queryItem> mvmb::load_query_file()
{
vector<vector<string>> temp_queryItemSet;
ifstream infile(querySetFilePath);
string temp;
if (!infile.is_open())
{
cout << querySetFilePath + "the file not is open on path" << endl;
exit(0);
}
while (getline(infile, temp))
{
vector<string> tempvec;
SplitString(temp, tempvec, ",");
temp_queryItemSet.push_back(tempvec);
}
infile.close();
vector<queryItem> ret_queryItemSet;
for (auto a : temp_queryItemSet)
{
dims_type temp_dims = {0};
measure_type temp_measure = 0.0;
for (int i = 0; i < dimsNum; ++i)
{
temp_dims[i] = atoi(a[i].c_str());
}
queryItem temp_queryItem(temp_dims, temp_measure);
ret_queryItemSet.push_back(temp_queryItem);
}
return ret_queryItemSet;
}
vector<queryItem> mvmb::loadQueryFile()
{
vector<vector<string>> temp_queryItemSet;
cout << "..." << endl;
ifstream infile(mvQueryFilePath);
string temp;
if (!infile.is_open())
{
cout << querySetFilePath + "file not open" << endl;
exit(0);
}
while (getline(infile, temp))
{
vector<string> tempvec;
SplitString(temp, tempvec, ",");
temp_queryItemSet.push_back(tempvec);
}
infile.close();
vector<queryItem> ret_queryItemSet;
for (auto a : temp_queryItemSet)
{
dims_type temp_dims = {0};
measure_type temp_measure = 0.0;
for (int i = 0; i < dimsNum; ++i)
{
temp_dims[i] = atoi(a[i].c_str());
}
queryItem temp_queryItem(temp_dims, temp_measure);
ret_queryItemSet.push_back(temp_queryItem);
}
return ret_queryItemSet;
}
void mvmb::match_best_MV()
{
}
void mvmb::displasy_cachaItem() const
{
cout << endl
<< endl
<< "the number of the data cells" << get_the_number_of_data_units() << endl;
for_each(MVMB_Container.begin(), MVMB_Container.end(), [](const queryItem &every_queryItem)
{ every_queryItem.display(); });
}
vector<int> mvmb::load_MV()
{
ifstream alg(basePath + "static_mv.txt");
string line;
vector<int> mv;
if (!alg.is_open())
{
cout << "fail to read mv" << endl;
exit(0);
}
while (getline(alg, line))
{
mv.push_back(atoi(line.c_str()));
}
return mv;
}
void mvmb::write_mv_to_file(vector<int> mv_flag)
{
ofstream alg;
alg.open(basePath + "static_mv.txt", ios::trunc);
if (!alg.is_open())
{
cout << "fail to write mv into file" << endl;
exit(0);
}
for (auto &mv : mv_flag)
{
alg << mv << endl;
}
}
void mvmb::dispaly_experimental_result_to_file(data_cell_num_type queryItemSet_size, int hit, time_t all_time) const
{
ofstream alg;
alg.open(basePath + "MVMBalg.txt", ios::trunc);
if (!alg.is_open())
{
cout << "fail to write results into files" << endl;
exit(0);
}
alg << "alg name:MVMB" << endl;
alg << "the number of query items:" << queryItemSet_size << endl;
alg << "cache limit size(the number of data cells):" << data_cells_limit_size << endl;
alg << "hit times:" << hit << endl;
alg << "hit rate:" << (float)hit / queryItemSet_size << endl;
alg << "total use time(s):" << all_time << endl;
alg << "the number of data cells:" << get_the_number_of_data_units() << endl;
}