Skip to content

Commit ebf779d

Browse files
committed
Minor code clean up to tree-csp-solver related code.
1 parent 4f79acb commit ebf779d

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

aima-core/src/main/java/aima/core/search/csp/TreeCSPSolver.java

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
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>
@@ -28,19 +29,18 @@
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
*/
4042
public 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
}

aima-core/src/test/java/aima/test/core/unit/search/csp/TreeCSPSolverTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.Before;
88
import org.junit.Test;
99

10+
import aima.core.search.csp.Assignment;
1011
import aima.core.search.csp.CSP;
1112
import aima.core.search.csp.Constraint;
1213
import aima.core.search.csp.Domain;
@@ -107,6 +108,10 @@ public void testCSPSolver() {
107108
csp.setDomain(NSW, colors);
108109
csp.setDomain(V, colors);
109110

110-
System.out.println(new TreeCSPSolver().solve(csp));
111+
TreeCSPSolver treeCSPSolver = new TreeCSPSolver();
112+
Assignment assignment = treeCSPSolver.solve(csp);
113+
Assert.assertNotNull(assignment);
114+
Assert.assertTrue(assignment.isComplete(csp.getVariables()));
115+
Assert.assertTrue(assignment.isSolution(csp));
111116
}
112117
}

0 commit comments

Comments
 (0)