CMSC 723: Introduction to Computational Linguistics

Download Report

Transcript CMSC 723: Introduction to Computational Linguistics

Notes adapted from lecture notes for CMSC 421 by B.J. Dorr
logic agents
“Thinking Rationally”
• Computational models of human “thought”
processes
• Computational models of human behavior
• Computational systems that “think” rationally
• Computational systems that behave rationally
2
Logical Agents
• Reflex agents find their way from Arad to
Bucharest by dumb luck
• Chess program calculates legal moves of its
king, but doesn’t know that no piece can be
on 2 different squares at the same time
• Logic (Knowledge-Based) agents combine
general knowledge with current percepts to
infer hidden aspects of current state prior to
selecting actions
– Crucial in partially observable environments
3
Outline
•
•
•
•
Knowledge-based agents
Wumpus world
Logic in general
Propositional and first-order logic
– Inference, validity, equivalence and satifiability
– Reasoning patterns
• Resolution
• Forward/backward chaining
4
Knowledge Base
Knowledge Base : set of sentences represented in a
knowledge representation language and represents
assertions about the world.
tell
ask
Inference rule: when one ASKs questions of the KB, the
answer should follow from what has been TELLed to the
KB previously.
5
Generic KB-Based Agent
6
Generic KB-Based Agent
• Takes percent as input and returns an action
• agent maintains a knowledge base KB
• Each time agent program is called it does
– TELLs knowledge base what It perceives
– ASKs knowledge base what action it should
perform
– Agent records its choice with TELL an executes
action
7
Abilities KB agent
• Agent must be able to:
– Represent states and actions,
– Incorporate new percepts
– Update internal representation of the world
– Deduce hidden properties of the world
– Deduce appropriate actions
8
Desription level
• The KB agent is similar to agents with internal
state
• Agents can be described at different levels
– Knowledge level
• What they know, regardless of the actual
implementation. (Declarative description)
– Implementation level
• Data structures in KB and algorithms that manipulate
them e.g propositional logic and resolution.
9
A Typical Wumpus World
Wumpus
10
A Typical Wumpus World
• Cave consisting of rooms connected by
passageways
• Agent has only one arrow
• Some of rooms contain bottomless pit that will
trap anyone who wanders into these rooms –
except for wumpus
11
Wumpus World PEAS Description
12
Wumpus World Characterization
•
•
•
•
•
•
Observable?
Deterministic?
Episodic?
Static?
Discrete?
Single-agent?
13
Wumpus World Characterization
•
•
•
•
•
•
Observable? No, only local perception
Deterministic?
Episodic?
Static?
Discrete?
Single-agent?
14
Wumpus World Characterization
•
•
•
•
•
•
Observable? No, only local perception
Deterministic? Yes, outcome exactly specified
Episodic?
Static?
Discrete?
Single-agent?
15
Wumpus World Characterization
• Observable? No, only local perception
• Deterministic? Yes, outcome exactly specified
• Episodic? No, sequential at the level of
actions
• Static?
• Discrete?
• Single-agent?
16
Wumpus World Characterization
• Observable? No, only local perception
• Deterministic? Yes, outcome exactly specified
• Episodic? No, sequential at the level of
actions
• Static? Yes, Wumpus and pits do not move
• Discrete?
• Single-agent?
17
Wumpus World Characterization
• Observable? No, only local perception
• Deterministic? Yes, outcome exactly specified
• Episodic? No, sequential at the level of
actions
• Static? Yes, Wumpus and pits do not move
• Discrete? Yes
• Single-agent?
18
Wumpus World Characterization
• Observable? No, only local perception
• Deterministic? Yes, outcome exactly specified
• Episodic? No, sequential at the level of
actions
• Static? Yes, Wumpus and pits do not move
• Discrete? Yes
• Single-agent? Yes, Wumpus is essentially a
natural feature.
19
Exploring the Wumpus World
[1,1] The KB initially contains the rules of the environment. The first percept is [none,
none,none,none,none], move to safe cell e.g. 2,1
[none, none,none,none,none] – no stench, no breeze, no glitter, no wall in front,
no scream
[2,1] breeze which indicates that there is a pit in [2,2] or [3,1], return to [1,1] to try next
safe cell
20
Exploring the Wumpus World
[1,2] Stench in cell which means that wumpus is in [1,3] or [2,2]
YET … not in [1,1]
YET … not in [2,2] or stench would have been detected in [2,1]
THUS … wumpus is in [1,3]
THUS [2,2] is safe because of lack of breeze in [1,2]
THUS pit in [1,3]
move to next safe cell [2,2]
21
Exploring the Wumpus World
[2,2] move to [2,3]
[2,3] detect glitter , smell, breeze
THUS pick up gold
THUS pit in [3,3] or [2,4]
22
What is a logic?
• A formal language
– Syntax – what expressions are legal (well-formed)
– Semantics – what legal expressions mean
• in logic the truth of each sentence with respect to each
possible world.
• E.g the language of arithmetic
– X+2 >= y is a sentence, x2+y is not a sentence
– X+2 >= y is true in a world where x=7 and y =1
– X+2 >= y is false in a world where x=0 and y =6
23
Entailment
• One thing follows from another
KB | = 
• KB entails sentence  if and only if  is
true in worlds where KB is true.
 E.g. x+y=4 entails 4=x+y
