07 - logical agents - richard j. povinellipovinelli.eece.mu.edu/teaching/eece6820/slides/07 -...
TRANSCRIPT
Page 1rev 1.0, 9/14/2003
Logical Agents
Dr. Richard J. Povinelli
rev 1.0, 9/14/2003 Page 2
ObjectivesYou should
be able to describe the characteristics of a knowledge-based agent including its knowledge base and inference mechanism. be able to employ propositional logic to design a knowledge-based agent.
rev 1.0, 9/14/2003 Page 3
Outline Knowledge based agents Wumpus world Logic in general – models and entailmentPropositional (Boolean) logic Equivalence, validity, satisfiabilityInference rules and theorem proving
forward chainingbackward chainingresolution
AIMA Slides © Stuart Russell and Peter Norvig, 2003
rev 1.0, 9/14/2003 Page 4
Knowledge bases
Knowledge base = set of sentences in a formal languageDeclarative approach to building an agent (or other system):
Tell it what it needs to know
Then it can Ask itself what to do---answers should follow from the KB Agents can be viewed at the knowledge level
i.e., what they know, regardless of how implemented
Or at the implementation leveli.e., data structures in KB and algorithms that manipulate them
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 5
A simple knowledge-based agent
The agent must be able to: Represent states, actions, etc. Incorporate new percepts Update internal representations of the world Deduce hidden properties of the world Deduce appropriate actions
AIMA Slides © Stuart Russell and Peter Norvig, 1998
function KB-Agent(percept) returns an actionstatic: KB, a knowledge base
t, a counter, initially 0, indicating timeTell(KB, Make-Percept-Sentence(percept, t))action ← Ask(KB, Make-Action-Query(t))Tell(KB, Make-Action-Sentence(action, t))t ← t + 1
return action
rev 1.0, 9/14/2003 Page 6
Wumpus World PEAS description Performance measure
gold +1000, death –1000, -1 per step, -10 for using the arrow
Environment Squares adjacent to wumpus are smelly Squares adjacent to pit are breezy Glitter if and only if gold is in the same square Shooting kills the wumpus if you are facing it Shooting uses up the only arrow Grabbing picks up the gold if in the same square Releasing drops the gold in the same square
ActuatorsLeft turn, Right turn, Forward, Grab, Release, Shoot
SensorsBreeze, Glitter, Smell
AIMA Slides © Stuart Russell and Peter Norvig, 2003
rev 1.0, 9/14/2003 Page 7
Wumpus world characterization Observable?? Deterministic?? Episodic?? Static?? Discrete?? Single-agent??
AIMA Slides © Stuart Russell and Peter Norvig, 2003
rev 1.0, 9/14/2003 Page 8
Wumpus world characterization Observable?? No – only local perceptionDeterministic?? Yes – outcomes exactly specifiedEpisodic?? No – sequential at the level of actionsStatic?? Yes – Wumpus and Pits do not moveDiscrete?? YesSingle-agent?? Yes – Wumpus is essentially a natural feature
AIMA Slides © Stuart Russell and Peter Norvig, 2003
rev 1.0, 9/14/2003 Page 9
CAT – Hunt the WumpusLet’s play hunt the wumpus for 10 minutes.Go to the following website
http://www.sunyit.edu/~schirot/Game/game.html
rev 1.0, 9/14/2003 Page 10
Other tight spots Breeze in (1,2) and (2,1)
no safe actions Assuming pits uniformly distributed,
(2,2) is most likely to have a pit
Smell in (1,1) cannot move
Can use a strategy of coercion: shoot straight aheadwumpus was there ⇒ dead ⇒ safe
wumpus wasn't there ⇒ safe
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 11
Logic in general Logics are formal languages for representing information such that conclusions can be drawn Syntax defines the sentences in the language Semantics define the “meaning” of sentences;
i.e., define truth of a sentence in a world Example: the language of arithmetic
x + 2 ≥ y is a sentence; x2 + y > is not a sentence x + 2 ≥ y is true iff the number x + 2 is no less than the number yx + 2 ≥ y is true in a world where x= 7, y = 1 x + 2 ≥ y is false in a world where x= 0, y = 6
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 12
Types of logic Logics are characterized by what they commit to as “primitives”Ontological commitment: what exists
facts? objects? time? beliefs?
Epistemological commitment: what states of knowledge?
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 13
Entailment KB α
Knowledge base KB entails sentence αif and only if (iff)α is true in all worlds where KB is true
Examplethe KB containing “the Giants won” and “the Reds won”entails “Either the Giants won or the Reds won”
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 14
Models Logicians typically think in terms of models, which are formallystructured worlds with respect to which truth can be evaluated We say m is a model of a sentence α if α is true in mM(α) is the set of all models of αThen KB α if and only if M(KB) ⊆ M(α)
Example KB = Giants won and Reds won α = Giants won
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 15
Entailment in the wumpus world
Situation after detecting nothing in [1,1], moving right, breeze in [2,1]Consider possible models for ?s assuming only pits 3 Boolean choices ⇒ 8 possible models
rev 1.0, 9/14/2003 Page 16
Wumpus models
rev 1.0, 9/14/2003 Page 17
Wumpus models
KB = wumpus-world rules + observations
rev 1.0, 9/14/2003 Page 18
Wumpus models
KB = wumpus-world rules + observationsα1 = “[1,2] is safe”, KB α1, proved by model checking
rev 1.0, 9/14/2003 Page 19
Wumpus models
KB = wumpus-world rules + observations
rev 1.0, 9/14/2003 Page 20
Wumpus models
KB = wumpus-world rules + observationsα2 = “[2,2] is safe”, KB α2
rev 1.0, 9/14/2003 Page 21
Inference KB i α = sentence α can be derived from KB by procedure i
Consequences of KB are a haystack; α is a needle.Entailment = needle in haystack; inference = finding it
Soundness: i is sound ifwhenever KB i α , it is also true that KB α
Completeness: i is complete ifwhenever KB α , it is also true that KB i α
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.
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 22
Propositional logic: SyntaxPropositional logic is the simplest logic—illustrates basic ideas The proposition symbols P1 , P2 , … are sentences If S is a sentence, ¬S is a sentence
negationIf S1 and S2 is a sentence, S1 ∨S2 is a sentence
conjunction If S1 and S2 is a sentence, S1 ∧ S2 is a sentence
disjunction If S1 and S2 is a sentence, S1 ⇒ S2 is a sentence
implication If S1 and S2 is a sentence, S1 ⇔ S2 is a sentence
biconditional
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 23
Propositional logic: Semantics Each model specifies true/false for each proposition symbol
A = True; B = True; C = False
Rules for evaluating truth with respect to a model m:
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 24
Truth tables for connectivesP Q ¬P P ∧ Q P ∨ Q P ⇒ Q P ⇔ Q
FALSE FALSE TRUE FALSE FALSE TRUE TRUEFALSE TRUE TRUE FALSE TRUE TRUE FALSETRUE FALSE FALSE FALSE TRUE FALSE FALSETRUE TRUE FALSE TRUE TRUE TRUE TRUE
rev 1.0, 9/14/2003 Page 25
Wumpus world sentencesLet 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”
rev 1.0, 9/14/2003 Page 26
Wumpus world sentencesLet 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
rev 1.0, 9/14/2003 Page 27
Truth tables for inference
B 1,1 B 2,1 P 1,1 P 1,2 P 2,1 P 2,2 P 3,1 KB α1
FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUEFALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE TRUE
FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUEFALSE TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUEFALSE TRUE FALSE FALSE FALSE TRUE FALSE TRUE TRUEFALSE TRUE FALSE FALSE FALSE TRUE TRUE TRUE TRUEFALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE
rev 1.0, 9/14/2003 Page 28
Inference by enumerationDepth-first enumeration of all models is sound and complete
O(2n) for n symbols; problem is co-NP-complete
function TT-Entails?(KB, α) returns true or falsesymbols ← a list of the proposition symbols in KB and αreturn TT-Check-All(KB, α, symbols, [])
function TT-Check-All(KB, α, symbols, model) returns true or falseif Empty?(symbols) then
if PL-True?(KB, model) then return PL-True?(α, model)else return true
else doP ← First(symbols); rest ← Rest(symbols)return TT-Check-All(KB, α, Rest, Extend(P, true, model) and
TT-Check-All(KB, α, rest, Extend(P, false, model)
rev 1.0, 9/14/2003 Page 29
Logical equivalenceTwo sentences are logically equivalent iff true in same models:
α ≡ β if and only if α β and β α(α ∧ β ) ≡ (β ∧ α ) commutativity of ∧(α ∨ β ) ≡ (β ∨ α ) commutativity of ∨
((α ∧ β ) ∧ γ) ≡ (α ∧ (β ∧ γ)) associativity of ∧((α ∨ β ) ∨ γ) ≡ (α ∨ (β) ∨ γ)) associativity of ∨
¬(¬α) ≡ α double-negation elimination(α ⇒ β ) ≡ (¬β ⇒ ¬α ) contraposition(α ⇒ β ) ≡ (¬α ∨β ) implication elimination(α ⇔ β ) ≡ (α ⇒ β ) ∧ (β ⇒ α ) biconditional elimination¬(α ∧ β ) ≡ (¬α ∨ ¬β ) de Morgan¬(α ∨ β ) ≡ (¬α ∧ ¬β ) de Morgan
(α ∧ (β ∨ γ)) ≡ ((α ∧ β ) ∨ (α ∧ γ)) distributivity of ∧ over ∨(α ∨ (β ∧ γ)) ≡ ((α ∨ β ) ∧ (α ∨ γ)) distributivity of ∨ over ∧
rev 1.0, 9/14/2003 Page 30
Validity and Satisfiability A sentence is valid if it is true in all models
A ∨ ¬A, A ⇒ A, (A ∧ (A ⇒ B)) ⇒ BValidity is connected to inference via the Deduction Theorem:
KB α if and only if (KB ⇒ α ) is valid
A sentence is satisfiable if it is true in some model A ∨ B, C
A sentence is unsatisfiable if it is true in no models e.g., A ∧ ¬A
Satisfiability is connected to inference via the following:
KB α if and only if (KB ∧ ¬α ) is unsatisfiable i.e., prove α by reductio ad absurdum
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 31
Proof methods Proof methods divide into (roughly) two kinds: Application of inference rules
Legitimate (sound) generation of new sentences from old Proof = a sequence of inference rule applications
Can use inference rules as operators in a standard search algorithms
Typically require translation of sentences into a normal form
Model checking truth table enumeration (always exponential in n)improved backtracking, e.g., Davis-Putnam-Logemann-Lovelandheuristic search in model space (sound but incomplete)
e.g., min-conflicts-like hill-climbing algorithms
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 32
Normal formsOther approaches to inference use syntactic operations on sentences, often expressed in standardized forms Conjunctive Normal Form (CNF – universal)
conjunction of disjunctions of literals (clauses)(A ∨ ¬B) ∧ (B ∨ ¬C ∨ ¬D)
Disjunctive Normal Form (DNF – universal) disjunction of conjunctions of literals (terms)(A∧B) ∨ (A∧¬C) ∨ (A∧¬D) ∨ (¬B∧¬C) ∨ (¬B∧¬D)
Horn Form (restricted) conjunction of Horn clauses (clauses with <= 1 positive literal)(A ∨ ¬B) ∧ (B ∨ ¬C ∨ ¬D) Often written as set of implications: B ⇒ A and (C ∧ D) ⇒ B
AIMA Slides © Stuart Russell and Peter Norvig, 1998
rev 1.0, 9/14/2003 Page 33
Inference rules for propositional logic Resolution (for CNF): complete for propositional logic
Modus Ponens (for Horn Form): complete for Horn KBs
Can be used with forward chaining or backward chainingThese algorithms are very natural and run in linear time AIMA Slides © Stuart Russell and Peter Norvig, 1998
,α β β γα γ
∨ ¬ ∨∨
1 1, , ,n nα α α α ββ∧ ∧ ⇒…
rev 1.0, 9/14/2003 Page 34
Forward chainingIdea: fire any rule whose premises are satisfied in the KB, add its conclusion to the KB, until query is foundP ⇒ QL ∧ M ⇒ PB ∧ L ⇒ MA ∧ P ⇒ LA ∧ B ⇒ LAB
rev 1.0, 9/14/2003 Page 35
Forward chaining algorithmfunction PL-FC-Entails?(KB, q) returns true or false
local variables:count, a table, indexed by clause, initially the number of premisesinferred, a table, indexed by symbol, each entry initially falseagenda, a list of symbols, initially the symbols known to be true
while agenda is not empty dop ← Pop(agenda)unless inferred[p] do
inferred[p] ← truefor each Horn clause c in whose premise p appears do
decrement count[c]if count[c] = 0 then do
if Head[c] = q then return truePush(Head[c](agenda)
return false
rev 1.0, 9/14/2003 Page 36
Forward chaining example
rev 1.0, 9/14/2003 Page 37
Forward chaining example
rev 1.0, 9/14/2003 Page 38
Forward chaining example
rev 1.0, 9/14/2003 Page 39
Forward chaining example
rev 1.0, 9/14/2003 Page 40
Forward chaining example
rev 1.0, 9/14/2003 Page 41
Forward chaining example
rev 1.0, 9/14/2003 Page 42
Forward chaining example
rev 1.0, 9/14/2003 Page 43
Forward chaining example
rev 1.0, 9/14/2003 Page 44
Proof of completenessFC derives every atomic sentence that is entailed by KB
1. FC reaches a fixed point where no new atomic sentences are derived
2. Consider the final state as a model m, assigning true/false to symbols
3. Every clause in the original KB is true in mProof: Suppose a clause a1 ∧ … ∧ ak ⇒ b is false in mlThen a1 ∧ … ∧ ak is true in m and b is false in mTherefore the algorithm has not reached a fixed point!
4. Hence m is a model of KB5. If KB q, q is true in every model of KB, including m
rev 1.0, 9/14/2003 Page 45
Backward chainingIdea: work backwards from the query q:
to prove q by BC,check if q is known already, or prove by BC all premises of some rule concluding q
Avoid loops: check if new subgoal is already on the goal stackAvoid repeated work: check if new subgoal1) has already been proved true, or2) has already failed
rev 1.0, 9/14/2003 Page 46
Backward chaining example
rev 1.0, 9/14/2003 Page 47
Backward chaining example
rev 1.0, 9/14/2003 Page 48
Backward chaining example
rev 1.0, 9/14/2003 Page 49
Backward chaining example
rev 1.0, 9/14/2003 Page 50
Backward chaining example
rev 1.0, 9/14/2003 Page 51
Backward chaining example
rev 1.0, 9/14/2003 Page 52
Backward chaining example
rev 1.0, 9/14/2003 Page 53
Backward chaining example
rev 1.0, 9/14/2003 Page 54
Backward chaining example
rev 1.0, 9/14/2003 Page 55
Backward chaining example
rev 1.0, 9/14/2003 Page 56
Backward chaining example
rev 1.0, 9/14/2003 Page 57
Forward vs. backward chainingFC is data-driven, cf. automatic, unconscious processing,
e.g., object recognition, routine decisionsMay do lots of work that is irrelevant to the goalBC is goal-driven, appropriate for problem-solving,
e.g., Where are my keys? How do I get into a PhD program?
Complexity of BC can be much less than linear in size of KB
rev 1.0, 9/14/2003 Page 58
ResolutionResolution inference rule (for CNF): complete for propositional logic
where li and mj are complementary literals.e.g.,
Resolution is sound and complete for propositional logic
1 1
1 1 1 1 1 1
,k n
i i k j j n
l l m ml l l l m m m m− + − +
∨ ∨ ∨ ∨∨ ∨ ∨ ∨ ∨ ∨ ∨ ∨ ∨ ∨ ∨
1,3 2,2 2,2
1,3
,P P PP
∨ ¬
rev 1.0, 9/14/2003 Page 59
Conversion to CNFB1,1 ⇔ (P1,2 ∨ P2,1)
1. Eliminate ⇔ , replacing (α ⇔ β ) with(α ⇒ β ) ∧ (β ⇒ α ).
(B1,1 ⇒ (P1,2 ∨ P2,1) ∧ (P1,2 ∨ P2,1) ⇒ B1,1)
2. Eliminate ⇒ , replacing (α ⇒ β ) with (¬α ∨ β ).(¬B1,1 ∨ P1,2 ∨ P2,1 ) ∧ (¬(P1,2 ∨ P2,1) ∨ B1,1 )
3. Move ¬ inwards using de Morgan's rules:(¬B1,1 ∨ P1,2 ∨ P2,1 ) ∧ ((¬ P1,2 ∧ ¬ P2,1) ∨ B1,1 )
4. Apply distributivity law ∨ over ∧ and flatten:(¬B1,1 ∨ P1,2 ∨ P2,1 ) ∧ (¬P1,2 ∨ B1,1) ∧ (¬P2,1 ∨ B1,1 )
rev 1.0, 9/14/2003 Page 60
Resolution algorithmProof by contradiction, i.e., show KB ∧ ¬α unsatisfiable
function PL-Resolution(KB, α) returns true or false
clauses ← the set of clauses in the CNF representation of KB ∧ ¬αnew ← }loop do
for each Ci, Cj in clauses doresolvents ← PL-Resolve(Ci, Cj )if resolvents contains the empty clause then return truenew ← new ∪ resolvents
if new ⊆ clauses then return falseclauses ← clauses ∪ new
rev 1.0, 9/14/2003 Page 61
Resolution exampleKB = (B1,1 ⇔ (P1,2 ∨ P2,1)) ∧ ¬B1,1, α = ¬P1,2
rev 1.0, 9/14/2003 Page 62
Summary Logical agents apply inference to a knowledge base to derive new information and make decisions Basic concepts of logic:
syntax: formal structure of sentences semantics: truth of sentences wrt models entailment: necessary truth of one sentence given another inference: deriving sentences from other sentences soundess: derivations produce only entailed sentences completeness: derivations can produce all entailed sentences
Wumpus world requires the ability to represent partial and negated information, reason by cases, etc. Forward, backward chaining are linear-time, complete for Horn clausesResolution is complete for propositional logicPropositional logic lacks expressive power
AIMA Slides © Stuart Russell and Peter Norvig, 1998