Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constraint Satisfaction Problems

Similar presentations


Presentation on theme: "Constraint Satisfaction Problems"— Presentation transcript:

1 Constraint Satisfaction Problems
Chapter 6

2 Outline CSP examples Backtracking search for CSPs
Problem structure and problem decomposition Local search for CSPs

3 Constraint satisfaction problems (CSPs)
Standard search problem: state is a “black box”—any old data structure that supports goal test, eval, successor CSP: state is defined by variables Xi with values from domain Di goal test is a set of constraints specifying allowable combinations of values for subsets of variables Simple example of a formal representation language Allows useful general-purpose algorithms with more power than standard search algorithms

4 Example: Map-Coloring
Northern Territory Western Australia Queensland South Australia New South Wales Victoria Tasmania Variables W A, N T , Q, N SW , V , SA, T Domains Di = {red, green, blue} Constraints: adjacent regions must have different colors e.g., W A ≠ T (if the language allows this), or (W A, N T ) ∈ {(red, green), (red, blue), (green, red), (green, blue), . . .}

5 Example: Map-Coloring (cont’d)
Northern Territory Western Australia Queensland South Australia New South Wales Victoria Tasmania Solutions are assignments satisfying all constraints, e.g., {W A = red, N T = green, Q = red, N SW = green, V = red, SA = blue, T = green}

6 Constraint graph Binary CSP: each constraint relates at most two variables Constraint graph: nodes are variables, arcs show constraints NT Q WA SA NSW V T General-purpose CSP algorithms use the graph structure to speed up search. E.g., Tasmania is an independent subproblem!

7 CSPs with discrete variables
finite domains; size d O(dn) complete assignments e.g., Boolean CSPs, incl. Boolean satisfiability (NP-complete) infinite domains (integers, strings, etc.) e.g., job scheduling, variables are start/end days for each job need a constraint language, e.g., StartJ ob1 + 5 ≤ StartJ ob3 linear constraints solvable, nonlinear undecidable

8 CSPs with continuous variables
linear constraints solvable in polynomial time by linear programinf (LP) methods e.g., precise start/end times for Hubble Telescope observations with astronomical, precedence, and power constraints

9 Varieties of constraints
Unary constraints involve a single variable, e.g., SA ≠ green Binary constraints involve pairs of variables, e.g., SA ≠ W A Higher-order constraints involve 3 or more variables, e.g., cryptarithmetic column constraints Preferences (soft constraints), e.g., red is better than green often representable by a cost for each variable assignment → constrained optimization problems

10 Example: Cryptarithmetic
T W O + T W O F T U W R O F O U R X3 X2 X1 (a) Variables: F T U W R O X1 X2 X3 Domains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Constraints alldiff(F, T, U, W, R, O) O + O = R + 10 · X1, X1 + W + W = U X2 X2 + T + T = O X3, X3 = F (b)

11 Problem b a c e d Consider the constraint graph on the right.
The domain for every variable is [1,2,3,4]. There are 2 unary constraints: variable “a” cannot take values 3 and 4. variable “b” cannot take value 4. d There are 8 binary constraints stating that variables connected by an edge cannot have the same value.

12 Example: 4-Queens Problem
{1,2,3,4} X2 X3 X4 1 2 3 4 5 Queen's Problem

13 Real-world CSPs Assignment problems e.g., who teaches what class
Timetabling problems e.g., which class is offered when and where? Hardware configuration Spreadsheets Transportation scheduling Factory scheduling Floorplanning Notice that many real-world problems involve real-valued variables

14 Standard search formulation (incremental)
Let’s start with the straightforward, dumb approach, then fix it States are defined by the values assigned so far Initial state: the empty assignment, ∅ Successor function: assign a value to an unassigned variable that does not conflict with current assignment.  fail if no legal assignments (not fixable!) Goal test: the current assignment is complete

15 Standard search formulation (incremental)
This is the same for all CSPs! Every solution appears at depth n with n variables  use depth-first search Path is irrelevant, so can also use complete-state formulation b = (n − ℓ)d at depth ℓ, hence n!dn leaves!!!!

16 Backtracking search Variable assignments are commutative, i.e., [W A = red then N T = green] same as [N T = green then W A = red] Only need to consider assignments to a single variable at each node  b = d and there are dn leaves Depth-first search for CSPs with single-variable assignments is called backtracking search Backtracking search is the basic uninformed algorithm for CSPs Can solve n-queens for n ≈ 25

