-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.cc
168 lines (141 loc) · 2.8 KB
/
node.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
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
/*
* Code by Chris Mansley
*/
#include <iostream>
#include <cmath>
#include <limits>
#include <algorithm>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include "node.hh"
#include "hoo.hh"
/*
*
*/
Node::Node(HOO *h) : hoo(h), left(NULL), right(NULL), mu(0), nsamples(0), depth(0), Sflag(true)
{
Bval = std::numeric_limits<double>::infinity();
}
/*
*
*/
Action Node::sampleAction()
{
Action a;
/* Grab sample from space */
for(int i=0; i<hoo->numDim; i++) {
a.push_back( gsl_ran_flat(hoo->rng, minRange[i], maxRange[i]) );
}
return a;
}
/*
*
*/
void Node::split()
{
/* Poor check (should throw error or something) */
if(isLeaf()) {
/* Generate children */
left = new Node(hoo);
right = new Node(hoo);
/* Pick splitting dimension */
splitDim = depth % hoo->numDim;
/* Grab split point */
splitValue = (maxRange[splitDim] - minRange[splitDim]) / 2.0 + minRange[splitDim];
/* Intialize nodes (expensive?) */
left->depth = right->depth = depth + 1;
left->maxRange = right->maxRange = maxRange;
left->minRange = right->minRange = minRange;
/* Split childrens ranges */
left->maxRange[splitDim] = splitValue;
right->minRange[splitDim] = splitValue;
}
}
/*
*
*/
double Node::rebuildSubTree()
{
/* If in set S, return B-value */
if(inS()) {
return Bval;
}
/* Compute values */
double U = mu + sqrt(2.0*log(hoo->totalSamples)/nsamples) + hoo->v1*pow(hoo->rho, depth);
/* Compute this node's B-value */
double b1 = left->rebuildSubTree();
double b2 = right->rebuildSubTree();
Bval = std::min(U, std::max(b1, b2));
return Bval;
}
/*
*
*/
void Node::bestAction(Action &a, bool greedy)
{
/* Terminate and sample from best node */
if(inS()) {
a = sampleAction();
return;
}
if(!greedy) {
/* Follow B-values down tree */
if(left->Bval < right->Bval)
right->bestAction(a, greedy);
else
left->bestAction(a, greedy);
} else {
/* Follow mu down tree */
if(left->mu < right->mu)
right->bestAction(a, greedy);
else
left->bestAction(a, greedy);
}
}
/*
*
*/
void Node::insertValue(Action a, double q)
{
/* Update the number of samples and mean of returns in this
subtree */
nsamples++;
mu = mu + (q-mu)/nsamples;
/* If in the set S, recursion is done */
if(inS()) {
Sflag = false;
split();
return;
}
/* Move down tree */
if(a[splitDim] < splitValue) {
left->insertValue(a, q);
} else {
right->insertValue(a, q);
}
}
/*
*
*/
void Node::clear()
{
if(isLeaf()) {
return;
}
left->clear();
right->clear();
delete left;
delete right;
}
/*
*
*/
void Node::print()
{
if(inS()) {
return;
}
std::cout << depth << " " << splitValue << std::endl;
left->print();
right->print();
}