Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computational Logic General Game PlayingLecture 4 Michael Genesereth / Nat Love Spring 2006.

Similar presentations


Presentation on theme: "Computational Logic General Game PlayingLecture 4 Michael Genesereth / Nat Love Spring 2006."— Presentation transcript:

1 Computational Logic General Game PlayingLecture 4 Michael Genesereth / Nat Love Spring 2006

2 2 Approaches to General Game Playing Logic - use logical reasoning for everything Computing roles Computing initial state Computing legality of moves Computing consequences of actions Computing goal achievement Computing termination Other - Convert to other representations Simplicity of logical formulation Simple, widely published algorithms

3 3 State Representation true(cell(1,1,x)) true(cell(1,2,b)) true(cell(1,3,b)) true(cell(2,1,b)) true(cell(2,2,o)) true(cell(2,3,b)) true(cell(3,1,b)) true(cell(3,2,b)) true(cell(3,3,x)) true(control(o))

4 4 Getting Started Initial State:Current State: init(cell(1,1,b)) true(cell(1,1,b)) init(cell(1,2,b)) true(cell(1,2,b)) init(cell(1,3,b)) true(cell(1,3,b)) init(cell(2,1,b)) true(cell(2,1,b)) init(cell(2,2,b)) true(cell(2,2,b)) init(cell(2,3,b)) true(cell(2,3,b)) init(cell(3,1,b)) true(cell(3,1,b)) init(cell(3,2,b)) true(cell(3,2,b)) init(cell(3,3,b)) true(cell(3,3,b)) init(control(x)) true(control(x))

5 5 Simulation (Step 1) Current State:Next in Current State: true(cell(1,1,x)) next(cell(1,1,x)) true(cell(1,2,b)) next(cell(1,2,b)) true(cell(1,3,b)) next(cell(1,3,o)) true(cell(2,1,b)) next(cell(2,1,b)) true(cell(2,2,o)) next(cell(2,2,o)) true(cell(2,3,b)) next(cell(2,3,b)) true(cell(3,1,b)) next(cell(3,1,b)) true(cell(3,2,b)) next(cell(3,2,b)) true(cell(3,3,x)) next(cell(3,3,x)) true(control(o)) next(control(x)) Latest moves: does(x,noop) does(o,mark(1,3))

6 6 Simulation (Step 2) Next in Previous State:New Current State: next(cell(1,1,x)) true(cell(1,1,x)) next(cell(1,2,b)) true(cell(1,2,b)) next(cell(1,3,o)) true(cell(1,3,o)) next(cell(2,1,b)) true(cell(2,1,b)) next(cell(2,2,o)) true(cell(2,2,o)) next(cell(2,3,b)) true(cell(2,3,b)) next(cell(3,1,b)) true(cell(3,1,b)) next(cell(3,2,b)) true(cell(3,2,b)) next(cell(3,3,x)) true(cell(3,3,x)) next(control(x)) true(control(x))

7 7 Legality Current State:Legality: true(cell(1,1,x)) legal(x,noop) true(cell(1,2,b)) true(cell(1,3,b)) legal(o,mark(1,2)) true(cell(2,1,b)) legal(o,mark(1,3)) true(cell(2,2,o)) legal(o,mark(2,1)) true(cell(2,3,b)) legal(o,mark(2,3)) true(cell(3,1,b)) legal(o,mark(3,1)) true(cell(3,2,b)) legal(o,mark(3,2)) true(cell(3,3,x)) true(control(o))

8 8 Goals and Termination Current State:Goals: true(cell(1,1,x)) goal(x,0) true(cell(1,2,o)) goal(o,100) true(cell(1,3,b)) true(cell(2,1,b)) Termination: true(cell(2,2,o)) terminal true(cell(2,3,x)) true(cell(3,1,b)) true(cell(3,2,o)) true(cell(3,3,x)) true(control(x))

