Constraint Satisfaction Sven Koenig, USC Russell and Norvig, 3 rd - - PDF document

constraint satisfaction
SMART_READER_LITE
LIVE PREVIEW

Constraint Satisfaction Sven Koenig, USC Russell and Norvig, 3 rd - - PDF document

12/18/2019 Constraint Satisfaction Sven Koenig, USC Russell and Norvig, 3 rd Edition, Chapter 6 These slides are new and can contain mistakes and typos. Please report them to Sven (skoenig@usc.edu). 1 Examples SAT-based planning (special


slide-1
SLIDE 1

12/18/2019 1

Constraint Satisfaction

Sven Koenig, USC

Russell and Norvig, 3rd Edition, Chapter 6 These slides are new and can contain mistakes and typos. Please report them to Sven (skoenig@usc.edu).

Examples

  • SAT-based planning (special case of Boolean satisfiability problems)
  • Variables
  • Domain for each variable: Boolean CSP
  • Constraints: Non-binary CSP

1 2

slide-2
SLIDE 2

12/18/2019 2

Constraint Satisfaction Problems (CSPs)

  • Boolean CSP: Domains for all variables are {true, false}
  • Binary CSP: Constraints are between at most two variables
  • Unary constraint: A constraint involving one variable
  • Binary constraint: A constraint involving two variables
  • Ternary constraint: A constraint involving three variables
  • Binary and Boolean CSPs (= 2-SAT problems) are polynomial time
  • solvable. Non-binary or non-Boolean CSPs are NP-hard to solve.
  • In the following, we study binary non-Boolean CSPs.

Examples

  • Map coloring
  • Variables
  • Domain for each variable
  • Constraints

3 4

slide-3
SLIDE 3

12/18/2019 3

Examples

  • Map coloring

WA ε {red, green, blue} NT ε {red, green, blue} SA ε {red, green, blue} Q ε {red, green, blue} NSW ε {red, green, blue} V ε {red, green, blue} T ε {red, green, blue} x y x ǂ y Constraint graph

Examples

  • Map coloring

WA ε {red, green, blue} NT ε {red, green, blue} SA ε {red, green, blue} Q ε {red, green, blue} NSW ε {red, green, blue} V ε {red, green, blue} T ε {red, green, blue} x\y red green blue red bad good good green good bad good blue good good bad x y

5 6

slide-4
SLIDE 4

12/18/2019 4

Examples

  • Map coloring

WA ε {red, green, blue} NT ε {red, green, blue} SA ε {red, green, blue} Q ε {red, green, blue} NSW ε {red, green, blue} V ε {red, green, blue} T ε {red, green, blue} x y red green blue red green blue

Examples

  • Eight-queens problem

A C B D E F G H 1 2 3 4 5 6 7 8

7 8

slide-5
SLIDE 5

12/18/2019 5

Examples

  • Different formulations are possible for the N-queens problem.
  • Formulation 1
  • Variables: Location of each queen
  • Domains: Set of all squares
  • Constraints: Any two queens cannot attack each other or be in the same square.
  • Number of states: (N2)N
  • Formulation 2
  • Variables: Occupied status for each square
  • Domains: Boolean
  • Constraints: Any two queens cannot attack each other. Exactly N variables have value true.
  • Number of states: 2(N2)
  • and many others

Examples

  • Different formulations are possible for the N-queens problem.
  • Our formulation
  • Variables: Column of each queen (Q1, …, QN)
  • Domains: Set of all rows {1, …, N}
  • Constraints:
  • Any two queens cannot attack each other.
  • Formally: Qi ǂ Qj and |Qi – Qj| ǂ Ii – j| for all 1 ≤ i,j ≤ N
  • Number of states: NN
  • We use this formulation because NN is smaller than the number of

states in the other formulations. (Take the logarithms of the three numbers to see that.)

9 10

slide-6
SLIDE 6

12/18/2019 6

Examples

  • So far, we have solved these kinds of problems with hillclimbing but

hillclimbing does not search systematically, e.g. has problems determining that there is no solution. We now look at search approaches that are systematic but are often slower than hillclimbing.

Depth-First Search

Q1 Q1 = 1 Q1 = 2 Q1 = 8 … Q2 Q2 = 1 Q2 = 2 Q2 = 8 … Q2 Q2 = 1 Q2 = 2 Q2 = 8 … Q2 … depth N

1 2 3

Q3 Q3 Q3 Q3 Q3 Q3 … … … … … … … …

  • In the following, we study enhancements of depth-first search

11 12

slide-7
SLIDE 7

12/18/2019 7

Backtracking Search

  • Skip an assignment if, after the assignment, at least one constraint

between assigned variables is not satisfied.

  • Backtrack once all assignments have been tried unsuccessfully.

Backtracking Search

13 14

slide-8
SLIDE 8

12/18/2019 8

Backtracking Search

1 3 2 4 5 6 7 8 1 2 3 4 5 6 7 8

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

15 16

slide-9
SLIDE 9

12/18/2019 9

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

17 18

slide-10
SLIDE 10

12/18/2019 10

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

19 20

slide-11
SLIDE 11

12/18/2019 11

Backtracking Search

1 2 3 4 5 6 7 8

  • Can’t place queen in column 6. Backtrack.

1 3 2 4 5 6 7 8

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

21 22

slide-12
SLIDE 12

12/18/2019 12

Backtracking Search

1 2 3 4 5 6 7 8

  • Can’t place queen in column 6. Backtrack.

1 3 2 4 5 6 7 8

Backtracking Search

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

23 24

slide-13
SLIDE 13

12/18/2019 13

Backjumping

  • When backtracking, do not backtrack to the preceding variable

but to the latest one that, when assigned a different value, has a chance to avoid the empty domain of the current variable.

  • Backtracking with backjumping never needs more steps than

backtracking (without backjumping) but can result in fewer steps.

Backjumping

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

  • Can’t place queen in column 6. Backtrack.

25 26

slide-14
SLIDE 14

12/18/2019 14

Backjumping

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

Backjumping

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

27 28

slide-15
SLIDE 15

12/18/2019 15

Forward Checking

  • Before assigning a value to a variable, remove all values from the

domains of the unassigned variables that are inconsistent with the assigned value. Skip the assignment if at least one unassigned variable has an empty domain afterward.

  • Backtrack once all assignments have been tried unsuccessfully.
  • Backtracking with forward checking never needs more steps than

backtracking with backjumping but can result in fewer steps.

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {1,2,3,4,5,6,7,8} Q3 ε {1,2,3,4,5,6,7,8} Q4 ε {1,2,3,4,5,6,7,8} Q5 ε {1,2,3,4,5,6,7,8} Q6 ε {1,2,3,4,5,6,7,8} Q7 ε {1,2,3,4,5,6,7,8} Q8 ε {1,2,3,4,5,6,7,8} 1 3 2 4 5 6 7 8

29 30

slide-16
SLIDE 16

12/18/2019 16

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {2,4,5,6,7,8} Q4 ε {2,3,5,6,7,8} Q5 ε {2,3,4,6,7,8} Q6 ε {2,3,4,5,7,8} Q7 ε {2,3,4,5,6,8} Q8 ε {2,3,4,5,6,7} 1 3 2 4 5 6 7 8

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,6,7,8} Q5 ε {2,4,7,8} Q6 ε {2,4,5,8} Q7 ε {2,4,5,6} Q8 ε {2,4,5,6,7} 1 3 2 4 5 6 7 8

