コード例 #1
0
    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)
コード例 #2
0
    def test_1d(self):
        """Test the triangulation for 1D inputs."""
        discretization = GridWorld([[0, 1]], 3)
        delaunay = _Triangulation(discretization, vertex_values=[0, 0.5, 0])
        vertex_values = delaunay.parameters

        test_points = np.array([[0, 0.2, 0.5, 0.6, 0.9, 1.]]).T
        test_point = test_points[[0], :]

        simplices = delaunay.find_simplex(test_points)
        true_simplices = np.array([0, 0, 1, 1, 1, 1])
        assert_allclose(simplices, true_simplices)
        assert_allclose(delaunay.find_simplex(test_point), true_simplices[[0]])

        values = delaunay(test_points)
        true_values = np.array([0, 0.2, 0.5, 0.4, 0.1, 0])[:, None]
        assert_allclose(values, true_values)

        value_constraint = delaunay.parameter_derivative(test_points)
        values = value_constraint.toarray().dot(vertex_values)
        assert_allclose(values, true_values)

        gradient = delaunay.gradient(test_points)
        true_gradient = np.array([1, 1, -1, -1, -1, -1])[:, None]
        assert_allclose(gradient, true_gradient)

        gradient_deriv = delaunay.gradient_parameter_derivative(test_points)
        gradient = gradient_deriv.toarray().dot(vertex_values)
        assert_allclose(gradient.reshape(-1, 1), true_gradient)
コード例 #3
0
    def test_multiple_dimensions(self):
        """Test delaunay in three dimensions."""
        limits = [[0, 1]] * 3
        discretization = GridWorld(limits, [2] * 3)
        delaunay = _Triangulation(discretization)
        assert_equal(delaunay.input_dim, 3)
        assert_equal(delaunay.discretization.nrectangles, 1)
        assert_equal(delaunay.nsimplex, np.math.factorial(3))

        corner_points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1],
                                  [0, 1, 1], [1, 1, 0], [1, 0, 1], [1, 1, 1]],
                                 dtype=np.float)

        values = np.sum(delaunay.discretization.index_to_state(np.arange(8)),
                        axis=1) / 3

        test_points = np.vstack((corner_points,
                                 np.array([[0, 0, 0.5], [0.5, 0, 0],
                                           [0, 0.5, 0], [0.5, 0.5, 0.5]])))
        corner_values = np.sum(corner_points, axis=1) / 3
        true_values = np.hstack(
            (corner_values, np.array([1 / 6, 1 / 6, 1 / 6, 1 / 2])))

        delaunay.parameters = values
        result = delaunay(test_points)
        assert_allclose(result, true_values[:, None], atol=1e-5)
コード例 #4
0
 def test_init(self):
     """Test initialisation."""
     limits = [[-1, 1], [-1, 1]]
     npoints = 4
     discretization = GridWorld(limits, npoints)
     pwc = PiecewiseConstant(discretization, np.arange(16))
     assert_allclose(pwc.parameters, np.arange(16)[:, None])
コード例 #5
0
 def test_gradient(self):
     """Test the gradient."""
     limits = [[-1, 1], [-1, 1]]
     npoints = 3
     discretization = GridWorld(limits, npoints)
     pwc = PiecewiseConstant(discretization)
     test_points = pwc.discretization.index_to_state(np.arange(pwc.nindex))
     gradient = pwc.gradient(test_points)
     assert_allclose(gradient, 0)
コード例 #6
0
    def test_dimensions_error(self):
        """Test dimension errors."""
        limits = [[-1.1, 1.5], [2.2, 2.4]]
        num_points = [7, 8]
        grid = GridWorld(limits, num_points)

        pytest.raises(DimensionError, grid._check_dimensions,
                      np.array([[1, 2, 3]]))

        pytest.raises(DimensionError, grid._check_dimensions, np.array([[1]]))
コード例 #7
0
def test_scipy_delaunay():
    """Test the fake replacement for Scipy."""
    limits = [[-1, 1], [-1, 2]]
    num_points = [2, 6]
    discretization = GridWorld(limits, num_points)
    sp_delaunay = ScipyDelaunay(limits, num_points)
    delaunay = _Triangulation(discretization)

    assert_equal(delaunay.nsimplex, sp_delaunay.nsimplex)
    assert_equal(delaunay.input_dim, sp_delaunay.ndim)
    sp_delaunay.find_simplex(np.array([[0, 0]]))
コード例 #8
0
    def test_values(self):
        """Test the evaluation function."""
        eps = 1e-10

        discretization = GridWorld([[0, 1], [0, 1]], [2, 2])
        delaunay = _Triangulation(discretization)

        test_points = np.array([[0, 0],
                                [1 - eps, 0],
                                [0, 1 - eps],
                                [0.5 - eps, 0.5 - eps],
                                [0, 0.5],
                                [0.5, 0]])
        nodes = delaunay.discretization.state_to_index(np.array([[0, 0],
                                                       [1, 0],
                                                       [0, 1]]))

        H = delaunay.parameter_derivative(test_points).toarray()

        true_H = np.zeros((len(test_points), delaunay.nindex),
                          dtype=np.float)
        true_H[0, nodes[0]] = 1
        true_H[1, nodes[1]] = 1
        true_H[2, nodes[2]] = 1
        true_H[3, nodes[[1, 2]]] = 0.5
        true_H[4, nodes[[0, 2]]] = 0.5
        true_H[5, nodes[[0, 1]]] = 0.5

        assert_allclose(H, true_H, atol=1e-7)

        # Test value property
        values = np.random.rand(delaunay.nindex)
        delaunay.parameters = values
        v1 = H.dot(values)[:, None]
        v2 = delaunay(test_points)
        assert_allclose(v1, v2)

        # Test the projections
        test_point = np.array([[-0.5, -0.5]])
        delaunay.parameters = np.array([0, 1, 1, 1])
        unprojected = delaunay(test_point)
        delaunay.project = True
        projected = delaunay(test_point)

        assert_allclose(projected, np.array([[0]]))
        assert_allclose(unprojected, np.array([[-1]]))
