1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of...

106
1 159.302 CSP and Games CSP and Games Introduction Introduction 5 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare

Transcript of 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of...

Page 1: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

1

159.302 CSP and Games CSP and Games

IntroductionIntroduction

55

Constraint Satisfaction Problems

Source of contents: MIT OpenCourseWare

Page 2: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

2

CSPCSPGeneral class of problems: General class of problems: BINARY CSPBINARY CSP

Application areas of CSPs: • scheduling tasks, robot planning tasks, puzzles, molecular structures,

sensor interpretation tasks, etc.

55

This diagram is called a constraint graph.

Variable Vi with values in domain Di

Unary constraint arc

Binary constraint arcUnary constraints just cut down domains.

Page 3: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

3

CSPCSPGeneral class of problems: General class of problems: BINARY CSPBINARY CSP

55

This diagram is called a constraint graph.

Variable Vi with values in domain Di

Unary constraint arc

Binary constraint arc

Unary constraints just cut down domains.

Basic problem:• Find a dj Є Dj for each Vi s.t. all constraints are satisfied (finding consistent labeling for variables)

Page 4: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

4

CSPCSPN-QueensN-Queens as CSP as CSP

Classic “benchmark” problemClassic “benchmark” problem

55

are board positions in N × N chessboardVariables

Place N queens on an N × N chessboard so that none can attack the other.

Q

Q

Q

Q

1

2

3

4

1 2 3 4

Queen or blankDomains

Two positions on a line (vertical, horizontal, diagonal) cannot both be Queen

Constraints

Page 5: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

5

CSPCSPLine labelingsLine labelings as CSP as CSP

55

are line junctionsVariables

Labeling lines in drawing as convex (+), concave (-), or boundary (>).

are set of legal labels for that junction typeDomains

shared lines between adjacent junctions must have same label.

Constraints

All legal junction labels for four junction types.

Page 6: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

6

CSPCSPScheduling Scheduling as CSPas CSP

55

are activitiesVariables

Choose time for activities (e.g. observations on Hubble telescope, or terms to take required classes).

are sets of start times (or “chunks” of time)Domains

1. Activities that use same resource cannot overlap in time.

2. Preconditions satisfied.

Constraints

activityactivity

timetime

Page 7: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

7

CSPCSPGraph Colouring Graph Colouring as CSPas CSP

55

are regionsVariables

Pick colours for map regions, avoiding coloring adjacent regions with the same colour.

are colours allowedDomains

adjacent regions must have different coloursConstraints

Page 8: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

8

CSPCSP3-SAT3-SAT as CSP as CSP

Boolean Satisfiability problems - the original NP-complete problemBoolean Satisfiability problems - the original NP-complete problem

55

are clausesVariables

Find values for boolean variables A, B, C, … that satisfy the formula.

(A or B or !C) and (!A or C or B)

boolean variable assignments that make the clause trueDomains

clauses with shared boolean variables must agree on value of variable.

Constraints

Page 9: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

9

CSPCSPModel-based recognitionModel-based recognition as CSP as CSP

55

are edges in modelVariables

Find given model in edge image, with rotation and translation allowed

set of edges in imageDomains

angle between model & image edges must matchConstraints

Page 10: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

10

CSPCSPGood News / Bad NewsGood News / Bad News

55

very general & interesting class problemsGood News

includes NP-Hard (intractable) problemsBad News

So, good behaviour is a function of domain and not the formulation as CSP.

Page 11: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

11

CSPCSPExampleExample

55

Given 40 courses (8.01, 8.2, …, 6.840) & 10 terms (Fall 1, Spring 1, …, Spring 5). Find a legal schedule.

Page 12: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

12

CSPCSPExampleExample

55

Given 40 courses (8.01, 8.2, …, 6.840) & terms (Fall 1, Spring 1, …, Spring 5). Find a legal schedule.

• Pre-requisities• Courses offered on limited terms• Limited number of courses per term• Avoid time conflicts

Constraints

Page 13: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

13

CSPCSPExampleExample

55

Given 40 courses 40 courses (8.01, 8.2, …, 6.840) & 10 terms 10 terms (Fall 1, Spring 1, …, Spring 5). Find a legal schedule.

• Pre-requisities• Courses offered on limited terms• Limited number of courses per term• Avoid time conflicts