31 32

slide-17
SLIDE 17

12/18/2019 17

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,7,8} Q5 ε {2,4,8} Q6 ε {4} Q7 ε {2,4,6} Q8 ε {2,4,6,7} 1 3 2 4 5 6 7 8

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,7,8} Q5 ε {4,8} Q6 ε {} Q7 ε {4,6} Q8 ε {4,7} 1 3 2 4 5 6 7 8

33 34

slide-18
SLIDE 18

12/18/2019 18

Forward Checking

1 2 3 4 5 6 7 8

  • Won’t be able to place queen in column 6.

Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,7,8} Q5 ε {4,8} Q6 ε {} Q7 ε {4,6} Q8 ε {4,7} 1 3 2 4 5 6 7 8

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,7,8} Q5 ε {4,8} Q6 ε {} Q7 ε {4,6} Q8 ε {4,7} 1 3 2 4 5 6 7 8

35 36

slide-19
SLIDE 19

12/18/2019 19

Forward Checking

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,7,8} Q5 ε {2,4} Q6 ε {4} Q7 ε {2,6} Q8 ε {2,4,6} 1 3 2 4 5 6 7 8

Arc Consistency

  • Repeatedly remove a value from the domain of a variable if there

exists another variable that has no value in its domain that is consistent with the value.

