Top Banner
1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)
49

1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

Dec 14, 2015

Download

Documents

Isabela Minney
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

1

Constraint Satisfaction Problems

A Quick Overview(based on AIMA book slides)

Page 2: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

2

Constraint satisfaction problems

What is a CSP?• Finite set of variables V1, V2, …, Vn

• Nonempty domain of possible values for each variable DV1, DV2, … DVn

• Finite set of constraints C1, C2, …, Cm

• Each constraint Ci limits the values that variables can take, e.g., V1 ≠ V2

A state is an assignment of values to some or all variables.

Consistent assignment: assignment does not violate the constraints.

Page 3: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

3

Constraint satisfaction problems

An assignment is complete when every variable has a value.

A solution to a CSP is a complete assignment that satisfies all constraints.

Some CSPs require a solution that maximizes an objective function.

Applications: • Scheduling the Hubble Space Telescope,

• Floor planning for VLSI,

• Map coloring,

• Cryptography

Page 4: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

4

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—So (WA,NT) must be in {(red,green),(red,blue),(green,red), …}

Page 5: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

5

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

Page 6: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

6

Constraint graph

Binary CSP: each constraint relates two variables

Constraint graph: • nodes are variables• arcs are constraints

CSP benefits• Standard representation pattern• Generic goal and successor functions• Generic heuristics (no domain specific expertise).

Graph can be used to simplify search.—e.g. Tasmania is an independent subproblem.

Page 7: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

7

Varieties of CSPs Discrete variables

• finite domains:—n variables, domain size d O(dn) complete assignments—e.g., Boolean CSPs, includes 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., StartJob1 + 5 ≤ StartJob3

Continuous variables• e.g., start/end times for Hubble Space Telescope observations• linear constraints solvable in polynomial time by linear

programming

Page 8: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

8

Varieties of constraints

Unary constraints involve a single variable, • e.g., SA ≠ green

Binary constraints involve pairs of variables,• e.g., SA ≠ WA

Higher-order constraints involve 3 or more variables• e.g., cryptarithmetic column constraints

Preference (soft constraints) e.g. red is better than green can be represented by a cost for each variable assignment => Constrained optimization problems.

Page 9: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

9

Example: Cryptarithmetic

Variables: F T U W R O X1 X2 X3

Domain: {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 + 10 · X2

• X2 + T + T = O + 10 · X3

• X3 = F, T ≠ 0, F ≠ 0

Page 10: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

10

CSP as a standard search problem

A CSP can easily be expressed as a standard search problem.• Initial State: the empty assignment {}.• Operators: Assign value to unassigned variable provided

that there is no conflict.• Goal test: assignment consistent and complete.• Path cost: constant cost for every step.• Solution is found at depth n, for n variables• Hence depth first search can be used

Page 11: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

11

Backtracking search

Variable assignments are commutative, • Eg [ WA = red then NT = green ]

equivalent to [ NT = green then WA = 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 basic uninformed algorithm for CSPs

Can solve n-queens for n ≈ 25

Page 12: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

12

Backtracking searchfunction BACKTRACKING-SEARCH(csp) % returns a solution or failure

return RECURSIVE-BACKTRACKING({} , csp)

function RECURSIVE-BACKTRACKING(assignment, csp) % returns a solution or failure

if assignment is complete then return assignment

var SELECT-UNASSIGNED-VARIABLE(VARIABLES[csp],assignment,csp)

for each value in ORDER-DOMAIN-VALUES(var, assignment, csp) do

if value is consistent with assignment according to CONSTRAINTS[csp] then

add {var=value} to assignment

result RECURSIVE-BACKTRACKING(assignment, csp)

if result failure then return result

remove {var=value} from assignment

return failure

Page 13: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

13

Backtracking example

Page 14: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

14

Backtracking example

Page 15: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

15

Backtracking example

Page 16: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

16

Backtracking example

Page 17: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

17

Improving backtracking efficiency

General-purpose methods can give huge speed gains:

• Which variable should be assigned next?• In what order should its values be tried?• Can we detect inevitable failure early?

Page 18: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

18

Most constrained variable

Most constrained variable:choose the variable with the fewest legal values

a.k.a. minimum remaining values (MRV) heuristic

Page 19: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

19

Most constraining variable

Tie-breaker among most constrained variables Most constraining variable:

• choose the variable with the most constraints on remaining variables

Page 20: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

20

Least constraining value

Given a variable, choose the least constraining value:• the one that rules out the fewest values in the remaining

variables

Combining these heuristics makes 1000 queens feasible

Page 21: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

21

Forward Checking

Idea: • Keep track of remaining legal values for unassigned

variables• Terminate search when any variable has no legal values

Page 22: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

22

Forward checking

Idea: • Keep track of remaining legal values for unassigned

variables• Terminate search when any variable has no legal values

Page 23: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

23

Forward checking

Idea: • Keep track of remaining legal values for unassigned

variables• Terminate search when any variable has no legal values

Page 24: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

24

Forward checking

Idea: • Keep track of remaining legal values for unassigned

variables• Terminate search when any variable has no legal values

No more value for SA: backtrack

Page 25: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

25

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

[4-Queens slides copied from B.J. Dorr]

Page 26: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

26

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 27: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

27

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , ,3,4}

Page 28: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

28

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , ,3,4}

