IPODwBDD
Revision | c31969cb1304d7bdab0154b4c0038ca5a017c6ca (tree) |
---|---|
Time | 2019-01-07 06:56:03 |
Author | Tatsuhiro Tsuchiya <tatsuhiro@ieee...> |
Commiter | Tatsuhiro Tsuchiya |
Build Boolean expression representing all constraints
@@ -2,6 +2,8 @@ | ||
2 | 2 | |
3 | 3 | import jdd.bdd.*; |
4 | 4 | import bits.*; |
5 | +import bits.exceptions.ProblemDenierException; | |
6 | + | |
5 | 7 | import java.util.*; |
6 | 8 | |
7 | 9 | abstract class Node { |
@@ -27,8 +29,22 @@ class NotOperator extends BooleanUnaryOperator { | ||
27 | 29 | |
28 | 30 | @Override |
29 | 31 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
30 | - // TODO Auto-generated method stub | |
31 | - return null; | |
32 | + IProblem res = null; | |
33 | + try { | |
34 | + IProblem child = Child.buildSAT(restricted, boolenVariables); | |
35 | +// if (child == null) | |
36 | +// res = Problem.trivialProblem(); | |
37 | +// else if (child.numberOfClauses() == 0) | |
38 | +// res = Problem.unsolvableProblem(); | |
39 | +// else { | |
40 | +// res = new ProblemDenier(child); | |
41 | +// } | |
42 | + res = new ProblemDenier(child); | |
43 | + } | |
44 | + catch (Exception e) { | |
45 | + e.printStackTrace(); | |
46 | + } | |
47 | + return res; | |
32 | 48 | } |
33 | 49 | } |
34 | 50 |
@@ -49,8 +65,14 @@ class IfOperator extends BooleanBinaryOperator { | ||
49 | 65 | |
50 | 66 | @Override |
51 | 67 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
52 | - // TODO Auto-generated method stub | |
53 | - return null; | |
68 | + IProblem res = null; | |
69 | + try { | |
70 | + IProblem tmp = new ProblemDenier(Left.buildSAT(restricted, boolenVariables)); | |
71 | + res = new Disjunction(tmp, Right.buildSAT(restricted, boolenVariables)); | |
72 | + } catch (Exception e) { | |
73 | + e.printStackTrace(); | |
74 | + } | |
75 | + return res; | |
54 | 76 | } |
55 | 77 | } |
56 | 78 |
@@ -67,8 +89,14 @@ class EqualityOperator extends BooleanBinaryOperator { | ||
67 | 89 | |
68 | 90 | @Override |
69 | 91 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
70 | - // TODO Auto-generated method stub | |
71 | - return null; | |
92 | + IProblem res = null; | |
93 | + try { | |
94 | + res = new ProblemDenier(new ExclusiveDisjunction(Left.buildSAT(restricted, boolenVariables), | |
95 | + Right.buildSAT(restricted, boolenVariables))); | |
96 | + } catch (Exception e) { | |
97 | + e.printStackTrace(); | |
98 | + } | |
99 | + return res; | |
72 | 100 | } |
73 | 101 | } |
74 | 102 |
@@ -85,8 +113,14 @@ class InequalityOperator extends BooleanBinaryOperator { | ||
85 | 113 | |
86 | 114 | @Override |
87 | 115 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
88 | - // TODO Auto-generated method stub | |
89 | - return null; | |
116 | + IProblem res = null; | |
117 | + try { | |
118 | + res = new ExclusiveDisjunction(Left.buildSAT(restricted, boolenVariables), | |
119 | + Right.buildSAT(restricted, boolenVariables)); | |
120 | + } catch (Exception e) { | |
121 | + e.printStackTrace(); | |
122 | + } | |
123 | + return res; | |
90 | 124 | } |
91 | 125 | } |
92 | 126 |
@@ -107,10 +141,22 @@ class IfthenelseOperator extends BooleanTrinaryOperator { | ||
107 | 141 | return f; |
108 | 142 | } |
109 | 143 | |
144 | + | |
110 | 145 | @Override |
111 | 146 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
112 | - // TODO Auto-generated method stub | |
113 | - return null; | |
147 | + // if p then q else r = (!p || q) && (p || r) | |
148 | + IProblem res = null; | |
149 | + try { | |
150 | + IProblem p = Left.buildSAT(restricted, boolenVariables); | |
151 | + IProblem q = Middle.buildSAT(restricted, boolenVariables); | |
152 | + IProblem r = Right.buildSAT(restricted, boolenVariables); | |
153 | + IProblem ifPart = new Disjunction(new ProblemDenier(p), q); | |
154 | + IProblem thenPart = new Disjunction(p, r); | |
155 | + res = new Conjunction(ifPart, thenPart); | |
156 | + } catch (Exception e) { | |
157 | + e.printStackTrace(); | |
158 | + } | |
159 | + return res; | |
114 | 160 | } |
115 | 161 | } |
116 | 162 |
@@ -140,21 +186,36 @@ class OrOperator extends BooleanMultinaryOperator { | ||
140 | 186 | } |
141 | 187 | |
142 | 188 | @Override |
189 | +// IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { | |
190 | +// IProblem res = null; | |
191 | +// try { | |
192 | +// res = new Disjunction(ChildList.get(0).buildSAT(restricted, boolenVariables), | |
193 | +// ChildList.get(1).buildSAT(restricted, boolenVariables)); | |
194 | +// for (int i = 2; i < ChildList.size(); i++) { | |
195 | +// res = new Disjunction(res, ChildList.get(i).buildSAT(restricted, boolenVariables)); | |
196 | +// } | |
197 | +// | |
198 | +// } | |
199 | +// catch (Exception e) { | |
200 | +// e.printStackTrace(); | |
201 | +// } | |
202 | +// return res; | |
203 | +// } | |
143 | 204 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
144 | 205 | IProblem res = null; |
145 | 206 | try { |
146 | - res = new Disjunction(ChildList.get(0).buildSAT(restricted, boolenVariables), | |
147 | - ChildList.get(1).buildSAT(restricted, boolenVariables)); | |
148 | - for (int i = 2; i < ChildList.size(); i++) { | |
149 | - res = new Disjunction(res, ChildList.get(i).buildSAT(restricted, boolenVariables)); | |
207 | + IProblem[] problemArray = new IProblem[ChildList.size()]; | |
208 | + for (int i = 0; i < ChildList.size(); i++) { | |
209 | + problemArray[i] = ChildList.get(i).buildSAT(restricted, boolenVariables); | |
150 | 210 | } |
151 | - | |
211 | + res = new Disjunction(problemArray); | |
152 | 212 | } |
153 | 213 | catch (Exception e) { |
154 | 214 | e.printStackTrace(); |
155 | 215 | } |
156 | 216 | return res; |
157 | 217 | } |
218 | + | |
158 | 219 | } |
159 | 220 | |
160 | 221 | class AndOperator extends BooleanMultinaryOperator { |
@@ -178,22 +239,39 @@ class AndOperator extends BooleanMultinaryOperator { | ||
178 | 239 | return f; |
179 | 240 | } |
180 | 241 | |
242 | +// @Override | |
243 | +// IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { | |
244 | +// IProblem res = null; | |
245 | +// try { | |
246 | +// res = new Conjunction(ChildList.get(0).buildSAT(restricted, boolenVariables), | |
247 | +// ChildList.get(1).buildSAT(restricted, boolenVariables)); | |
248 | +// for (int i = 2; i < ChildList.size(); i++) { | |
249 | +// res = new Conjunction(res, ChildList.get(i).buildSAT(restricted, boolenVariables)); | |
250 | +// } | |
251 | +// | |
252 | +// } | |
253 | +// catch (Exception e) { | |
254 | +// e.printStackTrace(); | |
255 | +// } | |
256 | +// return res; | |
257 | +// } | |
258 | + | |
181 | 259 | @Override |
182 | 260 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
183 | 261 | IProblem res = null; |
184 | 262 | try { |
185 | - res = new Conjunction(ChildList.get(0).buildSAT(restricted, boolenVariables), | |
186 | - ChildList.get(1).buildSAT(restricted, boolenVariables)); | |
187 | - for (int i = 2; i < ChildList.size(); i++) { | |
188 | - res = new Conjunction(res, ChildList.get(i).buildSAT(restricted, boolenVariables)); | |
263 | + IProblem[] problemArray = new IProblem[ChildList.size()]; | |
264 | + for (int i = 0; i < ChildList.size(); i++) { | |
265 | + problemArray[i] = ChildList.get(i).buildSAT(restricted, boolenVariables); | |
189 | 266 | } |
190 | - | |
267 | + res = new Conjunction(problemArray); | |
191 | 268 | } |
192 | 269 | catch (Exception e) { |
193 | 270 | e.printStackTrace(); |
194 | 271 | } |
195 | 272 | return res; |
196 | 273 | } |
274 | + | |
197 | 275 | } |
198 | 276 | |
199 | 277 | abstract class AtomicExpression extends Node { |
@@ -214,13 +292,21 @@ class TrueValue extends Constant { | ||
214 | 292 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
215 | 293 | IProblem res = null; |
216 | 294 | try { |
217 | - IBooleanVariable dum = BooleanVariable.getBooleanVariable("dum"); | |
218 | - res = new Problem(new IClause[] { new Clause().or(dum), new Clause().orNot(dum) }); | |
295 | + res = Problem.trivialProblem(); | |
219 | 296 | } catch (Exception e) { |
220 | 297 | e.printStackTrace(); |
221 | 298 | } |
222 | 299 | return res; |
223 | 300 | } |
301 | +// IProblem res = null; | |
302 | +// try { | |
303 | +// IBooleanVariable dum = BooleanVariable.getBooleanVariable("dum"); | |
304 | +// res = new Problem(new IClause[] { new Clause().or(dum), new Clause().orNot(dum) }); | |
305 | +// } catch (Exception e) { | |
306 | +// e.printStackTrace(); | |
307 | +// } | |
308 | +// return res; | |
309 | +// } | |
224 | 310 | } |
225 | 311 | |
226 | 312 | class FalseValue extends Constant { |
@@ -236,13 +322,23 @@ class FalseValue extends Constant { | ||
236 | 322 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
237 | 323 | IProblem res = null; |
238 | 324 | try { |
239 | - IBooleanVariable dum = BooleanVariable.getBooleanVariable("dum"); | |
240 | - res = new Problem(new IClause[] { new Clause().or(dum), new Clause().orNot(dum) }); | |
325 | + res = Problem.unsolvableProblem(); | |
241 | 326 | } catch (Exception e) { |
242 | 327 | e.printStackTrace(); |
243 | 328 | } |
244 | 329 | return res; |
245 | 330 | } |
331 | + | |
332 | +// IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { | |
333 | +// IProblem res = null; | |
334 | +// try { | |
335 | +// IBooleanVariable dum = BooleanVariable.getBooleanVariable("dum"); | |
336 | +// res = new Problem(new IClause[] { new Clause().or(dum), new Clause().orNot(dum) }); | |
337 | +// } catch (Exception e) { | |
338 | +// e.printStackTrace(); | |
339 | +// } | |
340 | +// return res; | |
341 | +// } | |
246 | 342 | } |
247 | 343 | |
248 | 344 | abstract class ComparisonOfValueAndValue extends AtomicExpression { |
@@ -298,15 +394,21 @@ class EqualityOfParameterAndValue extends ComparisonOfParameterAndValue { | ||
298 | 394 | |
299 | 395 | @Override |
300 | 396 | IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) { |
301 | - // TODO Auto-generated method stub | |
302 | 397 | // System.out.println("[" + p +"] = " + v); |
398 | + // pは(絶対値で)パラメータの番号が既にはいっている | |
399 | + int num = 0; | |
400 | + for (ParaIDAndNumLevels i: restricted) { | |
401 | + if (i.getID() == p) { | |
402 | + break; | |
403 | + } | |
404 | + num++; | |
405 | + } | |
303 | 406 | |
304 | 407 | IProblem res = null; |
305 | 408 | try { |
306 | - res = new BitFixer(boolenVariables[p][v], true); | |
307 | - System.out.println(res); | |
409 | + res = new BitFixer(boolenVariables[num][v], true); | |
410 | + // System.out.println(res); | |
308 | 411 | } catch (Exception e) { |
309 | - // TODO Auto-generated catch block | |
310 | 412 | e.printStackTrace(); |
311 | 413 | } |
312 | 414 | return res; |
@@ -14,6 +14,9 @@ class SATConstraintHandler implements ConstraintHandler { | ||
14 | 14 | // BooleanVariables |
15 | 15 | private IBooleanVariable[][] booleanVariables = null; |
16 | 16 | |
17 | + // Constraints | |
18 | + private IProblem booleanConstraint = null; | |
19 | + | |
17 | 20 | SATConstraintHandler(PList parameterList, List<Node> constraintList, |
18 | 21 | TreeSet<Integer> constrainedParametersArg) { |
19 | 22 | // set -> arrayList |
@@ -34,31 +37,40 @@ class SATConstraintHandler implements ConstraintHandler { | ||
34 | 37 | for (int j = 0; j < pidlevels.getNumLevels(); j++) { |
35 | 38 | String str = "p" + pidlevels.getID() + "_" + j; |
36 | 39 | booleanVariables[i][j] = BooleanVariable.getBooleanVariable(str); |
37 | - System.err.println(str); | |
40 | + // System.err.println(str); | |
38 | 41 | } |
42 | + i++; | |
39 | 43 | } |
40 | 44 | |
41 | - IBooleanVariable x = BooleanVariable.getBooleanVariable("x"); | |
42 | - IBooleanVariable y = BooleanVariable.getBooleanVariable("y"); | |
43 | - IBooleanVariable z = BooleanVariable.getBooleanVariable("z"); | |
44 | - // Construct the object that implements the constraint x & y = z : | |
45 | - IProblem bitAnder1 = new BitAnder(x, y, z); | |
46 | - System.out.println(bitAnder1); | |
47 | - System.out.println(bitAnder1.solve(Problem.defaultSolver())); | |
45 | +// IBooleanVariable x = BooleanVariable.getBooleanVariable("x"); | |
46 | +// IBooleanVariable y = BooleanVariable.getBooleanVariable("y"); | |
47 | +// IBooleanVariable z = BooleanVariable.getBooleanVariable("z"); | |
48 | +// // Construct the object that implements the constraint x & y = z : | |
49 | +// IProblem bitAnder1 = new BitAnder(x, y, z); | |
50 | +// System.out.println(bitAnder1); | |
51 | +// System.out.println(bitAnder1.solve(Problem.defaultSolver())); | |
48 | 52 | } catch (Exception e) { |
49 | 53 | // TODO Auto-generated catch block |
50 | 54 | e.printStackTrace(); |
51 | 55 | } |
52 | 56 | |
53 | - setSATConstraint(constraintList); | |
57 | + booleanConstraint = setSATConstraint(constraintList); | |
54 | 58 | } |
55 | 59 | |
56 | - private int setSATConstraint(List<Node> constraintList) { | |
60 | + private IProblem setSATConstraint(List<Node> constraintList) { | |
57 | 61 | // 制約式の論理積をとる |
58 | - for (Node n : constraintList) { | |
59 | - IProblem g = n.buildSAT(parameterToIDAndNumLevels, booleanVariables); | |
62 | + IProblem[] problemArray = new IProblem[constraintList.size()]; | |
63 | + for (int i = 0; i < constraintList.size(); i++) { | |
64 | + problemArray[i] = constraintList.get(i).buildSAT(parameterToIDAndNumLevels, booleanVariables); | |
65 | + } | |
66 | + IProblem res = null; | |
67 | + try { | |
68 | + res = new Conjunction(problemArray); | |
69 | + } | |
70 | + catch (Exception e) { | |
71 | + e.printStackTrace(); | |
60 | 72 | } |
61 | - return 0; | |
73 | + return res; | |
62 | 74 | } |
63 | 75 | |
64 | 76 | @Override |