Constraints

Note: CSPs are not for expressing (soft) preferences (e.g. minimise difficulty, balance subject areas, etc.)

Page 14: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

14

CSPCSPExampleExample

55

• Legal combinations of for example 4 courses (but this is huge set of values)

Variables

A. Terms?

Choice of Variables & ValuesChoice of Variables & Values

Domains

Page 15: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

15

CSPCSPExampleExample

55

• Legal combinations of for example 4 courses (but this is huge set of values)

Variables

A. Terms?

Choice of Variables & ValuesChoice of Variables & Values

Domains

• Courses offered during that termB. Terms Slots?

Subdivide terms into slots (e.g. 4 of them

(Fall 1, 1)(Fall 1, 2)(Fall 1, 3)(Fall 1, 4)

Page 16: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

16

CSPCSPExampleExample

55

• Legal combinations of for example 4 courses (but this is huge set of values)

Variables

A. Terms?

Choice of Variables & ValuesChoice of Variables & Values

Domains

• Courses offered during that termB. Terms Slots?

Subdivide terms into slots (e.g. 4 of them

(Fall 1, 1)(Fall 1, 2)(Fall 1, 3)(Fall 1, 4)

• Terms or term slots (term slots allow expressing constraint on limited number of courses / term)

C. Courses?

Page 17: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

17

CSPCSPExampleExample

55

Prerequisite

ConstraintsConstraints

Use courses as variables and term slots as values.

• For pairs of courses that must be ordered.

6.001 6.034

Term before

Term after

Page 18: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

18

CSPCSPExampleExample

55

Prerequisite

ConstraintsConstraints

Use courses as variables and term slots as values.

• For pairs of courses that must be ordered.

6.001 6.034

Term before

Term after

Courses offered only in some terms • Filter domain

Page 19: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

19

CSPCSPConstraintsConstraints

55

Prerequisite

Use courses as variables and term slots as values.

• For pairs of courses that must be ordered.

6.001 6.034

Term before

Term after

Courses offered only in some terms • Filter domain

Limit # courses

slot not equal

for all pairs of variables

• Use term-slots only once

Page 20: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

20

CSPCSPConstraintsConstraints

55

Use courses as variables and term slots as values.

Avoid time conflictsAvoid time conflictsterm not equal

• For pairs offered at same or overlapping times

PrerequisitePrerequisite • For pairs of courses that must be ordered.

6.001 6.034

Term before

Term after

Courses offered only in some termsCourses offered only in some terms • Filter domain

Limit # coursesLimit # courses

slot not equal

for all pairs of variables

• Use term-slots only once

Page 21: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

21

159.302 CSP CSP

Solving CSPsSolving CSPs

55

Source of contents: MIT OpenCourseWare

Page 22: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

22

Solving CSPsSolving CSPs 55

Approaches to solving CSPs are some combination of constraint propagation and search.

1. Constraint propagation – to eliminate values that could not be part of any solution

2. Search – to explore valid assignments

Page 23: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

23

Solving CSPsSolving CSPsConstraint Propagation (Constraint Propagation (akaaka Arc ConsistencyArc Consistency))

55

Arc consistency Arc consistency eliminates values from domain of variable that can never be part of a consistent solution.

Vi → Vj

Directed arc (Vi , Vj) is arc consistentconsistent if

arc. on the constraint by the allowed is y) (x,such that ji DyDx

For every

there exists some

Page 24: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

24

Solving CSPsSolving CSPsConstraint Propagation (aka Arc Consistency)Constraint Propagation (aka Arc Consistency)

55

Arc consistency eliminates values from domain of variable that can never be part of a consistent solution.

Vi → Vj

Directed arc (Vi , Vj) is arc consistent if

arc. on the constraint by the allowed is y) (x,such that ji DyDx

We can achieve consistency on arc by deleting values from Di (domain of variable at tail of constraint arc) that fail this condition.

Page 25: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

25

Solving CSPsSolving CSPsConstraint Propagation (aka Arc Consistency)Constraint Propagation (aka Arc Consistency)

55

Arc consistency eliminates values from domain of variable that can never be part of a consistent solution.

Vi → Vj

Directed arc (Vi , Vj) is arc consistent if

arc. on the constraint by the allowed is y) (x,such that ji DyDx

We can achieve consistency on arc by deleting values from Di (domain of variable at tail of constraint arc) that fail this condition.

Assume domains are of size d at the most, and there are e binary constraints.

Page 26: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

26

Solving CSPsSolving CSPsConstraint Propagation (aka Arc Consistency)Constraint Propagation (aka Arc Consistency)

55

Arc consistency eliminates values from domain of variable that can never be part of a consistent solution.

Vi → Vj

Directed arc (Vi , Vj) is arc consistent if

arc. on the constraint by the allowed is y) (x,such that ji DyDx

We can achieve consistency on arc by deleting values from Di (domain of variable at tail of constraint arc) that fail this condition.

Assume domains are size at most d and there are e binary constraints.

A simple algorithm for arc consistency is O(edO(ed33)) – note that just verifying arc consistency takes O(dO(d22)) for each arc.

Page 27: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

27

CSPCSPConstraint Propagation ExampleConstraint Propagation Example

55

Graph ColouringGraph Colouring

Initial domains are indicated

• Each variable is constrained to have values different from its neighbors

R, G

Different colour constraintR, G, B

G

V1

V2V3

Page 28: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

28

CSPCSPConstraint Propagation ExampleConstraint Propagation Example

55

Graph ColouringGraph Colouring

Initial domains are indicated

• Each undirected constraint arc is really two directed constraint arcs, the effects shown above are from examining both arcs.

R, G

Different colour constraintR, G, B

G

V1

V2V3

Arc examined

Value deleted

R, G

R, G, B

G

V1

V2V3

Page 29: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

29

CSPCSPConstraint Propagation ExampleConstraint Propagation Example

55

Graph ColouringGraph Colouring

Initial domains are indicated

• Each undirected constraint arc is really two directed constraint arcs, the effects shown above are from examining both arcs.

R, G

Different colour constraintR, G, B

G

V1

V2V3

Arc examined

Value deleted

V1-V2 none

R, G

R, G, B

G

V1

V2V3

Page 30: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

30

CSPCSPConstraint Propagation ExampleConstraint Propagation Example

55

Graph ColouringGraph Colouring

Initial domains are indicated

• Each undirected constraint arc is really two directed constraint arcs, the effects shown above are from examining both arcs.

R, G

Different colour constraintR, G, B

G

V1

V2V3

Arc examined

Value deleted

V1-V2 none

V1-V3 V1(G)

R, G

R, B

G

V1

V2V3

Page 31: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

31

CSPCSPConstraint Propagation ExampleConstraint Propagation Example

55

Graph ColouringGraph Colouring

Initial domains are indicated

• Each undirected constraint arc is really two directed constraint arcs, the effects shown above are from examining both arcs.

R, G

Different colour constraintR, G, B

G

V1

V2V3

Arc examined

Value deleted

V1-V2 none

V1-V3 V1(G)

V2-V3 V2(G)R

R, B

G

V1

V2V3

Page 32: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

32

CSPCSPConstraint Propagation ExampleConstraint Propagation Example

55

Graph ColouringGraph Colouring

Initial domains are indicated

• In general we need to make one pass through any arc whose head variable has changed until no further changes are observed before we can stop.

R, G

Different colour constraintR, G, B

G

V1

V2V3

Arc examined

Value deleted

V1-V2 none

V1-V3 V1(G)

V2-V3 V2(G)

V1-V2 V1(R)

V1-V3 none

V2-V3 none

R

B

G

V1

V2V3

Page 33: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

33

CSPCSPBut, arc consistency is not enough in general!But, arc consistency is not enough in general!

55

Graph ColouringGraph Colouring

R, G

R, G

R, G

V1

V2V3

• Arc consistent but NO SOLUTIONS

We need one colour for each variable!

Page 34: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

34

CSPCSPBut, arc consistency is not enough in general!But, arc consistency is not enough in general!

55

Graph ColouringGraph Colouring

R, G

R, G

R, G

V1

V2V3

• Arc consistent but NO SOLUTIONS

R, G

B, G

R, G

V1

V2V3

• Arc consistent but 2 SOLUTIONS: • B, R, G• B, G, R

Page 35: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

35

CSPCSPBut, arc consistency is not enough in general!But, arc consistency is not enough in general!

55

Graph ColouringGraph Colouring

R, G

R, G

R, G

V1

V2V3

• Arc consistent but NO SOLUTIONS

R, G

B, G

R, G

V1

V2V3

• Arc consistent but 2 SOLUTIONS: • B, R, G• B, G, R

R, G

B, G

R, G

V1

V2V3

• Arc consistent but 1 SOLUTION

Assume B, R not allowed

Page 36: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

36

CSPCSPBut, arc consistency is not enough in general!But, arc consistency is not enough in general!

55

Graph ColouringGraph Colouring

R, G

R, G

R, G

V1

V2V3

• Arc consistent but NO SOLUTIONS

R, G

B, G

R, G

V1

V2V3

• Arc consistent but 2 SOLUTIONS: • B, R, G• B, G, R

R, G

B, G

R, G

V1

V2 V3

• Arc consistent but 1 SOLUTION

Assume B, R not allowed

We need to apply SearchSearch algorithms to find solutions (if

there is any)

Page 37: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

37

CSPCSP 55

V1 assignments

When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search).

V2 assignments

V3 assignments

RG

B

R

R R R R

G G GR R

G G GG

R, G

R, G, B

R, G

V1

V2V3

Page 38: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

38

CSPCSP 55

V1 assignments

When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search).

