Uploaded by Kulneet Singh Choudhary

05 CSP

advertisement
ECE 4524
Constraint Satisfaction
Problems
(This packet borrows from material by Russell & Norvig, Lenaerts, Wyatt, and Dorr)
What do these problems have
in common?
Sudoku
Map coloring
2
n-queens
Outline
 Constraint Satisfaction Problems (CSP)
 Algorithm AC-3
 Backtracking search for CSPs
 Local search for CSPs
3
 Classical search problems (Chap. 3)
 State is a “black box” – use any data structure that supports successor
function, heuristic function, and goal test
 Heuristics are problem-specific
 Constraint Satisfaction Problems (Chap. 6)
 In some search problems, no explicit goal state is given;
instead, the goal test is a set of constraints on a possible solution that
must be satisfied
 The task is not to find a sequence of steps leading to a solution,
but instead to find a particular state that simultaneously satisfies all of
the given constraints
 Need to assign values to the constrained variables
(and each such assignment limits the range of subsequent choices)
 Normally the order of assignment is not of interest, although the
problem can still be regarded as a search through state space
 Allows useful general-purpose algorithms and heuristics, with more
power than standard search algorithms
 Can use a standard representation for many problems
4
Formally, a CSP is
 X = { X1, X2, …, Xn }, a finite set of variables
 D = { D1, D2, …, Dn }, a set of domains, one for
each variable (each domain is also a set)
 C, a set of constraints,
each defined as (scope, relation)
 scope - set of variables, a subset of Xi involved in
the relation
 relation - function of the scope variables that returns
True or False
5
Example:
map coloring
 Want to color each territory red, green, or blue, so that no two
adjacent territories share the same color
 Variables: ??
 Domains: ??
 Constraints: ??
6
Example:
map coloring
 Variables: X = {WA, NT, Q, NSW, V, SA, T}
 Domains:
Di = {red, green, blue}
 Constraints: adjacent regions must have different colors
7
e.g., WA ≠ NT, or
(WA,NT) ε {(red,green),(red,blue),(green,red),(green,blue),
(blue,red),(blue,green)}
Example:
map coloring
 Solutions are complete and consistent assignments
8
of values to variables
 Complete: every variable has a value
 Consistent: no constraint is violated
 e.g., WA = red, NT = green, Q = red, NSW = green,
V = red, SA = blue, T = green
CSP solutions
An assignment of values to variables can be
 consistent - violates no constraints
 partial - some variables are unassigned
 complete - all variables are assigned a value
A solution to the CSP is an assignment that is
complete and consistent
9
 A CSP can be easily expressed as a
standard search problem
 Incremental formulation
(start with this, then refine it)
 Initial state: the empty assignment {}.
 Successor function: Assign a value to an unassigned
variable such that no constraint is violated
 Goal test: if current assignment is complete, return true;
else return false
 Path cost: assume constant cost for every step
10
CSP as a standard search problem
 This is the same for all CSP’s !!!
(good)
 Assume n variables, d possible values
 With breadth-first search,
branching factor at the top level is b=nd
 Solution is found at depth n
(bad!)
(good)
(therefore depth-first search can be used)
 Path is irrelevant
(good)
 b=(n-l)d at depth l, hence n!dn leaves
(bad)
(but only
11
dn
complete assignments)
More terminology
 Domains may be continuous or discrete,
finite or infinite
 Constraints may be
 unary - only one variable in the scope
 e.g., SA ≠ green
 binary - only two variables in the scope
 e.g., SA
≠ WA
 n-ary - n variables in scope
 e.g., Sudoku row constraints, or column constraints, etc.
 global - arbitrary number of variables are in the scope
(often all of them)
(Many CSP algorithms assume binary constraints)
12
 A binary CSP is a CSP in which all of the
constraints are binary or unary
 Note: any non-binary CSP can be converted
into a binary CSP by introducing additional
variables
 A binary CSP can be represented as a
constraint graph
 one node for each variable
 an arc between two nodes indicates a constraint
involving the two variables
 a unary constraint appears as a self-referential arc
13
Example constraint graph
14
 Before starting the search,
and possibly during the search,
try to reduce the size of the problem by
examining constraints
 For binary constraints: Algorithm AC-3
15
Arc consistency algorithm AC-3
16
Small example
X1
X2 = X1 + 3
X1 != X3
X2
X3
X2 = 2X3
17
Domains:
D1 = {1, 2}
D2 = {4, 6}
D3 = {2, 4}
Backtracking search for CSPs
 A common approach
 Uninformed
 This is a form of depth-first search
 Try single-variable assignments at each step
 “Backtrack” when no legal assignments remain
 Currently, can solve n-queens for n ≈ 25
