Example #1
0
    def test_from_functions(self):

        # row vector input
        x = np.ones((1, 5))
        self.assertRaises(ValueError, Polyhedron.from_lower_bound, x)

        # from lower bound
        x = np.ones((2, 1))
        p = Polyhedron.from_lower_bound(x)
        A_lb = np.array([[-1., 0.], [0., -1.]])
        b_lb = np.array([[-1.], [-1.]])
        self.assertTrue(
            same_rows(np.hstack((A_lb, b_lb)), np.hstack((p.A, p.b))))

        # from upper bound
        p = Polyhedron.from_upper_bound(x)
        A_ub = np.array([[1., 0.], [0., 1.]])
        b_ub = np.array([[1.], [1.]])
        self.assertTrue(
            same_rows(np.hstack((A_ub, b_ub)), np.hstack((p.A, p.b))))

        # from upper and lower bounds
        p = Polyhedron.from_bounds(x, x)
        A = np.vstack((A_lb, A_ub))
        b = np.vstack((b_lb, b_ub))
        self.assertTrue(same_rows(np.hstack((A, b)), np.hstack((p.A, p.b))))

        # different size lower and upper bound
        y = np.ones((3, 1))
        self.assertRaises(ValueError, Polyhedron.from_bounds, x, y)

        # from lower bound of not all the variables
        indices = [1, 2]
        n = 3
        p = Polyhedron.from_lower_bound(x, indices, n)
        A_lb = np.array([[0., -1., 0.], [0., 0., -1.]])
        b_lb = np.array([[-1.], [-1.]])
        self.assertTrue(
            same_rows(np.hstack((A_lb, b_lb)), np.hstack((p.A, p.b))))

        # from lower bound of not all the variables
        indices = [0, 2]
        n = 3
        p = Polyhedron.from_upper_bound(x, indices, n)
        A_ub = np.array([[1., 0., 0.], [0., 0., 1.]])
        b_ub = np.array([[1.], [1.]])
        self.assertTrue(
            same_rows(np.hstack((A_ub, b_ub)), np.hstack((p.A, p.b))))

        # from upper and lower bounds of not all the variables
        indices = [0, 1]
        n = 3
        p = Polyhedron.from_bounds(x, x, indices, n)
        A = np.array([[-1., 0., 0.], [0., -1., 0.], [1., 0., 0.], [0., 1.,
                                                                   0.]])
        b = np.array([[-1.], [-1.], [1.], [1.]])
        self.assertTrue(same_rows(np.hstack((A, b)), np.hstack((p.A, p.b))))

        # too many indices
        indices = [1, 2, 3]
        n = 5
        self.assertRaises(ValueError, Polyhedron.from_lower_bound, x, indices,
                          n)
        self.assertRaises(ValueError, Polyhedron.from_upper_bound, x, indices,
                          n)
        self.assertRaises(ValueError, Polyhedron.from_bounds, x, x, indices, n)
Example #2
0
    def test_from_functions(self):

        # row vector input
        x = np.ones((1,5))
        self.assertRaises(ValueError, Polyhedron.from_lower_bound, x)

        # from lower bound
        x = np.ones(2)
        p = Polyhedron.from_lower_bound(x)
        A_lb = np.array([[-1., 0.], [0., -1.]])
        b_lb = np.array([-1.,-1.])
        self.assertTrue(same_rows(
            np.column_stack((A_lb, b_lb)),
            np.column_stack((p.A, p.b))
            ))

        # from upper bound
        p = Polyhedron.from_upper_bound(x)
        A_ub = np.array([[1., 0.], [0., 1.]])
        b_ub = np.array([1.,1.])
        self.assertTrue(same_rows(
            np.column_stack((A_ub, b_ub)),
            np.column_stack((p.A, p.b))
            ))

        # from upper and lower bounds
        p = Polyhedron.from_bounds(x, x)
        A = np.vstack((A_lb, A_ub))
        b = np.concatenate((b_lb, b_ub))
        self.assertTrue(same_rows(
            np.column_stack((A, b)),
            np.column_stack((p.A, p.b))
            ))

        # different size lower and upper bound
        y = np.ones((3,1))
        self.assertRaises(ValueError, Polyhedron.from_bounds, x, y)

        # from lower bound of not all the variables
        indices = [1, 2]
        n = 3
        p = Polyhedron.from_lower_bound(x, indices, n)
        A_lb = np.array([[0., -1., 0.], [0., 0., -1.]])
        b_lb = np.array([-1.,-1.])
        self.assertTrue(same_rows(
            np.column_stack((A_lb, b_lb)),
            np.column_stack((p.A, p.b))
            ))

        # from lower bound of not all the variables
        indices = [0, 2]
        n = 3
        p = Polyhedron.from_upper_bound(x, indices, n)
        A_ub = np.array([[1., 0., 0.], [0., 0., 1.]])
        b_ub = np.array([1.,1.])
        self.assertTrue(same_rows(
            np.column_stack((A_ub, b_ub)),
            np.column_stack((p.A, p.b))
            ))

        # from upper and lower bounds of not all the variables
        indices = [0, 1]
        n = 3
        p = Polyhedron.from_bounds(x, x, indices, n)
        A = np.array([[-1., 0., 0.], [0., -1., 0.], [1., 0., 0.], [0., 1., 0.]])
        b = np.array([-1.,-1.,1.,1.])
        self.assertTrue(same_rows(
            np.column_stack((A, b)),
            np.column_stack((p.A, p.b))
            ))

        # too many indices
        indices = [1, 2, 3]
        n = 5
        self.assertRaises(ValueError, Polyhedron.from_lower_bound, x, indices, n)
        self.assertRaises(ValueError, Polyhedron.from_upper_bound, x, indices, n)
        self.assertRaises(ValueError, Polyhedron.from_bounds, x, x, indices, n)

        # from symbolic
        n = 5
        m = 3
        x = sp.Matrix([sp.Symbol('x'+str(i), real=True) for i in range(n)])
        A = np.random.rand(m, n)
        b = np.random.rand(m)
        ineq = sp.Matrix(A)*x - sp.Matrix(b)
        P = Polyhedron.from_symbolic(x, ineq)
        np.testing.assert_array_almost_equal(P.A, A)
        np.testing.assert_array_almost_equal(P.b, b)
        C = np.random.rand(m, n)
        d = np.random.rand(m)
        eq = sp.Matrix(C)*x - sp.Matrix(d)
        P = Polyhedron.from_symbolic(x, ineq, eq)
        np.testing.assert_array_almost_equal(P.A, A)
        np.testing.assert_array_almost_equal(P.b, b)
        np.testing.assert_array_almost_equal(P.C, C)
        np.testing.assert_array_almost_equal(P.d, d)