V2 assignments

V3 assignments

RG

B

R

R R R R

G G GR R

G G GG

R, G

R, G, B

R, G

V1

V2V3

Backup at inconsistent assignment.

Inconsistent with V1 = R

Page 39: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

39

CSPCSP 55

V1 assignments

When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search).

V2 assignments

V3 assignments

RG

B

R

R R R R

G G GR R

G G GG

R, G

R, G, B

R, G

V1

V2V3

Backup at inconsistent assignment.

Inconsistent with V1 = R

Page 40: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

40

CSPCSP 55

V1 assignments

When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search).

V2 assignments

V3 assignments

RG

B

R

R R R R

G G GR R

G G GG

R, G

R, G, B

R, G

V1

V2V3

Backup at inconsistent assignment.

Inconsistent with V1 = R

Page 41: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

41

CSPCSP 55

V1 assignments

When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search).

V2 assignments

V3 assignments

RG

B

R

R R R R

G G GR R

G G GG

R, G

R, G, B

R, G

V1

V2V3

Backup at inconsistent assignment.

Inconsistent with V1 = R Inconsistent with V2 = G

Page 42: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

42

CSPCSP 55

V1 assignments

When we have too many values in domain (and/or constraints are weak) arc consistency doesn’t do much, so we need to search. Simplest approach is pure backtracking (depth-first search).

