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
C h a p t e r 5 Constraint Satisfaction Problems

2 Outline Constraint Satisfaction Problems (CSP)
Backtracking search for CSPs Local search for CSPs

3 Constraint Satisfaction Problems
Standard search problem: state is a "black box“ – any data structure that supports successor function, heuristic function, and goal test 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

4 Constraint satisfaction problems (CSPs)
Suppose we are looking at a map of Australia showing each of its states and territories, and that we are given the task of coloring each region either red, green, or blue in such a way that no neighboring regions have the same color. To formulate this as a CSP, we define: variables to be the regions: WA, NT, Q, NSW, V, SA, and T. The domain of each variable is the set {red, green, blue). The constraints require neighboring regions to have distinct colors; for example, the allowable combinations for WA and NT are the pairs: {(red, green), (red, blue), (green, red), (green, blue), (blue, red), (blue, green)) .

5 Example: Map-Coloring
Variables WA, NT, Q, NSW, V, SA, T Domains Di = {red, green, blue} Constraints: adjacent regions must have different colors e.g., WA ≠ NT, or (WA,NT) in {(red,green), (red,blue), (green,red), (green,blue), (blue,red), (blue,green)}

6 Example: Map-Coloring
Solutions are complete and consistent assignments WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green

7 Constraint graph The nodes of the graph correspond to variables of the problem and the arcs correspond to constraints. Binary CSP: each constraint relates two variables Map Coloring viewed as a constraint satisfaction problem The map-coloring problem represented as a constraint graph

8 Varieties of CSPs The simplest kind of CSP involves variables that are discrete and have finite domains. Map-coloring problems are of this kind. If the maximum domain size of any variable in a CSP is d, then the number of possible complete assignments is O(dn) that is, exponential in the number of variables. Finite-domain CSPs include Boolean CSPs, whose variables can be either true or false. Boolean CSPs include as special cases some NP-complete (nondeterministic polynomial) time problems .

9 Varieties of CSPs So , Discrete variables can be one of the following:
finite domains: n variables, domain size d  O(dn) complete assignments infinite domains: integers, strings, etc. e.g., job scheduling Continuous domain continuous-valued variables e.g., start/end times for Hubble Space Telescope observations (variables must obey a variety of astronomical, precedence, and power constraints)

10 Varieties of constraints
Unary constraints involve a single variable, e.g., SA ≠ green Binary constraints involve pairs of variables (relates two variables), e.g., SA ≠ WA Higher-order constraints involve 3 or more variables, e.g., cryptarithmetic puzzles (column constraints)

11 A cryptarithmetic problem
It is a mathematical puzzle in which each letter represents a digit (for example, if X=3, then XX=33). The object is to find the value of each letter. No two letters represent the same digit (If X=3, Y cannot be 3). And the leading digit of a multi-digit number must not be zero (Given the value ZW, Z cannot be 0). A good puzzle should have a unique solution, and the letters should make up a cute phrase . They can be quite challenging, often involving many steps.

12 Example: Cryptarithmetic
Each letter stands for a distinct digit; the aim is to find a substitution of digits for letters such that the resulting sum is arithmetically correct, with the added restriction that no leading zeroes are allowed 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) where XI, X2, and X3 are auxiliary variables representing the digit (0 or 1) carried over into the next column O + O = R + 10 · X1 X1 + W + W = U + 10 · X2 X2 + T + T = O + 10 · X3 X3 = F, T ≠ 0, F ≠ 0 The constraint hyper graph for the cryptarithmetic problem, showing the Alldzff constraint as well as the column addition constraints

13 Each puzzle may has one or many solutions or no solution
F=1 O=6 R=2 T=8 U=9 W=4 +846 1692 F=1 O=7 R=4 T=8 U=3 W=6 +867 1734 F=1 O=8 R=6 T=9 U=5 W=2 +928 1856 F=1 O=8 R=6 T=9 U=7 W=3 +938 1876 has 7 solutions: F=1 O=4 R=8 T=7 U=6 W=3 +734 1468 F=1 O=5 R=0 T=7 U=3 W=6 +765 1530 F=1 O=6 R=2 T=8 U=7 W=3 +836 1672

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

