17e20fd0c135abbd7c8a1218f43f2ab7f80782cb
[linpy.git] / pypol / tests / test_domains.py
1 import unittest
2
3 from pypol import *
4 #from ..domains import *
5 #from ..linexprs import symbols
6 #from ..polyhedra import *
7
8
9 class TestDomain(unittest.TestCase):
10
11 def setUp(self):
12 x, y = symbols('x y')
13 self.square1 = Polyhedron(inequalities=[x, 2 - x, y, 2 - y])
14 self.square2 = Polyhedron(inequalities=[x - 1, 3 - x , y - 1, 3 - y]) #correct representation
15 self.square3 = Polyhedron(inequalities=[x, 3 - x, y, 3 - y])
16 self.square4 = Polyhedron(inequalities=[x - 1, 2 - x, y - 1, 2 - y])
17 self.square5 = Polyhedron(inequalities=[x - 3, 6 - x, y - 3, 6 -y])
18 self.square6 = Polyhedron(equalities=[3 - y], inequalities=[x - 1, 3 - x, y - 1])
19 self.unbound_poly = Polyhedron(inequalities=[x, 3 - x, y])
20 self.universe = Polyhedron([])
21 self.empty = Empty
22 self.disjoint = And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
23 self.compliment = 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)))
24 self.hull = And(Ge(x, 0), Ge(-x + 2, 0), 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.__eq__(self.square1))
56 self.assertFalse(self.square1.__eq__(self.square2))
57
58 def test_isdisjoint(self):
59 self.assertFalse(self.square1.isdisjoint(self.square2))
60 self.assertTrue(self.square1.isdisjoint(self.square5))
61
62 def test_issubset(self):
63 self.assertTrue(self.square4.issubset(self.unbound_poly))
64 self.assertFalse(self.square1.issubset(self.square2))
65
66 def test_le(self):
67 self.assertTrue(self.square4.__le__(self.square3))
68 self.assertFalse(self.square3.__le__(self.square4))
69
70 def test_lt(self):
71 self.assertTrue(self.square4.__lt__(self.square3))
72 self.assertFalse(self.square3.__lt__(self.square4))
73
74 def test_compliment(self):
75 self.assertEqual(~self.square1, self.compliment)
76
77 def test_polyhedral_hull(self):
78 self.assertEqual(self.square1.polyhedral_hull(), self.hull)
79
80 def test_simplify(self):
81 self.assertEqual(self.universe.simplify(), self.universe)
82 self.assertEqual(self.empty.simplify(), Empty)
83
84 def test_sample(self):
85 self.assertEqual(self.empty.sample(), Empty)
86 self.assertEqual(self.universe.sample(), self.universe)
87 self.assertEqual(self.square6.sample(), self.sample)
88
89 def test_intersection(self):
90 self.assertEqual(self.square1.intersection(self.square2), self.intersection)
91
92 def test_and(self):
93 self.assertEqual(self.square2 & self.square1, self.intersection)
94
95 def test_union(self):
96 self.assertEqual(self.square1.union(self.square2), self.union)
97
98 def test_or(self):
99 self.assertEqual(self.square1.__or__(self.square2), self.union)
100
101 def test_add(self):
102 self.assertEqual(self.square2.__add__(self.square1), self.sum1)
103 self.assertEqual(Polyhedron(self.square1 + self.square2), self.sum2)
104
105 def test_difference(self):
106 self.assertEqual(self.square2 - self.square1, self.difference1)
107 self.assertEqual(Polyhedron(self.square2 - self.square1), self.difference2)
108 self.assertEqual(self.square2 - self.square2, Empty)
109 self.assertEqual(self.universe - self.universe, Empty)
110
111 def test_lexmin(self):
112 self.assertEqual(self.square1.lexmin(), self.lexmin)
113
114 def test_lexmax(self):
115 self.assertEqual(self.square1.lexmax(), self.lexmax)
116