V2 assignments

V3 assignments

RG

B

R

R R R R

G G GR R

G G GG

R, G

R, G, B

R, G

V1

V2V3

Backup at inconsistent assignment.

Inconsistent with V1 = R Inconsistent with V2 = G

Page 43: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

43

Solving CSPsSolving CSPsCombine Backtracking & Constraint PropagationCombine Backtracking & Constraint Propagation

55

A node in BT tree is a partial assignment in which the domain of each variable has been set (tentatively) to singleton set.

Use constraint propagation (arc-consistency) to propagate the effect of the tentative assignment, i.e. eliminate values inconsistent with current values.

Page 44: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

44

Solving CSPsSolving CSPsCombine Backtracking & Constraint PropagationCombine Backtracking & Constraint Propagation

55

A node in BT tree is a partial assignment in which the domain of each variable has been set (tentatively) to singleton set.

Use constraint propagation (arc-consistency) to propagate the effect of the tentative assignment, i.e. eliminate values inconsistent with current values.

How much propagation to do?

Page 45: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

45

Solving CSPsSolving CSPsCombine Backtracking & Constraint PropagationCombine Backtracking & Constraint Propagation

55

A node in BT tree is a partial assignment in which the domain of each variable has been set (tentatively) to singleton set.

Use constraint propagation (arc-consistency) to propagate the effect of the tentative assignment, i.e. eliminate values inconsistent with current values.

How much propagation to do?Answer: Not much, just local propagation from domains with unique assignments, which is called forward checking (FC). This conclusion is not necessarily obvious, but generally holds in practice.

Page 46: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

