99
1010/**
1111 *
12- * Artificial Intelligence A Modern Approach (3rd Ed.): Figure 6.11, Page 224.<br>
12+ * Artificial Intelligence A Modern Approach (3rd Ed.): Figure 6.11, Page
13+ * 224.<br>
1314 * <br>
1415 *
1516 * <pre>
2829 * if there is no consistent value then return failure
2930 * return assignment
3031 * </code>
32+ *
3133 * <pre>
3234 *
33- * Figure 6.11 The TREE-CSP-SOLVER algorithm for solving tree-structured CSPs. If the
34- * CSP has a solution, we will find it in linear time; if not, we will detect
35- * a contradiction.
35+ * Figure 6.11 The TREE-CSP-SOLVER algorithm for solving tree-structured CSPs.
36+ * If the CSP has a solution, we will find it in linear time; if not, we will
37+ * detect a contradiction.
3638 *
3739 * @author Anurag Rai
3840 *
3941 */
4042public class TreeCSPSolver extends SolutionStrategy {
4143
42- public static int [] parent ;
43-
4444 @ Override
4545 public Assignment solve (CSP csp ) {
4646
@@ -49,7 +49,6 @@ public Assignment solve(CSP csp) {
4949 List <Variable > l = csp .getVariables ();
5050 // Calculate the size
5151 int n = l .size ();
52- parent = new int [n ];
5352 // Select a random root from the List of Vaiables
5453 Variable root = Util .selectRandomlyFromList (l );
5554 // Sort the variables in topological order
@@ -95,33 +94,16 @@ public Assignment solve(CSP csp) {
9594 return assignment ;
9695 }
9796
98- private boolean makeArcConsistent (Variable xi , Variable xj , Constraint constraint , CSP csp ,
99- DomainRestoreInfo info ) {
100- boolean revised = false ;
101- Assignment assignment = new Assignment ();
102- for (Object iValue : csp .getDomain (xi )) {
103- assignment .setAssignment (xi , iValue );
104- boolean consistentExtensionFound = false ;
105- for (Object jValue : csp .getDomain (xj )) {
106- assignment .setAssignment (xj , jValue );
107- if (constraint .isSatisfiedWith (assignment )) {
108- consistentExtensionFound = true ;
109- break ;
110- }
111- }
112- if (!consistentExtensionFound ) {
113- info .storeDomainFor (xi , csp .getDomain (xi ));
114- csp .removeValueFromDomain (xi , iValue );
115- revised = true ;
116- }
117- }
118- return revised ;
119- }
97+ //
98+ // Supporting Code
99+ protected int [] parent ;
120100
121101 // Since the graph is a tree, topologicalSort is:
122102 // Level order traversal of the tree OR BFS on tree OR Pre-oder
123103 protected List <Variable > topologicalSort (CSP csp , List <Variable > l , Variable root ) {
124-
104+ // Track the parents
105+ parent = new int [l .size ()];
106+
125107 List <Variable > result = new ArrayList <>();
126108 Queue <Variable > q = new LinkedList <>(); // FIFO-Queue
127109
@@ -154,4 +136,27 @@ protected List<Variable> topologicalSort(CSP csp, List<Variable> l, Variable roo
154136 }
155137 return result ;
156138 }
139+
140+ protected boolean makeArcConsistent (Variable xi , Variable xj , Constraint constraint , CSP csp ,
141+ DomainRestoreInfo info ) {
142+ boolean revised = false ;
143+ Assignment assignment = new Assignment ();
144+ for (Object iValue : csp .getDomain (xi )) {
145+ assignment .setAssignment (xi , iValue );
146+ boolean consistentExtensionFound = false ;
147+ for (Object jValue : csp .getDomain (xj )) {
148+ assignment .setAssignment (xj , jValue );
149+ if (constraint .isSatisfiedWith (assignment )) {
150+ consistentExtensionFound = true ;
151+ break ;
152+ }
153+ }
154+ if (!consistentExtensionFound ) {
155+ info .storeDomainFor (xi , csp .getDomain (xi ));
156+ csp .removeValueFromDomain (xi , iValue );
157+ revised = true ;
158+ }
159+ }
160+ return revised ;
161+ }
157162}
0 commit comments