-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tiles.h
294 lines (256 loc) · 9.83 KB
/
Tiles.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
/*
* Copyright 2014 Saminda Abeyruwan ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* External documentation and recommendations on the use of this code is
* available at http://www.cs.umass.edu/~rich/tiles.html.
*
* This is an implementation of grid-style tile codings, based originally on
* the UNH CMAC code (see http://www.ece.unh.edu/robots/cmac.htm).
* Here we provide a procedure, "GetTiles", that maps floating-point and integer
* variables to a list of tiles. This function is memoryless and requires no
* setup. We assume that hashing colisions are to be ignored. There may be
* duplicates in the list of tiles, but this is unlikely if memory-size is
* large.
*
* The floating-point input variables will be gridded at unit intervals, so generalization
* will be by 1 in each direction, and any scaling will have
* to be done externally before calling tiles. There is no generalization
* across integer values.
*
* It is recommended by the UNH folks that num-tilings be a power of 2, e.g., 16.
*
* We assume the existence of a function "rand()" that produces successive
* random integers, of which we use only the low-order bytes.
*
* Modified by: Saminda Abeyruwan
* To be used as a single header file.
*/
#ifndef _TILES_H_
#define _TILES_H_
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include "Vector.h"
#include "Hashing.h"
namespace RLLib
{
template<class T>
class Tiles
{
protected:
int qstate[Hashing<T>::MAX_NUM_VARS];
int base[Hashing<T>::MAX_NUM_VARS];
int wrap_widths_times_num_tilings[Hashing<T>::MAX_NUM_VARS];
int coordinates[Hashing<T>::MAX_NUM_VARS * 2 + 1]; /* one interval number per relevant dimension */
Hashing<T>* hashing; /*The has function*/
Vector<int>* i_tmp_arr;
Vector<T>* f_tmp_arr;
public:
Tiles(Hashing<T>* hashing) :
hashing(hashing), i_tmp_arr(new PVector<int>(Hashing<T>::MAX_NUM_VARS)), f_tmp_arr(
new PVector<T>(Hashing<T>::MAX_NUM_VARS))
{
}
~Tiles()
{
delete i_tmp_arr;
delete f_tmp_arr;
}
void tiles(Vector<T>* the_tiles, // provided array contains returned tiles (tile indices)
int num_tilings, // number of tile indices to be returned in tiles
const Vector<T>* floats, // array of floating point variables
int num_floats, // number of active floating point variables
const Vector<int>* ints, // array of integer variables
int num_ints) // number of integer variables
{
int i, j;
int num_coordinates = num_floats + num_ints + 1;
for (int i = 0; i < num_ints; i++)
coordinates[num_floats + 1 + i] = ints->getEntry(i);
/* quantize state to integers (henceforth, tile widths == num_tilings) */
for (i = 0; i < num_floats; i++)
{
qstate[i] = (int) floor(floats->getEntry(i) * num_tilings);
base[i] = 0;
}
/*compute the tile numbers */
for (j = 0; j < num_tilings; j++)
{
/* loop over each relevant dimension */
for (i = 0; i < num_floats; i++)
{
/* find coordinates of activated tile in tiling space */
if (qstate[i] >= base[i])
coordinates[i] = qstate[i] - ((qstate[i] - base[i]) % num_tilings);
else
coordinates[i] = qstate[i] + 1 + ((base[i] - qstate[i] - 1) % num_tilings)
- num_tilings;
/* compute displacement of next tiling in quantized space */
base[i] += 1 + (2 * i);
}
/* add additional indices for tiling and hashing_set so they hash differently */
coordinates[i] = j;
the_tiles->setEntry(hashing->hash(coordinates, num_coordinates), 1.0f);
}
}
void tiles(Vector<T>* the_tiles, // provided array contains returned tiles (tile indices)
int num_tilings, // number of tile indices to be returned in tiles
const Vector<T>* floats, // array of floating point variables
const Vector<int>* ints, // array of integer variables
int num_ints) // number of integer variables
{
tiles(the_tiles, num_tilings, floats, floats->dimension(), ints, num_ints);
}
// No ints
void tiles(Vector<T>* the_tiles, int nt, const Vector<T>* floats)
{
tiles(the_tiles, nt, floats, i_tmp_arr, 0);
}
//one int
void tiles(Vector<T>* the_tiles, int nt, const Vector<T>* floats, int h1)
{
i_tmp_arr->setEntry(0, h1);
tiles(the_tiles, nt, floats, i_tmp_arr, 1);
}
// two ints
void tiles(Vector<T>* the_tiles, int nt, const Vector<T>* floats, int h1, int h2)
{
i_tmp_arr->setEntry(0, h1);
i_tmp_arr->setEntry(1, h2);
tiles(the_tiles, nt, floats, i_tmp_arr, 2);
}
// three ints
void tiles(Vector<T>* the_tiles, int nt, const Vector<T>* floats, int h1, int h2, int h3)
{
i_tmp_arr->setEntry(0, h1);
i_tmp_arr->setEntry(1, h2);
i_tmp_arr->setEntry(2, h3);
tiles(the_tiles, nt, floats, i_tmp_arr, 3);
}
// one float, No ints
void tiles1(Vector<T>* the_tiles, int nt, const T& f1)
{
f_tmp_arr->setEntry(0, f1);
tiles(the_tiles, nt, f_tmp_arr, 1, i_tmp_arr, 0);
}
// one float, one int
void tiles1(Vector<T>* the_tiles, int nt, const T& f1, int h1)
{
f_tmp_arr->setEntry(0, f1);
i_tmp_arr->setEntry(0, h1);
tiles(the_tiles, nt, f_tmp_arr, 1, i_tmp_arr, 1);
}
// one float, two ints
void tiles1(Vector<T>* the_tiles, int nt, const T& f1, int h1, int h2)
{
f_tmp_arr->setEntry(0, f1);
i_tmp_arr->setEntry(0, h1);
i_tmp_arr->setEntry(1, h2);
tiles(the_tiles, nt, f_tmp_arr, 1, i_tmp_arr, 2);
}
// one float, three ints
void tiles1(Vector<T>* the_tiles, int nt, const T& f1, int h1, int h2, int h3)
{
f_tmp_arr->setEntry(0, f1);
i_tmp_arr->setEntry(0, h1);
i_tmp_arr->setEntry(1, h2);
i_tmp_arr->setEntry(2, h3);
tiles(the_tiles, nt, f_tmp_arr, 1, i_tmp_arr, 3);
}
// two floats, No ints
void tiles2(Vector<T>* the_tiles, int nt, const T& f1, const T& f2)
{
f_tmp_arr->setEntry(0, f1);
f_tmp_arr->setEntry(1, f2);
tiles(the_tiles, nt, f_tmp_arr, 2, i_tmp_arr, 0);
}
// two floats, one int
void tiles2(Vector<T>* the_tiles, int nt, const T& f1, const T& f2, int h1)
{
f_tmp_arr->setEntry(0, f1);
f_tmp_arr->setEntry(1, f2);
i_tmp_arr->setEntry(0, h1);
tiles(the_tiles, nt, f_tmp_arr, 2, i_tmp_arr, 1);
}
// two floats, two ints
void tiles2(Vector<T>* the_tiles, int nt, const T& f1, const T& f2, int h1, int h2)
{
f_tmp_arr->setEntry(0, f1);
f_tmp_arr->setEntry(1, f2);
i_tmp_arr->setEntry(0, h1);
i_tmp_arr->setEntry(1, h2);
tiles(the_tiles, nt, f_tmp_arr, 2, i_tmp_arr, 2);
}
// two floats, three ints
void tiles2(Vector<T>* the_tiles, int nt, const T& f1, const T& f2, int h1, int h2, int h3)
{
f_tmp_arr->setEntry(0, f1);
f_tmp_arr->setEntry(1, f2);
i_tmp_arr->setEntry(0, h1);
i_tmp_arr->setEntry(1, h2);
i_tmp_arr->setEntry(2, h3);
tiles(the_tiles, nt, f_tmp_arr, 2, i_tmp_arr, 3);
}
void tileswrap(Vector<T>* the_tiles, // provided array contains returned tiles (tile indices)
int num_tilings, // number of tile indices to be returned in tiles
const Vector<T>* floats, // array of floating point variables
int num_floats, // number of active floating point variables
int wrap_widths[], // array of widths (length and units as in floats)
const Vector<int>* ints, // array of integer variables
int num_ints) // number of integer variables
{
int i, j;
int num_coordinates = num_floats + num_ints + 1;
for (int i = 0; i < num_ints; i++)
coordinates[num_floats + 1 + i] = ints->getEntry(i);
/* quantize state to integers (henceforth, tile widths == num_tilings) */
for (i = 0; i < num_floats; i++)
{
qstate[i] = (int) floor(floats->getEntry(i) * num_tilings);
base[i] = 0;
wrap_widths_times_num_tilings[i] = wrap_widths[i] * num_tilings;
}
/*compute the tile numbers */
for (j = 0; j < num_tilings; j++)
{
/* loop over each relevant dimension */
for (i = 0; i < num_floats; i++)
{
/* find coordinates of activated tile in tiling space */
if (qstate[i] >= base[i])
coordinates[i] = qstate[i] - ((qstate[i] - base[i]) % num_tilings);
else
coordinates[i] = qstate[i] + 1 + ((base[i] - qstate[i] - 1) % num_tilings)
- num_tilings;
if (wrap_widths[i] != 0)
coordinates[i] = coordinates[i] % wrap_widths_times_num_tilings[i];
if (coordinates[i] < 0)
{
while (coordinates[i] < 0)
coordinates[i] += wrap_widths_times_num_tilings[i];
}
/* compute displacement of next tiling in quantized space */
base[i] += 1 + (2 * i);
}
/* add additional indices for tiling and hashing_set so they hash differently */
coordinates[i] = j;
the_tiles->setEntry(hashing->hash(coordinates, num_coordinates), 1.0f);
}
return;
}
};
} // namespace RLLib
#endif