-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdec_time_prediction.hpp
160 lines (130 loc) · 4.58 KB
/
dec_time_prediction.hpp
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
#pragma once
#include <array>
#include <sstream>
#include <string>
#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/stringize.hpp>
#include "util.hpp"
#define DS2I_FEATURE_TYPES (n)(size)(sum_of_logs)(entropy)(nonzeros)(max_b)(pfor_b)(pfor_exceptions)
namespace ds2i { namespace time_prediction {
constexpr size_t num_features = BOOST_PP_SEQ_SIZE(DS2I_FEATURE_TYPES);
enum class feature_type {
BOOST_PP_SEQ_ENUM(DS2I_FEATURE_TYPES), end
};
feature_type parse_feature_type(std::string const& name)
{
if (false) {
#define LOOP_BODY(R, DATA, T) \
} else if (name == BOOST_PP_STRINGIZE(T)) { \
return feature_type::T; \
/**/
BOOST_PP_SEQ_FOR_EACH(LOOP_BODY, _, DS2I_FEATURE_TYPES);
#undef LOOP_BODY
} else {
throw std::invalid_argument("Invalid feature name " + name);
}
}
std::string feature_name(feature_type f)
{
switch (f) {
#define LOOP_BODY(R, DATA, T) \
case feature_type::T: return BOOST_PP_STRINGIZE(T); \
/**/
BOOST_PP_SEQ_FOR_EACH(LOOP_BODY, _, DS2I_FEATURE_TYPES);
#undef LOOP_BODY
default: throw std::invalid_argument("Invalid feature type");
}
}
class feature_vector {
public:
feature_vector()
{
std::fill(m_features.begin(), m_features.end(), 0);
}
float& operator[](feature_type f) { return m_features[(size_t)f]; }
float const& operator[](feature_type f) const { return m_features[(size_t)f]; }
stats_line& dump(stats_line& sl) const
{
for (size_t i = 0; i < num_features; ++i) {
feature_type ft = (feature_type)i;
sl(feature_name(ft), (*this)[ft]);
}
return sl;
}
protected:
std::array<float, num_features> m_features;
};
class predictor : public feature_vector {
public:
predictor()
: m_bias(0)
{}
predictor(std::vector<std::pair<std::string, float>> const& values)
{
for (auto const& kv: values) {
if (kv.first == "bias") {
bias() = kv.second;
} else {
(*this)[parse_feature_type(kv.first)] = kv.second;
}
}
}
float& bias() { return m_bias; }
float const& bias() const { return m_bias; }
float operator()(feature_vector const& f) const
{
float result = bias();
for (size_t i = 0; i < num_features; ++i) {
feature_type ft = (feature_type)i;
result += (*this)[ft] * f[ft];
}
return result;
}
protected:
float m_bias;
};
void values_statistics(std::vector<uint32_t> values, feature_vector& f)
{
std::sort(values.begin(), values.end());
f[feature_type::n] = values.size();
if (values.empty()) return;
uint32_t last_value = values.front();
size_t group_begin = 0;
double entropy = 0;
double sum_of_logs = 0;
double nonzeros = 0;
uint32_t max_b = 0;
for (size_t i = 1; i <= values.size(); ++i) {
if (i == values.size() || values[i] != last_value) {
size_t group_size = i - group_begin;
entropy += group_size * log2(double(values.size()) / group_size);
sum_of_logs += group_size * log2(double(last_value) + 1);
if (last_value != 0) {
nonzeros += group_size;
}
uint32_t b = last_value ? succinct::broadword::msb(last_value) + 1 : 0;
max_b = std::max(max_b, b);
if (i < values.size()) {
last_value = values[i];
group_begin = i;
}
}
}
f[feature_type::entropy] = entropy;
f[feature_type::sum_of_logs] = sum_of_logs;
f[feature_type::nonzeros] = nonzeros;
f[feature_type::max_b] = max_b;
}
bool read_block_stats(std::istream& is, uint32_t& list_id, std::vector<uint32_t>& block_counts)
{
thread_local std::string line;
uint32_t count;
block_counts.clear();
if (!std::getline(is, line)) return false;
std::istringstream iss(line);
iss >> list_id;
while (iss >> count) block_counts.push_back(count);
return true;
}
}}