0% found this document useful (0 votes)
34 views20 pages

06 CSP

Uploaded by

shahzad.dar
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)
34 views20 pages

06 CSP

Uploaded by

shahzad.dar
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/ 20

CS 5/7320

Artificial Intelligence

Constraint
Satisfaction
Problems
AIMA Chapter 6

Slides by Michael Hahsler


based on Slides by Svetlana Lazepnik
with figures from the AIMA textbook

This work is licensed under a Creative Commons


Attribution-ShareAlike 4.0 International License.
Constraint satisfaction + variables
problems (CSPs) can have no
value!

Definition:
• State is defined by a set of variables Xi (= factored state description)
• Each variable can have a value from domain Di or be unassigned (partial solution).

• Constraints are a set of rules specifying allowable combinations of values for subsets of variables (e.g., 𝑋1 ≠
𝑋7 or 𝑋2 > 𝑋9 + 3)

• Solution: a state that is a


a) Consistent assignment: satisfies all constraints
b) Complete assignment: assigns value to each variable

Differences: ”generic” tree search:


• Atomic states (variables are only used to create human readable labels or calculate heuristics)
• States are always complete assignments.
• Constrains are implicit in the transition function.
Differences: Local search
• Factored representation to find local moves.
• Always complete assignments.
• Constraints may not be met.

General-purpose algorithms for CSP with more power than standard search algorithms exit.
Example: Map Coloring (Graph coloring)
Problem Constraint graph

• Variables representing state: WA, NT, Q, NSW, V, SA, T


• Variable Domains: {red, green, blue}
• Constraints: adjacent regions must have different colors
e.g.,
WA ≠ NT ⇔ (WA, NT) in {(red, green), (red, blue),
(green, red), (green, blue), (blue, red), (blue, green)}
Example: Map Coloring

Solutions are complete and consistent assignments, e.g.,

WA = red, NT = green, Q = red, NSW = green,


V = red, SA = blue, T = green
Example: N-Queens
Xij
• Variables: 𝑋𝑖𝑗 for 𝑖, 𝑗 ∈ {1, 2, … , 𝑁} j

• Domains: {0, 1} # Queen: no/yes

• Constraints:
i
i,j Xij = N
(Xij, Xik)  {(0, 0), (0, 1), (1, 0)} # cannot be in same col.
(Xij, Xkj)  {(0, 0), (0, 1), (1, 0)} # cannot be in same row. for 𝑖, 𝑗, 𝑘 ∈ {1, 2, … , 𝑁}
(Xij, Xi+k, j+k)  {(0, 0), (0, 1), (1, 0)} # cannot be diagonal
(Xij, Xi+k, j–k)  {(0, 0), (0, 1), (1, 0)} # cannot be diagonal
N-Queens: Alternative formulation
Q1 Q2 Q3 Q4
• Variables: 𝑄1 , 𝑄2 , … , 𝑄𝑁
• Domains: {1, 2, … , 𝑁} # row for each col.
4
3
• Constraints:
2
 i, j non-threatening (Qi , Q j)
1
Example:
Q1 = 2, Q2 = 4, Q3 = 1, Q4 = 3
Example: Cryptarithmetic Puzzle
• Variables: T, W, O, F, U, R Given Puzzle:
X1, X2 Find values for the letters.
• Domains: {0, 1, 2, …, 9} Each letter stands for a
• Constraints: different digit.
Alldiff(T, W, O, F, U, R) X2 X1
O + O = R + 10 * X1
W + W + X1 = U + 10 * X2
T + T + X2 = O + 10 * F
T ≠ 0, F ≠ 0
Example: Sudoku

• Variables: Xij
• Domains: {1, 2, …, 9}
• Constraints:
Alldiff(Xij in the same unit)
Alldiff(Xij in the same row)
Xij
Alldiff(Xij in the same column)
Some Popular Types of CSPs

• Boolean Satisfiability Problem (SAT)


Find variable assignments that makes a Boolean expression
(often expressed in conjunctive normal form) evaluate as true.