• Entailment is a relationship between
sentences that is based on semantics.
24
Models
• Logicians typically think in terms of models,
which are formally structured worlds with
respect to which truth can be evaluated.
• m is a model of a sentence  if  is true in
m
• M() is the set of all models of 
25
Wumpus world model
26
Wumpus world model
27
Wumpus world model
28
Wumpus world model
29
Wumpus world model
30
Wumpus world model
31
Logical inference
• The notion of entailment can be used for logic
inference.
– Model checking (see wumpus example): enumerate all
possible models and check whether  is true.
• If an inference algorithm i can derive  from KB
– KB |-i 
– Sentence  is derived from KB by i or i derives  from KB
• Soundness: i is sound if whenever KB |-i , it is also
true that KB |= 
• Completeness : i is complete if whenever KB |=  it
is also true that KB|-i 
32
Schematic perspective
If KB is true in the real world, then any sentence  derived
From KB by a sound inference procedure is also true in the
real world.
33
Logical inference
• Preview: we will define a logic (first-order
logic) which is expressive enough to say
almost anything of interest, and for which
there exists a sound and complete inference
procedure.
• That is, the procedure will answer any
question whose answer follows from what is
known by the KB.
34
Propositional logic: Syntax
• Propositional logic is the simplest logic - illustrates
basic ideas
• The proposition symbols P1, P2 etc are sentences
• If S is a sentence, S is a sentence (negation)
• If S1 and S2 are sentences, S1  S2 is a sentence
(conjunction)
• If S1 and S2 are sentences, S1  S2 is a sentence
(disjunction)
• If S1 and S2 are sentences, S1  S2 is a sentence
(implication)
• If S1 and S2 are sentences, S1  S2 is a sentence
(biconditional)
35
Truth tables for connectives
P
Q
false false
false true
true false
true true
P
PQ
PQ
PQ
PQ
true
true
false
false
false
false
false
true
false
true
true
true
true
true
false
true
true
false
false
true
36
Propositional logic: Semantics
• Each model species true/false for each proposition symbol
• E.g. P1,2 P2,2 P3,1
true true false
• (With these symbols, 8 possible models, can be enumerated
automatically.)
• Rules for evaluating truth with respect to a model m:
•  S is true iff S is false
• S1  S2 is true iff S1 is true and S2 is true
• S1  S2 is true iff S1 is true or S2 is true
• S1  S2 is true iff S1 is false or S2 is true
• i.e., is false iff S1 is true and S2 is false
• S1  S2 is true iff  S2 is true and S2  S1 is true
• Simple recursive process evaluates an arbitrary sentence,
e.g.,
•  P1,2  (P2,2  P3,1) = true  (false  true)=true  true=true
37
Wumpus world sentences
• Let Pi,j be true if there is a pit in [i, j].
• Let Bi,j be true if there is a breeze in [i, j].
 P1,1
 B1,1
B2,1
• “Pits cause breezes in adjacent squares"
B1,1  (P1,2  P2,1)
B2,1  (P1,1  P2,2  P3,1)
“A square is breezy if and only if there is an adjacent pit"
38
Truth tables for inference
39
Logical equivalence
40
Validity and satisability
41
Inference Rules
Modus ponens
, 

And elimination


Logical equivalences on pg 40 apply
Eg biconditional elimination yields 2 inference rules

and
()  ()
()  ()

