-
Notifications
You must be signed in to change notification settings - Fork 0
/
TileCoding.py
252 lines (214 loc) · 9.53 KB
/
TileCoding.py
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
'''
Created on Jun 3, 2016
@author: mgomrokchi
'''
import random
import math
import operator
_maxnumfloats = 20 # maximum number of variables used in one grid
_maxLongint = 2147483647 # maximum integer
_maxLongintBy4 = int(_maxLongint / 4) # maximum integer divided by 4
_randomTable = [random.randrange(_maxLongintBy4) for i in range(2048)] # table of random numbers
# The following are temporary variables used by tiles.
_qstate = [0 for i in range(_maxnumfloats)]
_base = [0 for i in range(_maxnumfloats)]
_safteydict = {'unsafe':0, 'safe':1, 'super safe':2}
_UNSAFE = 0
_SAFE = 1
_SUPER_SAFE = 2
class CollisionTable:
"Structure to handle collisions"
def __init__(self, sizeval=2048, safetyval='safe'):
# if not power of 2 error
if not powerOf2(sizeval):
print("error - size should be a power of 2")
self.size = sizeval
self.safety = _safteydict[safetyval] # one of 'safe', 'super safe' or 'unsafe'
self.calls = 0
self.clearhits = 0
self.collisions = 0
self.data = [-1 for i in range(self.size)]
def __str__(self):
#print("Prepares a string for printing whenever this object is printed")
return "Collision table: " + \
" Safety : " + str(self.safety) + \
" Usage : " + str(self.usage()) + \
" Size :" + str(self.size) + \
" Calls : "+ str(self.calls) + \
" Collisions : " + str(self.collisions)
def print_ (self):
#"Prints info about collision table"
print ("usage: " + str(self.usage())+ " size: "+ str(self.size)+ " calls: "+str(self.calls)+ " clearhits: " + str(self.clearhits)+" collisions: "+ str(self.collisions)+ " safety: "+ str(self.safety))
def reset (self):
#"Reset Ctable values"
self.calls = 0
self.clearhits = 0
self.collisions = 0
self.data = [-1 for i in range(self.size)]
def stats (self):
#"Return some statistics of the usage of the collision table"
return self.calls, self.clearhits, self.collisions, self.usage
def usage (self):
#"count how many entries in the collision table are used"
use = 0
for d in self.data:
if d >= 0:
use += 1
return use
def startTiles (coordinates, numtilings, floats, ints=[]):
#"Does initial assignments to _coordinates, _base and _qstate for both GetTiles and LoadTiles"
global _base, _qstate
numfloats = len(floats)
i = numfloats + 1 # starting place for integers
for v in ints: # for each integer variable, store it
coordinates[i] = v
i += 1
i = 0
for float in floats: # for real variables, quantize state to integers
_base[i] = 0
_qstate[i] = int(math.floor(float * numtilings))
i += 1
def fixcoord (coordinates, numtilings, numfloats, j):
#"Fiddles with _coordinates and _base - done once for each tiling"
global _base, _qstate
for i in range(numfloats): # for each real variable
if _qstate[i] >= _base[i]:
coordinates[i] = _qstate[i] - ((_qstate[i] - _base[i]) % numtilings)
else:
coordinates[i] = _qstate[i]+1 + ((_base[i] - _qstate[i] - 1) % numtilings) - numtilings
_base[i] += 1 + (2*i)
#_hashnum[i] = _randomTable[(coordinates[i] + _increment[i]) & 2047]
coordinates[numfloats] = j
def hashUNH (ints, numInts, m, increment=449):
"Hashing of array of integers into below m, using random table"
res = 0
for i in range(numInts):
res += _randomTable[(ints[i] + i*increment) % 2048]
#res += _randomTable[(ints[i] + i*increment) & 2047]
#res = reduce(operator.add, [_randomTable[(ints[i] + i*increment) & 2047] for i in xrange(numInts)])
#res = reduce(operator.add,_hashnum)
return res % m
def hash (ints, numInts, ct):
"Returns index in collision table corresponding to first part of ints (an array)"
ct.calls += 1
memSize = ct.size
j = hashUNH(ints, numInts, memSize)
if ct.safety == _SUPER_SAFE:
ccheck = ints[:] # use whole list as check
else: # for safe or unsafe, use extra hash number as check
ccheck = hashUNH(ints, numInts, _maxLongint, 457)
if ccheck == ct.data[j]: # if new data same as saved data, add to hits
ct.clearhits += 1
elif ct.data[j] < 0: # first time, set up data
ct.clearhits += 1
ct.data[j] = ccheck
elif ct.safety == _UNSAFE: # collison, but we don't care
ct.collisions += 1
else: # handle collision - rehash
h2 = 1 + 2*hashUNH(ints, numInts, _maxLongintBy4)
i = 1
while ccheck != ct.data[j]: # keep looking for a new spot until we find an empty spot
ct.collisions += 1
j = (j + h2) % memSize
if i > memSize: # or we run out of space
print("Tiles: Collision table out of memory")
return -1 # force it to stop if out of memory
if ct.data[j] < 0:
ct.data[j] = ccheck
i += 1
return j
def powerOf2 (n):
lgn = math.log(n, 2)
return (lgn - math.floor(lgn)) == 0
def mod(num, by):
if num >= 0:
return num % by
else:
return (by + (num % by)) % by
def fixcoordwrap(coordinates, numtilings, numfloats, j, wrapwidths):
global _widthxnumtilings, _qstate, _base
for i in range(numfloats): # loop over each relevant dimension
# find coordinates of activated tile in tiling space
#if _qstate[i] >= _base[i]:
coordinates[i] = _qstate[i] - ((_qstate[i] - _base[i]) % numtilings)
#else:
# _coordinates[i] = _qstate[i]+1 + ((_base[i] - _qstate[i] - 1) % numtilings) - numtilings
if wrapwidths[i] != 0:
#_coordinates[i] = mod(_coordinates[i], _widthxnumtilings[i])
coordinates[i] = coordinates[i] % _widthxnumtilings[i]
_base[i] += 1 + (2 * i) # compute displacement of next tiling in quantized space
coordinates[numfloats] = j # add indices for tiling and hashing_set so they hash differently
def tiles (numtilings, memctable, floats, ints=[]):
'''Returns list of numtilings tiles corresponding to variables (floats and ints),
hashed down to mem, using ctable to check for collisions'''
if isinstance(memctable, CollisionTable):
hashfun = hash
else:
hashfun = hashUNH
numfloats = len(floats)
numcoord = 1 + numfloats + len(ints)
_coordinates = [0]*numcoord
startTiles (_coordinates, numtilings, floats, ints)
tlist = [None] *numtilings
for j in range(numtilings): # for each tiling
fixcoord(_coordinates, numtilings, numfloats, j)
hnum = hashfun(_coordinates, numcoord, memctable)
tlist[j] = hnum
return tlist
def loadtiles (tiles, startelement, numtilings, memctable, floats, ints=[]):
'''Loads numtilings tiles into array tiles, starting at startelement, corresponding
to variables (floats and ints), hashed down to mem, using ctable to check for collisions'''
if isinstance(memctable, CollisionTable):
hashfun = hash
else:
hashfun = hashUNH
numfloats = len(floats)
numcoord = 1 + numfloats + len(ints)
_coordinates = [0]*numcoord
startTiles (_coordinates, numtilings, floats, ints)
for j in range(numtilings):
fixcoord(_coordinates, numtilings, numfloats, j)
hnum = hashfun(_coordinates, numcoord, memctable)
tiles[startelement + j] = hnum
def tileswrap(numtilings, memctable, floats, wrapwidths, ints=[]):
'''Returns list of numtilings tiles corresponding to variables (floats and ints),
hashed down to mem, using ctable to check for collisions - wrap version'''
global _widthxnumtilings
if isinstance(memctable, CollisionTable):
hashfun = hash
else:
hashfun = hashUNH
numfloats = len(floats)
numcoord = 1 + numfloats + len(ints)
_coordinates = [0]*numcoord
tiles = [None] * numtilings
startTiles (_coordinates, numtilings, floats, ints)
_widthxnumtilings = [wrapwidths[i] * numtilings for i in range(numfloats)]
for j in range(numtilings):
fixcoordwrap(_coordinates, numtilings, numfloats, j, wrapwidths)
hnum = hashfun(_coordinates, numcoord, memctable)
tiles[j] = hnum
return tiles
def loadtileswrap(tiles, startelement, numtilings, memctable, floats, wrapwidths, ints=[]):
'''Returns list of numtilings tiles corresponding to variables (floats and ints),
hashed down to mem, using ctable to check for collisions - wrap version'''
global _widthxnumtilings
if isinstance(memctable, CollisionTable):
hashfun = hash
else:
hashfun = hashUNH
numfloats = len(floats)
numcoord = 1 + numfloats + len(ints)
_coordinates = [0]*numcoord
startTiles (_coordinates, numtilings, floats, ints)
_widthxnumtilings = [wrapwidths[i] * numtilings for i in range(numfloats)]
for j in range(numtilings):
fixcoordwrap(_coordinates, numtilings, numfloats, j, wrapwidths)
hnum = hashfun(_coordinates, numcoord, memctable)
tiles[startelement + j] = hnum
#def stateAggegatorApproximator(self):
# return phi
#def polyApproximator(self):
#return Phi
#def gaussianApproximator(self):
#return Phi