0% found this document useful (0 votes)
10 views

Constraint Satisfaction Problem

Uploaded by

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

Constraint Satisfaction Problem

Uploaded by

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

Artificial

Intelligence

Constraint
Satisfaction
Problems
Constraint + variables
Satisfaction can have no
value!
Problems (CSPs)
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
WA ≠ NT ⇔ (WA, NT) in {(red, green), (red,
e.g.,
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

•Variables: 𝑋𝑋𝑖𝑖 for 𝑖, 𝑖𝑖 ∈ {1, 2, Xij


… , 𝑁}
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
• Variables: 𝑄𝑄1, 𝑄𝑄2, … , 𝑄𝑄𝑁
Q1 Q2 Q3 Q4
• Domains: {1, 2, … , 𝑁} # row for each
4
col. 3
• Constraints:
2
 i, j non-threatening (Qi , Qj)
1
Example:
Q1 = 2, Q2 = 4, Q3 = 1, Q4 = 3
Example: Cryptarithmetic

Puzzle Given Puzzle:
Variables: T, W, O, F, U, R
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)
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(X ij 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?
Similar to move ordering in games.
• In what order should its values be tried?
• Can we detect inevitable failure early? Tree pruning (like in alpha-beta search)
Variable/Value Ordering

Which variable should be assigned next?


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

In which order should its values be tried?


• Choose the least constraining value:
• The value that rules out the fewest values in the remaining
variables.
Early Detection of
Forward
Failure: 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-
Backtracking Search With
Ordering and Early Failure
Detection

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 Local Search works only with
• Allow incomplete states.
• States must satisfy all constraints.
vs. • Only “complete” states (all variables assigned)
• Allows states with unsatisfied constraints.

Local search can attempt to reduce unsatisfied constraints 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