cs188 sp23 Note08
cs188 sp23 Note08
Ordering
We’ve delineated that when solving a CSP, we fix some ordering for both the variables and values involved.
In practice, it’s often much more effective to compute the next variable and corresponding value "on the fly"
with two broad principles, minimum remaining values and least constraining value:
• Minimum Remaining Values (MRV) - When selecting which variable to assign next, using an MRV
policy chooses whichever unassigned variable has the fewest valid remaining values (the most con-
strained variable). This is intuitive in the sense that the most constrained variable is most likely to run
out of possible values and result in backtracking if left unassigned, and so it’s best to assign a value to
it sooner than later.
• Least Constraining Value (LCV) - Similarly, when selecting which value to assign next, a good policy
to implement is to select the value that prunes the fewest values from the domains of the remain-
ing unassigned values. Notably, this requires additional computation (e.g. rerunning arc consis-
tency/forward checking or other filtering methods for each value to find the LCV), but can still yield
speed gains depending on usage.
Structure
A final class of improvements to solving constraint satisfaction problems are those that exploit their struc-
ture. In particular, if we’re trying to solve a tree-structured CSP (one that has no loops in its constraint
graph), we can reduce the runtime for finding a solution from O(d N ) all the way to O(nd 2 ), linear in the
number of variables. This can be done with the tree-structured CSP algorithm, outlined below:
• First, pick an arbitrary node in the constraint graph for the CSP to serve as the root of the tree (it
doesn’t matter which one because basic graph theory tells us any node of a tree can serve as a root).
• Convert all undirected edges in the tree to directed edges that point away from the root. Then linearize
(or topologically sort) the resulting directed acyclic graph. In simple terms, this just means order the
nodes of the graph such that all edges point rightwards. Noting that we select node A to be our root
and direct all edges to point away from A, this process results in the following conversion for the CSP
presented below:
• Finally, perform a forward assignment. Starting from X1 and going to Xn , assign each Xi a value
consistent with that of its parent. Because we’ve enforced arc consistency on all of these arcs, no
matter what value we select for any node, we know that its children will each all have at least one
consistent value. Hence, this iterative assignment guarantees a correct solution, a fact which can be
proven inductively without difficulty.
The tree structured algorithm can be extended to CSPs that are reasonably close to being tree-structured
with cutset conditioning. Cutset conditioning involves first finding the smallest subset of variables in a
constraint graph such that their removal results in a tree (such a subset is known as a cutset for the graph).
For example, in our map coloring example, South Australia (SA) is the smallest possible cutset:
Local Search
As a final topic of interest, backtracking search is not the only algorithm that exists for solving constraint
satisfaction problems. Another widely used algorithm is local search, for which the idea is childishly simple
but remarkably useful. Local search works by iterative improvement - start with some random assignment to
values then iteratively select a random conflicted variable and reassign its value to the one that violates the
fewest constraints until no more constraint violations exist (a policy known as the min-conflicts heuristic).
Under such a policy, constraint satisfaction problems like N-queens becomes both very time efficient and
space efficient to solve. For example, in following example with 4 queens, we arrive at a solution after only
2 iterations:
In fact, local search appears to run in almost constant time and have a high probability of success not only
for N-queens with arbitrarily large N, but also for any randomly generated CSP! However, despite these
advantages, local search is both incomplete and suboptimal and so won’t necessarily converge to an optimal
solution. Additionally, there is a critical ratio around which using local search becomes extremely expensive:
The figure above shows the one dimensional plot of an objective function on the state space. For that
function we wish to find the state that corresponds to the highest objective value. The basic idea of local
We will be covering three such algorithms, hill-climbing, simulated annealing and genetic algorithms. All
these algorithms are also used in optimization tasks to either maximize or minimize an objective function.
Hill-Climbing Search
The hill-climbing search algorithm (or steepest-ascent) moves from the current state towards a neighboring
state that increases the objective value. The algorithm does not maintain a search tree but only the states and
the corresponding values of the objective. The “greediness" of hill-climbing makes it vulnerable to being
trapped in local maxima (see figure 4.1), as locally those points appear as global maxima to the algorithm,
and plateaux (see figure 4.1). Plateaux can be categorized into “flat" areas at which no direction leads to
improvement (“flat local maxima") or flat areas from which progress can be slow (“shoulders"). Variants of
hill-climbing, like stochastic hill-climbing which selects an action randomly among the uphill moves, have
been proposed . This version of hill-climbing has been shown in practice to converge to higher maxima at
the cost of more iterations.
The probability of choosing a state to “reproduce" is propositional to the value of that state. We proceed to
select pairs of states to reproduce according to these probabilities (column (c) in Fig. 4.6). Offsprings are
generated by crossing over the parent strings at the crossover point. That crossover point is chosen randomly
for each pair. Finally, each offspring is susceptible to some random mutation with independent probability.
The pseudocode of the genetic algorithm can be seen in the following picture.
Summary
It’s important to remember that constraint satisfaction problems in general do not have an efficient algorithm
which solves them in polynomial time with respect to the number of variables involved. However, by using
various heuristics, we can often find solutions in an acceptable amount of time:
• Filtering - Filtering handles pruning the domains of unassigned variables ahead of time to prevent
unnecessary backtracking. The two important filtering techniques we’ve covered are forward checking
and arc consistency.
• Ordering - Ordering handles selection of which variable or value to assign next to make backtracking
as unlikely as possible. For variable selection, we learned about a MRV policy and for value selection
we learned about a LCV policy.
• Structure - If a CSP is tree-structured or close to tree-structured, we can run the tree-structured CSP
algorithm on it to derive a solution in linear time. Similarly, if a CSP is close to tree structured, we
can use cutset conditioning to transform the CSP into one or more independent tree-structured CSPs
and solve each of these separately.