17 Backtracking search function BACKTRACKING-SEARCH (csp)
returns a solution, or failure return BACKTRACK({ }, csp)

18 Backtracking search (cont’d)
function BACKTRACK (assignment, csp) returns a solution, or failure if assignment is complete then return assignment var ← SELECT-UNASSIGNED-VAR(csp) for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do if value is consistent with assignment then add { var = value } to assignment inferences ← INFERENCE(csp, var, value) if inferences ≠ failure then add inferences to assignment result ← BACKTRACK (assignment, csp) if result ≠ failure then return result remove { var = value } and inferences from assignment return failure

19 Backtracking Example

20 Backtracking Example

21 Backtracking Example

22 Backtracking Example

23 Improving backtracking efficiency
General-purpose methods can give 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
choose the variable with the fewest legal values

25 Most constraining variable
Tie-breaker among most constrained variables Most constraining variable: choose the variable with the most constraints on remaining variables

26 Least constraining value
Given a variable, choose the least constraining value: the one that rules out the fewest values in the remaining variables Allows 1 value for SA Allows 0 value for SA Combining these heuristics makes 1000 queens feasible

27 Forward checking Idea: Keep track of remaining legal values for unassigned variables Idea: Terminate search when any variable has no legal values WA NT Q NSW V SA T

28 Forward checking Idea: Keep track of remaining legal values for unassigned variables Idea: Terminate search when any variable has no legal values WA NT Q NSW V SA T

29 Forward checking Idea: Keep track of remaining legal values for unassigned variables Idea: Terminate search when any variable has no legal values WA NT Q NSW V SA T

30 Forward checking Idea: Keep track of remaining legal values for unassigned variables Idea: Terminate search when any variable has no legal values WA NT Q NSW V SA T

31 Constraint propagation
Forward checking propagates information from assigned to unassigned variables, but doesn’t provide early detection for all failures: WA NT Q NSW V SA T N T and SA cannot both be blue! Constraint propagation repeatedly enforces constraints locally

32 Example: 4-Queens Problem
{1,2,3,4} X2 X3 X4 1 2 3 4

33 Example: 4-Queens Problem
{1,2,3,4} X2 X3 X4 1 2 3 4

34 Example: 4-Queens Problem
{1,2,3,4} X2 { , ,3,4} X3 { ,2, ,4} X4 { ,2,3, } 1 2 3 4

35 Example: 4-Queens Problem
{1,2,3,4} X2 { , ,3,4} X3 { ,2, ,4} X4 { ,2,3, } 1 2 3 4

36 Example: 4-Queens Problem
{1,2,3,4} X2 { , ,3,4} X3 { , , , } X4 { , ,3, } 1 2 3 4

37 Example: 4-Queens Problem
{1,2,3,4} X2 { , , ,4} X3 { ,2, ,4} X4 { ,2,3, } 1 2 3 4

38 Example: 4-Queens Problem
{1,2,3,4} X2 { , , ,4} X3 { ,2, ,4} X4 { ,2,3, } 1 2 3 4

39 Example: 4-Queens Problem
{1,2,3,4} X2 { , , ,4} X3 { ,2, , } X4 { , ,3, } 1 2 3 4

40 Example: 4-Queens Problem
{1,2,3,4} X2 { , , ,4} X3 { ,2, , } X4 { , ,3, } 1 2 3 4

41 Example: 4-Queens Problem
{1,2,3,4} X2 { , ,3,4} X3 { ,2, , } X4 { , , , } 1 2 3 4

42 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 WA NT Q NSW V SA T

43 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 WA NT Q NSW V SA T

44 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 WA NT Q NSW V SA T If X loses a value, neighbors of X need to be rechecked

45 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 WA NT Q NSW V SA T 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

46 Arc consistency algorithm
function AC-3 (csp) returns false if an inconsistency is found and true otherwise inputs: csp, a binary CSP with components (X, D, C) local variables: queue, a queue of arcs, initially all the arcs in csp while queue is not empty do (Xi, Xj ) ← REMOVE-FIRST(queue) if REVISE(csp, Xi, Xj ) then if size of Di = 0 then return false for each Xk in Xi.NEIGHBORS-{Xj } do add (Xk , Xi) to queue return true

