Пример #1
0
    def test_plane_through_points(self):

        # n-dimensional case
        for n in range(2, 10):
            points = [p for p in np.eye(n)]
            a, d = plane_through_points(points)
            np.testing.assert_array_almost_equal(a, np.ones(n) / np.sqrt(n))
            self.assertAlmostEqual(d, 1. / np.sqrt(n))

        # 2d case through origin
        points = [np.array([1., -1.]), np.array([-1., 1.])]
        a, d = plane_through_points(points)
        self.assertAlmostEqual(a[0] / a[1], 1.)
        self.assertAlmostEqual(d, 0.)
Пример #2
0
def _get_inner_simplex(A, b, vertices, tol=1.e-7):
    """
    Constructs a simplex contained in the porjection.

    Arguments
    ----------
    A : numpy.ndarray
        Left-hand side of the inequalities describing the higher dimensional polytope.
    b : numpy.ndarray
        Right-hand side of the inequalities describing the higher dimensional polytope.
    vertices : list of numpy.ndarray
        List of two vertices of the projection.
    tol : float
        Maximal expansion of a facet to consider it a facet of the projection.

    Returns
    ----------
    vertices : list of numpy.ndarray
        List of vertices of the simplex contained in the projection.
    """

    # initialize LPs
    n = vertices[0].shape[0]

    # expand increasing at every iteration the dimension of the space
    for i in range(2, n+1):
        a, d = plane_through_points([v[:i,:] for v in vertices])
        f = np.vstack((a, np.zeros((A.shape[1]-i, 1))))
        sol = linear_program(f, A, b)

        # check the length of the expansion wrt to the plane, if zero expand in the opposite direction
        expansion = np.abs(a.T.dot(sol['argmin'][:i, :]) - d) # >= 0
        if expansion < tol:
            lp.f = - lp.f
            sol = lp.solve()
        vertices.append(sol['argmin'][:n,:])

    return vertices