Docsity

Solving Complex Problems with Backtracking, Forward Checking, and Heuristics - Prof. Jae O, Exams of Computer Science

Partial preview of the text

Download Solving Complex Problems with Backtracking, Forward Checking, and Heuristics - Prof. Jae O and more Exams Computer Science in PDF only on Docsity!

Constraint Satisfaction Problems

Chapter 5

Modified for Fall 2006

Chapter 5

Outline

CSP examples

uninformed search and informed search

General search applied to CSPs

Backtracking

Forward checking

Heuristics for CSPs

Modified for Fall 2006

Chapter 5

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

V

i with

values

from

domain

D

i

That is,

D

i defines possible values for

V

i .

goal test

is a set of

constraints

specifying

allowable combinations of values for subsets of variables

Allows useful

general-purpose

algorithms with more power

Modified for Fall 2006 than standard search algorithms

Chapter 5

Example: 4-Queens as a CSP

Assume one queen in each column. Which row does each one go in? Variables

Q 1 , Q 2 , Q 3 , Q 4

Domains

D i = { 1 ,

Constraints

Q

i 6 =

Q

j (cannot be in same row)

Q

i −

(^) Q

j | 6 =

| i −

(^) j | (or same diagonal)

1

Q

2

Q

E.g., possible values for Translate each constraint into set of allowable values for its variables

Q

1 , Q

2 ) are

Modified for Fall 2006

Chapter 5

Example: Cryptarithmetic

Variables

D E M N O R S Y

Domains

S

E

N D

M O

R

E

M

O

N

E

Y

Constraints

M

S

unary

constraints)

Y

D

E

or

Y = D + E −

, etc.

D

E

D

M

D

N

(^) , etc.

Modified for Fall 2006

Chapter 5

Example: Map coloring

Color a map so that no adjacant countries have the same color Variables

Countries

C

i

Domains

Red, Blue, Green

Constraints

C

1 6 = C 2 , C 1 6

C

5 , etc.

1

C

2

C

3

C

C 5

C 6

4

C

Constraint graph:

1

C

2

C

3

C C 5 C 6 4

C

Modified for Fall 2006

Chapter 5

Applying standard search

Initial stateStates are defined by the values assigned so far We can still use standard search algorithms for CSPs.

: all variables unassigned

Operators

: assign a value to an unassigned variable

Goal test

: all variables assigned, no constraints violated

Modified for Fall 2006 Notice that this is the same for all CSPs!

Chapter 5

Standard search applied to map-coloring

ASSIGNED UNASSIGNED

C1 C2 C

ASSIGNED UNASSIGNED

C1 = RED C2 C

ASSIGNED UNASSIGNED

ASSIGNED UNASSIGNED

C2 = BLUE C1 C

C3 = GREENC1 C

Modified for Fall 2006

Chapter 5

Backtracking search

Use depth-first search, but

  1. fix the order of assignment,

⇒ b = | D i |

(can be done in the

Successors

function)

  1. check for constraint violations

The constraint violation check can be implemented in two ways:

  1. modify

Successors

to assign only values that

are allowed, given the values already assigned

or 2) check constraints are satisfied before expanding a state

Can solveBacktracking search is the basic uninformed algorithm for CSPs

n -queens for

n

≈

Modified for Fall 2006

Chapter 5

Forward checking

Idea

: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values

Simplified map-coloring example:

red

blue

green

C

1

C

2

C

3

C

4

C

5

1

C

2

C

3

C

C 5

4

C

Can solve

n -queens up to

n

≈

Modified for Fall 2006

Chapter 5

Forward checking

Idea

: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values

Simplified map-coloring example:

red

blue

green

C

1

C

2

×

C

3

×

C

4

×

×

C

5

×

×

1

C

2

C

3

C

C 5

4

C

Modified for Fall 2006

Chapter 5

Forward checking

Idea

: Keep track of remaining legal values for unassigned variables Terminate search when any variable has no legal values

Simplified map-coloring example:

red

blue

green

C

1

C

2

×

C

3

×

C

4

×

×

C 5 × × × 1

C

2

C

3

C

C 5

4

C

Modified for Fall 2006

Chapter 5

Heuristics for CSPs – Cont.

least-constraining-value heuristic. (i.e., which color to use first?)

Try to give more freedom for future choices. Given

C

1 (^) =

(^) Green

C

2 (^) =

(^) Red

, choose what for

C

3 (^) =

(^) ??

C

3 (^) =

(^) Red

over

Blue

least-constraining-value

1

C

2

C

3

C

C 5

C 6

C

Can solve

n -queens for

n

≈

Modified for Fall 2006

Chapter 5

19

Iterative algorithms for CSPs

To apply to CSPs:“complete” states, i.e., all variables assigned Hill-climbing, simulated annealing typically work with

operatorsallow states with unsatisfied constraints

reassign

variable values

min-conflictsVariable selection: randomly select any conflicted variable

heuristic:

i.e., hillclimb withchoose value that violates the fewest constraints

h ( n ) = total number of violated constraints

Modified for Fall 2006

Chapter 5