42
Inference Rules
• All inferences may not apply in the opposite
direction
43
Inference Rules
• Wumpus world KB
There is no pit in [1,1] etc
R1:  P1,1
R2: B1,1  (P1,2  P2,1)
R3: B2,1  (P1,1  P2,2  P3,1)
R4:  B1,1
R5: B2,1
44
Inference Rules
• Show there is no pit in [1,2]
• biconditional elimination to R2
R6: (B1,1(P1,2  P2,1))  ((P1,2  P2,1 )  B1,1)
And elimination to R6
R7: ((P1,2  P2,1 )  B1,1)
logical equivalence for contrapositives
R8: ( B1,1 (P1,2  P2,1))
Apply modus ponens with R8 with precept R4
R9: (P1,2  P2,1))
De Morgan
R10: P1,2   P2,1)
Neither [1,2] nor [2,1] contains a pit
45
Searching for Proofs
• Finding proofs is exactly like finding solutions to
search problems.
• Can search forward (forward chaining) to
derive goal or search backward (backward
chaining) from the goal.
• Searching for proofs is not more efficient than
enumerating models, but in many practical
cases, it’s more efficient because we can
ignore irrelevant propositions
46
Resolution
• So far – soundness of inference algorithm
– soundness – truth preserving – inference
algorithm derives only entailed sentences
– Unsound – makes things up as it goes along
• Completeness of inference algorithms?
– Completeness – algorithm can derive any
sentence that is entailed
47
Resolution
• Wumpus world
– agent returns from [2,1] to [1,1], then goes
to [1,2] – stench, but no breeze
– Add facts to knowledge base
• R11: B1,2
• R12: B1,2  (P1,1  P2,2  P1,3)
– Same process that led to R10 – we can
derive
• R13: P2,2
• R14: P1,3
48
Resolution
– Biconditional elimination to R3, …
• R15: P1,1  P2,2  P3,1
– Resolution
• P2,2 in R13 resolves with P2,2 in R15 to give
R16: P1,1  P3,1
In other words, if there Is a pit in one of
[1,1],[2,2],[3,1], and it is not in [2,2] then it is in
[1,1] or [3,1]
• Similarly, P1,1 in R1 resolves with P1,1 in R16 to
give
R17: P3,1
In other words, if there Is a pit of [1,1] or [3,1],
and it is not in [1,1] then it is in [3,1]
49
Resolution
• We used the unit resolution inference rule
above
li and m are complimentary literals (one is the
negative of the other)
• A Full Resolution Rule is a generalization of
this rule
50
Resolution
• For clauses of length two:
51
Conjunctive normal form
• Conjunctive Normal Form is a disjunction of
literals
• Example:
literals
(A 
B   C)  (B  D)  ( A)  (B  C)
clause - Disjunction of literals
52
Conjunctive normal form
• Convert R2 (B1,1  (P1,2  P2,1) into CNF
• Steps
– Eliminate , replacing    with ()()
(B1,1  (P1,2  P2,1 ))  ((P1,2  P2,1 )) B1,1)
– Eliminate , replacing    with 
(B1,1  P1,2  P2,1)  ((P1,2  P2,1 ))  B1,1)
–  must appear only in literals
(B1,1  P1,2  P2,1)  ((P1,2   P2,1 )  B1,1)
– Distribute
(B1,1P1,2 P2,1)  (P1,2  B1,1)  (  P2,1B1,1)
• Conjunction of three clauses – CNF form
53
Resolution Algorithm
• To show KB |= , we show (KB   ) is
unsatisfiable.
• This is a proof by contradiction.
• First convert (KB   ) into CNF.
• Then apply resolution rule to resulting clauses.
• The process continues until:
– there are no new clauses that can be added
(KB does not entail )
– two clauses resolve to yield empty clause
(KB entails )
54
Simple Inference in Wumpus World
• Agent is in [1,1]
– KB = R2  R4 = (B1,1  (P1,2  P2,1))   B1,1
– Wish to prove  which is, say,  P1,2
– Convert KB   P1,2 to CNF
55
Horn Clauses
• A disjunction of literals where at most one is
positive
( L1,1  Breeze  B1,1) is a horn clause
( B1,1  P1,2  P2,1)) is not a horn clause
• every Horn clause can be written as an
implication whose premise is a conjunction of
positive literals and whose conclusion is a
single positive literal
( L1,1  Breeze  B1,1) can be written as
(L1,1  Breeze)  B1,1
( W1,1   W1,2 ) can be written as
( W1,1  W1,2)  False
56
Horn Clauses
• Inference with Horn clauses can be done
through the forward chaining and backward
chaining algorithms
57
Forward Chaining
• Fire any rule whose premises are satisfied in
the KB.
• Add its conclusion to the KB until query is
found.
58
Forward Chaining Example
PQ
LMP
BLM
APL
ABL
A
B
59
Forward Chaining Example
60
Forward Chaining Example
61
Forward Chaining Example
62
Forward Chaining Example
63
Forward Chaining Example
64
Forward Chaining Example
65
Forward Chaining Example
66
Backward Chaining
• Motivation: Need goal-directed reasoning in
order to keep from getting overwhelmed with
irrelevant consequences
• Main idea:
– Work backwards from query q
– To prove q:
• Check if q is known already
• Prove by backward chaining all premises of some rule
concluding q
67
Backward Chaining Example
PQ
LMP
BLM
APL
ABL
A
B
68
Backward Chaining Example
69
Backward Chaining Example
70
Backward Chaining Example
71
Backward Chaining Example
72
Backward Chaining Example
73
Backward Chaining Example
74
Backward Chaining Example
75
Backward Chaining Example
76
Backward Chaining Example
77
Backward Chaining Example
78
Forward Chaining vs. Backward
Chaining
• FC is data-driven—it may do lots of work
irrelevant to the goal
• BC is goal-driven—appropriate for problemsolving
79