Exemplo n.º 1
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)
Exemplo n.º 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)

        # 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)