9 9 Game Descriptions Defnitions for Universal Intensional Relations: role(player) init(proposition) Intensional Relations defined using true and does : next(proposition) Intensional Relations defined in terms of true : legal(player,action) goal(proposition,score) terminal

10 10 Abstract Example of Inference Data: m(a,b) n(b,b) n(b,c) Rules: p(X,Z) :- m(X,Y) & n(Y,Z) q(Z) :- p(X,Z) & ~n(Z,Z) Results: p(a,b) p(a,c) q(c)

11 11 Ground Reduction

12 12 Example Data: m(a,b) n(b,b) n(b,c) Rules: p(a,c) :- m(a,c) p(a,c) :- m(a,b) & n(b,c) q(c) :- p(a,c) & ~n(c,c) Results: q(c)

13 13 Ground Reduction (Part 1) All problems are treated as conjunctions consisting of zero or more conjuncts. Basic Loop: Remove first conjunct from conjunction and replace by its reduction (another conjunction of zero or more conjuncts). If there is no reduction, fail. Succeed when there are no conjuncts left.

14 14 Ground Reduction (Part 2) Atomic Sentences with Extensional Relations If sentence is true is in the database, reduction is the empty conjunction Atomic Sentences with Intensional Relations For each rule with the first conjunct as head, remove conjunct, replace by body of rule, and loop. Goal: p(a,c) & ~n(c,c) Rule: p(a,c) :- m(a,b) & n(b,c) Reduction: m(a,b) & n(b,c) & ~n(c,c)

15 15 Ground Reduction (Part 3) Disjunctions multiple reductions, one for each disjunct Goal: ( p(a,c) | q(a,c)) & ~n(c,c) Reduction: p(a,c) & ~n(c,c) Reduction: q(a,c) & ~n(c,c) Negations (Negation as Failure) Try to prove the embedded sentence If succeed, negation is false, i.e. no reduction. If fail, reduction is the empty conjunction.

16 16 Example Data: Rules: m(a,b) p(a,c) :- m(a,c) n(b,b) p(a,c) :- m(a,b) & n(b,c) n(b,c) q(c) :- p(a,c) & ~n(c,c) Call: q(c) Call: p(a,c) & ~n(c,c) Call: m(a,c) & ~n(c,c) Fail Call: m(a,b) & n(b,c) & ~n(c,c) Call: n(b,c) & ~n(c,c) Call: ~n(c,c) Call: n(c,c) Fail Exit …

17 17 General Reduction

18 18 Example Data: m(a,b) n(b,b) n(b,c) Rules: p(X,Z) :- m(X,Y) & n(Y,Z) q(Z) :- p(X,Z) & ~n(Z,Z) Question: Is q(c) true?

19 19 General Reduction Goal: p(a,Y) & q(Y,Z) Rule: p(X,b) :- m(X) & n(X) Reduction: m(a) & n(a) & q(b,Z)

20 20 Substititions A substitution is a finite set of pairs of variables and terms, called replacements. {X  a, Y  f(b), V  W} The result of applying a substitution  to an expression  is the expression  obtained from  by replacing every occurrence of every variable in the substitution by its replacement. p(X,X,Y,Z){X  a,Y  f(b),V  W}=p(a,a,f(b),Z)

21 21 Unification A substitution  is a unifier for an expression  and an expression  if and only if  = . p(X,Y) {X  a,Y  b,V  b} = p(a,b) p(a,V) {X  a,Y  b,V  b} = p(a,b) If two expressions have a unifier, they are said to be unifiable. Otherwise, they are nonunifiable. p(X,X) p(a,b)

22 22 Non-Uniqueness of Unification Unifier 1: p(X,Y){X  a,Y  b,V  b} = p(a,b) p(a,V){X  a,Y  b,V  b} = p(a,b) Unifier 2: p(X,Y){X  a,Y  f(W),V  f(W)} = p(a,f(W)) p(a,V){X  a,Y  f(W),V  f(W)} = p(a,f(W)) Unifier 3: p(X,Y){X  a,Y  V} = p(a,V) p(a,V){X  a,Y  V} = p(a,V)

23 23 Most General Unifier A substitution  is a most general unifier (mgu) of two expressions if and only if it is as general as or more general than any other unifier. Theorem: If two expressions are unifiable, then they have an mgu that is unique up to variable permutation. p(X,Y){X  a,Y  V} = p(a,V) p(a,V){X  a,Y  V} = p(a,V) p(X,Y){X  a,V  Y} = p(a,Y) p(a,V){X  a,V  Y} = p(a,Y)

24 24 Example Call: mgu( p(X,b), p(a,Y), {} ) Call: mgu( p, p, {} ) Exit: {} Call: mgu( X, a, {} ) Exit: {X  a} Call: mgu( b, Y, {X  a} ) Exit: {Y  b,X  a}

25 25 Example Call: mgu( p(X,X), p(a,b), {} ) Call: mgu( p, p, {} ) Exit: {} Call: mgu( X, a, {} ) Exit: {X  a} Call: mgu( X, b, {X  a} ) Call: mgu( a, b, {X  a} ) Exit: nil

26 26 Example Call: mgu( p(f(X),f(X)), p(Y,f(a)), {} ) Call: mgu( p, p, {} ) Exit: {} Call: mgu( f(X), Y, {} ) Exit: {Y  f(X)} Call: mgu( f(X), f(a), {Y  f(X)} ) Call: mgu( f, f, {Y  f(X)} ) Exit: {Y  f(X)} Call: mgu( X, a, {Y  f(X)} ) Exit: {X  a,Y  f(X)}

27 27 Example Call: mgu( p(X,X), p(Y,f(Y)), {} ) Call: mgu( p, p, {} ) Exit: {} Call: mgu( X, Y, {} ) Exit: {X  Y} Call: mgu( X, f(Y), {X  Y} ) Call: mgu( Y, f(Y), {X  Y} ) Exit: nil

28 28 General Reduction Example Data:Rules: m(a,b) p(X,Z) :- m(X,Y) & n(Y,Z) n(b,c) q(V) :- p(U,V) n(b,d) Call: q(c) {} Call: p(U,c) {V  c} Call: m(U,Y) & n(Y,c){V  c,X  U,Z  c} Call: n(b,c) {V  c,X  U,Z  c,U  a,Y  b} Exit: n(b,c) {V  c,X  U,Z  c,U  a,Y  b} Exit: m(a,b) & n(b,c){V  c,X  U,Z  c,U  a,Y  b} Exit: p(a,c) {V  c,X  U,Z  c,U  a,Y  b} Exit: q(c) {V  c,X  U,Z  c,U  a,Y  b}

29 29 Answer Extraction

30 30 Example Data: m(a,b) n(b,b) n(b,c) Rules: p(X,Z) :- m(X,Y) & n(Y,Z) q(Z) :- p(X,Z) & ~n(Z,Z) Question: Find all X such that q(X) true. (GGP version: Find all P such that next(P) true.)

31 31 Answer Extraction Set up the target pattern and a list of answers as global variables. Pattern: X Answers: {c, d} Each time the goal is proved with a different variable binding, compute a new answer by substituting the current binding list into the target pattern and then add the result to the answers list.

32 32 Example Data:Rules: m(a,b) p(X,Z) :- m(X,Y) & n(Y,Z) n(b,c) q(V) :- p(U,V) n(b,d) Call: q(W) {} Call: p(U,W) {V  W} Call: m(U,Y) & n(Y,W){V  W,X  U,Z  W} Call: n(b,W) {V  W,X  U,Z  W,U  a,Y  b} Exit: n(b,c) {V  W,X  U,Z  W,U  a,Y  b,W  c} c Redo: n(b,W) {V  W,X  U,Z  W,U  a,Y  b} Exit: n(b,W) {V  W,X  U,Z  W,U  a,Y  b,W  d} d Redo: n(b,W) Fail: n(b,W) Fail: m(U,Y) & n(Y,W) Fail: p(U,W) Fail: q(W)

