-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisopt_tree.h
1135 lines (800 loc) · 36 KB
/
disopt_tree.h
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* disopt_tree.h
* fast-opt
*
*
*/
/* The MIT License
Copyright (c) 2012 John C. Mu.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef DISOPT_TREE_H
#define DISOPT_TREE_H
#include "stl.h"
#include "general_utils.h"
#include "opt_utils.h"
#include "gamma_table.h"
#include "opt_tree.h"
struct dis_tree_node_sparse {
float lphi; // this is also the density if MAP tree
int count; // number of points in this region
// the count is negative if we stop cutting
void init() {
count = -1;
lphi = -c::inf;
}
dis_tree_node_sparse() {
init();
}
dis_tree_node_sparse(int a) {
init();
}
double get_lphi_unif(int depth) {
if (count >= 0) {
return (count * depth * c::l2) -c::l2;
} else {
return -c::inf;
}
}
double get_lphi2(int depth) {
if (!isinf(lphi))return lphi;
else return get_lphi_unif(depth) + c::l2;
}
};
struct dis_working_unit_t {
vector<vector<double> > data;
current_region curr_reg;
opt_region working_reg;
uint32_t node_idx;
dis_working_unit_t(int num_children) {
curr_reg.init(num_children);
working_reg.init(num_children);
node_idx = c::ra_null_val;
}
dis_working_unit_t(vector<vector<double> > &data, current_region &curr_reg
, opt_region &working_reg, uint32_t node_idx) {
this->data = data;
this->curr_reg = curr_reg;
this->working_reg = working_reg;
this->node_idx = node_idx;
}
};
struct dis_pile_t {
vector<dis_working_unit_t> good_regions;
vector<uint32_t> map_nodes;
};
struct dis_mouse_params_t {
int start_dimension;
int num_children;
int count_lim;
int top_depth; // the depth of the parent
int top_count_lim;
int max_depth;
int top_max_depth;
dis_working_unit_t* wup;
opt_region_hash<uint32_t>* region_cache;
gamma_table *gt;
region_allocator<dis_tree_node_sparse> *ra;
int64_t *num_nodes;
int64_t *num_zero_nodes;
pthread_mutex_t* locker;
};
class disopt_tree {
private:
region_allocator<dis_tree_node_sparse> ra;
uint32_t root;
int num_children;
double count_ratio;
double top_count_ratio;
int max_depth;
int top_max_depth;
void init(int num_children, double count_ratio, double top_count_ratio,
int max_depth, int top_max_depth) {
this->num_children = num_children;
pair<uint32_t, dis_tree_node_sparse*> out = ra.create_node(num_children);
root = out.first;
this->count_ratio = count_ratio;
this->top_count_ratio = top_count_ratio;
this->max_depth = max_depth;
this->top_max_depth = top_max_depth;
}
public:
disopt_tree(int num_children, double count_ratio, double top_count_ratio,
int max_depth, int top_max_depth) {
init(num_children, count_ratio, top_count_ratio, max_depth, top_max_depth);
}
disopt_tree(int num_children) {
init(num_children, 0.01, 0.001, 3, num_children * 20);
}
void store_region(uint32_t map_node, opt_region_hash<uint32_t> &map_regions,
region_allocator<map_tree_node> *map_ra, opt_region &working_reg, int count, int depth) {
map_regions.insert(working_reg, map_node);
(*map_ra)[map_node]->set_area(-depth);
(*map_ra)[map_node]->set_count(count);
}
static uint32_t get_child(region_allocator<dis_tree_node_sparse> &ra, opt_region working_reg,
opt_region_hash<uint32_t> ®ion_cache,
int dim, int cut, int count=-1, bool is_diff=true) {
if (!working_reg.cut(dim, cut)) {
cerr << "CANNOT CUT: ";
working_reg.print_region();
cerr << '\n';
exit(2);
}
uint32_t working_hash = region_cache.hash(working_reg);
pair<uint32_t, bool> out = region_cache.find(working_reg, working_hash);
if (out.second) {
return out.first;
} else {
if(count == -1){
cerr << "Fatal Error: Region not found.. " << dim << "," << cut << '\n';
return c::ra_null_val;
}
pair<uint32_t, dis_tree_node_sparse*> out2 = ra.create_node(working_reg.num_children());
out.first = out2.first;
if (is_diff) {
out2.second->count = count;
} else {
out2.second->count = -count;
}
//cerr << "INSERT" << '\n';
region_cache.insert(working_reg, out.first, working_hash);
return out.first;
}
}
static uint32_t get_child(opt_region working_reg,
opt_region_hash<uint32_t> ®ion_cache,
int dim, int cut) {
if (!working_reg.cut(dim, cut)) {
cerr << "CANNOT CUT: ";
working_reg.print_region();
cerr << '\n';
exit(2);
}
uint32_t working_hash = region_cache.hash(working_reg);
pair<uint32_t, bool> out = region_cache.find(working_reg, working_hash);
if (out.second) {
return out.first;
} else {
return c::ra_null_val;
}
}
static void compute_lphi(region_allocator<dis_tree_node_sparse> &ra, opt_region &working_reg,
opt_region_hash<uint32_t> ®ion_cache,
uint32_t curr_node, int depth, gamma_table >, int calling_loc,
vector<vector<double> > &data, current_region &curr_reg, int num_children) {
//cerr << "compute_lphi(" << depth << "): ";
//working_reg.print_region(cerr);
//cerr << '\n';
vector<double> lphi_list; // should pre-allocate
double max_val = (ra[curr_node]->count * depth * c::l2) - c::l2;
lphi_list.push_back(max_val);
//if(calling_loc == 3) cerr << "max_val: " << max_val << '\n';
double ld = -log(num_children) - c::lpi - c::l2;
//if(calling_loc == 3) cerr << "ld: " << ld << '\n';
for (int i = 0; i < num_children; i++) {
uint32_t child_id[2];
child_id[0] = get_child(working_reg, region_cache, i, 0);
child_id[1] = get_child(working_reg, region_cache, i, 1);
// check for null
/*
for (int f = 0; f < c::cuts; f++) {
if (child_id[f] == c::ra_null_val) {
cerr << "(" << calling_loc << ")fully NULL child!!! " << f << "|" << i << ',' << depth << "|"
<< child_id[0] << "," << child_id[1] << '\n';
cerr << "curr_count: " << ra[curr_node]->count << '\n';
for (int j = 0; j < num_children; j++) {
cerr << "child(" << j << "): "
<< get_child(working_reg, region_cache, j, 0) << ","
<< get_child(working_reg, region_cache, j, 1) << "\n";
}
cerr << "reg: ";
working_reg.print_region(cerr);
cerr << '\n';
exit(2);
}
}
*/
int child_1_count = ra[child_id[0]]->count;
int child_2_count = ra[child_id[1]]->count;
if (child_1_count < 0 && child_2_count < 0) {
cerr << "neg count!!! " << i << ',' << depth << '\n';
exit(2);
}
//if(calling_loc == 3){
// cerr << i << ":" << child_1_count << ',' << child_2_count << '\n';
// cerr << "lphi: " << ra[child_id[0]]->get_lphi2(depth + 1) << ","
// << ra[child_id[1]]->get_lphi2(depth + 1) << '\n';
// cerr << "gt: " << gt.compute_lD2(ra[curr_node]->count, child_1_count, child_2_count)<<'\n';
//}
if (child_1_count < 0) {
child_1_count = -child_1_count;
}
if (child_2_count < 0) {
child_2_count = -child_2_count;
}
double val = ld;
val += ra[child_id[0]]->get_lphi2(depth + 1);
val += ra[child_id[1]]->get_lphi2(depth + 1);
val += gt.compute_lD2(ra[curr_node]->count, child_1_count, child_2_count);
//if(calling_loc == 3) cerr << "val: " << val << '\n';
lphi_list.push_back(val);
if (val > max_val) {
max_val = val;
}
}
double lphi = max_val;
double sum = 0;
for (int i = 0; i < (num_children + 1); i++) {
sum += exp(lphi_list[i] - max_val);
}
if (sum > 0) lphi += log(sum);
ra[curr_node]->lphi = lphi;
//if(calling_loc == 3) cerr << "lphi: " << lphi << '\n';
}
static void* small_opt_thread(void* params) {
dis_mouse_params_t* p = (dis_mouse_params_t*) params;
int start_dimension = p->start_dimension;
int top_depth = p->top_depth;
int num_children = p->num_children;
int count_lim = p->count_lim;
//int top_count_lim = p->top_count_lim;
int max_depth = p->max_depth;
int top_max_depth = p->top_max_depth;
dis_working_unit_t* wup = p->wup;
opt_region_hash<uint32_t>* region_cache = p->region_cache;
gamma_table *gt = p->gt;
region_allocator<dis_tree_node_sparse> *ra = p->ra;
int64_t *num_nodes = p->num_nodes;
int64_t *num_zero_nodes = p->num_zero_nodes;
//pthread_mutex_t* locker = p->locker;
vector<pile_t<uint32_t,vector<double> > > pile;
pile.push_back(pile_t<uint32_t,vector<double> > ());
pile[0].node = wup->node_idx;
pile[0].dim = start_dimension;
pile[0].cut = 0;
pile[0].data = wup->data;
current_region curr_reg = wup->curr_reg;
opt_region working_reg = wup->working_reg;
bool done = false;
int depth = 0;
//cerr << "%%%%!!!----dimension" << start_dimension << "\n";
while (!done) {
if (pile.size() == 0) {
done = true;
continue;
}
uint32_t curr_node_idx = pile[depth].node;
//pthread_mutex_lock(locker);
dis_tree_node_sparse curr_node = *((*ra)[curr_node_idx]);
//pthread_mutex_unlock(locker);
bool backup = false;
//cerr << "----\nDEPTH("<< depth <<"): " << (depth + top_depth)
// << " count: "<< curr_node.count << '\n';
//cerr << "START dim:cut --- " << pile[depth].dim << ":" << pile[depth].cut << '\n';
if (curr_node.count <= count_lim
|| (depth + top_depth) >= top_max_depth
|| (depth) >= max_depth
|| working_reg.full()) {
//cerr << "BOTTOM BACKUP\n";
backup = true;
} else {
if ((depth == 0 && pile[depth].dim != start_dimension)
|| pile[depth].dim > num_children - 1) {
//cerr << "CUTALL BACKUP\n";
if (depth != 0) {
compute_lphi(*ra, working_reg, *region_cache,
curr_node_idx, depth + top_depth, *gt,
1, pile[depth].data, curr_reg, num_children);
//cerr << "COMPUTE LPHI: " << (*((*ra)[curr_node_idx])).lphi << '\n';
}
backup = true;
}
}
bool is_diff = true;
if (!backup) {
int curr_dim = pile[depth].dim;
int curr_cut = pile[depth].cut;
//cerr << "NB dim:cut --- " << curr_dim << ":" << curr_cut << '\n';
pile.push_back(pile_t<uint32_t,vector<double> > ());
depth++;
is_diff = cut_region(pile[depth - 1].data, pile[depth].data,
curr_dim, curr_cut, curr_reg.get_lim(curr_dim));
curr_reg.cut(curr_dim, curr_cut);
working_reg.cut(curr_dim, curr_cut);
pile[depth].dim = 0;
pile[depth].cut = 0;
int curr_count = pile[depth].data.size();
uint32_t working_hash = region_cache->hash(working_reg);
//pthread_mutex_lock(locker);
pair<uint32_t, bool> new_node = region_cache->find(working_reg, working_hash);
//pthread_mutex_unlock(locker);
if (!new_node.second) {
//cerr << "not found: " << curr_count << "\n";
// MUTEX
//pthread_mutex_lock(locker);
pair<uint32_t, dis_tree_node_sparse*> out = ra->create_node(num_children);
new_node.first = out.first;
if (is_diff) {
out.second->count = curr_count;
} else {
out.second->count = -curr_count;
}
//cerr << "INSERT" << '\n';
region_cache->insert(working_reg, new_node.first, working_hash);
(*num_nodes)++;
if ((*ra)[new_node.first]->count <= count_lim)(*num_zero_nodes)++;
pile[depth].node = new_node.first;
// UNMUTEX
//pthread_mutex_unlock(locker);
} else {
//cerr << "found node(" << (*ra)[new_node.first]->count << "): " << curr_dim << "," << curr_cut << '\n';
//ra[curr_node]->set_child(curr_dim, curr_cut, new_node.first);
pile[depth].node = new_node.first;
//cerr << "fUNCUT: " << curr_dim << '\n';
}
}
if (backup) {
//cerr << "BACKUP\n";
//pthread_mutex_lock(locker);
// estimate phi
//int curr_count = curr_node.count;
//int curr_depth = (depth + top_depth);
//cerr << "curr_count: " << curr_count << '\n';
//pthread_mutex_unlock(locker);
//cerr << "BEFORE depth: " << depth << '\n';
depth--;
pile.pop_back();
if (depth < 0) continue;
//cerr << "BEFORE dim:cut --- " << pile[depth].dim << ":" << pile[depth].cut << '\n';
//working_reg.print_region();
//cerr << '\n';
//cerr << "AFTER depth: " << depth << '\n';
curr_reg.uncut(pile[depth].dim, pile[depth].cut);
working_reg.uncut(pile[depth].dim);
//working_reg.print_region();
//cerr << '\n';
if (pile[depth].cut < c::cuts - 1) {
pile[depth].cut++;
} else if (pile[depth].dim <= num_children - 1) {
pile[depth].dim++;
pile[depth].cut = 0;
}
//cerr << "AFTER dim:cut --- " << pile[depth].dim << ":" << pile[depth].cut << '\n';
continue;
}
}
return NULL;
}
// compute the discrepancy Z-statistic
// This is the symmetric discrepancy
// Need to rescale the data to [0,1]^d cube
double compute_dis(dis_working_unit_t &w) {
int n = (int) w.data.size();
if (n == 0) {
return 0;
}
int d = w.data[0].size();
// min,range
vector<pair<double,double> > lims = w.curr_reg.get_limits2();
double U1 = 0;
for (int k = 0; k < n; k++) {
double pow_temp = 1;
for (int j = 0; j < d; j++) {
double val = (w.data[k][j]-lims[j].first)/lims[j].second;
pow_temp *= (1 + 2 * val - 2 * val * val);
}
U1 += pow_temp;
}
U1 = U1 / (double) n;
int lim = n; // this may be a limit for the quadratic part later
double U2 = 0;
for (int k = 0; k < lim; k++) {
for (int l = k + 1; l < lim; l++) {
double pow_temp = 1;
for (int j = 0; j < d; j++) {
pow_temp *= (1 - fabs((w.data[k][j] - w.data[l][j])/lims[j].second));
}
U2 += pow_temp;
}
}
U2 *= (pow((int)2,(double)d+1))/((double)lim*(lim-1));
const double M = 4 / (double)3;
double x1 = pow((9 / (double)5),(double)d) - pow((6 / (double)9),(double)d);
//double x2 = pow((int)2,(double)d) - pow((16 / (double)9),(double)d);
double temp_val = pow(M,(double)d);
double A = (sqrt(n)*(U1 - temp_val)+sqrt(lim)*2*(U2-temp_val))/(5*sqrt(x1));
return A;
}
// Choose the cut based on discrepancy
int compute_dis_cut(dis_working_unit_t &w) {
int n = (int) w.data.size();
if (n == 0) {
return 0;
}
int d = w.data[0].size();
// min,range
vector<pair<double,double> > lims = w.curr_reg.get_limits2();
int best_d = -1;
int min_score = 1000; // this is pretty big :)
for (int dim = 0; dim < d; dim++) {
double div = lims[dim].first + lims[dim].second/(double)2;
int counts[2] = {0,0};
double U1[2] = {0.0,0.0};
for (int k = 0; k < n; k++) {
int idx = 0;
if(w.data[k][dim] > div){
idx = 1;
}
counts[idx]++;
double pow_temp = 1;
for (int j = 0; j < d; j++) {
double val = 0;
if (j == dim) {
double res = (lims[j].second/2.0);
if (idx == 0) {
val = (w.data[k][j] - lims[j].first) / (res);
} else {
val = (w.data[k][j] - lims[j].first - res) / (res);
}
} else {
val = (w.data[k][j] - lims[j].first) / lims[j].second;
}
pow_temp *= (1 + 2 * val - 2 * val * val);
}
U1[idx] += pow_temp;
}
for(int i = 0;i<2;i++){
if(counts[i]>1) U1[i] = U1[i] / (double) counts[i];
}
double U2[2] = {0.0,0.0};
for (int k = 0; k < n; k++) {
int idx = 0;
if(w.data[k][dim] > div){
idx = 1;
}
for (int l = k + 1; l < n; l++) {
if (w.data[l][dim] > div && idx == 0) {
continue;
}else if(w.data[l][dim] <= div && idx == 1){
continue;
}
double pow_temp = 1;
for (int j = 0; j < d; j++) {
double div_val = lims[j].second;
if(j == dim){
div_val = div_val/2.0;
}
pow_temp *= (1 - fabs((w.data[k][j] - w.data[l][j])/div_val));
}
U2[idx] += pow_temp;
}
}
for (int i = 0; i < 2; i++) {
if(counts[i]>1) {
U2[i] *= (pow((int) 2, (double) d + 1))/((double) counts[i] * (counts[i] - 1));
}
}
const double M = 4 / (double) 3;
double x1 = pow((9 / (double) 5), (double) d) - pow((6 / (double) 9), (double) d);
//double x2 = pow((int) 2, (double) d) - pow((16 / (double) 9), (double) d);
double temp_val = pow(M, (double) d);
double A[2] = {0.0, 0.0};
for (int i = 0; i < 2; i++) {
if (counts[i] > 1) {
double temp_val2 = sqrt(counts[i]);
A[i] = (temp_val2*(U1[i] - temp_val) + temp_val2*2 * (U2[i] - temp_val)) / (5 * sqrt(x1));
}
}
//double max_A = max(A[0],A[1]);
double max_A = min(A[0],A[1]); // try the min
if(max_A < min_score){
min_score = max_A;
best_d = dim;
}
}
return best_d;
}
void do_small_opt(dis_working_unit_t &w,
opt_region_hash<uint32_t> ®ion_cache, gamma_table >,
MT_random &rand_gen,
int64_t &num_nodes, int64_t &num_zero_nodes, int start_depth,
int top_count_lim) {
int count_lim = (int) floor(w.data.size() * count_ratio);
if (count_lim < top_count_lim) count_lim = top_count_lim;
//pthread_t* t_group = new pthread_t[num_children]; // just do 3 threads for now :)
//pthread_mutex_t* locker = new pthread_mutex_t();
//pthread_mutex_init(locker, NULL);
dis_mouse_params_t* params = new dis_mouse_params_t[num_children];
for (int d = 0; d < num_children; d++) {
params[d].count_lim = count_lim;
params[d].top_count_lim = top_count_lim;
params[d].gt = >
//params[d].locker = locker;
params[d].max_depth = max_depth;
params[d].top_max_depth = top_max_depth;
params[d].num_children = num_children;
params[d].num_nodes = &num_nodes;
params[d].num_zero_nodes = &num_zero_nodes;
params[d].ra = &ra;
params[d].region_cache = ®ion_cache;
params[d].start_dimension = d;
params[d].top_depth = start_depth;
params[d].wup = &w;
}
for (int d = 0; d < num_children; d++) {
// Start threads
small_opt_thread((void*) &(params[d]));
//pthread_create(&(t_group[d]), NULL, small_opt_thread, (void*) &(params[d]));
// no threading for now
//}
// wait for threads
//for (int d = 0; d < num_children; d++) {
//void* output;
//pthread_join(t_group[d], &output);
}
//delete locker;
delete [] params;
//delete [] t_group;
}
int get_map_dim(dis_working_unit_t &w, opt_region_hash<uint32_t> ®ion_cache, gamma_table >,
int map_depth) {
// Choose MAP dimension
int map_dim = 0;
vector<uint32_t>child_idx_0(num_children);
vector<uint32_t>child_idx_1(num_children);
for (int i = 0; i < num_children; i++) {
if (w.working_reg.full()) {
w.working_reg.print_region();
cerr << "\n";
}
child_idx_0[i] = get_child(w.working_reg, region_cache, i, 0);
child_idx_1[i] = get_child(w.working_reg, region_cache, i, 1);
}
double max_post_prob = -c::inf;
for (int i = 0; i < num_children; i++) {
double post_prob = gt.compute_lD2(ra[w.node_idx]->count
, ra[child_idx_0[i]]->count, ra[child_idx_1[i]]->count);
post_prob += ra[child_idx_0[i]]->get_lphi2(map_depth + 1);
post_prob += ra[child_idx_1[i]]->get_lphi2(map_depth + 1);
//#ifdef DEBUG_MAP2
//cerr << "gt: " << gt.compute_lD2(ra[wit->node_idx]->count
// , ra[child_idx_0[i]]->count, ra[child_idx_1[i]]->count) <<'\n';
//cerr << "lphi_3_0: " << ra[child_idx_0[i]]->get_lphi3(map_depth + 1) << '\n';
//cerr << "lphi_3_1: " << ra[child_idx_1[i]]->get_lphi3(map_depth + 1) << '\n';
//cerr << "post_prob[" << ra[child_idx_0[i]]->count << "|" << ra[child_idx_1[i]]->count << "](" << i << ") = " << post_prob << '\n';
//#endif
if (post_prob > max_post_prob) {
map_dim = i;
max_post_prob = post_prob;
}
}
//#ifdef DEBUG_MAP2
//cerr << map_dim << ',';
//#endif
return map_dim;
}
void construct_disopt_tree(vector<vector<double> > &data,
map_tree &map_region_tree, opt_region_hash<uint32_t> &map_regions,
bool prune_tree) {
MT_random rand_gen;
// need to treat the empty tree/data case
int64_t num_nodes = 0;
int64_t num_zero_nodes = 0;
int N = data.size();
int count_lim = 0;
if (top_count_ratio < 1) {
count_lim = (int) floor((double) N * top_count_ratio); // this is for the whole tree
} else {
count_lim = (int) top_count_ratio;
}
if (count_lim < 1)count_lim = 1;
cerr << "Full tree stopping at " << count_lim << " points\n";
cerr << "Each look-ahead stopping at " << max_depth << " levels\n";
cerr << "Full tree stopping at " << top_max_depth << " levels\n";
gamma_table gt(N);
ra[root]->count = N;
region_allocator<map_tree_node> *map_ra = map_region_tree.get_ra();
opt_region_hash<uint32_t> region_cache(25);
current_region start_region(num_children);
opt_region start_working(num_children);
vector<dis_pile_t> llpile;
llpile.push_back(dis_pile_t());
llpile[0].good_regions.push_back(dis_working_unit_t(data, start_region,
start_working, root));
llpile[0].map_nodes.push_back(map_region_tree.get_full_tree());
int map_depth = 0;
// Now create the MAP tree!
// Do a breadth first search on the MAP tree
bool done = false;
int count = 1;
while (!done) {
// if no good regions we are done!
if (llpile.size() == 0) {
done = true;
continue;
}
if (llpile[map_depth].good_regions.size() == 0) {
llpile.pop_back();
map_depth--;
continue;
}
vector<dis_working_unit_t>::iterator wu_it = (llpile[map_depth].good_regions.end() - 1);
vector<uint32_t>::iterator map_node_it = (llpile[map_depth].map_nodes.end() - 1);
// if too deep we stop all the regions
if (map_depth >= top_max_depth || (int) wu_it->data.size() <= count_lim
|| wu_it->working_reg.full()) {
cerr << "mini-STOP count: " << ra[wu_it->node_idx]->count << '\n';
store_region(*map_node_it, map_regions, map_ra, wu_it->working_reg,
ra[wu_it->node_idx]->count, map_depth);
llpile[map_depth].good_regions.pop_back();
llpile[map_depth].map_nodes.pop_back();
count++;
continue;
}
// for each good region
int curr_data_size = wu_it->data.size();
cerr << "Good [" << map_depth << "](" << count << "/"
<< llpile[map_depth].good_regions.size()
<< "), Data size: " << curr_data_size << '\n';
// If there are many points in the region, use discrepancy to
// determine whether to cut
bool dis_non_unif = false; // not uniform as determined by discrepancy
int dis_cut_dim = -1;
// Compute discrepancy
if (curr_data_size > 1000) {
double A = compute_dis(*wu_it);
// If discrepancy says not to cut, we run OPT. Otherwise, we
// simply use the discrepancy of the sub-regions to determine
// the best cut
if (A > 4){
cerr << "Cut by discrepancy! A: " << A << '\n';
// we cut!
dis_non_unif = true;
// choose the cut which is most uniform
dis_cut_dim = compute_dis_cut(*wu_it);
}
}
if(!dis_non_unif) {
// do a smaller OPT for the good region to determine which
// dimension to cut
do_small_opt(*wu_it,
region_cache, gt, rand_gen,
num_nodes, num_zero_nodes, map_depth, count_lim);
cerr << "\nNodes:" << num_nodes
<< " : " << num_zero_nodes
<< " : " << (num_nodes - num_zero_nodes) << '\n';
compute_lphi(ra, wu_it->working_reg, region_cache,
wu_it->node_idx, map_depth, gt, 3, wu_it->data, wu_it->curr_reg, num_children);
cerr << "lphi: " << ra[wu_it->node_idx]->get_lphi2(map_depth) << "\n";
}
// compute the posterior pho and decide if we stop
double post_rho = -c::l2;
if(!dis_non_unif) {
post_rho += map_depth * c::l2 * ra[wu_it->node_idx]->count; // phi_0
post_rho -= ra[wu_it->node_idx]->get_lphi2(map_depth);
}
// if not stop we split and add the two regions to the good region list
if (!dis_non_unif && post_rho>-c::l2) {
// STOP
cerr << "STOP count: " << ra[wu_it->node_idx]->count << '\n';
store_region(*map_node_it, map_regions, map_ra, wu_it->working_reg,
ra[wu_it->node_idx]->count, map_depth);
llpile[map_depth].good_regions.pop_back();
llpile[map_depth].map_nodes.pop_back();
count++;
} else {
// Choose MAP dimension
cerr << "MAP dim: ";
int map_dim = -1;
if (!dis_non_unif) {
map_dim = get_map_dim(*wu_it, region_cache, gt, map_depth);
} else {
map_dim = dis_cut_dim;
}
cerr << '\n';