0% found this document useful (0 votes)
25 views7 pages

cs188 sp23 Note07

Uploaded by

amanchauhan2603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

cs188 sp23 Note07

Uploaded by

amanchauhan2603
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CS 188 Introduction to Artificial Intelligence

Spring 2023 Note 7


These lecture notes are heavily based on notes originally written by Nikhil Sharma.
Last updated: January 15, 2023

Constraint Satisfaction Problems


In the previous note, we learned how to find optimal solutions to search problems, a type of planning
problem. Now, we’ll learn about solving a related class of problems, constraint satisfaction problems
(CSPs). Unlike search problems, CSPs are a type of identification problem, problems in which we must
simply identify whether a state is a goal state or not, with no regard to how we arrive at that goal. CSPs are
defined by three factors:

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?

We can formulate this problem as a CSP as follows:

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 -

CS 188, Spring 2023, Note 7 1


• ∀i, j, k (Xi j , Xik ) ∈ {(0, 0), (0, 1), (1, 0)}. This constraint states that if two variables have the same
value for i, only one of them can take on a value of 1. This effectively encapsulates the condition
that no two queens can be in the same row.
• ∀i, j, k (Xi j , Xk j ) ∈ {(0, 0), (0, 1), (1, 0)}. Almost identically to the previous constraint, this con-
straint states that if two variables have the same value for j, only one of them can take on a value
of 1, encapsulating the condition that no two queens can be in the same column.
• ∀i, j, k (Xi j , Xi+k, j+k ) ∈ {(0, 0), (0, 1), (1, 0)}. With similar reasoning as above, we can see that
this constraint and the next represent the conditions that no two queens can be in the same major
or minor diagonals, respectively.
• ∀i, j, k (Xi j , Xi+k, j−k ) ∈ {(0, 0), (0, 1), (1, 0)}.
• ∑ Xi j = N. This constraint states that we must have exactly N grid positions marked with a 1,
i, j
and all others marked with a 0, capturing the requirement that there are exactly N queens on the
board.

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:

CS 188, Spring 2023, Note 7 2


• Unary Constraints - Unary constraints involve a single variable in the CSP. They are not represented
in constraint graphs, instead simply being used to prune the domain of the variable they constrain
when necessary.

• 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.

Consider map coloring the map of Australia:

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.

CS 188, Spring 2023, Note 7 3


Solving Constraint Satisfaction Problems
Constraint satisfaction problems are traditionally solved using a search algorithm known as backtracking
search. Backtracking search is an optimization on depth first search used specifically for the problem of
constraint satisfaction, with improvements coming from two main principles:

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.

The pseudocode for how recursive backtracking works is presented below:

For a visualization of how this process works, consider the partial search trees for both depth first search
and backtracking search in map coloring:

CS 188, Spring 2023, Note 7 4


Note how DFS regretfully colors everything red before ever realizing the need for change, and even then
doesn’t move too far in the right direction towards a solution. On the other hand, backtracking search only
assigns a value to a variable if that value violates no constraints, leading to a significantly less backtracking.
Though backtracking search is a vast improvement over the brute-forcing of depth first search, we can get
more gains in speed still with further improvements through filtering, variable/value ordering, and structural
explotation.

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:

CS 188, Spring 2023, Note 7 5


We begin by adding all arcs between unassigned variables sharing a constraint to a queue Q, which gives us

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

Q = [SA → NSW, NSW → SA, SA → NT, NT → SA,V → NSW, NSW → V, SA → V ]

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:

CS 188, Spring 2023, Note 7 6


The AC-3 algorithm has a worst case time complexity of O(ed 3 ), where e is the number of arcs (directed
edges) and d is the size of the largest domain. Overall, arc consistency is more holistic of a domain pruning
technique than forward checking and leads to fewer backtracks, but requires running significantly more
computation in order to enforce. Accordingly, it’s important to take into account this tradeoff when deciding
which filtering technique to implement for the CSP you’re attempting to solve.
As an interesting parting note about consistency, arc consistency is a subset of a more generalized notion
of consistency known as k-consistency, which when enforced guarantees that for any set of k nodes in the
CSP, a consistent assignment to any subset of k − 1 nodes guarantees that the kth node will have at least
one consistent value. This idea can be further extended through the idea of strong k-consistency. A graph
that is strong k-consistent possesses the property that any subset of k nodes is not only k-consistent but
also k − 1, k − 2, . . . , 1 consistent as well. Not surprisingly, imposing a higher degree of consistency on a
CSP is more expensive to compute. Under this generalized definition for consistency, we can see that arc
consistency is equivalent to 2-consistency.

CS 188, Spring 2023, Note 7 7

You might also like