33 33 More Tedious Details

34 34 Note Initial State Version 1: init(cell(1,1,b)) init(cell(1,2,b)) init(cell(1,3,b)) init(cell(2,1,b)) init(cell(2,2,b)) init(cell(2,3,b)) init(cell(3,1,b)) init(cell(3,2,b)) init(cell(3,3,b)) Initial State Version 2: init(cell(X,Y,b)) :- d(X) & d(Y) d(1) d(2) d(3)

35 35 Note This is the top-down method of deduction. There is also a bottom-up method that works from data to deduce answers “level by level”. Top-down is goal-oriented, does not expend energy on deductions unrelated to the goal. It has two serious disadvantages: redundant computation and the possibility of infinite loops. Bottom-up fixes these two problems but can waste energy on deducing things unrelated to the goal. Magic sets fixes this but it is very complicated.

36 36 Unification

37 37 Most General Unification function mgu (x, y, al) {if x = y then al else if varp(x) then mguvar(x, y, al) else if atom(x) then {if varp(y) then mguvar(y, x, al) else if atom(y) then if x = y} else if varp(y) then mguvar(y, x, al) else if atom(y) then nil else if al  mgu(car(x),car(y), al) then mgu(cdr(x), cdr(y),al)}

38 38 Most General Unification (concluded) function mguindvar (x, y, al) {var dum; if dum  assoc(x, al) then mgu(right(dum), y, al) else if cons(x  y, al)}

39 39 Problem ~hates(X,X) hates(Y,f(Y))

40 40 Solution Before assigning a variable to an expression, first check that the variable does not occur within that expression. This is called, oddly enough, the occur check test. Prolog does not do the occur check (and is proud of it).

41 41 Most General Unification (continued) function mguindvar (x, y, al) {var dum; if dum  assoc(x, al) then mgu(right(dum), y, al) else if mguchkp(x, y, al) then nil else if cons(x  y, al)} function mguchkp (p, q, al) {if p=q then true else if varp(p) then mguchkp(p,right(assoc(q,al)),al) else if atom(q) then false else some(lambda(x).mguchkp(p,x,al),q)}

42 42 Evaluation

43 43 Evaluation Subroutines Database: Call: theory Exit: {m(a,b),n(b,c),n(b,d)} Subroutines: Call: truep( m(X,Y)&n(Y,Z), theory) Exit: true Call: truex( Z, m(X,Y)&n(Y,Z), theory) Exit: c Call: trues( Z, m(X,Y)&n(Y,Z), theory) Exit: {c,d}

44 44 Evaluation var thing var theory var answers function trues (thing, p, theory) {var answers  nil; eval(p, nil, {} ); reverse(remove-duplicates(answers))} function eval(p, pl, al) {if atom(p) then evalrs(p, pl, al) else if car(p) = not then evalnot(p, pl, al) else if car(p) = and then eval(cadr(p),append(cddr(p), pl), al) else evalrs(p, pl, al)}

45 45 Evaluation (continued) function evalnot (p, pl, al) {if not eval(cadr(p), nil, al) then evalexit(pl, al)} function evalrs (p, pl, al) {var fact; var bl; for x in theory do if bl  mguexp(p, x, al) then evalexit(pl, bl)} function evalexit (pl, al) {if pl then eval(car(pl), cdr(pl), al) else answers  cons(plug(thing, al), answers)}

46 46 Example Call: trues( Z, m(X,Y)&n(Y,Z), {m(a,b),n(b,c),n(b,d)} ) Call: eval( m(X,Y)&n(Y,Z), nil, {} ) … Exit: nil answers = {d,c} Call: remove-duplicates( {d,c} ) Exit: {d,c} Call: nreverse( {d,c} ) Exit: {c,d}