46

CSPCSP 55

V1 assignments

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

V2 assignments

V3 assignments

R

R, G

R, G, B

R, G

V1

V2 V3

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 47: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

47

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

R

G

R

G

V1

V2V3

G

We eliminate any values that are inconsistent with the assignment.

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 48: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

48

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

R

G

R

V1

V2V3

G

We have a conflict whenever a domain becomes empty.

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 49: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

49

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

G

When backing up, we need to restore domain values, since deletions were done to reach consistency with tentative assignments considered during search.

R, G

R, G, B

R, G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 50: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

50

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

G

We eliminate G from V2 and V3.

R

G

R

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 51: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

51

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

G

We now consider V2 = R and propagate.

R

G

R

V1

V2 V3

R

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 52: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

52

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

G

The domain of V3 is now empty and so we fail and backup.

R

G

V1

V2 V3

R

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 53: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

53

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

B

R, G

R, G, B

R, G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

So, we move to consider V1 = B and propagate.

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 54: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

54

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

B

R, G

B

R, G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

The propagation does not delete any values. We pick V2 = R and propagate.

R

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 55: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

55

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

B

R

B

G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

This removes the R values in the domains of V1 and V3.

R

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 56: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

56

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

B

R

B

G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

We pick V3 = G and have a consistent assignment.

R

G

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 57: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

57

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

B

R

B

G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

We can continue the process to find the other consistent solution.

R

G

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

Page 58: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

58

CSPCSP 55

V1 assignments

V2 assignments

V3 assignments

B

Backtracking with Forward Checking (BT-FC)Backtracking with Forward Checking (BT-FC)

R

B

G

V1

V2 V3

When examining an assignment Vi = dk, remove any values inconsistent with that assignment from neighboring domains in constraint graph.

No need to check previous assignments

R

G

Generally preferable to pure BT.

Page 59: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

59

159.302 CSP and Games CSP and Games

Solving CSPs: Other Solving CSPs: Other StrategiesStrategies

55

Source of contents: MIT OpenCourseWare

Page 60: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

60

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Traditional backtracking uses fixed ordering ordering ofof variables variables & & valuesvalues, e.g. random order or place variables with constraints first.

You can usually do better by choosing an order dynamically as the search proceeds.

Ordering of variables can have ahave a substantial effect on the cost of substantial effect on the cost of finding the answerfinding the answer. We can re-

order variables based on information available during a

search.

Page 61: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

61

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Traditional backtracking uses fixed ordering of variables & values, e.g. random order or place variables with constraints first.

You can usually do better by choosing an order dynamically as the search proceeds.

• Most constrained variablewhen doing forward-checking, pick variable with fewest legal values to assign next (minimise branching factor)

Page 62: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

62

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Traditional backtracking uses fixed ordering of variables & values, e.g. random order or place variables with constraints first.

You can usually do better by choosing an order dynamically as the search proceeds.

• Most constrained variablewhen doing forward-checking, pick variable with fewest legal values to assign next (minimise branching factor)

• Least constraining valuechoose value that rules out the fewest values from

neighboring domains

Page 63: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

63

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Traditional backtracking uses fixed ordering of variables & values, e.g. random order or place variables with constraints first.

You can usually do better by choosing an order dynamically as the search proceeds.

• Most constrained variablewhen doing forward-checking, pick variable with fewest legal values to assign next (minimise branching factor)

• Least constraining valuechoose value that rules out the fewest values from

neighboring domains

e.g. This combination improves feasible N-Queens performance from about n=30 with just FC to about n=1000 with FC & ordering

Page 64: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

64

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Which country should we colour next?

The 4-Colour Map-Colouring Problem illustrates a simple

situation for variable and value ordering.

Colours: Colours: RR, , GG, , BB, , YY

Which colour should we pick for it?

Page 65: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

65

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Which country should we colour next?

The 4-Colour Map-Colouring Problem illustrates a simple

situation for variable and value ordering.

Colours: Colours: RR, , GG, , BB, , YY

Which colour should we pick for it?

E is most constrained variable (smallest domain)

Page 66: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

66

Solving CSPsSolving CSPsBT-FC with Dynamic OrderingBT-FC with Dynamic Ordering

55

Which country should we colour next?

