CS 430 Lecture 22 - University of...

36
Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 1 Lecture 22 Reminder: Homework 4 due next Thursday Results of Dots tournament posted Example Prolog code is on csserver in /home/hwang/cs430/prolog/ Questions?

Transcript of CS 430 Lecture 22 - University of...

Page 1: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 1

Lecture 22

Reminder: Homework 4 due next Thursday Results of Dots tournament posted Example Prolog code is on csserver in

/home/hwang/cs430/prolog/ Questions?

Page 2: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 2

Outline

Chapter 8 – First-Order Logic Using First-Order Logic

Chapter 9 - Inference in First-Order Logic Propositional vs. First-Order Inference Unification Forward and Backward Chaining

Prolog - gprolog

Page 3: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 3

Review: Symbols

FOL has three kinds of symbols constant symbols representing objects, e.g.,

Richard, John, TheCrown predicate symbols representing relations, e.g.,

Brother, OnHead, Person, King, Crown function symbols representing functions, e.g.,

LeftLeg

By convention, these symbols start with uppercase letter

Page 4: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 4

Review: Terms

A term is a logical expression that refers to an object constant symbol is a term function symbol followed by an "argument" list variable, by convention starting with lowercase

A term with no variables is a ground term.

Page 5: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 5

Review: Sentences

An atomic sentence (or atom for short) is formed from a predicate symbol optionally followed by a list of terms as "arguments" or with the equality predicate symbol.

An atomic sentence is true in a given model if the relation referred to by the predicate symbol holds among the objects referred to by the arguments.

Can construct more complex sentences by using logical connectives.

Page 6: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 6

Review: Quantification - "For all" / - "There exists"

Express properties of entire collections objects using quantifiers. Universal quantification makes statements about every object. E.g., x King(x) Person(x) "If x is a king, x is a

person"

Existential quantification makes a statement about some object in the universe without naming it.

E.g., x Crown(x) OnHead(x, John) "King John has a crown on his head"

Page 7: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 7

Using First-Order Logic

A KB represents a domain, some part of the world about which we wish to express some knowledge.

Sentences, called assertions, are added to a KB using Tell as in propositional logic. Questions, called queries or goals, are made using Ask.

Page 8: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 8

Using First-Order Logic

When query has a variable, would like to know what values make query sentence true. E.g. AskVars(KB, Person(x))

Response is a substitution or binding list { x/John }, { x/Richard }

AskVars is usually reserved for KBs consisting only of Horn clauses (disjunctions of literals of which at most one is positive), since in such KBs, every way of making a query true will bind the variables to specific values.

Page 9: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 9

WW Examples

Percept is a vector of 5 elements and a time. E.g., a typical sentence would be Percept([Stench,Breeze,Glitter,None,None],5)

Actions can be represented by logical terms Turn(Right), Turn(Left), Forward, Shoot, Grab,

Climb

To determine which action is the best: AskVars( a BestAction(a,5))

which returns binding such as { a/Grab }

Page 10: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 10

WW Examples

Raw percept data implies certain facts about the current state. E.g., t,s,g,m,c Percept([s,Breeze,g,m,c],t) Breeze(t)

t,s,b,m,c Percept([s,b,Glitter,m,c],t) Glitter(t)

Note quantification over t. Simple "reflex" behavior:

t Glitter(t) BestAction(Grab,t)

Page 11: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 11

WW Examples

Don't want to name every square, otherwise each adjacency would be an explicit fact. Use a complex term with row and column appearing as integer variables: [r,c]

Adjacency: x,y,a,b Adjacent([x,y],[a,b])

(x=a (y=b+1 y=b-1)) (y=b (x=a-1 x=a+1))

Pits don't need to be named. Use a predicate: Pit([x,y]) is true if square [x,y] has a pit

Page 12: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 12

WW Examples

Only one Wumpus, so just use constant symbol as unary predicate. Similarly for the Agent.

Agent's location changes over time. Define At(Agent,s,t). Wumpus location is fixed using quantification over time: t At(Wumpus,[2,2],t)

Objects can only be in one location at a time:

x,s1,s

2,t At(x,s

1,t) At(x,s

2,t) s

1=s

2

Page 13: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 13

WW Examples

Infer properties of square from properties of current percept: s,t At(Agent,s,t) Breeze(t) Breezy(s)

