foundations of computational agents
The third edition of Artificial Intelligence: foundations of computational agents, Cambridge University Press, 2023 is now available (including full text).
Although depth-first search over the search space of assignments is usually a substantial improvement over generate and test, it still has various inefficiencies that can be overcome.
In Example 4.12, the variables ${A}$ and ${B}$ are related by the constraint $$. The assignment ${A}{\mathrm{=}}{\mathrm{4}}$ is inconsistent with each of the possible assignments to ${B}$ because ${\text{\mathit{d}\mathit{o}\mathit{m}}}{\mathit{}}{\mathrm{(}}{B}{\mathrm{)}}{\mathrm{=}}{\mathrm{\{}}{\mathrm{1}}{\mathrm{,}}{\mathrm{2}}{\mathrm{,}}{\mathrm{3}}{\mathrm{,}}{\mathrm{4}}{\mathrm{\}}}$. In the course of the backtrack search (see Figure 4.1), this fact is rediscovered for different assignments to ${B}$ and ${C}$. This inefficiency can be avoided by the simple expedient of deleting ${\mathrm{4}}$ from ${\text{\mathit{d}\mathit{o}\mathit{m}}}{\mathit{}}{\mathrm{(}}{A}{\mathrm{)}}$, once and for all. This idea is the basis for the consistency algorithms.
The consistency algorithms are best thought of as operating over the network of constraints formed by the CSP:
There is a node for each variable. These nodes are drawn as circles or ovals.
There is a node for each constraint. These nodes are drawn as rectangles.
Associated with each variable, $X$, is a set ${D}_{X}$ of possible values. This set of values is initially the domain of the variable.
For every constraint $c$, and for every variable $X$ in the scope of $c$, there is an arc $$.
Such a network is called a constraint network.
Consider Example 4.12. There are three variables ${A}$, ${B}$, and ${C}$, each with domain ${\mathrm{\{}}{\mathrm{1}}{\mathrm{,}}{\mathrm{2}}{\mathrm{,}}{\mathrm{3}}{\mathrm{,}}{\mathrm{4}}{\mathrm{\}}}$. The constraints are $$ and $$. In the constraint network, shown in Figure 4.2, there are four arcs:
$$ | ||
$$ | ||
$$ | ||
$$ |
The constraint ${X}{\mathrm{\ne}}{\mathrm{4}}$ has one arc:
$$ |
The constraint ${X}{\mathrm{+}}{Y}{\mathrm{=}}{Z}$ has three arcs:
$$ | ||
$$ | ||
$$ |
In the simplest case, when a constraint has just one variable in its scope, the arc is domain consistent if every value of the variable satisfies the constraint.
The constraint ${B}{\mathrm{\ne}}{\mathrm{3}}$ has scope ${\mathrm{\{}}{B}{\mathrm{\}}}$. With this constraint, and with ${{D}}_{{B}}{\mathrm{=}}{\mathrm{\{}}{\mathrm{1}}{\mathrm{,}}{\mathrm{2}}{\mathrm{,}}{\mathrm{3}}{\mathrm{,}}{\mathrm{4}}{\mathrm{\}}}$, the arc $$ is not domain consistent because ${B}{\mathrm{=}}{\mathrm{3}}$ violates the constraint. If the value 3 were removed from the domain of ${B}$, then it would be domain consistent.
Suppose constraint $c$ has scope $\{X,{Y}_{1},\mathrm{\dots},{Y}_{k}\}$. Arc $$ is arc consistent if, for each value $x\in {D}_{X}$, there are values ${y}_{1},\mathrm{\dots},{y}_{k}$ where ${y}_{i}\in {D}_{{Y}_{i}}$, such that $c(X=x,{Y}_{1}={y}_{1},\mathrm{\dots},{Y}_{k}={y}_{k})$ is satisfied. A network is arc consistent if all its arcs are arc consistent.
Consider the network of Example 4.14 shown in Figure 4.2. None of the arcs are arc consistent. The first arc is not arc consistent because for ${A}{\mathrm{=}}{\mathrm{4}}$ there is no corresponding value for ${B}$ for which $$. If ${\mathrm{4}}$ were removed from the domain of ${A}$, then it would be arc consistent. The second arc is not arc consistent because there is no corresponding value for ${A}$ when ${B}{\mathrm{=}}{\mathrm{1}}$.
If an arc $$ is not arc consistent, there are some values of $X$ for which there are no values for ${Y}_{1},\mathrm{\dots},{Y}_{k}$ for which the constraint holds. In this case, all values of $X$ in ${D}_{X}$ for which there are no corresponding values for the other variables can be deleted from ${D}_{X}$ to make the arc $$ consistent. When a value is removed from a domain, this may make some other arcs that were previously consistent no longer consistent.
The generalized arc consistency (GAC) algorithm is given in Figure 4.3. It makes the entire network arc consistent by considering a set to_do of potentially inconsistent arcs, the to-do arcs. The set to_do initially consists of all the arcs in the graph. While the set is not empty, an arc $$ is removed from the set and considered. If the arc is not arc consistent, it is made arc consistent by pruning the domain of variable $X$. All of the previously consistent arcs that could, as a result of pruning $X$, have become inconsistent are placed back into the set to_do. These are the arcs $$, where ${c}^{\prime}$ is a constraint different from $c$ that involves $X$, and $Z$ is a variable involved in ${c}^{\prime}$ other than $X$.
Consider the algorithm GAC operating on the network from Example 4.14, with constraints $$, $$. Initially, all of the arcs are in the to_do set. Here is one possible sequence of selections of arcs.
Suppose the algorithm first selects the arc $$. For ${A}{=}{4}$, there is no value of ${B}$ that satisfies the constraint. Thus, ${4}$ is pruned from the domain of ${A}$. Nothing is added to to_do because there is no other arc not in to_do.
Suppose that $$ is selected next. The value ${1}$ is pruned from the domain of ${B}$. Again no arc is added to to_do.
Suppose that $$ is selected next. The value ${4}$ is removed from the domain of ${B}$. Because the domain of ${B}$ has been reduced, the arc $$ must be added back into the to_do set because the domain of ${A}$ could potentially be reduced further now that the domain of ${B}$ is smaller.
If the arc $$ is selected next, the value ${A}{=}{3}$ is pruned from the domain of ${A}$.
The remaining arc on to_do is $$. The values ${1}$ and ${2}$ are removed from the domain of ${C}$. No arcs are added to to_do because ${C}$ is not involved in any other constraints, and to_do becomes empty.
The algorithm then terminates with ${{D}}_{{A}}{\mathrm{=}}{\mathrm{\{}}{\mathrm{1}}{\mathrm{,}}{\mathrm{2}}{\mathrm{\}}}$, ${{D}}_{{B}}{\mathrm{=}}{\mathrm{\{}}{\mathrm{2}}{\mathrm{,}}{\mathrm{3}}{\mathrm{\}}}$, ${{D}}_{{C}}{\mathrm{=}}{\mathrm{\{}}{\mathrm{3}}{\mathrm{,}}{\mathrm{4}}{\mathrm{\}}}$. Although this has not fully solved the problem, it has greatly simplified it. For example, depth-first backtrackingsearch would now solve the problem more efficiently.
Consider applying GAC to the scheduling problem of Example 4.9. The network shown in Figure 4.4 has already been made domain consistent (the value 3 has been removed from the domain of ${B}$ and ${\mathrm{2}}$ has been removed from the domain of ${C}$). Suppose arc $$ is considered first. The arc is not arc consistent because ${D}{\mathrm{=}}{\mathrm{1}}$ is not consistent with any value for ${C}$ in ${{D}}_{{C}}$, so 1 is deleted from ${{D}}_{{D}}$. ${{D}}_{{D}}$ becomes ${\mathrm{\{}}{\mathrm{2}}{\mathrm{,}}{\mathrm{3}}{\mathrm{,}}{\mathrm{4}}{\mathrm{\}}}$ and arcs $$, and $$ could be added to to_do but they are in to_do already.
Suppose arc $$ is considered next; then ${{D}}_{{C}}$ is reduced to ${\mathrm{\{}}{\mathrm{3}}{\mathrm{,}}{\mathrm{4}}{\mathrm{\}}}$ and arc $$ goes back into the to_do set to be reconsidered.
Suppose arc $$ is next; then ${{D}}_{{D}}$ is further reduced to the singleton ${\mathrm{\{}}{\mathrm{4}}{\mathrm{\}}}$. Processing arc $$ prunes ${{D}}_{{C}}$ to ${\mathrm{\{}}{\mathrm{3}}{\mathrm{\}}}$. Making arc $$ consistent reduces ${{D}}_{{A}}$ to ${\mathrm{\{}}{\mathrm{4}}{\mathrm{\}}}$. Processing $$ reduces ${{D}}_{{B}}$ to ${\mathrm{\{}}{\mathrm{1}}{\mathrm{,}}{\mathrm{2}}{\mathrm{\}}}$. Then arc $$ reduces ${{D}}_{{B}}$ to ${\mathrm{\{}}{\mathrm{2}}{\mathrm{\}}}$. Finally, arc $$ reduces ${{D}}_{{E}}$ to ${\mathrm{\{}}{\mathrm{1}}{\mathrm{\}}}$. All arcs remaining in the queue are consistent, and so the algorithm terminates with the to_do set empty. The set of reduced variable domains is returned. In this case, the domains all have size 1 and there is a unique solution: ${A}{\mathrm{=}}{\mathrm{4}}$, ${B}{\mathrm{=}}{\mathrm{2}}$, ${C}{\mathrm{=}}{\mathrm{3}}$, ${D}{\mathrm{=}}{\mathrm{4}}$, ${E}{\mathrm{=}}{\mathrm{1}}$.
Regardless of the order in which the arcs are considered, the algorithm will terminate with the same result, namely, an arc consistent network and the same set of reduced domains. Three cases are possible, depending on the state of the network upon termination:
In the first case, one domain becomes empty, indicating there is no solution to the CSP. Note that, as soon as any one domain becomes empty, all the domains of connected nodes will become empty before the algorithm terminates.
In the second case, each domain has a singleton value, indicating that there is a unique solution to the CSP, as in Example 4.19.
In the third case, every domain is non-empty and at least one has multiple values left in it. In this case, we do not know whether there is a solution or what the solutions look like (unless just a single domain has multiple values). Methods to solve the problem in this case are explored in the following sections.
The following example shows that it is possible for a network to be arc consistent even though there is no solution.
Suppose there are three variables, ${A}$, ${B}$ and ${C}$, each with the domain ${\mathrm{\{}}{\mathrm{1}}{\mathrm{,}}{\mathrm{2}}{\mathrm{,}}{\mathrm{3}}{\mathrm{\}}}$. Consider the constraints ${A}{\mathrm{=}}{B}$, ${B}{\mathrm{=}}{C}$, and ${A}{\mathrm{\ne}}{C}$. This is arc consistent: no domain can be pruned using any single constraint. However, there are no solutions. There is no assignment to the three variables that satisfies the constraints.
Consider the time complexity of the generalized arc consistency algorithm for binary constraints. Suppose there are $c$ binary constraints, and the domain of each variable is of size $d$. There are $2c$ arcs. Checking an arc $$ involves, in the worst case, iterating through each value in the domain of $Y$ for each value in the domain of $X$, which takes $O({d}^{2})$ time. This arc may need to be to be checked once for every element in the domain of $Y$, thus GAC for binary variables can be done in time $O(c{d}^{3})$, which is linear in $c$, the number of constraints. The space used is $O(nd)$ where $n$ is the number of variables and $d$ is the domain size. Exercise 4 explores the complexity of more general constraints.
Various extensions to arc consistency are also possible. The domains need not be finite; they may be specified using intensions, not just lists of their values. It is also possible to prune the constraints if the constraints are represented extensionally: if a value has been pruned for a variable $X$, this value can be pruned from all constraints that involve $X$. Higher-order consistency techniques, such as path consistency, consider $k$-tuples of variables at a time, not just pairs of variables that are connected by a constraint. For example, by considering all three variables, you can recognize that there is no solution in Example 4.20. These higher-order methods are often less efficient for solving a problem than using arc consistency augmented with the methods described below.