managing search

43
Managing Search General Game Playing Lecture 5 Michael Genesereth / Nat Love Spring 2006

Upload: cullen-short

Post on 02-Jan-2016

29 views

Category:

Documents


0 download

DESCRIPTION

General Game PlayingLecture 5. Managing Search. Michael Genesereth / Nat Love Spring 2006. Game Tree Search. X. O. X. O. X. X. X. X. O. X. O. X. O. O. X. X. X. X. X. O. O. O. O. X. X. O. X. O. O. X. X. X. X. O. X. O. X. O. X. X. X. O. O. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Managing Search

Managing Search

General Game Playing Lecture 5

Michael Genesereth / Nat Love Spring 2006

Page 2: Managing Search

2

Game Tree SearchX O X

O

X O X

O X

X O X

O X

O

X O X

O X

OX

X O X

O X

O X

X O X

O X

O

X

100

100

100

100

100100 100

100

X O X

O

X

X O X

O

X

X O X

O

X

X O X

O X

Page 3: Managing Search

3

Resource Limitations

Large state spaces ~5000 states in Tic-Tac-Toe ~1030 states in Chess

Even larger game trees ~900,000 nodes in Tic-Tac-Toe

Limited Resources Memory Time (start clock, move clock)

Page 4: Managing Search

4

Managing Search

Search Direction

Search Strategy

Incremental Game Tree Search

Game Graph Search

Page 5: Managing Search

5

Search Direction

Page 6: Managing Search

6

Forward Search

f

j

k

c

d

a

e

g

m

b

l

n

h

i

Page 7: Managing Search

7

Backward Search

f

j

k

c

d

n

e

g

b

b

a

c

h

i

Page 8: Managing Search

8

Bidirectional Search

ma

Page 9: Managing Search

9

Search Strategy

Page 10: Managing Search

10

Depth First Search

a

c

hg

d

ji

b

fe

a b e f c g h d i j

Advantage: Small intermediate storageDisadvantage: Susceptible to garden pathsDisadvantage: Susceptible to infinite loops

Page 11: Managing Search

11

Breadth First Search

a b c d e f g h i j

a

c

hg

d

ji

b

fe

Advantage: Finds shortest pathDisadvantage: Consumes large amount of space

Page 12: Managing Search

12

Time Comparison

Branching 2 and depth d and solution at depth k

Time Best Worst

Depth k 2d −2d−k

Breadth 2k−1 2k−1

Page 13: Managing Search

13

Time Comparison

Analysis for branching b and depth d and solution at depth k.

Time Best Worst

Depth kbd−bd−k

b−1

Breadthbk−1−1b−1

+1bk−1b−1

Page 14: Managing Search

14

Space Comparison

Total depth d and solution depth k.

Space Binary General

Depth d (b−1)×(d−1)+1

Breadth 2k−1 bk−1

Page 15: Managing Search

15

Iterative Deepening

Run depth-limited search repeatedly,

starting with a small initial depth,

incrementing on each iteration,

until success or run out of alternatives.

Page 16: Managing Search

16

Example

a

c

hg

d

ji

b

fe

aa b c da b e f c g h d i j

Advantage: Small intermediate storageAdvantage: Finds shortest pathAdvantage: Not susceptible to garden pathsAdvantage: Not susceptible to infinite loops

Page 17: Managing Search

17

Time Comparison

Depth Iterative Depth

1 1 1

2 4 3

3 11 7

4 26 15

5 57 31

n 2n+1−n−2 2n−1

Page 18: Managing Search

18

General Results

Theorem [Korf]: The cost of iterative deepening search is b/(b-1) times the cost of depth-first search (where b is the branching factor).

Theorem: The space cost of iterative deepening is no greater than the space cost for depth-first search.

Page 19: Managing Search

19

Incremental Game Tree Search

Page 20: Managing Search

20

Game Tree Search

Problem: The clock may be too short to permit complete search on a single move.

Approach: Incremental Tree Search

Page 21: Managing Search

21

Incremental Game Graph Search

Page 22: Managing Search

22

Nodes Versus States

The game tree for Tic-Tac-Toe has approximately 900,000 nodes. There are approximately 5,000 distinct states. Searching the tree requires 180 times more work than searching the graph.

One small hitch: The graph is implicit in the state description and must be built in advance or incrementally. Recognizing a repeated state takes time that varies with the size of the graph thus far seen. Solution: Hashing.

Page 23: Managing Search

23

Single Player Game Graph

s3s1

s2

s4

Page 24: Managing Search

24

Multiple Player Game Graph

s3s1

s2

s4

aa

bb

baab

Page 25: Managing Search

25

Bipartite Game Graph

s3

s1

a

b

s2

s4

aa

bb

ba

ab

Page 26: Managing Search

26

Bipartite Association List

Simple Association List((a . s2) (b . s3))

Bipartite Association List:((a . (((a a) . s2) ((a b) . s1))) (b . (((b a) . s3) ((b b) . s4))))

Page 27: Managing Search

27

Incremental Tree Search Code

Page 28: Managing Search

28

Players

class player (thing) {var match nil; var role nil; var roles nil; var theory nil; var startclock 0; var playclock 0 var hasher nil; var action nil; var root nil; var fringe nil}

Page 29: Managing Search

29

Tree Expansion

