• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

IPODwBDD


Commit MetaInfo

Revisionc31969cb1304d7bdab0154b4c0038ca5a017c6ca (tree)
Time2019-01-07 06:56:03
AuthorTatsuhiro Tsuchiya <tatsuhiro@ieee...>
CommiterTatsuhiro Tsuchiya

Log Message

Build Boolean expression representing all constraints

Change Summary

Incremental Difference

--- a/src/v1/Node.java
+++ b/src/v1/Node.java
@@ -2,6 +2,8 @@
22
33 import jdd.bdd.*;
44 import bits.*;
5+import bits.exceptions.ProblemDenierException;
6+
57 import java.util.*;
68
79 abstract class Node {
@@ -27,8 +29,22 @@ class NotOperator extends BooleanUnaryOperator {
2729
2830 @Override
2931 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;
3248 }
3349 }
3450
@@ -49,8 +65,14 @@ class IfOperator extends BooleanBinaryOperator {
4965
5066 @Override
5167 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;
5476 }
5577 }
5678
@@ -67,8 +89,14 @@ class EqualityOperator extends BooleanBinaryOperator {
6789
6890 @Override
6991 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;
72100 }
73101 }
74102
@@ -85,8 +113,14 @@ class InequalityOperator extends BooleanBinaryOperator {
85113
86114 @Override
87115 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;
90124 }
91125 }
92126
@@ -107,10 +141,22 @@ class IfthenelseOperator extends BooleanTrinaryOperator {
107141 return f;
108142 }
109143
144+
110145 @Override
111146 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;
114160 }
115161 }
116162
@@ -140,21 +186,36 @@ class OrOperator extends BooleanMultinaryOperator {
140186 }
141187
142188 @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+// }
143204 IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) {
144205 IProblem res = null;
145206 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);
150210 }
151-
211+ res = new Disjunction(problemArray);
152212 }
153213 catch (Exception e) {
154214 e.printStackTrace();
155215 }
156216 return res;
157217 }
218+
158219 }
159220
160221 class AndOperator extends BooleanMultinaryOperator {
@@ -178,22 +239,39 @@ class AndOperator extends BooleanMultinaryOperator {
178239 return f;
179240 }
180241
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+
181259 @Override
182260 IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) {
183261 IProblem res = null;
184262 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);
189266 }
190-
267+ res = new Conjunction(problemArray);
191268 }
192269 catch (Exception e) {
193270 e.printStackTrace();
194271 }
195272 return res;
196273 }
274+
197275 }
198276
199277 abstract class AtomicExpression extends Node {
@@ -214,13 +292,21 @@ class TrueValue extends Constant {
214292 IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) {
215293 IProblem res = null;
216294 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();
219296 } catch (Exception e) {
220297 e.printStackTrace();
221298 }
222299 return res;
223300 }
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+// }
224310 }
225311
226312 class FalseValue extends Constant {
@@ -236,13 +322,23 @@ class FalseValue extends Constant {
236322 IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) {
237323 IProblem res = null;
238324 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();
241326 } catch (Exception e) {
242327 e.printStackTrace();
243328 }
244329 return res;
245330 }
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+// }
246342 }
247343
248344 abstract class ComparisonOfValueAndValue extends AtomicExpression {
@@ -298,15 +394,21 @@ class EqualityOfParameterAndValue extends ComparisonOfParameterAndValue {
298394
299395 @Override
300396 IProblem buildSAT(ArrayList<ParaIDAndNumLevels> restricted, IBooleanVariable[][] boolenVariables) {
301- // TODO Auto-generated method stub
302397 // 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+ }
303406
304407 IProblem res = null;
305408 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);
308411 } catch (Exception e) {
309- // TODO Auto-generated catch block
310412 e.printStackTrace();
311413 }
312414 return res;
--- a/src/v1/SATConstraintHandler.java
+++ b/src/v1/SATConstraintHandler.java
@@ -14,6 +14,9 @@ class SATConstraintHandler implements ConstraintHandler {
1414 // BooleanVariables
1515 private IBooleanVariable[][] booleanVariables = null;
1616
17+ // Constraints
18+ private IProblem booleanConstraint = null;
19+
1720 SATConstraintHandler(PList parameterList, List<Node> constraintList,
1821 TreeSet<Integer> constrainedParametersArg) {
1922 // set -> arrayList
@@ -34,31 +37,40 @@ class SATConstraintHandler implements ConstraintHandler {
3437 for (int j = 0; j < pidlevels.getNumLevels(); j++) {
3538 String str = "p" + pidlevels.getID() + "_" + j;
3639 booleanVariables[i][j] = BooleanVariable.getBooleanVariable(str);
37- System.err.println(str);
40+ // System.err.println(str);
3841 }
42+ i++;
3943 }
4044
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()));
4852 } catch (Exception e) {
4953 // TODO Auto-generated catch block
5054 e.printStackTrace();
5155 }
5256
53- setSATConstraint(constraintList);
57+ booleanConstraint = setSATConstraint(constraintList);
5458 }
5559
56- private int setSATConstraint(List<Node> constraintList) {
60+ private IProblem setSATConstraint(List<Node> constraintList) {
5761 // 制約式の論理積をとる
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();
6072 }
61- return 0;
73+ return res;
6274 }
6375
6476 @Override