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

56
Computational Logic General Game Playing Lecture 4 Michael Genesereth / Nat Love Spring 2006
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    226
  • download

    1

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

Computational Logic

General Game Playing Lecture 4

Michael Genesereth / Nat Love Spring 2006

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

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

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

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

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

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

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

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

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

Ground Reduction

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

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

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

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

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

General Reduction

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

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

Substititions

A substitution is a finite set of pairs of variables and terms, called replacements.

{Xa, Yf(b), VW}

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){Xa,Yf(b),VW}=p(a,a,f(b),Z)

21

Unification

A substitution is a unifier for an expression and an expression if and only if =.

p(X,Y) {Xa,Yb,Vb} = p(a,b)p(a,V) {Xa,Yb,Vb} = 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

Non-Uniqueness of Unification

Unifier 1:p(X,Y){Xa,Yb,Vb} = p(a,b)p(a,V){Xa,Yb,Vb} = p(a,b)

Unifier 2:p(X,Y){Xa,Yf(W),Vf(W)} = p(a,f(W))p(a,V){Xa,Yf(W),Vf(W)} = p(a,f(W))

Unifier 3:p(X,Y){Xa,YV} = p(a,V)p(a,V){Xa,YV} = p(a,V)

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){Xa,YV} = p(a,V)p(a,V){Xa,YV} = p(a,V)

p(X,Y){Xa,VY} = p(a,Y)p(a,V){Xa,VY} = p(a,Y)

24

Example

Call: mgu( p(X,b), p(a,Y), {})

Call: mgu( p, p, {}) Exit: {}

Call: mgu( X, a, {}) Exit: {Xa}

Call: mgu( b, Y, {Xa}) Exit: {Yb,Xa}

Exit: {Yb,Xa}

25

Example

Call: mgu( p(X,X), p(a,b), {})

Call: mgu( p, p, {}) Exit: {}

Call: mgu( X, a, {}) Exit: {Xa}

Call: mgu( X, b, {Xa}) Call: mgu( a, b, {Xa}) Exit: nil Exit: nil

Exit: nil

26

Example

Call: mgu( p(f(X),f(X)), p(Y,f(a)), {})

Call: mgu( p, p, {}) Exit: {}

Call: mgu( f(X), Y, {}) Exit: {Yf(X)}

Call: mgu( f(X), f(a), {Yf(X)}) Call: mgu( f, f, {Yf(X)}) Exit: {Yf(X)} Call: mgu( X, a, {Yf(X)}) Exit: {Xa,Yf(X)} Exit: {Xa,Yf(X)}

Exit: {Xa,Yf(X)}

27

Example

Call: mgu( p(X,X), p(Y,f(Y)), {})

Call: mgu( p, p, {}) Exit: {}

Call: mgu( X, Y, {}) Exit: {XY}

Call: mgu( X, f(Y), {XY}) Call: mgu( Y, f(Y), {XY}) Exit: nil Exit: nil

Exit: nil

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) {Vc} Call: m(U,Y) & n(Y,c){Vc,XU,Zc} Call: n(b,c) {Vc,XU,Zc,Ua,Yb}

Exit: n(b,c) {Vc,XU,Zc,Ua,Yb} Exit: m(a,b) & n(b,c){Vc,XU,Zc,Ua,Yb} Exit: p(a,c) {Vc,XU,Zc,Ua,Yb}Exit: q(c) {Vc,XU,Zc,Ua,Yb}

29

Answer Extraction

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

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

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) {VW} Call: m(U,Y) & n(Y,W){VW,XU,ZW} Call: n(b,W) {VW,XU,ZW,Ua,Yb}

Exit: n(b,c) {VW,XU,ZW,Ua,Yb,Wc} c Redo: n(b,W) {VW,XU,ZW,Ua,Yb} Exit: n(b,W) {VW,XU,ZW,Ua,Yb,Wd} d

Redo: n(b,W)Fail: n(b,W)

Fail: m(U,Y) & n(Y,W) Fail: p(U,W)Fail: q(W)

33

More Tedious Details

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

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

Unification

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 almgu(car(x),car(y), al) then mgu(cdr(x), cdr(y),al)}

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

Problem

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

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

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

Evaluation

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

Evaluation

var thingvar theoryvar 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

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

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}Exit: {c,d}

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} Exit: nil answers = {d,c}Exit: nil answers = {d,c}

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: {Yb,Xa} Call: evalexit([n(Y,Z)], {Yb,Xa}) … 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}Exit: nil answers = {d,c}

49

Example (continued)

Call: eval(n(Y,Z), nil, {Yb,Xa}) Call: evalrs(n(Y,Z), nil, {Yb,Xa}) Call: mguexp(n(Y,Z),m(a,b),{Yb,Xa}) Exit: nil Call: mguexp(n(Y,Z),n(b,c),{Yb,Xa}) Exit: {Zc,Yb,Xa} Call: evalexit(nil,{Zc,Yb,Xa}) Exit: nil answers = {c} Call: mguexp(n(Y,Z),n(b,d),{Yb,Xa}) Exit: {Zd,Yb,Xa} Call: evalexit(nil,{Zd,Yb,Xa}) Exit: nil answers = {d,c} Exit: nil answers = {d,c} Exit: nil answers = {d,c}

50

Deduction

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

Backward Chaining

var thingvar theoryvar 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

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

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}Exit: {a,b}

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: {XW} Call: chain(m(WW,XX), {XWW}) Call: mguexp(m(WW,XX), m(a,b), {XWW}) Exit: {WWa,XXb,XWW} Call: mguexp(m(WW,XX), m(a,b), {XWW}) Exit: {WWb,XXc,XWW} Exit: nil answers = {b,a} Exit: nil answers = {b,a}Exit: nil answers = {b,a}

56