47 47 Example (continued) Call: eval( m(X,Y)&n(Y,Z), nil, {} ) Call: evaland( m(X,Y)&n(Y,Z), nil, {} ) Call: eval( m(X,Y), [n(Y,Z)], {} ) … Exit: nil answers = {d,c}

48 48 Example (continued) Call: eval( m(X,Y), [n(Y,Z)], {} ) Call: evalrs( m(X,Y), [n(Y,Z)], {} ) Call: mguexp( m(X,Y), m(a,b), {} ) Exit: {Y  b,X  a} Call: evalexit( [n(Y,Z)], {Y  b,X  a} ) … Exit: nil answers = {d,c} Call: mguexp( m(X,Y), n(b,c), {} ) Exit: nil Call: mguexp( m(X,Y), n(b,d), {} ) Exit: nil Exit: nil answers = {d,c}

49 49 Example (continued) Call: eval( n(Y,Z), nil, {Y  b,X  a} ) Call: evalrs( n(Y,Z), nil, {Y  b,X  a} ) Call: mguexp( n(Y,Z), m(a,b), {Y  b,X  a} ) Exit: nil Call: mguexp( n(Y,Z), n(b,c), {Y  b,X  a} ) Exit: {Z  c,Y  b,X  a} Call: evalexit( nil, {Z  c,Y  b,X  a} ) Exit: nil answers = {c} Call: mguexp( n(Y,Z), n(b,d), {Y  b,X  a} ) Exit: {Z  d,Y  b,X  a} Call: evalexit( nil, {Z  d,Y  b,X  a} ) Exit: nil answers = {d,c}

50 50 Deduction

51 51 Reasoning Subroutines Database: Call: theory Exit: {m(a,b), m(b,c), p(W) <= m(W,X)} Subroutines: Call: findp( p(X), theory) Exit: true Call: findx( W, p(X), theory) Exit: a Call: finds( W, p(X), theory) Exit: {a,b}

52 52 Backward Chaining var thing var theory var answers function finds (thing, p, theory) {var answers  nil; chain(p, nil, {} ); nreverse(remove-duplicates(answers))} function chain(p, pl, al) {if atom(p) then chainrs(p, pl, al) else if car(p) = not then chainnot(p, pl, al) else if car(p) = and then chain(cadr(p),append(cddr(p), pl), al) else chainrs(p, pl, al)}

53 53 Backward Chaining (continued) function chainnot (p, pl, al) {if not chain(cadr(p), nil, al) then chainexit(pl, al)} function chainrs (p, pl, al) {var rule; var bl; for x in theory do {rule  stdize(x) if bl  mguexp(p, rule, al) then chainexit(pl, bl) else if bl  mguexp(p, cadr(rule), al) then chain(caddr(rule), append (cdddr(rule),pl),bl)} function chainexit (pl, al) {if pl then chain(car(pl), cdr(pl), al) else answers  cons(plug(thing, al), answers)}

54 54 Example Call: trues( W, p(W), {m(a,b),m(b,c),p(W)<=m(W,X)} ) Call: chain( p(W), nil, {} ) … Exit: nil answers = {b,a} Call: remove-duplicates( {b,a} ) Exit: {b,a} Call: nreverse( {b,a} ) Exit: {a,b}

55 55 Example (continued) Call: chain( p(X), nil, {} ) Call: chainrs( p(X), nil, {} ) Call: mguexp( p(X), m(a,b), {} ) Exit: nil Call: mguexp( p(X), p(WW), {} ) Exit: {X  W} Call: chain( m(WW,XX), {X  WW} ) Call: mguexp( m(WW,XX), m(a,b), {X  WW} ) Exit: {WW  a,XX  b,X  WW} Call: mguexp( m(WW,XX), m(a,b), {X  WW} ) Exit: {WW  b,XX  c,X  WW} Exit: nil answers = {b,a}

56 56


Download ppt "Computational Logic General Game PlayingLecture 4 Michael Genesereth / Nat Love Spring 2006."

Similar presentations


Ads by Google