Need only one axiom for each property of squares. E.g., s Breezy(s) r Adjacent(r,s) Pit(r)

Also only one successor-state axiom per predicate. E.g., t HaveArrow(t+1) (HaveArrow(t) Shoot(t))

Page 14: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 14

Inference Rules for Quantifiers

Universal Instantiation (UI) says we can infer any sentence obtained by substituting a ground term for the variable. Introduce Subst(,) to denote the result of applying the substitution to the sentence .

v Subst({ v/g }, )

for any variable v and ground term g.

Page 15: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 15

Inference Rules for Quantifiers

Existential Instantiation (EI) says replace the variable by a single new constant symbol.

v Subst({ v/k }, )

where constant symbol k does not appear elsewhere in KB. E.g. from sentence x Crown(x) OnHead(x, John), we can infer Crown(C

1) OnHead(C

1, John)

C1 is called a Skolem constant.

Page 16: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 16

Inference Rules for Quantifiers

Note that UI can be applied many times to produce many different consequences, while EI can only be applied once, then the existentially quantified sentence can be discarded.

These can be used to propositionalize an FOL KB. Turns out to be a complete algorithm, any entailed sentence can be proved (answer is yes), but cannot always answer no to every nonentailed sentence. Said to be semidecidable.

Page 17: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 17

FOL Inference Rule

Consider KB with symbols Richard and John, and sentences:

x King(x) Greedy(x) Evil(x)King(John)Greedy(John)

Propositionalization creates sentences like

King(Richard) Greedy(Richard) Evil(Richard)

before it can prove query Evil(John)

Page 18: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 18

FOL Inference Rule

A more direct way to prove Evil(John) would be to say: to use the rule that greedy kings are evil, find some x such that x is a king and x is greedy, then infer that this x is evil.

More generally, if there is some substitution that makes each of the conjuncts of the premise of the implication identical to sentences already in the KB, we can assert the conclusion after applying . E.g. { x/John }.

Page 19: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 19

FOL Inference Rule

This can be applied to quantified sentences as well. E.g. instead of Greedy(John) have

y Greedy(y) – "Everyone is greedy"

Find a substitution for both the variables in the implication and the variables for sentences in the KB: { x/John, y/John }

Page 20: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 20

FOL Inference Rule

This inference process is captured by the Generalized Modus Ponens rule.

For atomic sentences pi, p

i', and q, where there

is a substitution such that Subst(,p

i') = Subst(,p

i), for all i

p1', p

2', … , p

n', (p

1 p

2 … p

n q)

Subst(,q)

Key advantage is that this rule makes only those substitutions that are required to proceed.

Page 21: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 21

Unification

The process of finding substitutions is called unification and is a key component of all FOL inference algorithms.

Unify algorithm takes two sentences and returns a unifier for them if one exists:

Unify (p,q) = where Subst(,p) = Subst(,q)

Some examples. Who does John know?

Unify (Knows(John,x), Knows(John,Jane)) = { x/Jane}Unify (Knows(John,x), Knows(y,Bill)) = { x/Bill, y/John}

Page 22: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 22

Unification

More examples:

Unify (Knows(John,x), Knows(y,Mother(y))) = { y/John, x/Mother(John) }Unify (Knows(John,x), Knows(x,Elizabeth)) = fail

Last one fails because x cannot be both John and Elizabeth. But Knows(x,Elizabeth) means "Everyone knows Elizabeth", so should be able to infer John knows Elizabeth. Need to standardizing apart one of the sentences – rename its variables to avoid name clashes.

Page 23: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 23

Unification

Without loss of meaning, rename the second x to x

17 (a new variable name) to get:

Unify (Knows(John,x), Knows(x17

,Elizabeth))

= { x/Elizabeth, x17

/John }

There can be more than one substitution, so which one to use? Turns out for every unifiable pair of expressions, there is a single most general unifier (MGU) that is unique up to renaming and substitution of variables.

Page 24: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 24

Definite Clauses

As with propositional logic, can restrict KB sentences to definite clauses – an atomic sentence or an implication whose antecedents is a conjunction of positive literals and whose consequence is a single positive literal.

Unlike propositional logic, FOL literals can contain variables, which are assumed to be universally quantified (so the quantifiers are typically omitted in this case).

