df7b01c3dec925c236c862db928e5c20f82afa33
[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.sample = And(Eq(y - 3, 0), Eq(x - 1, 0))
26 self.intersection = And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
27 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)))
28 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)))
29 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))
30 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)))
31 self.difference2 = And(Ge(x + y - 4, 0), Ge(-x + 3, 0), Ge(-y + 3, 0))
32 self.lexmin = And(Eq(y, 0), Eq(x, 0))
33 self.lexmax = And(Eq(y - 2, 0), Eq(x - 2, 0))
34
35 def test_new(self):
36 with self.assertRaises(TypeError):
37 Polyhedron(1)
38
39 def test_disjoint(self):
40 self.assertEqual(self.square1.disjoint(), self.disjoint)
41
42 def test_isempty(self):
43 self.assertFalse(self.square1.isempty())
44 self.assertTrue(self.empty.isempty())
45
46 def test_isuniverse(self):
47 self.assertFalse(self.square1.isuniverse())
48 self.assertTrue(self.universe.isuniverse())
49
50 def test_isbounded(self):
51 self.assertTrue(self.square1.isbounded())
52 self.assertFalse(self.unbound_poly.isbounded())
53
54 def test_eq(self):
55 self.assertTrue(self.square1 == self.square1)
56 self.assertFalse(self.square1 == self.square2)
57
58 def test_isdisjoint(self):
59 self.assertFalse(self.square1.isdisjoint(self.square2))
60 self.assertFalse(self.universe.isdisjoint(self.square1))
61 self.assertTrue(self.square1.isdisjoint(self.square5))
62 self.assertTrue(self.empty.isdisjoint(self.square1))
63
64 def test_issubset(self):
65 self.assertTrue(self.square4.issubset(self.unbound_poly))
66 self.assertFalse(self.square1.issubset(self.square2))
67 self.assertTrue(self.square1.issubset(self.universe))
68 self.assertTrue(self.empty.issubset(self.square1))
69
70 def test_le(self):
71 self.assertTrue(self.square4 <= self.square3)
72 self.assertFalse(self.square3 <= self.square4)
73 self.assertTrue(self.empty <= self.square1)
74 self.assertTrue(self.square1 <= self.universe)
75
76 def test_lt(self):
77 self.assertTrue(self.square4 < self.square3)
78 self.assertFalse(self.square3 < self.square4)
79 self.assertTrue(self.empty < self.square1)
80 self.assertTrue(self.square1 < self.universe)
81
82 def test_complement(self):
83 self.assertEqual(~self.square1, self.complement)
84 self.assertEqual(~self.universe, Empty)
85 self.assertEqual(~self.empty, self.universe)
86
87 def test_polyhedral_hull(self):
88 self.assertEqual(self.square1.polyhedral_hull(), self.hull)
89 self.assertEqual(self.universe.polyhedral_hull(), self.universe)
90 self.assertEqual(self.empty.polyhedral_hull(), self.empty)
91
92 def test_project_out(self):
93 self.assertEqual(self.square1.project_out(symbols('x')), self.dropped)
94 self.assertEqual(self.square1.project_out(symbols('x y')), self.universe)
95 self.assertEqual(self.universe.project_out([]), self.universe)
96 self.assertEqual(self.empty.project_out([]), Empty)
97
98 def test_simplify(self):
99 self.assertEqual(self.universe.simplify(), self.universe)
100 self.assertEqual(self.empty.simplify(), Empty)
101
102 def test_sample(self):
103 self.assertEqual(self.square6.sample(), self.sample)
104 self.assertEqual(self.empty.sample(), Empty)
105 self.assertEqual(self.universe.sample(), self.universe)
106
107 def test_intersection(self):
108 self.assertEqual(self.square1.intersection(self.square2), self.intersection)
109
110 def test_and(self):
111 self.assertEqual(self.square2 & self.square1, self.intersection)
112 self.assertEqual(self.square1 & self.universe, self.square1)
113 self.assertEqual(self.empty & self.square1, Empty)
114 self.assertEqual(self.universe & self.universe, self.universe)
115 self.assertEqual(self.universe & self.empty, Empty)
116 self.assertEqual(self.empty & self.empty, Empty)
117
118 def test_union(self):
119 self.assertEqual(self.square1.union(self.square2), self.union)
120 self.assertEqual(self.square1.union(self.empty), self.square1)
121 self.assertEqual(self.square1.union(self.universe), self.universe)
122 self.assertEqual(self.universe.union(self.universe), self.universe)
123 self.assertEqual(self.empty.union(self.empty), self.empty)
124
125 def test_or(self):
126 self.assertEqual(self.square1 | self.square2, self.union)
127
128 def test_add(self):
129 self.assertEqual(self.square2 + self.square1, self.sum1)
130 self.assertEqual(Polyhedron(self.square1 + self.square2), self.sum2)
131 self.assertEqual(self.universe + self.square1, self.universe)
132 self.assertEqual(self.empty + self.square1, self.square1)
133 self.assertEqual(self.universe + self.universe, self.universe)
134
135 def test_difference(self):
136 self.assertEqual(self.square2 - self.square1, self.difference1)
137 self.assertEqual(Polyhedron(self.square2 - self.square1), self.difference2)
138 self.assertEqual(self.square2 - self.square2, Empty)
139 self.assertEqual(self.universe - self.universe, Empty)
140
141 def test_lexmin(self):
142 self.assertEqual(self.square1.lexmin(), self.lexmin)
143 self.assertEqual(self.universe.lexmin(), self.universe)
144 self.assertEqual(self.empty.lexmin(), Empty)
145
146 def test_lexmax(self):
147 self.assertEqual(self.square1.lexmax(), self.lexmax)
148 self.assertEqual(self.universe.lexmax(), self.universe)
149 self.assertEqual(self.empty.lexmax(), Empty)