Improve comparison methods in LinExpr
[linpy.git] / linpy / tests / test_domains.py
1 # Copyright 2014 MINES ParisTech
2 #
3 # This file is part of LinPy.
4 #
5 # LinPy is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # LinPy is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with LinPy. If not, see <http://www.gnu.org/licenses/>.
17
18 import unittest
19
20 from ..domains import *
21 from ..linexprs import Symbol, symbols
22 from ..polyhedra import *
23
24
25 class TestDomain(unittest.TestCase):
26
27 def setUp(self):
28 x, y = symbols('x y')
29 self.square1 = Polyhedron(inequalities=[x, 2 - x, y, 2 - y])
30 self.square2 = Polyhedron(inequalities=[x - 1, 3 - x , y - 1, 3 - y]) #correct representation
31 self.square3 = Polyhedron(inequalities=[x, 3 - x, y, 3 - y])
32 self.square4 = Polyhedron(inequalities=[x - 1, 2 - x, y - 1, 2 - y])
33 self.square5 = Polyhedron(inequalities=[x - 3, 6 - x, y - 3, 6 -y])
34 self.square6 = Polyhedron(equalities=[3 - y], inequalities=[x - 1, 3 - x, y - 1])
35 self.unbound_poly = Polyhedron(inequalities=[x, 3 - x, y])
36 self.universe = Polyhedron([])
37 self.empty = Empty
38 self.disjoint = And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
39 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)))
40 self.hull = And(Ge(x, 0), Ge(-x + 2, 0), Ge(y, 0), Ge(-y + 2, 0))
41 self.dropped = And(Ge(y, 0), Ge(-y + 2, 0))
42 self.intersection = And(Ge(x - 1, 0), Ge(-x + 2, 0), Ge(y - 1, 0), Ge(-y + 2, 0))
43 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)))
44 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)))
45 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))
46 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)))
47 self.difference2 = And(Ge(x + y - 4, 0), Ge(-x + 3, 0), Ge(-y + 3, 0))
48 self.lexmin = And(Eq(y, 0), Eq(x, 0))
49 self.lexmax = And(Eq(y - 2, 0), Eq(x - 2, 0))
50
51 def test_new(self):
52 with self.assertRaises(TypeError):
53 Polyhedron(1)
54
55 def test_disjoint(self):
56 self.assertEqual(self.square1.make_disjoint(), self.disjoint)
57 self.assertEqual(self.empty.make_disjoint(), Empty)
58 self.assertEqual(self.universe.make_disjoint(), self.universe)
59
60 def test_isempty(self):
61 self.assertFalse(self.square1.isempty())
62 self.assertTrue(self.empty.isempty())
63 self.assertFalse(self.universe.isempty())
64
65 def test_isuniverse(self):
66 self.assertFalse(self.square1.isuniverse())
67 self.assertTrue(self.universe.isuniverse())
68
69 def test_isbounded(self):
70 self.assertTrue(self.square1.isbounded())
71 self.assertFalse(self.unbound_poly.isbounded())
72
73 def test_eq(self):
74 self.assertTrue(self.square1 == self.square1)
75 self.assertFalse(self.square1 == self.square2)
76 self.assertFalse(self.empty == self.universe)
77
78 def test_isdisjoint(self):
79 self.assertFalse(self.square1.isdisjoint(self.square2))
80 self.assertFalse(self.universe.isdisjoint(self.square1))
81 self.assertTrue(self.square1.isdisjoint(self.square5))
82 self.assertTrue(self.empty.isdisjoint(self.square1))
83
84 def test_issubset(self):
85 self.assertTrue(self.square4.issubset(self.unbound_poly))
86 self.assertFalse(self.square1.issubset(self.square2))
87 self.assertTrue(self.square1.issubset(self.universe))
88 self.assertTrue(self.empty.issubset(self.square1))
89
90 def test_le(self):
91 self.assertTrue(self.square4 <= self.square3)
92 self.assertFalse(self.square3 <= self.square4)
93 self.assertTrue(self.empty <= self.square1)
94 self.assertTrue(self.square1 <= self.universe)
95
96 def test_lt(self):
97 self.assertTrue(self.square4 < self.square3)
98 self.assertFalse(self.square3 < self.square4)
99 self.assertTrue(self.empty < self.square1)
100 self.assertTrue(self.square1 < self.universe)
101
102 def test_complement(self):
103 self.assertEqual(~self.square1, self.complement)
104 self.assertEqual(~self.universe, Empty)
105 self.assertEqual(~self.empty, self.universe)
106
107 def test_aspolyhedron(self):
108 self.assertEqual(self.square1.aspolyhedron(), self.hull)
109 self.assertEqual(self.universe.aspolyhedron(), self.universe)
110 self.assertEqual(self.empty.aspolyhedron(), self.empty)
111
112 def test_project(self):
113 self.assertEqual(self.square1.project(symbols('x')), self.dropped)
114 self.assertEqual(self.square1.project(symbols('x y')), self.universe)
115 self.assertEqual(self.universe.project([]), self.universe)
116 self.assertEqual(self.empty.project([]), Empty)
117
118 def test_sample(self):
119 self.assertEqual(self.square6.sample(), {Symbol('x'): 1, Symbol('y'): 3})
120 with self.assertRaises(ValueError):
121 self.empty.sample()
122 self.assertEqual(self.universe.sample(), {})
123
124 def test_intersection(self):
125 self.assertEqual(self.square1.intersection(self.square2), self.intersection)
126
127 def test_and(self):
128 self.assertEqual(self.square2 & self.square1, self.intersection)
129 self.assertEqual(self.square1 & self.universe, self.square1)
130 self.assertEqual(self.empty & self.square1, Empty)
131 self.assertEqual(self.universe & self.universe, self.universe)
132 self.assertEqual(self.universe & self.empty, Empty)
133 self.assertEqual(self.empty & self.empty, Empty)
134
135 def test_union(self):
136 self.assertEqual(self.square1.union(self.square2), self.union)
137 self.assertEqual(self.square1.union(self.empty), self.square1)
138 self.assertEqual(self.square1.union(self.universe), self.universe)
139 self.assertEqual(self.universe.union(self.universe), self.universe)
140 self.assertEqual(self.empty.union(self.empty), self.empty)
141
142 def test_or(self):
143 self.assertEqual(self.square1 | self.square2, self.union)
144
145 def test_add(self):
146 self.assertEqual(self.square2 + self.square1, self.sum1)
147 self.assertEqual(Polyhedron(self.square1 + self.square2), self.sum2)
148 self.assertEqual(self.universe + self.square1, self.universe)
149 self.assertEqual(self.empty + self.square1, self.square1)
150 self.assertEqual(self.universe + self.universe, self.universe)
151
152 def test_difference(self):
153 self.assertEqual(self.square2 - self.square1, self.difference1)
154 self.assertEqual(Polyhedron(self.square2 - self.square1), self.difference2)
155 self.assertEqual(self.square2 - self.square2, Empty)
156 self.assertEqual(self.universe - self.universe, Empty)
157
158 def test_lexmin(self):
159 self.assertEqual(self.square1.lexmin(), self.lexmin)
160 self.assertEqual(self.universe.lexmin(), self.universe)
161 self.assertEqual(self.empty.lexmin(), Empty)
162
163 def test_lexmax(self):
164 self.assertEqual(self.square1.lexmax(), self.lexmax)
165 self.assertEqual(self.universe.lexmax(), self.universe)
166 self.assertEqual(self.empty.lexmax(), Empty)