コード例 #9
0
    def setup(self):
        """Create testing environment."""
        with tf.Session(graph=tf.Graph()) as sess:
            npoints = 3

            discretization = GridWorld([[0, 1], [0, 1]], npoints)
            parameters = np.sum(discretization.all_points**2,
                                axis=1,
                                keepdims=True)
            trinp = _Triangulation(discretization, vertex_values=parameters)

            tri = Triangulation(discretization, vertex_values=parameters)

            test_points = np.array([[-10, -10], [0.2, 0.7], [0, 0], [0, 1],
                                    [1, 1], [-0.2, 0.5], [0.43, 0.21]])

            sess.run(tf.global_variables_initializer())
            yield sess, tri, trinp, test_points
コード例 #10
0
    def test_gradient(self):
        """Test the gradient_at function."""
        discretization = GridWorld([[0, 1], [0, 1]], [2, 2])
        delaunay = _Triangulation(discretization)

        points = np.array([[0, 0],
                           [1, 0],
                           [0, 1],
                           [1, 1]], dtype=np.int)
        nodes = delaunay.discretization.state_to_index(points)

        # Simplex with node values:
        # 3 - 1
        # | \ |
        # 1 - 2
        # --> x

        values = np.zeros(delaunay.nindex)
        values[nodes] = [1, 2, 3, 1]

        test_points = np.array([[0.01, 0.01],
                                [0.99, 0.99]])

        true_grad = np.array([[1, 2], [-2, -1]])

        # Construct true H (gradient as function of values)
        true_H = np.zeros((2 * delaunay.input_dim, delaunay.nindex))

        true_H[0, nodes[[0, 1]]] = [-1, 1]
        true_H[1, nodes[[0, 2]]] = [-1, 1]
        true_H[2, nodes[[2, 3]]] = [-1, 1]
        true_H[3, nodes[[1, 3]]] = [-1, 1]

        # Evaluate gradient with and without values
        H = delaunay.gradient_parameter_derivative(test_points).toarray()
        delaunay.parameters = values
        grad = delaunay.gradient(test_points)

        # Compare
        assert_allclose(grad, true_grad)
        assert_allclose(H, true_H)
        assert_allclose(true_grad,
                        H.dot(values).reshape(-1, delaunay.input_dim))
コード例 #11
0
    def test_find_simplex(self):
        """Test the simplices on the grid."""
        limits = [[-1, 1], [-1, 2]]
        num_points = [3, 7]
        discretization = GridWorld(limits, num_points)
        delaunay = _Triangulation(discretization)

        # Test the basic properties
        assert_equal(delaunay.discretization.nrectangles, 2 * 6)
        assert_equal(delaunay.input_dim, 2)
        assert_equal(delaunay.nsimplex, 2 * 2 * 6)
        assert_equal(delaunay.discretization.offset, np.array([-1, -1]))
        assert_equal(delaunay.discretization.unit_maxes,
                     np.array([2, 3]) / (np.array(num_points) - 1))

        # test the simplex indices
        lower = delaunay.triangulation.find_simplex(np.array([0, 0])).squeeze()
        upper = 1 - lower

        test_points = np.array([[0, 0],
                                [0.9, 0.45],
                                [1.1, 0],
                                [1.9, 2.9]])

        test_points += np.array(limits)[:, 0]

        true_result = np.array([lower, upper, 6 * 2 + lower, 11 * 2 + upper])
        result = delaunay.find_simplex(test_points)

        assert_allclose(result, true_result)

        # Test the ability to find simplices
        simplices = delaunay.simplices(result)
        true_simplices = np.array([[0, 1, 7],
                                   [1, 7, 8],
                                   [7, 8, 14],
                                   [13, 19, 20]])
        assert_equal(np.sort(simplices, axis=1), true_simplices)

        # Test point ouside domain (should map to bottom left and top right)
        assert_equal(lower, delaunay.find_simplex(np.array([[-100., -100.]])))
        assert_equal(delaunay.nsimplex - 1 - lower,
                     delaunay.find_simplex(np.array([[100., 100.]])))
コード例 #12
0
    def test_evaluation(self):
        """Evaluation tests for piecewise constant function."""
        limits = [[-1, 1], [-1, 1]]
        npoints = 3
        discretization = GridWorld(limits, npoints)
        pwc = PiecewiseConstant(discretization)

        vertex_points = pwc.discretization.index_to_state(np.arange(
            pwc.nindex))
        vertex_values = np.sum(vertex_points, axis=1, keepdims=True)
        pwc.parameters = vertex_values

        test = pwc(vertex_points)
        assert_allclose(test, vertex_values)

        outside_point = np.array([[-1.5, -1.5]])
        test1 = pwc(outside_point)
        assert_allclose(test1, np.array([[-2]]))

        # Test constraint evaluation
        test2 = pwc.parameter_derivative(vertex_points)
        test2 = test2.toarray().dot(vertex_values)
        assert_allclose(test2, vertex_values)
コード例 #13
0
 def test_integer_numpoints(self):
     """Check integer numpoints argument."""
     grid = GridWorld([[1, 2], [3, 4]], 2)
     assert_equal(grid.num_points, np.array([2, 2]))
コード例 #14
0
    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)