x y z x y z x y z x y z After arc consistency

37 38

slide-20
SLIDE 20

12/18/2019 20

Arc Consistency Maintaining Arc Consistency

  • Before assigning a value to a variable, remove all values from the

domains of the unassigned variables that are inconsistent with the assigned value. Then, repeatedly remove a value from the domain of an unassigned variable if there exists another unassigned variable that has no value in its domain that is consistent with the value.

  • Skip the assignment if at least one unassigned variable has an empty

domain afterward.

  • Backtrack once all assignments have been tried unsuccessfully.
  • Backtracking with maintaining arc consistency never needs more

steps than backtracking with forward checking but can result in fewer steps.

39 40

slide-21
SLIDE 21

12/18/2019 21

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,7,8} Q5 ε {2,4,8} Q6 ε {4} Q7 ε {2,4,6} Q8 ε {2,4,6,7} 1 3 2 4 5 6 7 8 After forward propagation

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {} Q5 ε {8} Q6 ε {4} Q7 ε {2} Q8 ε {7}

  • Won’t be able to place queen in column 4.

1 3 2 4 5 6 7 8 After maintaining arc consistency Other results are possible but at least one domain will be empty.

41 42

slide-22
SLIDE 22

12/18/2019 22

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 1 3 2 4 5 6 7 8

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,6,7,8} Q5 ε {2,4,7,8} Q6 ε {2,4,5,8} Q7 ε {2,4,5,6} Q8 ε {2,4,5,6,7} 1 3 2 4 5 6 7 8 After forward propagation

43 44

slide-23
SLIDE 23

12/18/2019 23

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2} Q5 ε {7} Q6 ε {5} Q7 ε {} Q8 ε {}

  • Won’t be able to place queens in columns 7 and 8.

1 3 2 4 5 6 7 8 After maintaining arc consistency Other results are possible but at least one domain will be empty.

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2} Q5 ε {2,4} Q6 ε {2,5} Q7 ε {2,4,5,6} Q8 ε {4,5,6} 1 3 2 4 5 6 7 8 After forward propagation

45 46

slide-24
SLIDE 24

12/18/2019 24

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2} Q5 ε {} Q6 ε {8} Q7 ε {6} Q8 ε {4}

  • Won’t be able to place queen in column 5.

1 3 2 4 5 6 7 8 After maintaining arc consistency Other results are possible but at least one domain will be empty.

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {2,6} Q5 ε {2,4,7} Q6 ε {2,4} Q7 ε {2,5,6} Q8 ε {2,4,5,6,7} 1 3 2 4 5 6 7 8 After forward propagation

47 48

slide-25
SLIDE 25

12/18/2019 25

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {5,6,7,8} Q4 ε {6} Q5 ε {4} Q6 ε {2} Q7 ε {5} Q8 ε {}

  • Won’t be able to place queen in column 8.

1 3 2 4 5 6 7 8 After maintaining arc consistency Other results are possible but at least one domain will be empty.

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {2,6,7,8} Q4 ε {3,5,7,8} Q5 ε {2,3,6,8} Q6 ε {2,3,5,7} Q7 ε {2,3,5,6,8} Q8 ε {2,3,5,6,7} 1 3 2 4 5 6 7 8 After forward propagation

49 50

slide-26
SLIDE 26

12/18/2019 26

Maintaining Arc Consistency

1 2 3 4 5 6 7 8 Q1 ε {1,2,3,4,5,6,7,8} Q2 ε {3,4,5,6,7,8} Q3 ε {2,6,7,8} Q4 ε {3,5,7,8} Q5 ε {2,3,6,8} Q6 ε {2,3,5,7} Q7 ε {2,3,5,6,8} Q8 ε {2,3,5,6,7} Since any queen can attack at most three squares in any column, the remaining queens can still be placed on the board. 1 3 2 4 5 6 7 8 After maintaining arc consistency

