def test_0d(self): """Check that initialization works for 1d-discretization.""" grid = GridWorld([[0, 1]], 3) test = np.array([[0.1, 0.4, 0.9]]).T res = np.array([0, 1, 2]) assert_allclose(grid.state_to_index(test), res) res = np.array([0, 0, 1]) assert_allclose(grid.state_to_rectangle(test), res) assert_allclose(grid.rectangle_to_state(res), res[:, None] * 0.5)
def test_index_state_conversion(self): """Test all index conversions.""" limits = [[-1.1, 1.5], [2.2, 2.4]] num_points = [7, 8] grid = GridWorld(limits, num_points) # Forward and backwards convert all indeces indeces = np.arange(grid.nindex) states = grid.index_to_state(indeces) indeces2 = grid.state_to_index(states) assert_equal(indeces, indeces2) # test 1D input grid.state_to_index([0, 2.3]) grid.index_to_state(1) # Test rectangles rectangles = np.arange(grid.nrectangles) states = grid.rectangle_to_state(rectangles) rectangles2 = grid.state_to_rectangle(states + grid.unit_maxes / 2) assert_equal(rectangles, rectangles2) rectangle = grid.state_to_rectangle(100 * np.ones((1, 2))) assert_equal(rectangle, grid.nrectangles - 1) rectangle = grid.state_to_rectangle(-100 * np.ones((1, 2))) assert_equal(rectangle, 0) # Test rectangle corners corners = grid.rectangle_corner_index(rectangles) corner_states = grid.rectangle_to_state(rectangles) corners2 = grid.state_to_index(corner_states) assert_equal(corners, corners2) # Test point outside grid test_point = np.array([[-1.2, 2.]]) index = grid.state_to_index(test_point) assert_equal(index, 0)