47 Arc consistency algorithm (cont’d)
function REVISE (csp, Xi, Xj ) returns true iff we revise the domain of Xi revised ← false for each x in Di do if no value y in Dj allows (x, y) to satisfy the constraint between Xi and Xj then delete x from Di then revised ← true return revised O(n2d3), can be reduced to O(n2d2) but cannot detect all failures in polynomial time.

48 Back-tracking or back-jumping?
{Q=red , NSW= green, V= blue, T=red} red green ? blue red blue green

49 Local search for CSPs Use complete-state representation
Initial state = all variables assigned values Successor states = change 1 (or more) values For CSPs allow states with unsatisfied constraints (unlike backtracking) operators reassign variable values hill-climbing with n-queens is an example Variable selection: randomly select any conflicted variable. Value selection: min-conflicts heuristic Select new value that results in a minimum number of conflicts with the other variables

50 Local search for CSP 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 minimize CONFLICTS (var,v,current,csp) set var = value in current return failure

51 Min-conflicts example 1
h=5 h=3 h=1 Use of min-conflicts heuristic in hill-climbing.

52 Min-conflicts example 2
A two-step solution for an 8-queens problem using min-conflicts heuristic At each stage a queen is chosen for reassignment in its column The algorithm moves the queen to the min-conflict square breaking ties randomly.

53 Advantages of local search
Local search can be particularly useful in an online setting Airline schedule example  E.g., mechanical problems require than 1 plane is taken out of service  Can locally search for another “close” solution in state-space  Much better (and faster) in practice than finding an entirely new schedule. The runtime of min-conflicts is roughly independent of problem size. Can solve the millions-queen problem in roughly 50 steps. Why?  n-queens is easy for local search because of the relatively high density of solutions in state-space.

54 Problem structure Tasmania and mainland are independent subproblems
Q WA SA NSW V T Tasmania and mainland are independent subproblems Identifiable as connected components of constraint graph

55 Problem structure contd.
Suppose each subproblem has c variables out of n total Worst-case solution cost is n/c · dc, linear in n E.g., n = 80, d = 2, c = 20 280 = 4 billion years at 10 million nodes/sec 4 · 220 = 0.4 seconds at 10 million nodes/sec

56 Tree-structured CSPs A E B D C F Theorem: if the constraint graph has no loops, the CSP can be solved in O(n d2) time Compare to general CSPs, where worst-case time is O(dn) This property also applies to logical and probabilistic reasoning: an important example of the relation between syntactic restrictions and the complexity of reasoning.

57 Algorithm for tree-structured CSPs
1. Choose a variable as root, order variables from root to leaves such that every node’s parent precedes it in the ordering A E B D A B C D E F F C For j from n down to 2, apply MAKE-ARC-CONSISTENT(P arent(Xj ), Xj ) (will remove inconsistent values) For i from 1 to n, assign Xi consistently with P arent(Xi)

58 Algorithm for tree-structured CSPs (cont’d)
function TREE-CSP-SOLVER (csp) returns a solution, or failure inputs: csp, a binary CSP with components (X, D, C) n ← number of variables in X assignment ← an empty assignment root ← any variable in X X ← TOPOLOGICALSORT(X, root) for j = n down to 2 do MAKE-ARC-CONSISTENT(P arent(Xj ), Xj ) if it cannot be made consistent then return failure for i = 1 to n do assignment [Xi] ← any consistent value from Di if there is no consistent value then return failure return assignment

59 Nearly tree-structured CSPs
Conditioning: instantiate a variable, prune its neighbors’ domains NT NT Q Q WA WA SA NSW NSW V V T T

60 Nearly tree-structured CSPs
Cutset conditioning: instantiate (in all ways) a set of variables such that the remaining constraint graph is a tree Cutset size c =⇒ runtime O(dc · (n − c)d2), very fast for small c

61 Summary CSPs are a special kind of problem: states defined by values of a fixed set of variables goal test defined by constraints on variable values Backtracking = depth-first search with one variable assigned per node Variable ordering and value selection heuristics help significantly Forward checking prevents assignments that guarantee later failure

62 Summary Constraint propagation (e.g., arc consistency) does additional work to constrain values and detect inconsistencies The CSP representation allows analysis of problem structure Tree-structured CSPs can be solved in linear time (Iterative min-conflicts is usually effective in practice)


Download ppt "Constraint Satisfaction Problems"

Similar presentations


Ads by Google