Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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]]))
Beispiel #4
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]]))
Beispiel #5
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
Beispiel #6
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))
Beispiel #7
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.]])))