The 4-Colour Map-Colouring Problem illustrates a simple

situation for variable and value ordering.

Colours: Colours: RR, , GG, , BB, , YY

Which colour should we pick for it?

E is most constrained variable (smallest domain)

Red – least constraining value (eliminates fewest values from neighboring domains)

Page 67: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

67

Solving CSPsSolving CSPsIncremental RepairIncremental Repair (Min-Conflict Heuristic) (Min-Conflict Heuristic)

55

1. Initialise a candidate solution using “greedy” heuristic – get solution “near” correct one.

2. Select a variable in conflict and assign it a value that minimises the number of conflicts (break ties randomly).

• Can use this heuristic as part of systematic backtracker that uses heuristics to do value ordering or in a local hill-climber (without backup).

Size(n)

Sec.(Sparc 1)

Performance on N-Queens (with good initial guess)

Page 68: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

68

Solving CSPsSolving CSPsMin-Conflict HeuristicMin-Conflict Heuristic

55

The pure hill climber (without backtracking) can get stuck in local minima. Can add random moves to attempt getting out of minima – generally quite effective. Can also use weights on violated constraints & increase weight every cycle if it remains violated.

• Restart the search with a new random initial state.• Randomised hill-climber used to solve SAT problems. One of the most effective

methods ever found for this problem.

GSAT

GSAT can solve SAT problems of mind-boggling complexity. It has set a

new standard for classifying SAT problems as “hardhard”, because almost

any random problem is “easy” for GSAT.

Page 69: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

69

Solving CSPsSolving CSPsGSAT as Heuristic SearchGSAT as Heuristic Search

55

State Space:State Space: Space of all full assignments to variables

Initial State:Initial State: a random full assignment

Goal State:Goal State: a satisfying assignment

Actions:Actions: flip value of one variable in current assignment

Heuristic:Heuristic: the number of satisfied clauses (constraints); we want to maximise this score. Alternatively, minimise the number of unsatisfied clauses (constraints).

Page 70: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

70

Solving CSPsSolving CSPsAlgorithm: Algorithm: GSAT(F)GSAT(F)

55

• For i=1 to MaxTries• Select a complete random assignment A• Score = number of satisfied clauses• For i=1 to MaxFlips

• If (A satisfies all clauses in F) { return A }• Else { Flip a variable that maximises the Score }• Flip a randomly chosen variable if no variable flip increases the Score

MaxTries and MaxFlips are user-defined. These guard against local minimalocal minima in the

search.

Page 71: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

71

Solving CSPsSolving CSPsAlgorithm: Algorithm: WALKSAT(F)WALKSAT(F)

55

• For i=1 to MaxTries• Select a complete random assignment A• Score = number of satisfied clauses• For i=1 to MaxFlips

• If (A satisfies all clauses in F) { return A }• Else {

• With probability p //GSATGSAT• Flip a variable that maximises the Score• Flip a randomly chosen variable if no variable flip increases the Score

• With probability (1-p) //Random WalkRandom Walk• Pick a random unsatisfied clause C• Flip a randomly chosen variable in C

}

It turns out that adding more randomnessmore randomness is a more effective strategy!

Page 72: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

72

159.302 CSP and Games CSP and Games

Introduction to GamesIntroduction to Games

55

Source of contents: MIT OpenCourseWare

Approaches to building two player games

Page 73: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

73

GamesGamesBoard Games & SearchBoard Games & Search

55

1949 1949 Shannon paper

1951 1951 Turing paper

1958 1958 Bernstein paper

55-60 55-60 Simon-Newell program(α-β McCarthy?)

66-67 66-67 MacHack 6 (MIT AI)

70’s 70’s NW Chess 4.5

80’s 80’s Cray Blitz

90’s 90’s Belle, Hitech, Deep Thought, Deep Blue

• Move generationMove generation• Static evaluationStatic evaluation• Min-MaxMin-Max• Alpha-BetaAlpha-Beta• Practical MattersPractical Matters

Claude Shannon and his electromechanical mouse Theseus, one of the earliest experiments in artificial intelligence.Image Copyright 2001 Lucent Technologies, Inc. All rights reserved.

Page 74: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

74

GamesGamesGame Tree SearchGame Tree Search

55

Initial State:Initial State: initial board position and player