Maintaining Path Consistency

  • Before assigning a value to a variable, remove all values from the

domains of the unassigned variables that are inconsistent with the assigned value. Then, repeatedly remove a consistent pair of values from the cross product of the domains of two variables if there exists another variable that has no value in its domain that is consistent with the pair of values.

  • Skip the assignment if at least one unassigned variable has an empty

domain afterward.

  • Backtrack once all assignments have been tried unsuccessfully.
  • Backtracking with maintaining path consistency never needs more

steps than backtracking with maintaining arc consistency but can result in fewer steps.

51 52

slide-27
SLIDE 27

12/18/2019 27

Map Coloring with Two Colors

x y z x y z red green x y z x ǂ y x ǂ z y ǂ z red green red green

Map Coloring with Two Colors

  • Depth-first search

x y y z z z z x = red x = green y = red y = green y = red y = green z = red z = green z = red z = green z = red z = green z = red z = green

53 54

slide-28
SLIDE 28

12/18/2019 28

Map Coloring with Two Colors

  • Backtracking or backjumping

x y y z z z z x = red x = green y = red y = green y = red y = green z = red z = green z = red z = green z = red z = green z = red z = green

Map Coloring with Two Colors

x y z x y z red green x y z x y z red green After assigning red to x

  • Forward checking

Forward checking does not detect that the problem is unsolvable. x y z x y z x ǂ y x ǂ z y ǂ z red green red green red green red green

55 56

slide-29
SLIDE 29

12/18/2019 29

Map Coloring with Two Colors

  • Forward checking

x y y z z z z x = red x = green y = red y = green y = red y = green z = red z = green z = red z = green z = red z = green z = red z = green

Map Coloring with Two Colors

x y z x y z red green x y z x y z red green After assigning red to x

  • Maintaining arc consistency

Maintaining arc consistency detects that the problem is unsolvable. x y z x y z x ǂ y x ǂ z y ǂ z red green red green red green red green

57 58

slide-30
SLIDE 30

12/18/2019 30

Map Coloring with Two Colors

  • Maintaining arc consistency

x y y z z z z x = red x = green y = red y = green y = red y = green z = red z = green z = red z = green z = red z = green z = red z = green

Map Coloring with Two Colors

x y z x y z red green After assigning red to x

  • Maintaining path consistency

x y z x ǂ y x ǂ z y ǂ z Depth-first search never gets here. red green red green Maintaining path consistency detects that the problem is unsolvable.

59 60

slide-31
SLIDE 31

12/18/2019 31

Map Coloring with Two Colors

  • Maintaining path consistency

x y y z z z z x = red x = green y = red y = green y = red y = green z = red z = green z = red z = green z = red z = green z = red z = green

Constraint Satisfaction

Q1 Q1 = 1 Q1 = 2 Q1 = 8 … Q2 = 1 Q2 = 2 Q2 = 8 … Q2 Q2 = 1 Q2 = 2 Q2 = 8 … Q2 … depth N

1 2 3

Q3 Q3 Q3 Q3 Q3 Q3 … … … … … … … … Which variable to choose here? Which variable to choose here? Which variable to choose here? In which order to try the assignments? … …

61 62

slide-32
SLIDE 32

12/18/2019 32

Constraint Satisfaction

  • Which variable to choose next among all unassigned variables?
  • Choose the one whose domain has the smallest cardinality.
  • Break tries by choosing the one that is involved in the largest number of constraints

with other unassigned variables.

  • Which value to assign to the chosen variable?
  • Choose the value that rules out the fewest values for the variables that the chosen

variable has constraints with and that does not make the problem unsolvable.

  • Which kind of forward or backward checks/maintenance to use?
  • From least to most powerful: back jumping, forward checking, maintaining arc

consistency and maintaining path consistency. None of them are powerful enough to eliminate the search. More powerful methods take more time and it might be faster to do the search instead. For many problems, forward checking or maintaining arc consistency are sufficient.

Constraint Satisfaction

  • Want to play around with constraint satisfaction algorithms?
  • Go here: http://aispace.org/constraint/

63 64