18
19
Backtracking example (first few steps)
20
Backtracking example (first few steps)
21
Backtracking example (first few steps)
22
Backtracking example (first few steps)
23
Improving performance
 Q: Can we introduce heuristics, as
in previous search problems?
 A: Yes, and this often leads to huge
gains in speed
 Which variable should be assigned next?
 In what order should its values be tried?
 Can we detect inevitable failure early?
 Can we take advantage of problem structure?
24
Most constrained variable
 Q:
Which variable should be assigned next?
 Rule: Choose the variable with the fewest legal
values
 Also called minimum remaining values (MRV)
heuristic
25
Most constraining variable
 Q:
Which variable should be assigned next?
 Rule: Select the variable that is involved in the
largest number of constraints on other
unassigned variables
 Very useful as a tie breaker.
 Also called the degree heuristic
26
Least constraining value
 Q:
In what order should its values be tried?
 Rule: Given a variable, choose the value that
leaves the maximum flexibility for subsequent
variable assignments
 Combining these heuristics makes 1000-queens
feasible
27
CSP heuristics, so far:
 Most constrained variable
 Most constraining variable
 Least constraining variable
 There is another “trick” that we can use . . .
28
Forward checking – try to detect
inevitable failure early
Idea:
 Keep track of remaining legal values for unassigned variables
 Terminate search when any variable has no legal values
29
Forward checking
Idea:
 Keep track of remaining legal values for unassigned variables
 Terminate search when any variable has no legal values
30
Forward checking
Idea:
 Keep track of remaining legal values for unassigned variables
 Terminate search when any variable has no legal values
31
Forward checking
Idea:
 Keep track of remaining legal values for unassigned variables
 Terminate search when any variable has no legal values
32
Notice that all values in the domain of SA have been eliminated
So, there is no reason to continue search in this direction
Another example that uses
forward-checking: the 4-Queens problem
1
2
3
4
X1
{1,2,3,4}
X2
{1,2,3,4}
X3
{1,2,3,4}
X4
{1,2,3,4}
1
2
3
4
[4-Queens slides copied from B.J. Dorr]
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{1,2,3,4}
X3
{1,2,3,4}
X4
{1,2,3,4}
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{ , ,3,4}
X3
{ ,2, ,4}
X4
{ ,2,3, }
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{ , ,3,4}
X3
{ ,2, ,4}
X4
{ ,2,3, }
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{1,2,3,4}
X2
{ , ,3,4}
X3
{ , , , }
X4
{ ,2, , }
1
2
3
4
Dead end
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{1,2,3,4}
X3
{1,2,3,4}
X4
{1,2,3,4}
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, ,3, }
X4
{1, ,3,4}
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, ,3, }
X4
{1, ,3,4}
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{1, ,3, }
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{1, ,3, }
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{ , ,3, }
1
2
3
4
Example: 4-Queens Problem
1
2
3
4
X1
{ ,2,3,4}
X2
{ , , ,4}
X3
{1, , , }
X4
{ , ,3, }
1
2
3
4
44
A solution has been found!
Constraint propagation
 Forward checking propagates information
from assigned to unassigned variables, but doesn't provide
early detection for all failures
 Consider the following case: NT and SA cannot both be
blue!
 Constraint propagation is the general term for
repeatedly enforcing constraints locally
45
Arc consistency (again – use AC-3)
 Simplest form of propagation makes each arc
consistent
 X Y is consistent iff
for every value x of variable X there is some allowed y
 E.g., SA → NSW is consistent iff
SA=blue and NSW=red
46
Arc consistency
 Simplest form of propagation makes each arc
consistent
 X Y is consistent iff
for every value x of X there is some allowed y
 E.g., NSW → SA is consistent iff
NSW=red and SA=blue
NSW=blue and SA=???
Arc can be made consistent by removing blue from NSW
47
Arc consistency
 Simplest form of propagation makes each arc
consistent
 X Y is consistent iff
for every value x of X there is some allowed y
 If X loses a value, neighbors of X need to be rechecked
48
Arc consistency
 Simplest form of propagation makes each arc
consistent
 X Y is consistent iff
for every value x of X there is some allowed y
 If X loses a value, neighbors of X need to be rechecked
 Arc consistency detects failure earlier than forward checking
 Can be run as a preprocessor or after each assignment
49
Real-world applications of CSP
 Scheduling
 Graph layout
 Temporal reasoning
 Network management
 Building design
 Natural language
 Planning
processing
 Molecular biology /
genomics
 VLSI design
 Test generation for
digital circuits
 Optimization/satisfaction
 Computer vision
 Cryptography
50
Examples of scheduling
 How to schedule experiments on an expensive piece





51
of equipment, e.g. Large Hadron Collider
or Hubble Space Telescope
How to schedule courses (or meetings in general)
How to schedule jobs on CPUs or computer clusters
How to schedule tasks on a large construction project
How to pack containers and vehicles
etc.
Typical uses of CSP
 Solutions:
 Does a solution exist?
 Find one solution
 Find all solutions
 Given a partial instantiation, do any of the
