Logical Agents
Russell & Norvig - AIMA2e
Ioan Alfred Letia
http://cs-gw.utcluj.ro/letia
[email protected]Articial Intelligence 07, 2004 1
Outline
Knowledge-based agents The wumpus world Logic Propositional logic: A very simple logic Reasoning patterns in propositional logic Effective propositional inference Agents based on propositional logic
Articial Intelligence 07, 2004 2
Knowledge Bases
Inference engine Knowledge base domainindependent algorithms domainspecific content
Knowledge base = set of sentences in a formal language Declarative approach to building an agent (or other system): T ELL it what it needs to know Then it can A SK itself what to doanswers 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 level, i.e. data structures in KB and algorithms that manipulate them
Articial Intelligence 07, 2004 3
Knowledge-Based Agents
function KB-AGENT ( percept) returns an action static: KB, a knowledge base t, a counter, initially 0, indicating time T ELL (KB, M AKE -P ERCEPT-S ENTENCE ( percept, t)) action A SK (KB, M AKE -ACTION -Q UERY (t)) T ELL (KB, M AKE -ACTION -S ENTENCE (action, t)) t t+1 return action
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
Articial Intelligence 07, 2004 4
Wumpus World PEAS
Stench
Bree z e
PIT
Bree z e
Stench Gold
PIT
Bree z e
Performance measure gold +1000, death -1000, -1 per step, -10 for using arrow
2 1
Stench
Bree z e
Bree z e
PIT
Bree z e
Environment Squares adjacent to wumpus are smelly Squares adjacent to pit are breezy Glitter iff gold is in the same square Shooting kills wumpus if you are facing it Shooting uses up the only arrow Grabbing picks up gold if in same square Releasing drops the gold in same square
1
START
Sensors Breeze, Glitter, Smell Actuators Left turn, Right turn, Forward, Grab, Release, Shoot
Articial Intelligence 07, 2004 5
Wumpus World Characterization
Observable??
Articial Intelligence 07, 2004 6
Wumpus World Characterization
Observable?? Deterministic?? Noonly local perception
Articial Intelligence 07, 2004 6
Wumpus World Characterization
Observable?? Episodic?? Noonly local perception Deterministic?? Yesoutcomes exactly specied
Articial Intelligence 07, 2004 6
Wumpus World Characterization
Observable?? Noonly local perception Deterministic?? Yesoutcomes exactly specied Episodic?? Nosequential at the level of actions Static??
Articial Intelligence 07, 2004 6
Wumpus World Characterization
Observable?? Noonly local perception Deterministic?? Yesoutcomes exactly specied Episodic?? Nosequential at the level of actions Static?? Discrete?? YesWumpus and Pits do not move
Articial Intelligence 07, 2004 6
Wumpus World Characterization
Observable?? Noonly local perception Deterministic?? Yesoutcomes exactly specied Episodic?? Nosequential at the level of actions Static?? Discrete?? YesWumpus and Pits do not move Yes
Single-agent??
Articial Intelligence 07, 2004 6
Wumpus World Characterization
Observable?? Noonly local perception Deterministic?? Yesoutcomes exactly specied Episodic?? Nosequential at the level of actions Static?? Discrete?? YesWumpus and Pits do not move Yes YesWumpus is essentially a natural
Single-agent?? feature
Articial Intelligence 07, 2004 6
Exploring a Wumpus World
OK
OK
A
OK
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
B
A
OK
OK
A
OK
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
P?
B
A
OK
P?
OK
A
OK
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
P?
B
A
OK
P?
OK S
A A
OK
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
P?
P
B
A
OK
P? OK
OK S
A A
OK
W
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
P?
P
B
A
OK
A
P? OK
OK S
A A
OK
W
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
P?
OK
P
B
A
OK
A
P? OK
OK
OK S
A A
OK
W
Articial Intelligence 07, 2004 7
Exploring a Wumpus World
P?
OK
P
B
A
OK
P? BGS OK OK A A OK
A
OK S
A
W
Articial Intelligence 07, 2004 7
Wumpus State of Knowledge
1,4 2,4 3,4 4,4 1,3 2,3 3,3 4,3 A B G OK P S V W = Agent = Breeze = Glitter, Gold = Safe square = Pit = Stench = Visited = Wumpus 1,4 2,4 3,4 4,4 1,3 2,3 3,3 4,3
1,2
2,2
3,2
4,2
1,2
2,2
P?
3,2
4,2
OK 1,1 A OK OK 2,1 3,1 4,1 1,1
OK 2,1 V OK A B OK 3,1 P? 4,1
(a)
(b)
The rst step taken by the agent in the wumpus world (a) The initial situation, after percept [None,None,None,None,None] (b) After one move, with percept [None,Breeze,None,None,None]
Articial Intelligence 07, 2004 8
Wumpus State of Knowledge 2
1,4 2,4 3,4 4,4 1,3 2,3 3,3 4,3 A B G OK P S V W = Agent = Breeze = Glitter, Gold = Safe square = Pit = Stench = Visited = Wumpus 1,4 2,4 P? 3,4 4,4 1,3 W! 2,3 3,3 P? 4,3
W!
A S G B
1,2
A S OK
2,2
3,2
4,2
1,2
OK 2,1 B V OK 3,1 P! 4,1 1,1
S V OK
2,2 V OK 2,1 B V OK
3,2
4,2
1,1 V OK
3,1
P!
4,1
V OK
(a)
(b)
Two later stages in the progress of the agent (a) After the third move, with percept [Stench,None,None,None,None] (b) After the fth move, with percept [Stench,Breeze,Glitter,None,None]
Articial Intelligence 07, 2004 9
Other Tight Spots
P? B
A
OK
P? P?
Breeze in (1,2) and (2,1) no safe actions Assuming pits uniformly distributed, (2,2) has pit w/ prob 0.86, vs. 0.31
OK B
A A
OK P?
Smell in (1,1)
cannot move
S
A
Can use a strategy of coercion: shoot straight ahead wumpus was there dead safe wumpus wasnt there safe
Articial Intelligence 07, 2004 10
Logic in General
Logics are formal languages for representing information such that conclusions can be drawn Syntax denes the sentences in the language Semantics dene the meaning of sentences; i.e., dene truth of a sentence in a world E.g., the language of arithmetic is a sentence; is not a sentence is true iff the number is no less than the number is true in a world where is false in a world where
Articial Intelligence 07, 2004 11
Entailment
Entailment means that one thing follows from another:
Knowledge base entails sentence is true in all worlds where is true
if and only if
E.g., the KB containing the Giants won and the Reds won entails Either the Giants won or the Reds won E.g., entails
Entailment is a relationship between sentences (i.e., syntax) that is based on semantics Note: brains process syntax (of some sort)
Articial Intelligence 07, 2004 12
Models
Logicians typically think in terms of models, which are formally structured worlds with respect to which truth can be evaluated We say is a model of a sentence if is true in
"
is the set of all models of
#$"
"
x x x x x x x x xx x x x x x x x x x x x x x x x x x x xx x x x x x x x x x x x x x x x
Then
if and only if
M(
E.g. = Giants won and Reds won = Giants won
!
x
M(KB)
x
Articial Intelligence 07, 2004 13
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
? ?
B
A A
8 possible models
Articial Intelligence 07, 2004 14
Wumpus Models
2 2
Breeze Breeze
PIT
PIT
1 2 3 3
2 2
PIT
PIT
2
Breeze Breeze
1
Breeze
PIT
3
1 1 1 2 3 1
1 2 3
2 2
PIT
PIT
PIT
Breeze Breeze
PIT
3
PIT
PIT
1 2 3
Breeze
PIT
3
Articial Intelligence 07, 2004 15
Wumpus Models
2 2
Breeze Breeze
PIT
PIT
1 2 3 3
KB
2
PIT
PIT
2
Breeze Breeze
1
Breeze
PIT
3
1 1 1 2 3 1
1 2 3
2 2
PIT
PIT
PIT
Breeze Breeze
PIT
3
PIT
PIT
1 2 3
Breeze
PIT
3
= wumpus-world rules + observations
Articial Intelligence 07, 2004 15
Wumpus Models
2 2
Breeze Breeze
PIT
PIT
1 2 3 3
KB
2
1
2
PIT
PIT
2
Breeze Breeze
1
Breeze
PIT
3
1 1 1 2 3 1
1 2 3
2 2
PIT
PIT
PIT
Breeze Breeze
PIT
3
PIT
PIT
1 2 3
Breeze
PIT
3
= wumpus-world rules + observations = [1,2] is safe,
%
, proved by model checking
Articial Intelligence 07, 2004 15
%
Wumpus Models
2 2
Breeze Breeze
PIT
PIT
1 2 3 3
KB
2
PIT
PIT
2
Breeze Breeze
1
Breeze
PIT
3
1 1 1 2 3 1
1 2 3
2 2
PIT
PIT
PIT
Breeze Breeze
PIT
3
PIT
PIT
1 2 3
Breeze
PIT
3
= wumpus-world rules + observations
Articial Intelligence 07, 2004 15
Wumpus Models
2 2
Breeze Breeze
PIT
PIT
1 2 3 3
KB
2
PIT
PIT
2
Breeze Breeze
1
Breeze
PIT
3
1 1 1 2 3 1
1 2 3
2 2
PIT
PIT
PIT
Breeze Breeze
PIT
3
PIT
PIT
1 2 3
Breeze
PIT
3
= wumpus-world rules + observations = [2,2] is safe,
' &
&
Articial Intelligence 07, 2004 15
Inference
= sentence procedure can be derived from by
)0(
Consequences of are a haystack; is a needle. Entailment = needle in haystack; inference = nding it
Soundness: is sound if whenever true that
1
Completeness: is complete if whenever also true that
Preview: we will dene a logic (rst-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 .
)2(
)2(
, it is also , it is
Articial Intelligence 07, 2004 16
Follows vs. Entails
Sentences
Semantics
Sentence Entails
Semantics
Representation
World
Aspects of the real world
Follows
Aspect of the real world
Articial Intelligence 07, 2004 17
Propositional Logic: Syntax
Propositional logic is the simplest logicillustrates basic ideas The proposition symbols If is a sentence, , etc are sentences is a sentence is a sentence is a sentence is a sentence is a sentence (negation)
If and are sentences, (conjunction)
%5
&5
If and are sentences, (disjunction)
%5
&5
If and are sentences, (implication)
%5
&5
%5 %5
%5 8
If and are sentences, (biconditional)
%5
&5
&5
&5
&5
%5 7
%4
&5
&4
Articial Intelligence 07, 2004 18
Propositional Logic: Semantics
Each model species true/false for each proposition symbol, E.g.
FEGD A% 4 @ H &
FEGD A& 4 @ H &
(8 possible models, can be enumerated) Rules for evaluating truth with respect to a model is true iff is false is true iff is true and is true iff is true or is true iff is false or i.e., is false iff is true and is true iff is true and : is true is true is true is false is true
%5 %5 8 7 &5 &5 &5 65
P B QI @ C4 S TR % H %5 %5 %5 %5 5
%5
&5
%5
&5
&5
Simple recursive process evaluates an arbitrary sentence, =
@ A% 4 & 7! @A& 4 & 8 @ C4 %B
"
7! PI STR
H " FEGD H FEG7D H FEGD H
Articial Intelligence 07, 2004 19
FEGD
8EGFD
%5
&5 &5 &5 &5
%5
G DE H G DE H U FEGD H G DE H G DE H G DE H H G DE H H P I G DE S TR H G DE H 9 U
Articial Intelligence 07, 2004 20
G DE 4
PI PI S S TR TR H H H PQI G DE QI P S TR S TR H H H P I P I FEGD STR TR S H H H 6 4 P P P QI QI QI S S S TR TR TR H H H 7 G DE H H G DE P QI S TR 8 G DE H PI PI S S TR TR H H 4 4 U 4 U 4 U
Truth Tables for Connectives
Wumpus World Sentences
@ @) )4 V V
Let Let be true if there is a pit in be true if there is a breeze in
Pits cause breezes in adjacent squares
6 6 @A& A% A% 4 @ @ % % %
1W XY
.
Articial Intelligence 07, 2004 21
1W XY
.
Wumpus World Sentences
@ @) )4 V V
Let Let be true if there is a pit in be true if there is a breeze in
Pits cause breezes in adjacent squares
A square is breezy if and only if there is an adjacent pit
@ @ A% 4 A% 4 % & 8 8 @A& 4 A& 4 @ & % 8 " @ C4 B% "
Articial Intelligence 07, 2004 21
@ @ A& A% % %
6 6 @A& A% A% 4 @ @ % % %
1W XY
.
1W XY
.
FEGD
. . . . . .
H H FEGD
. . . . . .
P QI STR H FEGD H
. . .
P QI STR H EGFD H PI STR H P QI STR H H FEGD PI STR H H PI STR H
. . .
P QI STR
FEGD H G DE
. . .
H PI STR H P QI STR H FEGD H PI STR H PI STR H P QI STR H PI STR
P P QI QI STR TR S H H EGFD FEGD H H
H G DE
. . .
H FEGD
. . .
. . .
H FEGD
. . .
. . .
H G DE H PI STR H H
Articial Intelligence 07, 2004 22
Truth Tables for Inference
G DE
. . .
. . .
P QI STR H
. . .
H FEGD H G DE
. . .
PI PI STR TR S H H PQI QI P STR TR S H H PI PI STR TR S H H EGFD P I S TR H H PQI G DE STR H H P QI STR H FEGD H G DE H H H PI STR H G DE H H G DE H FEGD H G DE G DE
. . . . . .
PI STR
PI STR
P P QI QI @A% S S TR TR % H H P I P I @A& S S TR TR % H H P I P I @A% 4 S S TR TR % H H PQI QI @A% 4 P S S TR TR & H H P I P I @A& 4 S S TR TR % H H P I P I @A& 4 S S TR TR & H H P B G DE QI @ C4 S TR % H H PI PI S S TR TR H H H G DE H %
Inference by Enumeration
Depth-rst enumeration of all models is sound and complete
a !
"
for symbols; problem is co-NP-complete
Articial Intelligence 07, 2004 23
Logical Equivalence
Two sentences are logically equivalent iff true in same if and only if and models:
d
d" ! ! 66 ! fe8 fe7 8 7 d" d" " " " d" d" c c c c c c c ! ! ! ! ! ! d d 6 6 d 8 7 8 7 ! ! " " 8 d d d" 8e 7e 6 " "" ""
commutativity of commutativity of associativity of associativity of double-negation elimination contraposition implication elimination biconditional elimina de Morgan de Morgan distributivity of over distributivity of over
!!
!!
d"
! 6! d 8e 8 "" d " c c !! ! 6 7 7 d" 6 8 d" ! ef7 ""
6! 9 7 d" d" c c ! !! 6 8 6 d" d" 7 ! d "
"
""
!!
d"
fe8
""
Articial Intelligence 07, 2004 24
7e
Validity and Satisability
A sentence is valid if it is true in all models, e.g., , , ,
""
Articial Intelligence 07, 2004 25
EG g
Validity is connected to inference via the Deduction Theorem: if and only if is valid
A sentence is satisable if it is true in some model e.g., ,
A sentence is unsatisable if it is true in no models e.g.,
Satisability is connected to inference via the following: if and only if is unsatisable i.e., prove by reductio ad absurdum
8
"
"
Proof Methods
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 alg. Typically require translation of sentences into a normal form Model checking truth table enumeration (always exponential in ) improved backtracking, e.g., DavisPutnamLogemannLoveland heuristic search in model space (sound but incomplete) e.g., min-conicts-like hill-climbing algorithms
Articial Intelligence 07, 2004 26
Resolution
1. Conjunctive Normal Form (CNFuniversal) conjunction of disjunctions of literals
P?
P
B
A
OK
A
P? OK
qr
OK S
OK
A
clauses
"
!
E.g.,
6
8 6 %u vvw8 8 xyu % vvw8 8 v a 8 vv 8 v
Articial Intelligence 07, 2004 27
2. Resolution inference rule (for CNF): complete for propositional logic
t" 8
%u vv 8
) u % 8 ) u % vv 8 v 8 x u 8 % vv 8 v
8 a
where
and
are complementary literals. E.g.,
)u
@ A% 4 B 8 @A& 4 @A% 4 & B
3. Resolution is sound and complete for propositional logic
@ A& 4 &
@ A% % 9 !
2. Eliminate
1. Eliminate
@ A% % 8 % 8
, replacing
6 @ A% @ A% % 8 @ A% 4 & 8 @A& 4 % d 6 8 @ A% % " " " 7 !!
with
! 6 6 % ! ! 6 ! @ A% !
@ A% 4 & 8 9 @A&% 4
, replacing
"
Conversion to CNF
""
9 d 8 d " @ A% 4 & 8 @A& 4 %
.
with
4. Apply distributivity law ( over ) and atten:
3. Move inwards using de Morgans rules and double-negation:
@ A% 4 & 8 @A& 4 % " 7 !! 6 @A% 4 & 7 6 @A& 4 % @ A% 4 & 8 @A& 4 % " 7 ! 6! @A% 4 & 8 @A& 4 % " 8 @A% %
" @ A% % "
! d" 7 d
.
@ A% 4 & 8 @A& 4 % " 7 ! 6 @A% 4 & 8 @A% % " 7 ! 6 @A& 4 % 8 @A% %
Articial Intelligence 07, 2004 28
! "
PL Resolution
Proof by contradiction, i.e., show unsatisable
7 6
Articial Intelligence 07, 2004 29
Resolution Example
@ A% 4 ! & A% @ % ! @ A% 4 & 8 @A& 4 % ""
P2,1 B1,1
B1,1 P1,2 P2,1
@ A%
P1,2 B1,1
6
B1,1
P1,2
B1,1 P1,2 B1,1
P1,2 P2,1 P1,2
B1,1 P2,1 B1,1
P1,2 P2,1 P2,1
P2,1
P1,2
Articial Intelligence 07, 2004 30
Forward and Backward Chaining
Horn Form (restricted) KB = conjunction of Horn clauses Horn clause = proposition symbol; or (conjunction of symbols) symbol E.g.,
!
h"
Modus Ponens (for Horn Form): complete for Horn KBs
"
d % vv 7 v
7 % a
Can be used with forward chaining or backward chaining. These algorithms are very natural and run in linear time
Articial Intelligence 07, 2004 31
a
Forward Chaining
Q P M L
A
B
Idea: re any rule whose premises are satised in the , add its conclusion to the , until query is found
Articial Intelligence 07, 2004 32
PL Forward Chaining
Articial Intelligence 07, 2004 33
Forward Chaining Example
Q 1 P 2 M 2 L 2 A 2 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 1 P 2 M 2 L 1 A 1 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 1 P 2 M 1 L 1 A 0 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 1 P 1 M 0 L 1 A 0 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 1 P 0 M 0 L 1 A 0 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 0 P 0 M 0 L 0 A 0 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 0 P 0 M 0 L 0 A 0 B
Articial Intelligence 07, 2004 34
Forward Chaining Example
Q 0 P 0 M 0 L 0 A 0 B
Articial Intelligence 07, 2004 34
Proof of Completeness
FC derives every atomic sentence that is entailed by 1. FC reaches a xed point where no new atomic sentences are derived 2. Consider the nal state as a model true/false to symbols , assigning
3. Every clause in the original is true in Proof: Suppose a clause is false in Then is true in and is false in Therefore the algorithm has not reached a xed point!
4. Hence 5. If
is a model of ,
Px 7
%P
is true in every model of
xP 7
%P
, including
Articial Intelligence 07, 2004 35
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Backward Chaining Example
Q
M L
B
Articial Intelligence 07, 2004 36
Forward vs. Backward Chaining
1. FC is data-driven, cf. automatic, unconscious processing, e.g., object recognition, routine decisions 2. May do lots of work that is irrelevant to the goal 3. BC is goal-driven, appropriate for problem-solving, e.g., Where are my keys? How do I get into a PhD program? 4. Complexity of BC can be much less than linear in size of KB
Articial Intelligence 07, 2004 37
Effective Propositional Inference
Articial Intelligence 07, 2004 38
Local Search Algorithms
Articial Intelligence 07, 2004 39
Hard Satisability Problems
Articial Intelligence 07, 2004 40
Agents Based on Propositional Logic
Articial Intelligence 07, 2004 41
Circuit-Based Agent
Breeze Forward
Stench
TurnLeft
Glitter
TurnRight
Bump
Grab
Scream
Alive Shoot
Part of circuit-based agent for wumpus world, inputs, outputs, circuit grabbing gold, and circuit for determining whether wumpus is alive. Registers are shown as rectangles and one-step delays as small triangles.
Articial Intelligence 07, 2004 42
Circuit-Based Agent 2
Breeze
Forward
Stench
L2,1
FacingLeft
TurnLeft
Glitter
L1,1
TurnRight Grab
Bump
Shoot
Scream
L1,2
FacingDown
The circuit for determining whether the agent is at [1.1]. Every location and orientation has a similar circuit attached.
Articial Intelligence 07, 2004 43
Knowledge Propositions
Articial Intelligence 07, 2004 44
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 a sentence given another inference: deriving sentences from other sentences soundness: 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 clauses; Resolution is complete for propositional logic Propositional logic lacks expressive power
Articial Intelligence 07, 2004 45