15 [ WA = red then NT = green ] same as [ NT = green then WA = red ]
Backtracking search Commutativity A problem is commutative if the order of application of any given set of actions has no effect on the outcome. [ WA = red then NT = green ] same as [ NT = green then WA = red ] This is the case for CSPs because, when assigning values to variables, we reach the same partial assignment, regardless of order. Therefore, all CSP search algorithms generate successors by considering possible assignments for only a single variable at each node in the search tree. For example, at the root node of a search tree for coloring the map of Australia, we might have a choice between SA = red, SA = green, and SA = blue, but we would never choose between SA = red and WA = blue. With this restriction, the number of leaves is dn, as we would hope

16 Backtracking search The term backtracking search is used for a depth-first search that chooses values for one variable at a time and backtracks when a variable has no legal values left to assign. Depth-first search for CSPs with single-variable assignments is called backtracking search

17 Backtracking example Part of the search tree generated by simple backtracking for the map-coloring problem

18 Backtracking example Part of the search tree generated by simple backtracking for the map-coloring problem

19 Backtracking example Part of the search tree generated by simple backtracking for the map-coloring problem

20 Backtracking example Part of the search tree generated by simple backtracking for the map-coloring problem

21 Backtracking example Part of the search tree generated by simple backtracking for the map-coloring problem

22 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?

23 Backtracking search Variable and value ordering
The backtracking algorithm contains the line By default, SELECT-UNASSIGNED-VARIABLE simply selects the next unassigned variable in the order given by the list VARIABLES[csp].

24 minimum remaining values (MRV)
Backtracking search Most constrained variable minimum remaining values (MRV) minimum remaining values (MRV) also has been called the "most constrained variable" or "fail-first" heuristic, the latter because it picks a variable that is most likely to cause a failure soon. For example, after the assignments for WA = red and NT = green, there is only one possible value for SA, so it makes sense to assign SA = blue next rather than assigning Q. after SA is assigned, the choices for Q, NS W, and V are all forced. This intuitive idea-choosing the variable with the fewest "legal" values is called the minimum remaining values (MRV) heuristic. If there is a variable X with zero legal values remaining, the MRV heuristic will select X and failure will be detected immediately-avoiding pointless searches through other variables which always will fail when X is finally selected

25 Minimum remaining values (MRV)
1 1 most constrained variable

26 Most constraining variable
Backtracking search Most constrained variable Most constraining variable Most constraining variable: choose the variable with the most constraints on remaining variables 3 3 2 3 5 2

27 Local search for CSPs Local-search algorithms turn out to be very effective in solving many CSPs. They use a complete-state formulation Hill-climbing, simulated annealing typically work with "complete" states, i.e., all variables assigned To apply to CSPs: allow states with unsatisfied constraints operators reassign variable values Variable selection: randomly select any conflicted variable Value selection by min-conflicts heuristic: choose value that violates the fewest constraints i.e., hill-climb with h(n) = total number of violated constraints

28 Min-conflicts heuristic
The MIN-CONFLICTS algorithm for solving CSPs by local search. The initial state may be chosen randomly or by a greedy assignment process that chooses a minimal conflict value for each variable in turn. The CONFLICTS function counts the number of constraints violated by a particular value, given the rest of the current assignment

29 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)

30 Summary Constraint satisfaction problems (or CSPs) consist of variables with constraints on them. Many important real-world problems can be described as CSPs. The structure of a CSP can be represented by its constraint graph. Backtracking search, a form of depth-first search, is commonly used for solving CSPs. The minimum remaining values and degree heuristics are domain-independent methods for deciding which variable to choose next in a backtracking search. The least constraining-value heuristic helps in ordering the variable values. Backtracking occurs when no legal assignment can be found for a variable. Conflict directed back jumping backtracks directly to the source of the problem. Local search using the min-conflicts heuristic has been applied to constraint satisfaction problems with great success.

31 END


Download ppt "Constraint Satisfaction Problems"

Similar presentations


Ads by Google