Skip to content

Instantly share code, notes, and snippets.

@tyrion
Last active February 18, 2018 09:38
Show Gist options
  • Save tyrion/810a9d65c905a5e7f3f4b36350a21c2a to your computer and use it in GitHub Desktop.
Save tyrion/810a9d65c905a5e7f3f4b36350a21c2a to your computer and use it in GitHub Desktop.
Tile Coding Software
# tiles.py -- Tile Coding Software
# Copyright (C) <2018> <Germano Gabbianelli>
#
# Based on Tiles3 by Richard S. Sutton:
# http://incompleteideas.net/tiles/tiles3.html
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
def tiles(a, n=None, d=None):
a = np.expand_dims(np.atleast_2d(a), 1)
k = a.shape[2] # number of dimensions
if n is None:
n = 4
while n < 4 * k:
n <<= 1 # same as * 2
else:
# n should be >= 4 * k
n = int(n)
if d is None:
d = np.arange(1, k * 2, 2) # first k odd numbers
else:
d = np.asanyarray(d)
a = np.floor(a * n)
d = np.arange(n).reshape(-1, 1) * d
return np.floor((a + d) / n)
def indices(a, min=0, max=1):
a = np.atleast_3d(a)
n = a.shape[1]
k = a.shape[2]
min = np.broadcast_to(min, k)
max = np.broadcast_to(max, k)
min, max = tiles([min, max], n)
n_tiles = max - min + 1
assert np.all(n_tiles == n_tiles[0])
n_tiles = n_tiles[0]
order = n_tiles.copy()
order[1:] = order[:-1] # shift all elements 1 to the right
order[0] = 1
a = ((a - min) * np.cumprod(order)).sum(-1)
idx = np.full(n, n_tiles.prod())
idx[0] = 0
idx = np.cumsum(idx)
return a + idx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment