Example #1
0
    def test_vertices(self):

        # basic eample
        A = np.array([[-1, 0.],[0., -1.],[2., 1.],[-0.5, 1.]])
        b = np.array([0.,0.,4.,2.])
        p = Polyhedron(A,b)
        u_list = [
            np.array([0.,0.]),
            np.array([2.,0.]),
            np.array([0.,2.]),
            np.array([.8,2.4])
        ]
        self.assertTrue(same_vectors(p.vertices, u_list))

        # 1d example
        A = np.array([[-1],[1.]])
        b = np.array([1.,1.])
        p = Polyhedron(A,b)
        u_list = [
            np.array([-1.]),
            np.array([1.])
        ]
        self.assertTrue(same_vectors(p.vertices, u_list))

        # unbounded
        x_min = np.zeros(2)
        p = Polyhedron.from_lower_bound(x_min)
        self.assertTrue(p.vertices is None)

        # lower dimensional (because of the inequalities)
        x_max = np.array([1.,0.])
        p.add_upper_bound(x_max)
        self.assertTrue(p.vertices is None)

        # empty
        x_max = - np.ones(2)
        p.add_upper_bound(x_max)
        self.assertTrue(p.vertices is None)

        # 3d case
        x_min = - np.ones(3)
        x_max = - x_min
        p = Polyhedron.from_bounds(x_min, x_max)
        u_list = [np.array(v) for v in product([1., -1.], repeat=3)]
        self.assertTrue(same_vectors(p.vertices, u_list))

        # 3d case with equalities
        x_min = np.array([-1.,-2.])
        x_max = - x_min
        p = Polyhedron.from_bounds(x_min, x_max, [0,1], 3)
        C = np.array([[1., 0., -1.]])
        d = np.zeros(1)
        p.add_equality(C, d)
        u_list = [
            np.array([1.,2.,1.]),
            np.array([-1.,2.,-1.]),
            np.array([1.,-2.,1.]),
            np.array([-1.,-2.,-1.])
        ]
        self.assertTrue(same_vectors(p.vertices, u_list))
Example #2
0
    def test_add_functions(self):

        # add inequalities
        A = np.eye(2)
        b = np.ones(2)
        p = Polyhedron(A, b)
        A = np.ones((1, 2))
        b = np.ones(1)
        p.add_inequality(A, b)
        A = np.ones((1, 1))
        b = 3. * np.ones(1)
        p.add_inequality(A, b, [1])
        A = np.array([[1., 0.], [0., 1.], [1., 1.], [0., 1.]])
        b = np.array([1., 1., 1., 3.])
        self.assertTrue(
            same_rows(np.column_stack((A, b)), np.column_stack((p.A, p.b))))
        c = np.ones(2)
        self.assertRaises(ValueError, p.add_inequality, A, c)

        # add equalities
        A = np.eye(2)
        b = np.ones(2)
        p.add_equality(A, b)
        A = 3. * np.ones((1, 1))
        b = np.ones(1)
        p.add_equality(A, b, [0])
        A = np.array([[1., 0.], [0., 1.], [3., 0.]])
        b = np.array([[1.], [1.], [1.]])
        self.assertTrue(
            same_rows(np.column_stack((A, b)), np.column_stack((p.C, p.d))))
        b = np.ones(2)
        self.assertRaises(ValueError, p.add_equality, A, b)

        # add lower bounds
        A = np.zeros((0, 2))
        b = np.zeros(0)
        p = Polyhedron(A, b)
        p.add_lower_bound(-np.ones(2))
        p.add_upper_bound(2 * np.ones(2))
        p.add_bounds(-3 * np.ones(2), 3 * np.ones(2))
        p.add_lower_bound(-4 * np.ones(1), [0])
        p.add_upper_bound(5 * np.ones(1), [0])
        p.add_bounds(-6 * np.ones(1), 6 * np.ones(1), [1])
        A = np.array([[-1., 0.], [0., -1.], [1., 0.], [0., 1.], [-1., 0.],
                      [0., -1.], [1., 0.], [0., 1.], [-1., 0.], [1., 0.],
                      [0., -1.], [0., 1.]])
        b = np.array([1., 1., 2., 2., 3., 3., 3., 3., 4., 5., 6., 6.])
        self.assertTrue(
            same_rows(np.column_stack((A, b)), np.column_stack((p.A, p.b))))

        # wrong size bounds
        A = np.eye(3)
        b = np.ones(3)
        p = Polyhedron(A, b)
        x = np.zeros(1)
        indices = [0, 1]
        self.assertRaises(ValueError, p.add_lower_bound, x, indices)
        self.assertRaises(ValueError, p.add_upper_bound, x, indices)
        self.assertRaises(ValueError, p.add_bounds, x, x, indices)
Example #3
0
    def test_remove_redundant_inequalities(self):

        # minimal facets only inequalities
        A = np.array([[1., 1.], [-1., 1.], [0., -1.], [0., 1.], [0., 1.],
                      [2., 2.]])
        b = np.array([[1.], [1.], [1.], [1.], [2.], [2.]])
        p = Polyhedron(A, b)
        mf = set(p.minimal_facets())
        self.assertTrue(mf == set([1, 2, 0]) or mf == set([1, 2, 5]))

        # add nasty redundant inequality
        A = np.zeros((1, 2))
        b = np.ones((1, 1))
        p.add_inequality(A, b)
        mf = set(p.minimal_facets())
        self.assertTrue(mf == set([1, 2, 0]) or mf == set([1, 2, 5]))

        # remove redundant facets
        p.remove_redundant_inequalities()
        A_min = np.array([[-1., 1.], [0., -1.], [1., 1.]])
        b_min = np.array([[1.], [1.], [1.]])
        self.assertTrue(
            same_rows(np.hstack((A_min, b_min)), np.hstack((p.A, p.b))))

        # both inequalities and equalities
        x_min = -np.ones((2, 1))
        x_max = -x_min
        p = Polyhedron.from_bounds(x_min, x_max)
        C = np.ones((1, 2))
        d = np.ones((1, 1))
        p.add_equality(C, d)
        self.assertEqual([2, 3], sorted(p.minimal_facets()))
        p.remove_redundant_inequalities()
        A_min = np.array([[1., 0.], [0., 1.]])
        b_min = np.array([[1.], [1.]])
        self.assertTrue(
            same_rows(np.hstack((A_min, b_min)), np.hstack((p.A, p.b))))

        # add (redundant) inequality coincident with the equality
        p.add_inequality(C, d)
        p.remove_redundant_inequalities()
        self.assertTrue(
            same_rows(np.hstack((A_min, b_min)), np.hstack((p.A, p.b))))

        # add (redundant) inequality
        p.add_inequality(np.array([[-1., 1.]]), np.array([[1.1]]))
        p.remove_redundant_inequalities()
        self.assertTrue(
            same_rows(np.hstack((A_min, b_min)), np.hstack((p.A, p.b))))

        # add unfeasible equality (raises: 'empty polyhedron, cannot remove redundant inequalities.')
        C = np.ones((1, 2))
        d = -np.ones((1, 1))
        p.add_equality(C, d)
        self.assertRaises(ValueError, p.remove_redundant_inequalities)

        # empty polyhderon (raises: 'empty polyhedron, cannot remove redundant inequalities.')
        x_min = np.ones((2, 1))
        x_max = np.zeros((2, 1))
        p = Polyhedron.from_bounds(x_min, x_max)
        self.assertRaises(ValueError, p.remove_redundant_inequalities)
Example #4
0
    def test_add_functions(self):

        # add inequalities
        A = np.eye(2)
        b = np.ones(2)
        p = Polyhedron(A, b)
        A = np.ones((1, 2))
        b = np.ones(1)
        p.add_inequality(A, b)
        A = np.ones((1, 1))
        b = 3.*np.ones(1)
        p.add_inequality(A, b, [1])
        A = np.array([[1., 0.], [0., 1.], [1., 1.], [0., 1.]])
        b = np.array([1.,1.,1.,3.])
        self.assertTrue(same_rows(
            np.column_stack((A, b)),
            np.column_stack((p.A, p.b))
            ))
        c = np.ones(2)
        self.assertRaises(ValueError, p.add_inequality, A, c)

        # add equalities
        A = np.eye(2)
        b = np.ones(2)
        p.add_equality(A, b)
        A = 3.*np.ones((1, 1))
        b = np.ones(1)
        p.add_equality(A, b, [0])
        A = np.array([[1., 0.], [0., 1.], [3., 0.]])
        b = np.array([[1.], [1.], [1.]])
        self.assertTrue(same_rows(
            np.column_stack((A, b)),
            np.column_stack((p.C, p.d))
            ))
        b = np.ones(2)
        self.assertRaises(ValueError, p.add_equality, A, b)

        # add lower bounds
        A = np.zeros((0, 2))
        b = np.zeros(0)
        p = Polyhedron(A, b)
        p.add_lower_bound(-np.ones(2))
        p.add_upper_bound(2*np.ones(2))
        p.add_bounds(-3*np.ones(2), 3*np.ones(2))
        p.add_lower_bound(-4*np.ones(1), [0])
        p.add_upper_bound(5*np.ones(1), [0])
        p.add_bounds(-6*np.ones(1), 6*np.ones(1), [1])
        A =np.array([[-1., 0.], [0., -1.], [1., 0.], [0., 1.], [-1., 0.], [0., -1.], [1., 0.], [0., 1.], [-1., 0.], [1., 0.], [0., -1.], [0., 1.] ])
        b = np.array([1.,1.,2.,2.,3.,3.,3.,3.,4.,5.,6.,6.])
        self.assertTrue(same_rows(
            np.column_stack((A, b)),
            np.column_stack((p.A, p.b))
            ))

        # wrong size bounds
        A = np.eye(3)
        b = np.ones(3)
        p = Polyhedron(A, b)
        x = np.zeros(1)
        indices = [0, 1]
        self.assertRaises(ValueError, p.add_lower_bound, x, indices)
        self.assertRaises(ValueError, p.add_upper_bound, x, indices)
        self.assertRaises(ValueError, p.add_bounds, x, x, indices)

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

        # throw some errors here
        P = Polyhedron(A, b)
        x = sp.Matrix([sp.Symbol('x'+str(i), real=True) for i in range(n+1)])
        A1 = np.random.rand(m, n+1)
        ineq = sp.Matrix(A1)*x - sp.Matrix(b)
        self.assertRaises(ValueError, P.add_symbolic_inequality, x, ineq)
        P = Polyhedron(A, b, C, d)
        C1 = np.random.rand(m, n+1)
        eq = sp.Matrix(C1)*x - sp.Matrix(b)
        self.assertRaises(ValueError, P.add_symbolic_equality, x, eq)