cs188 sp23 Note07
cs188 sp23 Note07
1. Variables - CSPs possess a set of N variables X1 , ..., XN that can each take on a single value from some
defined set of values.
2. Domain - A set {x1 , ..., xd } representing all possible values that a CSP variable can take on.
3. Constraints - Constraints define restrictions on the values of variables, potentially with regard to other
variables.
Consider the N-queens identification problem: given an N × N chessboard, can we find a configuration in
which to place N queens on the board such that no two queens attack each another?
1. Variables - Xi j , with 0 ≤ i, j < N. Each Xi j represents a grid position on our N × N chessboard, with i
and j specifying the row and column number respectively.
2. Domain - {0, 1}. Each Xi j can take on either the value 0 or 1, a boolean value representing the
existence of a queen at position (i, j) on the board.
3. Constraints -
Constraint satisfaction problems are NP-hard, which loosely means that there exists no known algorithm for
finding solutions to them in polynomial time. Given a problem with N variables with domain of size O(d) for
each variable, there are O(d N ) possible assignments, exponential in the number of variables. We can often
get around this caveat by formulating CSPs as search problems, defining states as partial assignments
(variable assignments to CSPs where some variables have been assigned values while others have not).
Correspondingly, the successor function for a CSP state outputs all states with one new variable assigned,
and the goal test verifies all variables are assigned and all constraints are satisfied in the state it’s testing.
Constraint satisfaction problems tend to have significantly more structure than traditional search problems,
and we can exploit this structure by combining the above formulation with appropriate heuristics to hone in
on solutions in a feasible amount of time.
Constraint Graphs
Let’s introduce a second CSP example: map coloring. Map coloring solves the problem where we’re given
a set of colors and must color a map such that no two adjacent states or regions have the same color.
Constraint satisfaction problems are often represented as constraint graphs, where nodes represent variables
and edges represent constraints between them. There are many different types of constraints, and each is
handled slightly differently:
• Binary Constraints - Binary constraints involve two variables. They’re represented in constraint
graphs as traditional graph edges.
• Higher-order Constraints - Constraints involving three or more variables can also be represented with
edges in a CSP graph, they just look slightly unconventional.
The constraints in this problem are simply that no two adjacent states can be the same color. As a result, by
drawing an edge between every pair of states that are adjacent to one another, we can generate the constraint
graph for the map coloring of Australia as follows:
The value of constraint graphs is that we can use them to extract valuable information about the structure of
the CSPs we are solving. By analyzing the graph of a CSP, we can determine things about it like whether
it’s sparsely or densely connected/constrained and whether or not it’s tree-structured. We’ll cover this more
in depth as we discuss solving constraint satisfaction problems in more detail.
1. Fix an ordering for variables, and select values for variables in this order. Because assignments are
commutative (e.g. assigning WA = Red, NT = Green is identical to NT = Green, WA = Red), this
is valid.
2. When selecting values for a variable, only select values that don’t conflict with any previously as-
signed values. If no such values exist, backtrack and return to the previous variable, changing its
value.
For a visualization of how this process works, consider the partial search trees for both depth first search
and backtracking search in map coloring:
Filtering
The first improvement to CSP performance we’ll consider is filtering, which checks if we can prune the
domains of unassigned variables ahead of time by removing values we know will result in backtracking.
A naïve method for filtering is forward checking, which whenever a value is assigned to a variable Xi ,
prunes the domains of unassigned variables that share a constraint with Xi that would violate the constraint
if assigned. Whenever a new variable is assigned, we can run forward checking and prune the domains
of unassigned variables adjacent to the newly assigned variable in the constraint graph. Consider our map
coloring example, with unassigned variables and their potential values:
Note how as we assign WA = red and then Q = green, the size of the domains for NT , NSW , and SA (states
adjacent to WA, Q, or both) decrease in size as values are eliminated. The idea of forward checking can be
generalized into the principle of arc consistency. For arc consistency, we interpret each undirected edge of
the constraint graph for a CSP as two directed edges pointing in opposite directions. Each of these directed
edges is called an arc. The arc consistency algorithm works as follows:
• Begin by storing all arcs in the constraint graph for the CSP in a queue Q.
• Iteratively remove arcs from Q and enforce the condition that in each removed arc Xi −→ X j , for every
remaining value v for the tail variable Xi , there is at least one remaining value w for the head variable
X j such that Xi = v, X j = w does not violate any constraints. If some value v for Xi would not work
with any of the remaining values for X j , we remove v from the set of possible values for Xi .
• If at least one value is removed for Xi when enforcing arc consistency for an arc Xi −→ X j , add arcs
of the form Xk −→ Xi to Q, for all unassigned variables Xk . If an arc Xk −→ Xi is already in Q during
this step, it doesn’t need to be added again.
• Continue until Q is empty, or the domain of some variable is empty and triggers a backtrack.
The arc consistency algorithm is typically not the most intuitive, so let’s walk through a quick example with
map coloring:
Q = [SA → V,V → SA, SA → NSW, NSW → SA, SA → NT, NT → SA,V → NSW, NSW → V ]
For our first arc, SA → V , we see that for every value in the domain of SA, {blue}, there is at least one value
in the domain of V , {red, green, blue}, that violates no constraints, and so no values need to be pruned from
SA’s domain. However, for our next arc V → SA, if we set V = blue we see that SA will have no remaining
values that violate no constraints, and so we prune blue from V ’s domain.
Because we pruned a value from the domain of V , we need to enqueue all arcs with V at the head - SA → V ,
NSW → V . Since NSW → V is already in Q, we only need to add SA → V , leaving us with our updated
queue
We can continue this process until we eventually remove the arc SA → NT from Q. Enforcing arc consis-
tency on this arc removes blue from SA’s domain, leaving it empty and triggering a backtrack. Note that the
arc NSW → SA appears before SA → NT in Q and that enforcing consistency on this arc removes blue from
the domain of NSW .
Arc consistency is typically implemented with the AC-3 algorithm (Arc Consistency Algorithm #3), for
which the pseudocode is as follows: