Fix methods Domain._sort_polygon_2d(), Domain._sort_polygon_3d()
[linpy.git] / pypol / tests / test_domains.py
1 import unittest
2
3 from ..domains import *
4 from ..linexprs import Symbol, symbols
5 from ..polyhedra import *
6
7
8 class TestDomain(unittest.TestCase):
9
10 def setUp(self):
11 x, y = symbols('x y')
12 self.square1 = Polyhedron(inequalities=[x, 2 - x, y, 2 - y])
13 self.square2 = Polyhedron(inequalities=[x - 1, 3 - x , y - 1, 3 - y]) #correct representation
14 self.square3 = Polyhedron(inequalities=[x, 3 - x, y, 3 - y])
15 self.square4 = Polyhedron(inequalities=[x - 1, 2 - x, y - 1, 2 - y])
16 self.square5 = Polyhedron(inequalities=[x - 3, 6 - x, y - 3, 6 -y])
17 self.square6 = Polyhedron(equalities=[3 - y], inequalities=[x - 1, 3 - x, y - 1])
18 self.unbound_poly = Polyhedron(inequalities=[x, 3 - x, y])
19 self.universe = Polyhedron([])
20 self.empty = Empty
21 self.disjoint = And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
22 self.complement = Or(Ge(-x - 1, 0), Ge(x - 3, 0), And(Ge(x, 0), Ge(-x + 2, 0), Ge(-y - 1, 0)), And(Ge(x, 0), Ge(-x + 2, 0), Ge(y - 3, 0)))
23 self.hull = And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
24 self.dropped = And(Ge(y, 0), Ge(-y + 2, 0))
25 self.intersection = And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
26 self.union = Or(And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0)))
27 self.sum1 = Or(And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0)), And(Ge(x - 1, 0), Ge(-x + 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0)))
28 self.sum2 =And(Ge(x, 0), Ge(y, 0), Ge(-y + 3, 0), Ge(-x + 3, 0), Ge(x - y + 2, 0), Ge(-x + y + 2, 0))
29 self.difference1 = Or(And(Eq(x - 3, 0), Ge(y - 1, 0), Ge(-y + 3, 0)), And(Eq(y - 3, 0), Ge(x - 1, 0), Ge(-x + 2, 0)))
30 self.difference2 = And(Ge(x + y - 4, 0), Ge(-x + 3, 0), Ge(-y + 3, 0))
31 self.lexmin = And(Eq(y, 0), Eq(x, 0))
32 self.lexmax = And(Eq(y - 2, 0), Eq(x - 2, 0))
33
34 def test_new(self):
35 with self.assertRaises(TypeError):
36 Polyhedron(1)
37
38 def test_disjoint(self):
39 self.assertEqual(self.square1.disjoint(), self.disjoint)
40 self.assertEqual(self.empty.disjoint(), Empty)
41 self.assertEqual(self.universe.disjoint(), self.universe)
42
43 def test_isempty(self):
44 self.assertFalse(self.square1.isempty())
45 self.assertTrue(self.empty.isempty())
46 self.assertFalse(self.universe.isempty())
47
48 def test_isuniverse(self):
49 self.assertFalse(self.square1.isuniverse())
50 self.assertTrue(self.universe.isuniverse())
51
52 def test_isbounded(self):
53 self.assertTrue(self.square1.isbounded())
54 self.assertFalse(self.unbound_poly.isbounded())
55
56 def test_eq(self):
57 self.assertTrue(self.square1 == self.square1)
58 self.assertFalse(self.square1 == self.square2)
59 self.assertFalse(self.empty == self.universe)
60
61 def test_isdisjoint(self):
62 self.assertFalse(self.square1.isdisjoint(self.square2))
63 self.assertFalse(self.universe.isdisjoint(self.square1))
64 self.assertTrue(self.square1.isdisjoint(self.square5))
65 self.assertTrue(self.empty.isdisjoint(self.square1))
66
67 def test_issubset(self):
68 self.assertTrue(self.square4.issubset(self.unbound_poly))
69 self.assertFalse(self.square1.issubset(self.square2))
70 self.assertTrue(self.square1.issubset(self.universe))
71 self.assertTrue(self.empty.issubset(self.square1))
72
73 def test_le(self):
74 self.assertTrue(self.square4 <= self.square3)
75 self.assertFalse(self.square3 <= self.square4)
76 self.assertTrue(self.empty <= self.square1)
77 self.assertTrue(self.square1 <= self.universe)
78
79 def test_lt(self):
80 self.assertTrue(self.square4 < self.square3)
81 self.assertFalse(self.square3 < self.square4)
82 self.assertTrue(self.empty < self.square1)
83 self.assertTrue(self.square1 < self.universe)
84
85 def test_complement(self):
86 self.assertEqual(~self.square1, self.complement)
87 self.assertEqual(~self.universe, Empty)
88 self.assertEqual(~self.empty, self.universe)
89
90 def test_aspolyhedron(self):
91 self.assertEqual(self.square1.aspolyhedron(), self.hull)
92 self.assertEqual(self.universe.aspolyhedron(), self.universe)
93 self.assertEqual(self.empty.aspolyhedron(), self.empty)
94
95 def test_project(self):
96 self.assertEqual(self.square1.project(symbols('x')), self.dropped)
97 self.assertEqual(self.square1.project(symbols('x y')), self.universe)
98 self.assertEqual(self.universe.project([]), self.universe)
99 self.assertEqual(self.empty.project([]), Empty)
100
101 def test_simplify(self):
102 self.assertEqual(self.universe.simplify(), self.universe)
103 self.assertEqual(self.empty.simplify(), Empty)
104
105 def test_sample(self):
106 self.assertEqual(self.square6.sample(), {Symbol('x'): 1, Symbol('y'): 3})
107 with self.assertRaises(ValueError):
108 self.empty.sample()
109 self.assertEqual(self.universe.sample(), {})
110
111 def test_intersection(self):
112 self.assertEqual(self.square1.intersection(self.square2), self.intersection)
113
114 def test_and(self):
115 self.assertEqual(self.square2 & self.square1, self.intersection)
116 self.assertEqual(self.square1 & self.universe, self.square1)
117 self.assertEqual(self.empty & self.square1, Empty)
118 self.assertEqual(self.universe & self.universe, self.universe)
119 self.assertEqual(self.universe & self.empty, Empty)
120 self.assertEqual(self.empty & self.empty, Empty)
121
122 def test_union(self):
123 self.assertEqual(self.square1.union(self.square2), self.union)
124 self.assertEqual(self.square1.union(self.empty), self.square1)
125 self.assertEqual(self.square1.union(self.universe), self.universe)
126 self.assertEqual(self.universe.union(self.universe), self.universe)
127 self.assertEqual(self.empty.union(self.empty), self.empty)
128
129 def test_or(self):
130 self.assertEqual(self.square1 | self.square2, self.union)
131
132 def test_add(self):
133 self.assertEqual(self.square2 + self.square1, self.sum1)
134 self.assertEqual(Polyhedron(self.square1 + self.square2), self.sum2)
135 self.assertEqual(self.universe + self.square1, self.universe)
136 self.assertEqual(self.empty + self.square1, self.square1)
137 self.assertEqual(self.universe + self.universe, self.universe)
138
139 def test_difference(self):
140 self.assertEqual(self.square2 - self.square1, self.difference1)
141 self.assertEqual(Polyhedron(self.square2 - self.square1), self.difference2)
142 self.assertEqual(self.square2 - self.square2, Empty)
143 self.assertEqual(self.universe - self.universe, Empty)
144
145 def test_lexmin(self):
146 self.assertEqual(self.square1.lexmin(), self.lexmin)
147 self.assertEqual(self.universe.lexmin(), self.universe)
148 self.assertEqual(self.empty.lexmin(), Empty)
149
150 def test_lexmax(self):
151 self.assertEqual(self.square1.lexmax(), self.lexmax)
152 self.assertEqual(self.universe.lexmax(), self.universe)
153 self.assertEqual(self.empty.lexmax(), Empty)
154
155 def test_num_parameters(self):
156 self.assertEqual(self.square1.num_parameters(), 2)
157 self.assertEqual(self.empty.num_parameters(), 0)
158 self.assertEqual(self.universe.num_parameters(), 0)
159
160 def test_involves_dims(self):
161 self.assertTrue(self.square1.involves_dims(symbols('x y')))
162 self.assertFalse(self.empty.involves_dims(symbols('x')))
163 self.assertFalse(self.universe.involves_dims(symbols('x')))