Operators:Operators: one for each legal move

Goal States:Goal States: winning board positions

Scoring Function:Scoring Function: assigns numeric value to states

Game tree:Game tree: encodes all possible games

•We are not looking for a path, only the next move to make (that hopefully leads to a winning position)

•Our best move depends on what the other player does.

Page 75: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

75

GamesGamesMove GenerationMove Generation

55

ChessChess b = 36 d > 40 3640 is big!

Page 76: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

76

GamesGamesPartial Game Tree for Tic-Tac-ToePartial Game Tree for Tic-Tac-Toe

55

Even for this trivial game, the search tree is quite big.

Page 77: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

77

GamesGamesScoring FunctionScoring Function

55

Assigns a numerical value to a board position.

Page 78: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

78

GamesGamesScoring Function: Static EvaluationScoring Function: Static Evaluation

55

A linear function in which some set of coefficients is used to weight a number of “features” of the board position.

Too weak to predict ultimate success.

Page 79: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

79

GamesGamesLimited look ahead + ScoringLimited look ahead + Scoring

55

The The Min-MaXMin-MaX Algorithm Algorithm

Page 80: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

80

GamesGamesMin-MaXMin-MaX Algorithm Algorithm

55

• function MAX·VALUE(state, depth)• if (depth == 0) then return EVAL(state)• v = -∞• For each s in SUCCESSORS(state) do

v = MAX(v, MIN·VALUE(s, depth – 1)) endreturn v

• function MIN·VALUE (state, depth)• if (depth == 0) then return EVAL(state)• v = ∞• For each s in SUCCESSORS(state) do

v = MIN(v, MAX·VALUE(s, depth – 1)) endreturn v

Page 81: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

81

GamesGamesUSCF RatingUSCF Rating

55

Somehow, it seems as if brute-force search is all that matters.

Page 82: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

82

GamesGamesDeep BlueDeep Blue

55

32 SP2 processors each with 8 dedicated chess processors= 256 CP

50-100 billion moves in 3 min 13-30 ply search

Page 83: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

83

GamesGamesAlpha-Beta PruningAlpha-Beta Pruning

55

α – is the lower bound on score

β – is the upper bound on score

2

2

2 7 1anything

maxmax

minmin

Page 84: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

84

GamesGamesAlpha-Beta PruningAlpha-Beta Pruning

55

function MAX·VALUE(state, α, β, depth)• if (depth == 0) then return EVAL(state)• For each s in SUCCESSORS(state) do

α = MAX(α, MIN·VALUE(s, α, β, depth-1))If(α ≥ β) Then return α //cut-off

endreturn α

function MIN·VALUE(state, α, β, depth)• if (depth == 0) then return EVAL(state)• For each s in SUCCESSORS(state) do

β = MIN(β, MAX·VALUE(s, α, β, depth-1))If(β ≤ α ) Then return β //cut-off

endreturn β

α – is the best score for MAX; β – is the best score for MINInitial call is MAX·VALUE(state, -∞, ∞, MAX·DEPTH)

Page 85: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

85

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

We start with an initial call to MAX·VALUE.

MAX·VALUE(state, -∞, ∞, MAX·DEPTH)

Page 86: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

86

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

MAX·VALUE now calls MIN·VALUE on the left successor with the same values of alpha and beta.MIN·VALUE now calls MAX·VALUE on its leftmost succesor.

- ∞, ∞

Page 87: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

87

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

MAX·VALUE is at the leftmost leaf, whose leaf value is 2 and so it returns that.

- ∞, ∞

Page 88: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

88

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

This first value, since it is less than ∞, becomes the new value of β in MIN·VALUE.

- ∞, 2

Page 89: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

89

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

So now we call MAX·VALUE with the next successor, which is also a leaf whose value is 7.

- ∞, 2

Page 90: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

90

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

7 is not less than 2 and so the final value of β is 2 for this node.

- ∞, 2

Page 91: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

91

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

- ∞, ∞

MIN·VALUE now returns 2 to its caller.

- ∞, 22

Page 92: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

92

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

The calling MAX·VALUE now sets α to 2, since it is bigger than -∞. Note that the range of [alpha-beta] says that the score will be greater or equal to 2 (and less than ∞).

- ∞, 22

Page 93: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