above
 Transform the constraint graph into an
equivalent constraint graph that is easier
to solve
52
Varieties of CSPs
 Discrete variables
 finite domains:
 n variables, domain size d ⇒ O(dn) complete assignments
 e.g., Boolean CSPs: each domain is {true, false}
 infinite domains:
 set of all integers, set of all strings, etc.
 e.g., job scheduling, where variables are start/end days for each
job: StartJob1 + 5 ≤ StartJob3
 Continuous variables
 very common in real-world problems
 e.g., start/end times for Hubble Space Telescope observations
 linear programming problems are a well-known type of this;
in spite of the difficulty, linear constraints are solvable in
polynomial time
53
Constraints (continued)
 In general, we could also include preferences
(“soft” constraints)
 e.g., red is better than green
 often representable by a cost for each variable
assignment
 → constrained optimization problems
54
Further improvements
 k-consistency
 2-consistency: all pairs of nodes are consistent
 3-consistency: any pair can be extended to a 3rd
neighboring variable
 . . . (“path consistency”)
 Checking special constraints
 Checking Alldiff(…) constraint
 (For these variables, the assigned values must be different)
 E.g. Cryptarithmetic, or {WA=red, NSW=red}
 Checking Atmost(…) constraint
 E.g., no more than 10 employees can be assigned to this project
 Commonly used for “bounds propagation” for larger value domains
55
Problems with backtracking
 Inefficiency: can explore areas of the
search space that aren’t likely to
succeed
 Variable ordering can help
 Thrashing: keep repeating the same
failed variable assignments
 Consistency checking can help
 “Intelligent backtracking” schemes can also help
56
More thoughts on backtracking
 Until now, we have considered chronological backtracking
 When a branch of the search fails, go back to the preceding
variable and try a different value for it
 The most recent decision is revisited
 Try to do better: dependency-directed backtracking
 In general, the term means to go back to a decision that actually
caused the failure, and continue the search from there
 For CSP, can keep track by maintaining a conflict set
(previously assigned variables that are connected to X by
constraints)
 The backjumping method backtracks to the most recent variable
in the conflict set
 Note: Forward checking can be used to determine the conflict set!
57
A different approach: “local search”
 Recall that traditional search methods typically work with
"complete" states (i.e., all variables assigned)
 We can also use complete state assignments to solve CSPs:
 allow states with unsatisfied constraints
 operators reassign variable values
 E.g., start with WA=red, NT=red, Q=red, …, T=red, and search
through the state space
 E.g, start with WA=random, NT=random, …
 Variable selection: randomly select any conflicted variable
 Value selection by min-conflicts heuristic:
 choose value that violates the fewest constraints
 i.e., hill-climbing with h(n) = total number of violated constraints
58
Min-Conflicts algorithm
for solving CSPs by local search
function MIN-CONFLICTS(csp, max_steps) return solution or failure
inputs: csp, a constraint satisfaction problem
max_steps, the number of steps allowed before giving up
current ← an initial complete assignment for csp
for i = 1 to max_steps do
if current is a solution for csp then return current
var ← a randomly chosen, conflicted variable from VARIABLES[csp]
value ← the value v for var that minimizes CONFLICTS(var,v,current,csp)
set var = value in current
return failure
59
Figure 6.8
Counts the number of constraints
violated by a particular value
Min-Conflicts example: 4-Queens




States: 4 queens in 4 columns (44 = 256 states)
Actions: move queen in column
Goal test: no attacks
Evaluation: h(n) = number of attacks
 Given random initial state, can solve n-queens in
almost constant time for arbitrary n with high
probability (e.g., n = 10,000,000)
60
 Min-Conflicts: how well does it work?
 Solves the million-queens problem in an average of 50
steps(!), after initial assignment
 (Works well because solutions are densely distributed
throughout the state space)
 Reduced some scheduling tasks
from 3 weeks to 10 minutes (Hubble Space Telescope)
 Largely responsible for resurgence of interest in local
search in 1990s
61
Summary
 Constraint Satisfaction Problems (CSP)
 A common representation is possible
 Generic goal and successor functions
 Generic heuristics are possible (no domain-specific expertise!)

Algorithm AC-3
 Remove arcs that are not consistent with constraints

Backtracking search for CSPs
 Commonly implemented using depth-first search, with each level in the tree
representing 1 additional assignment of a value to a variable
Forward checking prevents some assignments that guarantee later failure
 Constraint propagation does additional work to constrain values and detect
inconsistencies

 (e.g., just apply AC-3 to achieve arc consistency)

62
Alternative: Local search (iterative min-conflicts) is often very effective in practice
Download