Page 25: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 25

Definite Clauses

Not every KB can be converted to a set of definite clauses, but many can. Consider the following: Problem: The law says that it is a crime for an

American to sell weapons to hostile nations. The country Nono, an enemy of America, has some missiles, and all of its missiles were sold to it by Colonel West, who is American.

Prove: Colonel West is a criminal

Page 26: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 26

Facts and Rules

"It is a crime for an American to sell weapons to hostile nations"

(1) American(x) Weapon(y) Sells(x,y,z) Hostile(z) Criminal(x)

"Nono...has some missiles"

x Owns(Nono, x) Missile(x) by EI, And-Elim get(2) Owns(Nono, M

1) and (3) Missile(M

1)

"All of its missiles were sold to it by Colonel West"

(4) Missile(x) Owns(Nono,x) Sells (West,x,Nono)

Page 27: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 27

Fact and Rules

Missiles are weapons

(5) Missile(x) Weapon(x) Enemies of America are "hostile"

(6) Enemy(x, America) Hostile(x) "West, who is American"

(7) American(West) "The country Nono, an enemy of America"

(8) Enemy(Nono, America)

Page 28: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 28

Forward Chaining

Recall that forward chaining starts with the known facts and derives the consequences of implications. For the example, for the first iteration:

(1) has unsatisfied premises(4) is satisfied with { x/M

1 }, adding

Sells(West,M1,Nono)

(5) is satisfied with { x/M1 }, adding Weapon(M

1)

(6) is satisfied with { x/Nono }, adding Hostile(Nono)

Page 29: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 29

Forward Chaining

On the second iteration, (3) is satisfied with { x/West, y/M

1, z/Nono }, and Criminal(West) is

added. The proof tree shows that no new inferences are possible.

Page 30: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 30

Backward Chaining

substitutions as shown in the proof tree.

Recall that backward chaining starts with the goal and looks for known facts to support the proof. Unsatisfied premises are new subgoals to prove. The process keeps track of the accumulated

Page 31: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 31

Prolog

PROgramming LOGic language, uses first-order logic back-chaining to drive computations

Will use GNU Prolog (gprolog) for this course, most other Prolog implementations have similar syntax. Links available on course webpage

Identifier syntax convention is opposite FOL Variables start with uppercase letters Constants and predicate names start with

lowercase

Comments: % to end of line or /* */ pairs

Page 32: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 32

Basic Syntax - Terms

Atoms: symbols, single-quoted sequence, built-in operators

Numbers: the usual Variables: the usual, one special "anonymous"

variable underscore ( _ ) Structures: predicates and nested predicates,

names can be overloaded with different arity Terms cannot have space between

predicate and '('

Page 33: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 33

Basic Syntax - Built-Ins

Operators

:- left implication ()

, conjunction

; disjunction

Predicates halt exit the interpreter

write display argument

nl display newline

= unification

\= non-unification

is expression evaluation & assignment

Page 34: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 34

Interpreter

Read-execute-write loop against a KB.

Prompt is: ?­ Given a query sentence, responds "yes" if can

be proved and "no" if it cannot. All interaction is via query sentences, so

"command" to exit interpreter is:

?­ halt.

(note the period at the end of the sentence)

Page 35: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 35

Knowledge Bases

KB is a set of sentences.

<head> :- <term1>, <term

2>, ..., <term

n>.

means <term1> ∧ <term

2> ∧...∧ <term

n> <head>

sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y.

May consist only of a head. vertical (line(point(X,Y)), line(point(X,Z))).

If no variables, sentence is called a fact female(mary). parent(mary, george).

Page 36: CS 430 Lecture 22 - University of Evansvilleuenics.evansville.edu/~hwang/s12-courses/cs430/lecture22-fol-prolog.pdf · Prolog - gprolog. Thursday, March 29 CS 430 Artificial Intelligence

Thursday, March 29 CS 430 Artificial Intelligence - Lecture 22 36

Consulting

Reading in a file of facts and rules is called consulting.

Query to do this: [<filename>]. E.g.,?­ [criminal].

Note: no quotation marks around the file name. Also, will assume extension of ".pl"

To read from keyboard, consult the user with [user]. Ctrl-D indicates end of input.

Note: there are no retained sentences from previous consults.