NP-complete
(x1 ∨ ¬x2) ∧ (¬x1 ∨ x2 ∨ x3) ∧ ¬x1 = True
• Integer Programming
Variables are restricted to integers. Find a feasible solution that
satisfies all constraints. The traveling salesman problem can be
expressed as an integer program.

• Linear Programming
Variables are continuous and constraints
are linear (in)equalities.
Find a feasible solution using, e.g.,
the simplex algorithm.
Real-world CSPs

• Assignment problems
e.g., who teaches what class for a fixed schedule. Teacher cannot
be in two classes at the same time!
• Timetable problems
e.g., which class is offered when and where? No two classes in
the same room at the same problem.
• Scheduling in transportation and production (e.g., order
of production steps).
• Many problems can naturally also be formulated as
CSPs.

• More examples of CSPs: http://www.csplib.org/


CSP as a Standard Search Formulation
State:
• Values assigned so far
Initial state:
• The empty assignment { } (all variables are
unassigned)
Successor function:
• Choose an unassigned variable and assign it a value
that does not violate any constraints
• Fail if no legal assignment is found
Goal state:
• Any complete and consistent assignment.
Backtracking search

• In CSP’s, variable assignments are commutative


For example,
[WA = red then NT = green] is the same as
[NT = green then WA = red]. → Order is not important

• We can build a search tree that assigns the value to one


variable per level.
• Tree depth n (number of variables)
• Number of leaves: dn (d is the number of values per variable)

• Depth-first search for CSPs with single-variable assignments


is called backtracking search.
Example: Backtracking search (DFS)

fail
Backtracking search algorithm

Call: Recursive-Backtracking({}, csp)

Improving backtracking efficiency:


• Which variable should be assigned next?
• In what order should its values be tried?
• Can we detect inevitable failure early?
Which variable should be assigned next?
In which order should its values be tried?
• Most constrained variable:
• Keep track of remaining legal values for unassigned variables (using
constraints)
• Choose the variable with the fewest legal values left
• A.k.a. minimum remaining values (MRV) heuristic

• Choose the least constraining value:


• The value that rules out the fewest values in the remaining variables
Early detection of failure – Forward checking
Node consistency
• Keep track of remaining legal values for unassigned variables
• Terminate search when any variable has no legal values (i.e.,
minimum remaining values = 0)

Stop and backtrack

• NT and SA cannot both be blue! This violates the constraint.


Early detection of failure – Forward checking
Arc consistency
• X is arc consistent wrt Y iff for every value of X there is some
allowed value of Y.
• Make X arc consistent wrt Y by throwing out any values of X for
which there is no allowed value of Y.
1. NWS cannot be blue
because SA has to be
blue.
2. V cannot be red because
NSW has to be red.
3. SA cannot be blue
because NT is blue.
4. Fail and backtrack

• Arc consistency detects failure earlier than node consistency


• There are more consistency checks (path consistency, K-
consistency)
Backtracking search with inference

Call: Recursive-Backtracking({}, csp)

If (inference(csp, var, assignment) == failure)


return failure
# Check consistency here (called “inference”) and backtrack if we know that the
branch will lead to failure.
Local search for CSPs
CSP algorithms allow incomplete states, but only if they satisfy all constraints.

Local Search (e.g., Hill-climbing and simulated annealing) works only with
“complete” states, i.e., all variables assigned, but we can allow states with
unsatisfied constraints.

Attempt to improve states by the min-conflicts heuristic:


1. Select a conflicted variable and
2. Choose a new value that produces violates the fewest constraints (local
improvement step)
3. Repeat till all constraints are met.

Local search is often very effective for CSPs.


Summary
• CSPs are a special type of search problem:
• States are structured and defined by a set of variables and values
assignments
• Variables can be unassigned
• Goal test defined by
• Consistency with constraints
• Completeness of assignment

• Backtracking search = depth-first search where a successor state is


generated by a consistent value assignment to a single unassigned
variable
• Starts with {} and only considers consistent assignments.
• Variable ordering and value selection heuristics can help significantly
• Forward checking prevents assignments that guarantee later failure

• Local search can be used to search the space of all complete


assignments for consistent assignments = min-conflicts heuristic.

You might also like