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