1 # Copyright 2014 MINES ParisTech
3 # This file is part of Linpy.
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.
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.
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/>.
20 from ..domains
import *
21 from ..linexprs
import Symbol
, symbols
22 from ..polyhedra
import *
25 class TestDomain(unittest
.TestCase
):
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([])
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))
52 with self
.assertRaises(TypeError):
55 def test_disjoint(self
):
56 self
.assertEqual(self
.square1
.disjoint(), self
.disjoint
)
57 self
.assertEqual(self
.empty
.disjoint(), Empty
)
58 self
.assertEqual(self
.universe
.disjoint(), self
.universe
)
60 def test_isempty(self
):
61 self
.assertFalse(self
.square1
.isempty())
62 self
.assertTrue(self
.empty
.isempty())
63 self
.assertFalse(self
.universe
.isempty())
65 def test_isuniverse(self
):
66 self
.assertFalse(self
.square1
.isuniverse())
67 self
.assertTrue(self
.universe
.isuniverse())
69 def test_isbounded(self
):
70 self
.assertTrue(self
.square1
.isbounded())
71 self
.assertFalse(self
.unbound_poly
.isbounded())
74 self
.assertTrue(self
.square1
== self
.square1
)
75 self
.assertFalse(self
.square1
== self
.square2
)
76 self
.assertFalse(self
.empty
== self
.universe
)
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
))
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
))
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
)
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
)
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
)
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
)
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
)
118 def test_simplify(self
):
119 self
.assertEqual(self
.universe
.simplify(), self
.universe
)
120 self
.assertEqual(self
.empty
.simplify(), Empty
)
122 def test_sample(self
):
123 self
.assertEqual(self
.square6
.sample(), {Symbol('x'): 1, Symbol('y'): 3})
124 with self
.assertRaises(ValueError):
126 self
.assertEqual(self
.universe
.sample(), {})
128 def test_intersection(self
):
129 self
.assertEqual(self
.square1
.intersection(self
.square2
), self
.intersection
)
132 self
.assertEqual(self
.square2
& self
.square1
, self
.intersection
)
133 self
.assertEqual(self
.square1
& self
.universe
, self
.square1
)
134 self
.assertEqual(self
.empty
& self
.square1
, Empty
)
135 self
.assertEqual(self
.universe
& self
.universe
, self
.universe
)
136 self
.assertEqual(self
.universe
& self
.empty
, Empty
)
137 self
.assertEqual(self
.empty
& self
.empty
, Empty
)
139 def test_union(self
):
140 self
.assertEqual(self
.square1
.union(self
.square2
), self
.union
)
141 self
.assertEqual(self
.square1
.union(self
.empty
), self
.square1
)
142 self
.assertEqual(self
.square1
.union(self
.universe
), self
.universe
)
143 self
.assertEqual(self
.universe
.union(self
.universe
), self
.universe
)
144 self
.assertEqual(self
.empty
.union(self
.empty
), self
.empty
)
147 self
.assertEqual(self
.square1 | self
.square2
, self
.union
)
150 self
.assertEqual(self
.square2
+ self
.square1
, self
.sum1
)
151 self
.assertEqual(Polyhedron(self
.square1
+ self
.square2
), self
.sum2
)
152 self
.assertEqual(self
.universe
+ self
.square1
, self
.universe
)
153 self
.assertEqual(self
.empty
+ self
.square1
, self
.square1
)
154 self
.assertEqual(self
.universe
+ self
.universe
, self
.universe
)
156 def test_difference(self
):
157 self
.assertEqual(self
.square2
- self
.square1
, self
.difference1
)
158 self
.assertEqual(Polyhedron(self
.square2
- self
.square1
), self
.difference2
)
159 self
.assertEqual(self
.square2
- self
.square2
, Empty
)
160 self
.assertEqual(self
.universe
- self
.universe
, Empty
)
162 def test_lexmin(self
):
163 self
.assertEqual(self
.square1
.lexmin(), self
.lexmin
)
164 self
.assertEqual(self
.universe
.lexmin(), self
.universe
)
165 self
.assertEqual(self
.empty
.lexmin(), Empty
)
167 def test_lexmax(self
):
168 self
.assertEqual(self
.square1
.lexmax(), self
.lexmax
)
169 self
.assertEqual(self
.universe
.lexmax(), self
.universe
)
170 self
.assertEqual(self
.empty
.lexmax(), Empty
)
172 def test_involves_vars(self
):
173 self
.assertTrue(self
.square1
.involves_vars(symbols('x y')))
174 self
.assertFalse(self
.empty
.involves_vars(symbols('x')))
175 self
.assertFalse(self
.universe
.involves_vars(symbols('x')))