diff options
author | 2019-07-14 00:56:19 +0200 | |
---|---|---|
committer | 2019-07-14 00:56:19 +0200 | |
commit | fc505b6b171a2d54c3bad6078031b028b55131d3 (patch) | |
tree | 8eaa033e053e21ca60bd7f958055cc46e93b7cba /Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu | |
parent | Try fix statecode bug (diff) | |
download | VIATRA-Generator-fc505b6b171a2d54c3bad6078031b028b55131d3.tar.gz VIATRA-Generator-fc505b6b171a2d54c3bad6078031b028b55131d3.tar.zst VIATRA-Generator-fc505b6b171a2d54c3bad6078031b028b55131d3.zip |
Polyhedron abstraction with Z3 for cardinality propagation
Diffstat (limited to 'Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu')
-rw-r--r-- | Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/cardinality/Z3PolyhedronSolverTest.xtend | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/cardinality/Z3PolyhedronSolverTest.xtend b/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/cardinality/Z3PolyhedronSolverTest.xtend new file mode 100644 index 00000000..2d159752 --- /dev/null +++ b/Tests/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/tests/cardinality/Z3PolyhedronSolverTest.xtend | |||
@@ -0,0 +1,199 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.tests.cardinality | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.Dimension | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearConstraint | ||
5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.Polyhedron | ||
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedronSaturationOperator | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedronSaturationResult | ||
8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.Z3PolyhedronSolver | ||
9 | import org.junit.After | ||
10 | import org.junit.Before | ||
11 | import org.junit.Test | ||
12 | |||
13 | import static org.junit.Assert.* | ||
14 | |||
15 | class Z3PolyhedronSolverTest { | ||
16 | var Z3PolyhedronSolver solver | ||
17 | var PolyhedronSaturationOperator operator | ||
18 | |||
19 | @Before | ||
20 | def void setUp() { | ||
21 | solver = new Z3PolyhedronSolver(false) | ||
22 | } | ||
23 | |||
24 | @After | ||
25 | def void tearDown() { | ||
26 | destroyOperatorIfExists() | ||
27 | } | ||
28 | |||
29 | @Test | ||
30 | def void singleDimensionTest() { | ||
31 | val x = new Dimension("x", 0, 1) | ||
32 | createSaturationOperator(new Polyhedron(#[x], #[], #[x])) | ||
33 | |||
34 | val result = saturate() | ||
35 | |||
36 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
37 | assertEquals(0, x.lowerBound) | ||
38 | assertEquals(1, x.upperBound) | ||
39 | } | ||
40 | |||
41 | @Test | ||
42 | def void singleDimensionNegativeValueTest() { | ||
43 | val x = new Dimension("x", -2, -1) | ||
44 | createSaturationOperator(new Polyhedron(#[x], #[], #[x])) | ||
45 | |||
46 | val result = saturate() | ||
47 | |||
48 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
49 | assertEquals(-2, x.lowerBound) | ||
50 | assertEquals(-1, x.upperBound) | ||
51 | } | ||
52 | |||
53 | @Test | ||
54 | def void singleDimensionConstraintTest() { | ||
55 | val x = new Dimension("x", null, null) | ||
56 | val constraint = new LinearConstraint(#{x -> 2}, 0, 2) | ||
57 | createSaturationOperator(new Polyhedron(#[x], #[constraint], #[x])) | ||
58 | |||
59 | val result = saturate() | ||
60 | |||
61 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
62 | assertEquals(0, x.lowerBound) | ||
63 | assertEquals(1, x.upperBound) | ||
64 | } | ||
65 | |||
66 | @Test | ||
67 | def void singleDimensionConstraintUnitCoefficientTest() { | ||
68 | val x = new Dimension("x", null, null) | ||
69 | val constraint = new LinearConstraint(#{x -> 1}, 1, 3) | ||
70 | createSaturationOperator(new Polyhedron(#[x], #[constraint], #[x])) | ||
71 | |||
72 | val result = saturate() | ||
73 | |||
74 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
75 | assertEquals(1, x.lowerBound) | ||
76 | assertEquals(3, x.upperBound) | ||
77 | } | ||
78 | |||
79 | @Test | ||
80 | def void singleDimensionConstraintIntegerTest() { | ||
81 | val x = new Dimension("x", null, null) | ||
82 | val constraint = new LinearConstraint(#{x -> 2}, 0, 3) | ||
83 | createSaturationOperator(new Polyhedron(#[x], #[constraint], #[x])) | ||
84 | |||
85 | val result = saturate() | ||
86 | |||
87 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
88 | assertEquals(0, x.lowerBound) | ||
89 | assertEquals(1, x.upperBound) | ||
90 | } | ||
91 | |||
92 | @Test | ||
93 | def void singleDimensionUnboundedFromAboveTest() { | ||
94 | val x = new Dimension("x", 0, null) | ||
95 | createSaturationOperator(new Polyhedron(#[x], #[], #[x])) | ||
96 | |||
97 | val result = saturate() | ||
98 | |||
99 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
100 | assertEquals(0, x.lowerBound) | ||
101 | assertEquals(null, x.upperBound) | ||
102 | } | ||
103 | |||
104 | @Test | ||
105 | def void singleDimensionUnboundedFromBelowTest() { | ||
106 | val x = new Dimension("x", null, 0) | ||
107 | createSaturationOperator(new Polyhedron(#[x], #[], #[x])) | ||
108 | |||
109 | val result = saturate() | ||
110 | |||
111 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
112 | assertEquals(null, x.lowerBound) | ||
113 | assertEquals(0, x.upperBound) | ||
114 | } | ||
115 | |||
116 | @Test | ||
117 | def void singleDimensionUnsatisfiableTest() { | ||
118 | val x = new Dimension("x", 0, 1) | ||
119 | val constraint = new LinearConstraint(#{x -> 2}, -2, -1) | ||
120 | createSaturationOperator(new Polyhedron(#[x], #[constraint], #[x])) | ||
121 | |||
122 | val result = saturate() | ||
123 | |||
124 | assertEquals(PolyhedronSaturationResult.EMPTY, result) | ||
125 | } | ||
126 | |||
127 | @Test | ||
128 | def void equalityConstraintTest() { | ||
129 | val x = new Dimension("x", null, null) | ||
130 | val y = new Dimension("y", 1, 2) | ||
131 | val constraint = new LinearConstraint(#{x -> 2, y -> 2}, 6, 6) | ||
132 | createSaturationOperator(new Polyhedron(#[x, y], #[constraint], #[x])) | ||
133 | |||
134 | val result = saturate() | ||
135 | |||
136 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
137 | assertEquals(1, x.lowerBound) | ||
138 | assertEquals(2, x.upperBound) | ||
139 | } | ||
140 | |||
141 | @Test | ||
142 | def void saturateConstraintTest() { | ||
143 | val x = new Dimension("x", 0, 2) | ||
144 | val y = new Dimension("y", 1, 2) | ||
145 | val constraint = new LinearConstraint(#{x -> 2, y -> 1}, 0, 8) | ||
146 | createSaturationOperator(new Polyhedron(#[x, y], #[constraint], #[constraint])) | ||
147 | |||
148 | val result = saturate() | ||
149 | |||
150 | assertEquals(PolyhedronSaturationResult.SATURATED, result) | ||
151 | assertEquals(1, constraint.lowerBound) | ||
152 | assertEquals(6, constraint.upperBound) | ||
153 | } | ||
154 | |||
155 | @Test(expected=IllegalArgumentException) | ||
156 | def void unknownVariableTest() { | ||
157 | val x = new Dimension("x", 0, 1) | ||
158 | val y = new Dimension("y", 0, 1) | ||
159 | val constraint = new LinearConstraint(#{y -> 2}, 0, 2) | ||
160 | createSaturationOperator(new Polyhedron(#[x], #[constraint], #[x])) | ||
161 | |||
162 | saturate() | ||
163 | } | ||
164 | |||
165 | @Test | ||
166 | def void unsatisfiableMultipleInheritanceTest() { | ||
167 | val x = new Dimension("x", 0, 1) | ||
168 | val y = new Dimension("y", 0, 1) | ||
169 | val z = new Dimension("z", 0, 1) | ||
170 | createSaturationOperator(new Polyhedron( | ||
171 | #[x, y, z], | ||
172 | #[ | ||
173 | new LinearConstraint(#{x -> 1, y -> 1}, 1, 1), | ||
174 | new LinearConstraint(#{x -> 1, z -> 1}, 1, 1), | ||
175 | new LinearConstraint(#{y -> 1, z -> 1}, 1, 1) | ||
176 | ], | ||
177 | #[x, y, z] | ||
178 | )) | ||
179 | |||
180 | val result = saturate() | ||
181 | |||
182 | assertEquals(PolyhedronSaturationResult.EMPTY, result) | ||
183 | } | ||
184 | |||
185 | private def createSaturationOperator(Polyhedron polyhedron) { | ||
186 | destroyOperatorIfExists() | ||
187 | operator = solver.createSaturationOperator(polyhedron) | ||
188 | } | ||
189 | |||
190 | private def destroyOperatorIfExists() { | ||
191 | if (operator !== null) { | ||
192 | operator.close | ||
193 | } | ||
194 | } | ||
195 | |||
196 | private def saturate() { | ||
197 | operator.saturate | ||
198 | } | ||
199 | } | ||