function expands (player,count) {var node; for i 1 until i > count or null(player.fringe) do {node first(player.fringe); player.fringe rest(player.fringe); unless numberp(node.score) i i + 1; player.fringe nconc(player.fringe,expand(node))}; return done}

Page 30: Managing Search

30

Nodes

class node (thing) {var player nil; var data nil; var theory nil; var parent nil; var alist nil; var score nil}

Page 31: Managing Search

31

Single Player Node Expansion

function expand (node) {var match node.match; var role match.role; var old; var data; var al; var nl; for a in legals(role,node) {old node.data; node.data cons(does(role,a), old); data simulate(node); node.data old; new makenode(match, data, node.theory, node) if termp(new) then new.score reward(role, new); nl cons(new, nl); al acons(a, new, al)} node.alist nreverse(al) return nreverse(nl)}

Page 32: Managing Search

32

Multiple Player Node Expansion

function expand (node) {var match node.match; var role match.role; var old; var data; var al; var nl; for a in legals(role,node) {for j in joints(role, a, player.roles, node, nil, nil) {old node.data; node.data consactions(match.roles, j, old); data simulate(node); node.data old; new makenode(match, data, node.theory, node) if termp(new) then new.score reward(role, new); nl cons(new, nl); bl acons(j, new, bl)} al acons(a, nreverse(bl), al)} node.alist nreverse(al); return nreverse(nl)}

Page 33: Managing Search

33

Multiple Player Node Expansion

function expand (node) {var match node.match; var role match.role; var old; var data; var al; var nl; for a in legals(role,node) {for j in joints(role, a, player.roles, node, nil, nil) {old node.data; node.data consactions(match.roles, j, old); data simulate(node); node.data old; new makenode(match, data, node.theory, node) if termp(new) then new.score reward(role, new); nl cons(new, nl); bl acons(j, new, bl)} al acons(a, nreverse(bl), al)} node.alist nreverse(al); return nreverse(nl)}

Page 34: Managing Search

34

Subroutines

function consactions (roles, actions, data) {var nl; for role in roles, action in actions nl cons(does(role,action), nl) return nreverse(nl)}

function joints (role, action, roles, node, xl, nl) {if null(roles) then cons(reverse(xl),nl) else if car(roles) = role then joints(role,action,cdr(roles),node,cons(action,xl),nl) else for x in legals(car(roles),node) nl joints(role,action,cdr(roles),node, cons(x,xl),nl) return nl}

Page 35: Managing Search

35

Best Move

function bestmove (node) {var best; var score; for pair in node.alist do {score maxscore(pair.right); if score = 100 then return pair.left; else if score = 0; else if null(best) then best pair.left} return best or car(node.alist).left}

Page 36: Managing Search

36

Node Evaluation

function maxscore (node) {var score nil; var max 0; if node.score then node.score; else if null(node.alist) then nil; else for pair in node.alist do {score minscores(pair.right) if score = 100 then return 100 else if not numberp(score) then max nil else if null(max) else if score > max then max score} return max}

Page 37: Managing Search

37

Node Evaluation

function maxscore (node) {var score nil; var max 0; if node.score then node.score; else if null(node.alist) then nil; else for pair in node.alist do {score minscores(pair.right) if score = 100 then return 100 else if not numberp(score) then max nil else if null(max) else if score > max then max score} return max}

Page 38: Managing Search

38

Node Evaluation

function minscores (al) {var score nil; var min 100; for pair in al do {score maxscore(pair.right) if score = 0 then return 0 else if not numberp(score) then min nil else if null(min) else if score < min then min score} return min}

Page 39: Managing Search

39

Graph Search Code

Page 40: Managing Search

40

Node Expansion

function expand (node) {var player node.player; var old; var data; var al; var nl; for a in legals(role,node) {for j in joints(player.role, a, player.roles, node, nil, nil) {old node.data; node.data consactions(player.roles, j, old); data sort(simulate(node)); node.data old; if new gethash(data,player.hasher) then new else {new makenode(player, data, node.theory, node) gethash(data, player.hasher) new; if termp(new) then new.score reward(player.role, new); nl cons(new, nl)}; bl acons(j, new, bl)} al acons(a, nreverse(bl), al)} node.alist nreverse(al) return nreverse(nl)}

Page 41: Managing Search

41

Node Expansion With State Collapse

function expand (node) {var match node.match; var old; var data; var al; var nl; for a in legals(role,node) {for j in joints(match.role, a, match.roles, node, nil, nil) {old node.data; node.data consactions(match.roles, j, old); data sort(simulate(node),minlessp); node.data old; if new gethash(data, match.hasher) then new else {new makenode(match, data, node.theory, node) gethash(data, match.hasher) new; if termp(new) then new.score reward(match.role, new); nl cons(new, nl)}; bl acons(j, new, bl)} al acons(a, nreverse(bl), al)} node.alist nreverse(al) return nreverse(nl)}

Page 42: Managing Search

42

Ordering Code

function minlessp (x,y) {if numberp(x) then {if numberp(y) then x<y else true} else if symbolp(x) then {if numberp(y) then nil else if symbolp(y) then x.name<y.name else true} else if numberp(y) then nil else if symbolp(y) then nil else for l x then cdr(l) for m y then cdr(m) do {if null(l) then return not null(m) else if null(m) then return nil else if minlessp(car(l),car(m)) then return true else if car(l)=car(m) then nil else return nil}}

Page 43: Managing Search

43