forked from fuhaoda/ITR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomp_search.cpp
578 lines (478 loc) · 16 KB
/
comp_search.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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <algorithm>
#include <thread>
#include <numeric>
#include <bitset>
#include <sstream>
#include <map>
#include "comp_search.h"
CompSearch::CompSearch(unsigned depth, unsigned nthreads) : depth_{depth} {
if (depth != 1 && depth != 2 && depth != 3)
throw "Invalid search depth!";
nthreads_ = std::min(nthreads, std::thread::hardware_concurrency());
}
void CompSearch::preprocess(size_t i) {
Data *data = rdata[i].get();
nsample_ = data->nsample();
ncont_ = data->ncont();
nord_ = data->nord();
nnom_ = data->nnom();
decile_.resize(ncont_);
uniq_ord_.resize(nord_);
uniq_nom_.resize(nnom_);
cvar_.resize(ncont_ + nord_ + nnom_);
// Pack raw action values.
pack_actions(data->act());
// Scale responses.
scale_response(data->resp(), data->prob());
// Convert variables into cut masks.
set_cont_masks(data);
set_ord_masks(data);
set_nom_masks(data);
// Compute the total number of searches to examine
std::vector<size_t> vidx(data->nvar());
std::iota(vidx.begin(), vidx.end(), 0);
combination(std::vector<size_t>{}, vidx, 'c');
// Resize the search buffer
choices_.resize(total_choices_);
scores_.resize(total_choices_ << depth_);
// Set all the search choices
iter_ = 0;
combination(std::vector<size_t>{}, vidx, 's');
// None of the search scores have been sorted
ntop_ = 0;
index_.resize(scores_.size());
}
void CompSearch::pack_actions(const std::vector<int> &act) {
size_t q = nsample_ >> 3;
size_t r = nsample_ % 8;
act_.resize((r > 0 ? q + 1: q));
// Process the first q batches of actions
for (size_t i = 0; i < q; ++i) {
size_t i8 = i << 3;
act_[i] = (act[i8] << 28) | (act[i8 + 1] << 24) |
(act[i8 + 2] << 20) | (act[i8 + 3] << 16) |
(act[i8 + 4] << 12) | (act[i8 + 5] << 8) |
(act[i8 + 6] << 4) | act[i8 + 7];
}
// Process the remaining bits if r is not 0.
if (r) {
std::uint32_t val = 0;
q <<= 3;
for (size_t i = 0; i < r; ++i)
val |= act[q + i] << (28 - 4 * i);
q >>= 3;
act_[q] = val;
}
}
void CompSearch::scale_response(const std::vector<double> &resp,
const std::vector<double> &prob) {
size_t q = nsample_ >> 3;
int r = static_cast<int>(nsample_ % 8);
resp_.resize(nsample_);
T0_ = 0.0;
// Process the first q batches
for (size_t i = 0; i < q; ++i) {
size_t i8 = i << 3;
auto mask = act_[i];
for (int k = 7; k >= 0; --k) {
resp_[i8 + k] = resp[i8 + k] / prob[i8 + k];
T0_ += resp_[i8 + k] * (1 - (mask & 0xF));
mask >>= 4;
}
}
if (r) {
size_t i8 = q << 3;
auto mask = act_[q];
// Drop the bottom 32 - 4r bits that are all zero
mask >>= (32 - 4 * r);
for (int k = r - 1; k >= 0; --k) {
resp_[i8 + k] = resp[i8 + k] / prob[i8 + k];
T0_ += resp_[i8 + k] * (1 - (mask & 0xF));
mask >>= 4;
}
}
}
void CompSearch::set_cont_masks(const Data *data) {
std::vector<double> mapped(nsample_);
std::vector<size_t> sorted(nsample_);
double scaling_factor = 10.0 / nsample_;
size_t q = nsample_ >> 3;
size_t r = nsample_ % 8;
size_t len = q + (r > 0);
for (size_t i = 0; i < data->ncont(); ++i) {
// Get raw values
const std::vector<double> &raw = data->cont(i);
// Sort the raw values in ascending order
std::iota(sorted.begin(), sorted.end(), 0);
std::sort(sorted.begin(), sorted.end(),
[&raw](size_t i1, size_t i2) { return raw[i1] < raw[i2]; });
// Compute the deciles of the raw values
decile_[i].resize(10);
for (size_t j = 0; j < 9; ++j) {
double val = (j + 1) * nsample_ / 10.0 + 0.5;
// Get the integral and fractional parts
auto k = static_cast<size_t>(val);
double f = val - k;
// Get the values
double xk = raw[sorted[k - 1]];
double xk1 = raw[sorted[k]];
decile_[i][j] = (1 - f) * xk + f * xk1;
}
decile_[i][9] = raw[sorted[nsample_ - 1]];
// Convert the raw values into deciles
for (size_t j = 0; j < nsample_; ++j) {
// The kth component of the variable is in the jth sorted position
auto k = sorted[j];
// The percentile of the kth component is 100 * (j + 1 - 0.5) / nsample.
mapped[k] = (j + 0.5) * scaling_factor;
}
// Compute the overall variable index
size_t vidx = i;
// Each continuous variable has 10 cuts, one for each decile.
for (int value = 1; value <= 10; ++value) {
std::vector<std::uint32_t> mask(len);
for (size_t j = 0; j < q; ++j) {
size_t j8 = j << 3;
std::uint32_t val{0};
// Mask for sample j8 is stored in bits 28-31
// Mask for sample j8 + 1 is stored in bits 24-27
// Mask for sample j8 + 2 is stored in bits 20-23
// Mask for sample j8 + 3 is stored in bits 16-19
// Mask for sample j8 + 4 is stored in bits 12-15
// Mask for sample j8 + 5 is stored in bits 8-11
// Mask for sample j8 + 6 is stored in bits 4-7
// Mask for sample j8 + 7 is stored in bits 0-3
for (size_t k = 0; k < 8; ++k)
val |= (mapped[j8 + k] < value) << (28 - 4 * k);
mask[j] = val;
}
if (r) {
size_t j8 = q << 3;
std::uint32_t val{0};
for (size_t k = 0; k < r; ++k)
val |= (mapped[j8 + k] < value) << (28 - 4 * k);
mask[q] = val;
}
cvar_[vidx].mask.push_back(mask);
cvar_[vidx].value.push_back(value - 1);
}
}
}
void CompSearch::set_ord_masks(const Data *data) {
std::vector<int> mapped(nsample_);
size_t q = nsample_ >> 3;
size_t r = nsample_ % 8;
size_t len = q + (r > 0);
for (size_t i = 0; i < data->nord(); ++i) {
// Get raw values
const std::vector<int> &raw = data->ord(i);
// Collect the unique values of the raw values.
for (auto v : raw)
uniq_ord_[i].insert(v);
// Create a map from the value to its rank within the unique set.
std::map<int, int> reverse_map;
int rank = 0;
for (const auto &v : uniq_ord_[i])
reverse_map.insert(std::make_pair(v, rank++));
// Map the raw values into their ranks within the unique set.
for (size_t j = 0; j < nsample_; ++j)
mapped[j] = reverse_map[raw[j]];
// Compute the overall variable index
size_t vidx = data->ncont() + i;
// The number of cuts for each ordinal variable is the cardinality of the
// unique value set.
for (const auto &value : uniq_ord_[i]) {
std::vector<std::uint32_t> mask(len);
for (size_t j = 0; j < q; ++j) {
size_t j8 = j << 3;
std::uint32_t val{0};
for (size_t k = 0; k < 8; ++k)
val |= (mapped[j8 + k] <= value) << (28 - 4 * k);
mask[j] = val;
}
if (r) {
size_t j8 = q << 3;
std::uint32_t val{0};
for (size_t k = 0; k < r; ++k)
val |= (mapped[j8 + k] <= value) << (28 - 4 * k);
mask[q] = val;
}
cvar_[vidx].mask.push_back(mask);
cvar_[vidx].value.push_back(value);
}
}
}
void CompSearch::set_nom_masks(const Data *data) {
std::vector<int> mapped(nsample_);
size_t q = nsample_ >> 3;
size_t r = nsample_ % 8;
size_t len = q + (r > 0);
for (size_t i = 0; i < data->nnom(); ++i) {
// Get raw values
const std::vector<int> &raw = data->nom(i);
// Collect the unique values of the raw values.
for (auto v : raw)
uniq_nom_[i].insert(v);
// Create a map from the value to its rank within the unique set.
std::map<int, int> reverse_map;
int rank = 0;
for (const auto &v : uniq_nom_[i])
reverse_map.insert(std::make_pair(v, rank++));
// Map the raw values into bitmasks. If the order of value v in the unique
// set is r, then the bitmask is 1 << r. Here, we assume that there are at
// most 31 different unique values.
for (size_t j = 0; j < nsample_; ++j)
mapped[j] = (1 << reverse_map[raw[j]]);
// Compute the overall variable index
size_t vidx = data->ncont() + data->nord() + i;
// The number of cuts for each nominal variable is the number of subsets
// that contain no more than half of the unique values.
// Denote the number of unique values by p. Here, we loop through 0 to
// 2^p. Each iterator is interpreted as a bitmask, where bit j means the jth
// element in the unique set. For each integer, if the number of 1 bits is
// no more than half of p, it then represents a valid cut, and the subset
// consists of the elements corresponding to the 1 bits in the integer.
size_t p = uniq_nom_[i].size();
size_t max = 1u << p;
size_t half = p / 2;
for (size_t value = 0; value < max; ++value) {
std::bitset<32> subset(value);
if (subset.count() <= half) {
std::vector<std::uint32_t> mask(len);
for (size_t j = 0; j < q; ++j) {
size_t j8 = j << 3;
std::uint32_t val{0};
for (size_t k = 0; k < 8; ++k)
val |= ((mapped[j8 + k] & value) > 0) << (28 - 4 * k);
mask[j] = val;
}
if (r) {
size_t j8 = q << 3;
std::uint32_t val{0};
for (size_t k = 0; k < r; ++k)
val |= ((mapped[j8 + k] & value) > 0) << (28 - 4 * k);
mask[q] = val;
}
cvar_[vidx].mask.push_back(mask);
cvar_[vidx].value.push_back(static_cast<int>(value));
}
}
}
}
void CompSearch::combination(std::vector<size_t> curr,
std::vector<size_t> option, char mode) {
// Get the number of variables to choose
size_t nchoices = depth_ - curr.size();
if (nchoices == 0) {
// Need to choose no more
if (mode == 'c') {
count_choices(curr);
} else {
set_choices(curr);
}
} else if (option.size() < nchoices) {
// Does not have enough to choose from
return;
} else {
auto curr1 = curr;
curr1.insert(curr1.end(), option.begin(), option.begin() + 1);
option.erase(option.begin());
// Choose `nchoices` from options
combination(curr, option, mode);
// Choose `nchoices - 1` from options
combination(curr1, option, mode);
}
}
void CompSearch::count_choices(const std::vector<size_t> &choices) {
size_t nchoices = 1;
for (const auto &vidx : choices)
nchoices *= cvar_[vidx].value.size();
total_choices_ += nchoices;
}
void CompSearch::set_choices(const std::vector<size_t> &choices) {
std::vector<size_t> cidx;
std::vector<size_t> cbound;
for (const auto &vidx : choices) {
cidx.push_back(0);
cbound.push_back(cvar_[vidx].value.size());
}
set_choices_helper(choices, cidx, cbound, 0);
}
void CompSearch::set_choices_helper(const std::vector<size_t> &vidx,
std::vector<size_t> cidx,
std::vector<size_t> &cbound,
size_t curr) {
if (cidx[curr] == cbound[curr]) {
return;
} else {
if (curr < depth_ - 1) {
set_choices_helper(vidx, cidx, cbound, curr + 1);
} else {
for (size_t d = 0; d < depth_; ++d) {
choices_[iter_].vidx[d] = vidx[d];
choices_[iter_].cidx[d] = cidx[d];
}
iter_++;
}
cidx[curr]++;
set_choices_helper(vidx, cidx, cbound, curr);
}
}
void CompSearch::run() {
std::vector<std::thread> threads(nthreads_);
for (size_t i = 0; i < nthreads_; ++i)
threads[i] = std::thread(&CompSearch::worker, this, i);
for (auto &th : threads)
th.join();
}
void CompSearch::worker(size_t tid) {
// Compute the range of searches the worker needs to perform.
size_t first = 0, last = 0;
auto nchoice = choices_.size();
auto per_worker = nchoice / nthreads_;
auto remainder = nchoice % nthreads_;
if (tid < remainder) {
first = (per_worker + 1) * tid;
last = first + per_worker + 1;
} else {
first = per_worker * tid + remainder;
last = first + per_worker;
}
size_t stride = 1u << depth_;
for (size_t i = first; i < last; ++i) {
std::vector<double> v(1u << (depth_ + 1), 0.0);
double *ans = scores_.data() + i * stride;
std::vector<const std::uint32_t *> m(depth_);
for (size_t d = 0; d < depth_; ++d) {
auto vidx = choices_[i].vidx[d];
auto cidx = choices_[i].cidx[d];
m[d] = cvar_[vidx].mask[cidx].data();
}
size_t q = nsample_ >> 3;
size_t r = nsample_ % 8;
// We sort data into different buckets based on the values of response and
// cut masks. For example, there are 16 buckets if depth is 3:
// (0000)_2 (0001)_2 (0010)_2 (0011)_2
// (0100)_2 (0101)_2 (0110)_2 (0111)_2
// (1000)_2 (1001)_2 (1010)_2 (1011)_2
// (1100)_2 (1101)_2 (1110)_2 (1111)_2
for (size_t j = 0; j < q; ++j) {
size_t idx = act_[j];
for (size_t d = 0; d < depth_; ++d)
idx += m[d][j] << (depth_ - d);
size_t j8 = j << 3;
for (int k = 7; k >= 0; --k) {
// Read the bottom 4 bits
v[idx & 0xF] += resp_[j8 + k];
// Drop the bottom 4 bits
idx >>= 4;
}
}
if (r) {
size_t idx = act_[q];
for (size_t d = 0; d < depth_; ++d)
idx += m[d][q] << (depth_ - d);
// Drop the bottom bits that are all zero.
idx >>= (32 - 4 * r);
size_t j8 = q << 3;
for (int k = static_cast<int>(r) - 1; k >= 0; --k) {
v[idx & 0xF] += resp_[j8 + k];
idx >>= 4;
}
}
for (size_t j = 0; j < stride; ++j)
ans[j] = v[2 * j + 1] - v[2 * j];
}
}
void CompSearch::report_helper(size_t &ntop) {
// Cap ntop
ntop = std::min(ntop, scores_.size() - 1);
if (ntop > ntop_) {
// We need to sort again
ntop_ = ntop;
std::iota(index_.begin(), index_.end(), 0);
std::partial_sort(index_.begin(), index_.begin() + ntop_, index_.end(),
[&](size_t i1, size_t i2) {
return scores_[i1] > scores_[i2];
});
}
}
rVector CompSearch::score(size_t ntop) {
report_helper(ntop);
rVector retval(ntop);
double scale = 1.0 / nsample_;
for (size_t i = 0; i < ntop; ++i) {
size_t sid = index_[i]; // search id
retval[i] = (T0_ + scores_[sid]) * scale;
}
return retval;
}
uMatrix CompSearch::var(size_t ntop) {
report_helper(ntop);
uMatrix retval(ntop, depth_);
for (size_t i = 0; i < ntop; ++i) {
size_t sid = index_[i]; // search id
size_t cid = sid >> depth_; // choice id
for (size_t d = 0; d < depth_; ++d)
retval(i, d) = choices_[cid].vidx[d];
}
return retval;
}
sVector CompSearch::cut(size_t i) {
report_helper(i);
sVector out(depth_);
#ifdef USE_RCPP
// i is passed through R where array index starts from 1. Need to decrement
// before accessing the value.
i--;
#endif
size_t sid = index_[i]; // search id
size_t cid = sid >> depth_; // choice id
for (size_t d = 0; d < depth_; ++d)
out[d] = cut_val(choices_[cid].vidx[d], choices_[cid].cidx[d]);
return out;
}
sVector CompSearch::dir(size_t i) {
report_helper(i);
sVector out(depth_);
#ifdef USE_RCPP
i--;
#endif
size_t sid = index_[i]; // search id
size_t cid = sid >> depth_; // choice id
size_t mask = sid % (1 << depth_);
for (size_t d = 0; d < depth_; ++d)
out[d] = cut_dir(choices_[cid].vidx[d],
mask & (1u << (depth_ - 1u - d)));
return out;
}
std::string CompSearch::cut_val(size_t vidx, size_t cidx) const {
std::stringstream info;
if (vidx < ncont_) {
info << decile_[vidx][cidx] << " (percentile " << (cidx + 1) * 10 << ")";
} else if (vidx < ncont_ + nord_) {
info << cvar_[vidx].value[cidx] << " (" << cidx + 1 << " out of "
<< uniq_nom_[vidx - ncont_].size() << ")";
} else {
auto subset = cvar_[vidx].value[cidx];
if (subset == 0) {
info << "None";
} else {
unsigned iter = 0;
for (auto const &v : uniq_ord_[vidx - ncont_ - nord_]) {
if (subset & (1 << (iter++)))
info << v << " ";
}
}
}
return info.str();
}
std::string CompSearch::cut_dir(size_t vidx, size_t m) const {
std::string str;
if (vidx < ncont_ + nord_) {
str = (m ? " < " : " >= ");
} else {
str = (m ? " in " : " not in ");
}
return str;
}