93

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

MAX·VALUE now calls MIN·VALUE with an updated range of [alpha-beta].

- ∞, 22 2, ∞

Page 94: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

94

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

MIN·VALUE calls MAX·VALUE on the left leaf and it returns a value of 1.

- ∞, 22 2, ∞

Page 95: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

95

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

This is used to update beta in MIN·VALUE, since it is less than ∞. Note that at this point, we have a range where α=2 is greater than β=1.

- ∞, 22 2, 1

Page 96: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

96

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

This is used to update beta in MIN·VALUE, since it is less than ∞. Note that at this point, we have a range where α=2 is greater than β=1.

- ∞, 22 2, 1

This situation signals a cut-off in MIN·VALUE and it returns beta(=1), without looking at the right leaf.

β ≤ α

Cut-off!

Page 97: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

97

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

- ∞, 22 2, 1

This situation signals a cut-off in MIN·VALUE and it returns beta(=1), without looking at the right leaf.

β ≤ α

Cut-off!

So, basically we had already found a move that guaranteed us a score ≥ 2 so that when we got into a situation where the score was guaranteed to be ≤ 1, we could stop.

anything

Page 98: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

98

GamesGamesAlpha-Beta Pruning in actionAlpha-Beta Pruning in action

55

2 7 1

maxmax

minmin

2, ∞

- ∞, 22 2, 1

β ≤ α

Cut-off!

So, a total of 3 static evaluations were needed instead of the 4 we would have needed under pure Min·Max.

anything

Page 99: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

99

GamesGamesαα--ββ (NegaMax form) (NegaMax form) Alpha-Beta Pruning in a more compact formAlpha-Beta Pruning in a more compact form

55

function ALPHA·BETA(state, α, β, depth)• if (depth == 0) then return EVAL(state)• For each s in SUCCESSORS(state) do

α = MAX(α, ALPHA·BETA(s, -β, -α, depth-1))If(α ≥ β) Then return α //cut-off

endreturn α

α – is the best score for MAX; β – is the best score for MINInitial call is ALPHA·BETA(state, -∞, ∞, MAX·DEPTH)

Basically, this exploits the idea that minimizing is the same as maximising the negatives of the scores.

Page 100: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

100

GamesGamesKey points about Key points about αα--ββ

55

1. Guaranteed same value as Max-Min.

2. In a perfectly ordered tree, expected work is O(bd/2) vs. O(bd) for Max-Min, so can search twice as deep with the same effort!

3. With good move ordering, the actual running time is close to optimistic estimate.

Page 101: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

101

GamesGamesGame Program Game Program

55

1. Move generator (ordered moves) 50%

2. Static evaluation 40%

3. Search control 10%

In practice, • Openings• End games

Played by looking up moves in a Database

[all in place by late 60’s]

Page 102: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

102

GamesGamesMove Generator Move Generator

55

1. Legal moves

2. Ordered by• most valuable victim• least valuable agressor

3. Killer heuristic

Page 103: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

103

GamesGamesStatic EvaluationStatic Evaluation

55

Initially Very complex

70’s Very simple (material)

Now • Deep searches: moderately complex (hardware)

• PC programs: elaborate, hand-tuned

Page 104: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

104

GamesGamesPractical mattersPractical matters

55

Variable branching

Iterative Deepening• Order best move from last search first

• use previous backed up value to initialise [α, β]

• keep track of repeated positions (transposition tables)

Horizon Effect

• quiescence

• pushing the inevitable over search horizon

Parallelisation

Page 105: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

105

GamesGamesPractical mattersPractical matters

55

Backgammon

• Involves randomness – dice rolls

• machine-learning based player was able to draw the world champion

Bridge

• Involves hidden information – other player’s cards, and communication during bidding

• Computer players play well but do not bid well

Go

• No new elements but huge branching factor

• No good computer players exist

Page 106: 1 CSP and Games 159.302 CSP and Games Introduction 5 Constraint Satisfaction Problems Source of contents: MIT OpenCourseWare.

106

GamesGamesObservationsObservations

55

Computers excel in well-defined activities where rules are clear

• chess

• mathematics

Success comes after a long period of gradual refinement

For more details on building game programs, visit:

http://www.ics.uci.edu/~eppstein/180a/w99.html