Slides Kbagents
Slides Kbagents
CS335 Introduction to AI
Logical Agents
Francisco Iacobelli
Mine Sweep
Hunt the Wumpus Slightly modified version of the original:(Gregory Yob, circa 1973)
Knowledge Base
Knowledge representation language
Inference
Have background knowledge about the world
At every step:
Construct a sentence with assertion about percepts
Construct a sentence asking what action is next
Constructs a sentence asserting that action
PEAS:
Performance measure: +1000 for walk out w/gold; -1000 for
dying; -1 for each action, -10 for arrow
Environment a 4 × 4 grid. Agent starts at [1,1]; gold and pits
randomly distributed, etc..
Actuators Agent can move forward, left or right, grab, shoot
Sensors: {[Smell, Breeze, Glitter , Bump, Scream]}
[None,None,None,None,None]
[None,Breeze,None,None,None]
[Stench,None,None,None,None]
[Stench,Breeze,Glitter,None,None]
M(α) = {(H, M), (H, L), (H, B), (H, G), (M, H), (M, B), . . . , (L, H), (L, M), (L, L), (L, B), . . . , (G, G)}
Example models m.
m1 = (H, M); α = H daughterOf M.
m2 = (L, H); α = L daughterOf H.
m that satisfy αs (A.K.A. make m True): {(L, H), (L, M), (G, H), (G, M)}
Friends told me that Mary is not messy and she is always dependable. I also
know that Good rommates score 1 or 2 on cleanness and dependability.
Does Mary (KB) |= Good Roomate(α) .
(2,1) (2,2)
KB (1,1) α1
(1,2)
1
For example, a 1 on cleanness means he is very clean. a 3 means he is not at all
clean. (a, b) means a score of a on cleanness and b on dependability
[email protected] (@neiu.edu) AI July 21, 2021 16 / 70
Using Entailment to Get Answers
Is John a good Roomate?
Through friends, I know John is not messy and he is not always dependable. I
also know that Good rommates score 1 or 2 on cleanness and dependability .
(2,3)
(2,2) (1,1)
KB (1,2) α1
(1,3)
(2,1)
2
For example, a 1 on cleanness means he is very clean. a 3 means he is not at all
clean. (a, b) means a score of a on cleanness and b on dependability
[email protected] (@neiu.edu) AI July 21, 2021 17 / 70
Logic
Answer me this:Is it true that there are no pits in [1,2]?
Model the presence of pits in squares [1,2][2,2] and [3,1] with the
restriction α2 of no pits in [2,2]. Does KB |= α2 ?
3
Otherwise the grammar is ambiguous
[email protected] (@neiu.edu) AI July 21, 2021 20 / 70
Syntax
Using The Grammar, generation
CS
CS
S ∧ S
CS
S ∧ S
CS AS
( S )
CS
S ∧ S
CS AS
( S ) R
CS
CS
S ∧ S
CS AS
R
( S )
CS
S ∨ S
CS
S ∧ S
CS AS
R
( S )
CS
S ∨ S
AS AS
CS
S ∧ S
CS AS
R
( S )
CS
S ∨ S
AS AS
P Q
Is P ∧ Q ∨ ¬R a valid sentence?
That is, can you find a parse tree that generates these sentences?
R ∧ True ↔ P
R ∧ PQ ∨ True
([P ∧ ¬Q] ∧ R) ∨ ¬Q
True
¬P ∧ ¬¬Q
4
if P is true I claim that Q is true. Otherwise no claim
5
if P is true I claim that Q is true, if P is false I claim that Q is false. otherwise no
claim
[email protected] (@neiu.edu) AI July 21, 2021 25 / 70
Semantics
Can be expressed as Truth tables
P Q P ∧Q
T T T
Example: T F F
F T F
F F F
KB = R1 ∧ R2 ∧ R3 ∧ R4 ∧ R5
function TT-Check-All(KB,q,symbols,model)
if isEmpty(symbols)
if PL-True(KB,model)
return PL-True(q,model)
else
return true // if KB is false, always return true
else do
P=First(symbols)
rest=Rest(symbols)
return (TT-Check-All(KB,q,rest,model + {P=true}) AND
(TT-Check-All(KB,q,rest,model + {P=false})
(α ∧ β) ≡ (β ∧ α) 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 ∧
6 Chapter 7 39
There are many more, but these are the main ones
[email protected] (@neiu.edu) AI July 21, 2021 35 / 70
Inference By Theorem Proving
Concepts
Modus Ponens
α ⇒ β, α
β
If α implies β and α is true, then β is true
And Elimination
α∧β
α
α ⇐⇒ β
(α ⇒ β) ∧ (β ⇒ α)
(α ⇒ β) ∧ (β ⇒ α)
α ⇐⇒ β
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
Let’s say agent returns to [1,1] from [2,1] and goes to [1,2]
We add:
R11 : ¬B1,2
R12 : B1,2 ⇐⇒ (P1,1 ∨ P2,2 ∨ P1,3 )
until
there are no new clauses to be added
two clauses resolve to the empty class, which means KB |= α
[email protected] (@neiu.edu) AI July 21, 2021 45 / 70
Resolution
An algorithm
function PL-Resolution(KB,q)
// KB, the knowledge base. a sentence in prop logic.
// q, the query, a sentence in prop logic
clauses= contra(KB,q) //CNF representation of KB ∧ ¬q
new = {}
do
for each pair of clauses Ci,Cj in clauses
resolvents=PL-Resolve(Ci,Cj)
if resolvents contains the empty clause
return true
new = new + resolvents
if new is subset of clauses
return false
clauses = clauses + new
Horn Form
KB conjunction of Horn clauses
Horn Clause (at most one literal is Positive7 )
For example: (¬P ∨ ¬Q ∨ V ) is a Horn Clause.
so is (¬P ∨ ¬W ). But, (¬P ∨ Q ∨ V ) is not.
Definite Clauses: exactly one literal is positive.
function PL-FC-Entails(KB,q)
// KB, the knowledge base, a prop. sentence
// q, the query, a prop. sentence
count = a table //count[c] is num of symbols in c 0 s premise
inferred = a table //inferred[s] initially false for all s
agenda = a queue of symbols //Init w/symbols that are true
P ⇒ Q
L∧M ⇒ P P
B ∧L ⇒ M
A∧P ⇒ L M
A∧B ⇒ L
A L
B
A B
(a) (b)
Figure 7.16 FILES: figures/pl-horn-example.eps (Tue Nov 3 13:45:07 2009). (a) A set of Horn
clauses. (b) The corresponding AND – OR graph.
1
P
P⇒Q
2 L∧M ⇒P
M B∧L⇒M
A∧P ⇒L
2 A∧B ⇒L
L A
B
2 2
A B
1
P P⇒Q
2 L∧M ⇒P
B∧L⇒M
M A∧P ⇒L
2 A∧B ⇒L
L A
B
1 1
A B
1
P P⇒Q
2 L∧M ⇒P
B∧L⇒M
M
A∧P ⇒L
1 A∧B ⇒L
L A
B
1 0
A B
1
P P⇒Q
1 L∧M ⇒P
B∧L⇒M
M A∧P ⇒L
0 A∧B ⇒L
L A
B
1 0
A B
0
P P⇒Q
0 L∧M ⇒P
B∧L⇒M
M A∧P ⇒L
0 A∧B ⇒L
L A
B
0 0
A B
0
P P⇒Q
0 L∧M ⇒P
B∧L⇒M
M A∧P ⇒L
0 A∧B ⇒L
L A
B
0 0
A B
0
P P⇒Q
0 L∧M ⇒P
B∧L⇒M
M A∧P ⇒L
0 A∧B ⇒L
L A
B
0 0
A B
To prove q by B.C.
check if q is known or
prove by B.C. all premises of some rule concluding q
Avoid Loops: Check if new subgoal is already in goal stack Avoid
A B
A B
A B
A B
A B
A B
A B
A B
A B
A B
A B