|
| 1 | +package aima.core.search.csp; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Queue; |
| 7 | + |
| 8 | +import aima.core.util.Util; |
| 9 | + |
| 10 | +/** |
| 11 | + * |
| 12 | + * Artificial Intelligence A Modern Approach (3rd Ed.): Figure 6.11, Page 224.<br> |
| 13 | + * <br> |
| 14 | + * |
| 15 | + * <pre> |
| 16 | + * <code> |
| 17 | + * function TREE-CSP-SOLVER(csp) returns a solution, or failure |
| 18 | + * inputs: csp, a CSP with components X, D, C |
| 19 | + * n ← number of variables in X |
| 20 | + * assignment ← an empty assignment |
| 21 | + * root ← any variable in X |
| 22 | + * X ← TOPOLOGICALSORT(X, root ) |
| 23 | + * for j = n down to 2 do |
| 24 | + * MAKE-ARC-CONSISTENT(PARENT(Xj),Xj ) |
| 25 | + * if it cannot be made consistent then return failure |
| 26 | + * for i = 1 to n do |
| 27 | + * assignment[Xi] ← any consistent value from Di |
| 28 | + * if there is no consistent value then return failure |
| 29 | + * return assignment |
| 30 | + * </code> |
| 31 | + * <pre> |
| 32 | + * |
| 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. |
| 36 | + * |
| 37 | + * @author Anurag Rai |
| 38 | + * |
| 39 | + */ |
| 40 | +public class TreeCSPSolver extends SolutionStrategy { |
| 41 | + |
| 42 | + public static int[] parent; |
| 43 | + |
| 44 | + @Override |
| 45 | + public Assignment solve(CSP csp) { |
| 46 | + |
| 47 | + Assignment assignment = new Assignment(); |
| 48 | + // Get the list of Variables from CSP to calculate the size |
| 49 | + List<Variable> l = csp.getVariables(); |
| 50 | + // Calculate the size |
| 51 | + int n = l.size(); |
| 52 | + parent = new int[n]; |
| 53 | + // Select a random root from the List of Vaiables |
| 54 | + Variable root = Util.selectRandomlyFromList(l); |
| 55 | + // Sort the variables in topological order |
| 56 | + l = topologicalSort(csp, l, root); |
| 57 | + |
| 58 | + DomainRestoreInfo info = new DomainRestoreInfo(); |
| 59 | + |
| 60 | + for (int i = n - 1; i >= 1; i--) { |
| 61 | + Variable var = l.get(i); |
| 62 | + // get constraints to find the parent |
| 63 | + for (Constraint constraint : csp.getConstraints(var)) { |
| 64 | + if (constraint.getScope().size() == 2) { |
| 65 | + // if the neighbour is parent |
| 66 | + if (csp.getNeighbor(var, constraint) == l.get(parent[i])) { |
| 67 | + // make it Arc Consistent |
| 68 | + if (makeArcConsistent(l.get(parent[i]), var, constraint, csp, info)) { |
| 69 | + if (csp.getDomain(l.get(parent[i])).isEmpty()) { |
| 70 | + info.setEmptyDomainFound(true); |
| 71 | + assignment = null; |
| 72 | + return assignment; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + boolean assignment_consistent = false; |
| 80 | + for (int i = 0; i < n; i++) { |
| 81 | + Variable var = l.get(i); |
| 82 | + assignment_consistent = false; |
| 83 | + for (Object value : csp.getDomain(var)) { |
| 84 | + assignment.setAssignment(var, value); |
| 85 | + if (assignment.isConsistent(csp.getConstraints(var))) { |
| 86 | + assignment_consistent = true; |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + if (!assignment_consistent) { |
| 91 | + assignment = null; |
| 92 | + return assignment; |
| 93 | + } |
| 94 | + } |
| 95 | + return assignment; |
| 96 | + } |
| 97 | + |
| 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 | + } |
| 120 | + |
| 121 | + // Since the graph is a tree, topologicalSort is: |
| 122 | + // Level order traversal of the tree OR BFS on tree OR Pre-oder |
| 123 | + protected List<Variable> topologicalSort(CSP csp, List<Variable> l, Variable root) { |
| 124 | + |
| 125 | + List<Variable> result = new ArrayList<>(); |
| 126 | + Queue<Variable> q = new LinkedList<>(); // FIFO-Queue |
| 127 | + |
| 128 | + int i = 1; |
| 129 | + int parent_index = 0; |
| 130 | + int node_count = 0; |
| 131 | + q.add(root); |
| 132 | + |
| 133 | + while (!q.isEmpty()) { |
| 134 | + |
| 135 | + node_count = q.size(); // get number of nodes in the level |
| 136 | + |
| 137 | + while (node_count > 0) { |
| 138 | + |
| 139 | + Variable var = q.remove(); |
| 140 | + result.add(var); |
| 141 | + // for each binary constraint of the Variable |
| 142 | + for (Constraint constraint : csp.getConstraints(var)) { |
| 143 | + Variable neighbour = csp.getNeighbor(var, constraint); |
| 144 | + // check if neighbour is root |
| 145 | + if (result.contains(neighbour)) |
| 146 | + continue; |
| 147 | + parent[i] = parent_index; |
| 148 | + i++; |
| 149 | + q.add(neighbour); |
| 150 | + } |
| 151 | + node_count--; |
| 152 | + parent_index++; |
| 153 | + } |
| 154 | + } |
| 155 | + return result; |
| 156 | + } |
| 157 | +} |
0 commit comments