Page 29: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

29

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ , , , }

X4{ ,2, , }

X2{ , ,3,4}

Page 30: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

30

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ ,2, ,4}

X4{ ,2,3, }

X2{ , , ,4}

Page 31: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

31

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ ,2, , }

X4{ , ,3, }

X2{ , , ,4}

Page 32: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

32

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ ,2, , }

X4{ , ,3, }

X2{ , , ,4}

Page 33: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

33

Example: 4-Queens Problem

1

3

2

4

32 41

X1{1,2,3,4}

X3{ ,2, , }

X4{ , , , }

X2{ , , ,4}

Page 34: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

34

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1,2,3,4}

X4{1,2,3,4}

X2{1,2,3,4}

Page 35: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

35

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1, ,3, }

X4{1, ,3,4}

X2{ , , ,4}

Page 36: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

36

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1, ,3, }

X4{1, ,3,4}

X2{ , , ,4}

Page 37: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

37

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1, , , }

X4{1, ,3, }

X2{ , , ,4}

Page 38: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

38

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1, , , }

X4{1, ,3, }

X2{ , , ,4}

Page 39: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

39

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1, , , }

X4{ , ,3, }

X2{ , , ,4}

Page 40: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

40

Example: 4-Queens Problem

1

3

2

4

32 41

X1{ ,2,3,4}

X3{1, , , }

X4{ , ,3, }

X2{ , , ,4}

Page 41: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

41

Constraint Propagation Simplest form of propagation makes each arc consistent Arc X Y (link in constraint graph) is consistent iff

for every value x of X there is some allowed y

Page 42: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

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

Page 43: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

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

If X loses a value, neighbors of X need to be rechecked

Page 44: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

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

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

Page 45: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

45

function AC-3(csp) % returns the CSP, possibly with reduced domains

inputs: csp, a binary csp with variables {X1, X2, … , Xn}

local variables: queue, a queue of arcs initially the arcs in csp

while queue is not empty do

(Xi, Xj) REMOVE-FIRST(queue)

if REMOVE-INCONSISTENT-VALUES(Xi, Xj) then

for each Xk in NEIGHBORS[Xi ] do

add (Xk, Xi) to queue

function REMOVE-INCONSISTENT-VALUES(Xi, Xj) % returns true iff a value is removed

removed false

for each x in DOMAIN[Xi] do

if no value y in DOMAIN[Xj] allows (x,y) to satisfy the constraints between Xi and

Xj

then delete x from DOMAIN[Xi]; removed true

return removed

Arc Consistency Algorithm AC-3

Time complexity: O(n2d3)

Page 46: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

46

Local Search for CSPs

Hill-climbing methods 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) = number of violated constraints

Page 47: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

47

Example: n-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, we can solve n-queens for large n with high probability

Page 48: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

48

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

Page 49: 1 Constraint Satisfaction Problems A Quick Overview (based on AIMA book slides)

49

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

Constraint propagation (e.g., arc consistency) additionally constrains values and detects inconsistencies

Iterative min-conflicts is usually effective in practice