-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregressor.h
244 lines (215 loc) · 5.86 KB
/
regressor.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
/**
* \file
* \brief Defines the Regressor interface for representing Q-functions
* in FQI.
*
* Copyright (c) 2008-2014 Robert D. Vincent.
*/
/**
* Utility class for dealing with random tie-breaking in the policy.
*/
class TieBreaker {
int nties; /*!< Number of ties we've broken. */
public:
TieBreaker() { nties = 1; }
/**
* Returns true if we should randomly break the tie at this point.
*/
bool tie() {
nties += 1;
return rnd1() < (1.0 / nties);
}
};
/**
* Abstract class for a Q-function regressor.
*/
class Regressor {
public:
const int numActions; /*!< Number of actions */
/**
* Create an instance of a regressor that will represent the Q-function
* in the RL environment.
* \param na The number of actions in the RL environment.
*/
Regressor(int na): numActions(na) {
}
/**
* Virtual destructor.
*/
virtual ~Regressor() {}
/**
* Select the best action for the given state.
* \param s The state we want to evaluate.
*/
virtual int bestAction(const vector<double> &s) const = 0;
/**
* Return the best Q-value for the given state.
* \param s The state we want to evaluate.
*/
virtual double bestQvalue(const vector<double> &s) const = 0;
/**
* Train the regressor.
* \param ts The training dataset.
* \param updateOnly If true, freeze the tree structure and tests,
* updating the leaf values only.
*/
virtual void train(const vector<dataset> &ts, bool updateOnly) = 0;
/**
* Return the mean Q-value for the entire training set. Used for
* general bookkeeping and early termination calculations.
* \param ts The training dataset.
*/
virtual double meanQvalue(const vector<dataset> &ts) const = 0;
/**
* Return true if the regressor is "compound", that is, if it uses
* a separate substructure to represent each discrete action.
*/
virtual bool compound() const = 0;
};
/**
* Q-function regressor implemented with extremely randomized trees.
* This is the compound version that uses a separate forest to represent
* the Q-values for each of the possible actions.
*/
class ExtraTreeRegressor: public Regressor {
vector<ExtraTree> *regressor; /*!< A vector of extremely randomized trees. */
public:
/**
* Construct an ExtraTreeRegressor.
* \param na Number of actions.
* \param nd Number of dimensions.
* \param M Number of trees in regressor.
* \param nmin Minimum size of splittable node.
*/
ExtraTreeRegressor(int na, int nd, size_t M, size_t nmin): Regressor(na) {
ExtraTree et(nd, M, nmin);
regressor = new vector<ExtraTree>(na, et);
}
/**
* Destroy an ExtraTreeRegressor
*/
virtual ~ExtraTreeRegressor() {
delete regressor;
}
/**
* \return true
*/
bool compound() const { return true; }
int bestAction(const vector<double> &s) const {
double max_q = -DBL_MAX;
int max_n = 0;
TieBreaker tb;
for (int n = 0; n < numActions; n++) {
double q = (*regressor)[n].output(s);
if (q > max_q || (q == max_q && tb.tie())) {
max_q = q;
max_n = n;
}
}
return max_n;
}
double bestQvalue(const vector<double> &s) const {
double max_q = -DBL_MAX;
for (int a = 0; a < numActions; a++) {
double q = (*regressor)[a].output(s);
if (q > max_q) {
max_q = q;
}
}
return max_q;
}
double meanQvalue(const vector<dataset> &ts) const {
double q = 0.0;
int n = 0;
for (int a = 0; a < numActions; a++) {
for (size_t j = 0; j < ts[a].size(); j++) {
q += (*regressor)[a].output(ts[a].data[j].attributes);
n += 1;
}
}
return (q / n);
}
void train(const vector<dataset> &ts, bool updateOnly) {
for (int a = 0; a < numActions; a++) {
(*regressor)[a].train(ts[a], updateOnly);
}
}
};
/**
* Q-function regressor implemented with extremely randomized trees.
* This version uses a single tree to represent the joint state-action
* space.
*/
class SingleETRegressor: public Regressor {
ExtraTree *regressor; /*!< The single forest. */
int nd; /*!< The number of dimensions. */
public:
/** Create a SingleETRegressor.
* \param na Number of actions.
* \param nd Number of dimensions.
* \param M Number of trees per forest.
* \param nmin Smallest splittable node.
*/
SingleETRegressor(int na, int nd, size_t M, size_t nmin): Regressor(na) {
regressor = new ExtraTree(nd+1, M, nmin);
this->nd = nd;
}
virtual ~SingleETRegressor() {
delete regressor;
}
bool compound() const { return false; }
/**
* \param s The state to evaluate.
*/
int bestAction(const vector<double> &s) const {
double max_q = -DBL_MAX;
int max_a = 0;
TieBreaker tb;
vector<double> v = s;
v.push_back(0);
for (int a = 0; a < numActions; a++) {
v[nd] = a;
double q = regressor->output(v);
if (q > max_q || (q == max_q && tb.tie())) {
max_q = q;
max_a = a;
}
}
return max_a;
}
/**
* \param s The state to evaluate.
*/
double bestQvalue(const vector<double> &s) const {
double max_q = -DBL_MAX;
vector<double> v = s;
v.push_back(0);
for (int a = 0; a < numActions; a++) {
v[nd] = a;
double q = regressor->output(v);
if (q > max_q) {
max_q = q;
}
}
return max_q;
}
/**
* \param ts The training dataset.
*/
double meanQvalue(const vector<dataset> &ts) const {
double q = 0.0;
int n = 0;
for (size_t j = 0; j < ts[0].size(); j++) {
double t = regressor->output(ts[0].data[j].attributes);
//if (t < -1.0) {
//cout << t << " " << ts[0].data[j].attributes << " " << ts[0].data[j].output << endl;
//}
q += t;
n += 1;
}
return (q / n);
}
void train(const vector<dataset> &ts, bool updateOnly) {
regressor->train(ts[0], updateOnly);
}
};