Lecture 22

advertisement
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?
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
1
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
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
2
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
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
3
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
4
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
5
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"
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
6
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
7
Using First-Order Logic

When query has a variable, would like to know
what values make query sentence true. E.g.


Response is a substitution or binding list


AskVars(KB, Person(x))
{ 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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
8
WW Examples

Percept is a vector of 5 elements and a time.
E.g., a typical sentence would be


Actions can be represented by logical terms


Percept([Stench,Breeze,Glitter,None,None],5)
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 }
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
9
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)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
10
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
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
11
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,s2,t At(x,s1,t)  At(x,s2,t)  s1=s2
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
12
WW Examples

Infer properties of square from properties of
current percept:


Need only one axiom for each property of
squares. E.g.,


 s,t At(Agent,s,t)  Breeze(t)  Breezy(s)
 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))
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
13
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
14
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(C1)  OnHead(C1, John)

C1 is called a Skolem constant.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
15
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
16
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)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
17
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 }.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
18
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 }
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
19
FOL Inference Rule


This inference process is captured by the
Generalized Modus Ponens rule.
For atomic sentences pi, pi', and q, where there
is a substitution  such that
Subst(,pi') = Subst(,pi), for all i
p1', p2', … , pn', (p1  p2 … pn  q)
Subst(,q)

Key advantage is that this rule makes only
those substitutions that are required to proceed.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
20
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}
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
21
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
22
Unification

Without loss of meaning, rename the second x
to x17 (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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
23
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).
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
24
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
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
25
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, M1) and (3) Missile(M1)

"All of its missiles were sold to it by Colonel West"
(4) Missile(x)  Owns(Nono,x)  Sells (West,x,Nono)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
26
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)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
27
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/M1 }, adding
Sells(West,M1,Nono)
(5) is satisfied with { x/M1 }, adding Weapon(M1)
(6) is satisfied with { x/Nono }, adding Hostile(Nono)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
28
Forward Chaining

On the second iteration, (3) is satisfied with
{ x/West, y/M1, z/Nono }, and Criminal(West) is
added. The proof tree shows that no new
inferences are possible.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
29
Backward Chaining

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
substitutions
as shown in
the proof
tree.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
30
Prolog



PROgramming LOGic language, uses firstorder 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
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
31
Basic Syntax - Terms





Atoms: symbols, single-quoted sequence, builtin 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 '('
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
32
Basic Syntax - Built-Ins


Operators
:-
left implication ()
=
unification
,
conjunction
\=
non-unification
;
disjunction
is
expression
evaluation &
assignment
Predicates
exit the interpreter

halt

write display argument

nl
Thursday, March 29
display newline
CS 430 Artificial Intelligence - Lecture 22
33
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)
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
34
Knowledge Bases

KB is a set of sentences.

<head> :- <term1>, <term2>, ..., <termn>.



May consist only of a head.


means <term1> ∧ <term2> ∧...∧ <termn>  <head>
sibling(X, Y) :- parent(P, X), parent(P, Y), X \= Y.
vertical (line(point(X,Y)), line(point(X,Z))).
If no variables, sentence is called a fact


female(mary).
parent(mary, george).
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
35
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.
Thursday, March 29
CS 430 Artificial Intelligence - Lecture 22
36
Download