(Part 1) Propositional Logic 2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

Artificial Intelligence

Lecture 5
Propositional Logic 2
10-11-2022
Outline
 Wumpus World Example
 Definite clauses and Horn clauses
 Backward and Forward Chaining
Wumpus World Example
 The Wumpus world is a cave consisting of rooms
connected by passageways.
 Somewhere in the cave is the terrible wumpus, a beast
that eats anyone who enters its room.
 The wumpus can be shot by an agent, but the agent has
only one arrow.
 Some rooms contain pits.
 The cave contains a heap of gold.
 The agent’s goal is to find the gold and bring it back to
the start, where it will climb out of the cave, without
getting killed.
Wumpus World Example
Performance measure
• +1000 for going out of the cave with the
gold.
• -1000 for filling into the pit or eaten by the
Wumpus.
• -1 per each action.
• -10 for using the arrow.
• The game ends either when the agent dies
or when the agent climbs out of the cave.
Wumpus World description
 Environment
 4 x 4 grid of rooms.
 The agent always starts in square [1, 1] facing the right.
 Squares adjacent to wumpus are smelly.
 Squares adjacent to pit are breezy.
 Glitter if 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.
Wumpus World description

 Sensors: Stench, Breeze, Glitter, Bump, Scream


The percept at any square is a list of 5 symbols
[Stench, Breeze, Glitter, Bump, Scream]

 Actuators: Left turn, Right turn, Forward, Grab, Release, Shoot


Wumpus world characterization
 Fully Observable No – only local perception

 Deterministic Yes – outcomes exactly specified

 Static Yes – Wumpus and Pits do not move

 Discrete Yes

 Single-agent? Yes – Wumpus is essentially a natural


feature
Exploring a wumpus world

• The agent’s initial knowledge


base contains the rules of the
environment:
• Agent in [1,1] and [1,1] is a safe
square; denoted by
“A” and “OK”
• The first percept is
[None, None, None, None, None]
[1, 1]
Entailment in the wumpus world
 Situation after detecting nothing
in [1,1], moving right to [2, 1]:
breeze in [2,1]
 Squares [1, 2], [2, 2], and [3, 1]
1, 2 2, 2
may contain a pit
 The are 23 = 8 possible models 2, 1 3, 1

for the presence of pits in [1, 2],


The percept in [2, 1] is
[2, 2], and [3, 1] [Null, Breeze, Null, Null, Null]
Wumpus models

The 8 possible
models for squares
[1, 2], [2, 2], [3, 1]
Wumpus models

 KB = wumpus-world rules + The observations of


nothing in [1, 1] and breeze in [2, 1]
Wumpus models

[1, 2]

 KB = wumpus-world rules + the observations


 α1 = "[1,2] is safe" , means there is no pit in [1,2].
 KB ╞ α1, proved by model checking
Wumpus models

 KB = wumpus-world rules + the observations


 α2 = "[2,2] is safe", KB ╞ α2
 Agent cannot conclude there is a pit in [2, 2] or there is no pit in [2, 2].
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].

From KB and observation:


 P1,1 there is no pit in [1,1] by the rule of the game
B1,1 there is no breeze in [1,1]
B2,1 there is a breeze in [2,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"
Wumpus world sentences example
 KB is represented by rules: 𝑅1 , 𝑅2 , 𝑅3 , 𝑅4 , 𝑅5
 There is no pit in [1,1]:
𝑅1 :  P1,1

 "Pits cause breezes in adjacent squares"


𝑅2 : B1,1  (P1,2  P2,1)
𝑅3 : B2,1  (P1,1  P2,2  P3,1)
“A square is breezy if and only if there is an adjacent pit"
𝑅4 : B1,1 there is no breeze in [1,1]
𝑅5 : B2,1 there is a breeze in [2,1]
Wumpus world sentences example
(Cont.)
 To show that KB ╞  P1,2 by a model-checking:
enumerate the models and check that  P1,2 is true
(P1,2 is false) in all models in which KB is true.
 Since there are 7 propositional symbols:
B1,1 , B2,1 , P1,1 , P1,2 , P2,1 , P2,2, P3,1
There are 27 = 128 possible model
Truth tables for inference

27 = 128 possible model

𝛼 =  P1,2
Truth tables for inference

In the 3 models in which the KB is true :


• 𝑃1,2 is false therefore ¬𝑃1,2 is true (there is no pit in [1, 2]) in all models in which KB is true
• 𝑃2,2 is true in 2 of these models and false in one, therefore we cannot tell whether
there is a pit in [2,2]
Notes
 In the KB of the wumpus example:
(𝑅1  𝑅2  𝑅3  𝑅4  𝑅5 ) is satisfiable; there are 3
models in which the KB is true.
 α is valid iff α is unsatisfiable , by contrapositive

 α is satisfiable iff α is not valid


Wumpus World example using
inference rules
Consider the knowledge base: 𝑅1 - 𝑅5
𝑅1 :  P1,1 there is no pit in [1,1]
𝑅2 : B1,1  (P1,2  P2,1)
𝑅3 : B2,1  (P1,1  P2,2  P3,1)
"A square is breezy if and only if there is an adjacent pit"
𝑅4 : B1,1 there is no breeze in [1,1]
𝑅5 : B2,1 there is a breeze in [2,1]
Prove ¬𝑃1,2 (there is no pit in [1, 2])
1. Apply the biconditional elimination to 𝑅2 to obtain
𝑅6 : ( B1,1 ⇒(P1,2  P2,1))  ((P1,2  P2,1)⇒ B1,1 )
Wumpus World example using
inference rules (Cont.)
2. Then apply and-elimination to get
𝑅7 : ((P1,2  P2,1)⇒ B1,1 )
3. This is equivalent to by contraposition rule
𝑅8 : (¬B1,1 ⇒¬(P1,2  P2,1))
4. Apply Modus Ponens to 𝑅8 and 𝑅4 (¬B1,1 ) to get
𝑅9 : ¬(P1,2  P2,1)
5. By De Morgan’s rule
𝑅10 : ¬P1,2  ¬ P2,1 neither [1, 2] nor [2, 1] contains a pit
Wumpus world example (Cont.)
 Add to the KB
𝑅11 : ¬B1,2
𝑅12 : B1,2  (P1,1  P2,2  P1,3)
 By the same method beginning from 𝑅12 we can prove ¬P2,2
and ¬P1,3
H. W.

 Using 𝑅1 , …, 𝑅12 prove ¬P2,2 and ¬P1,3


Wumpus world example (Cont.)
 Add to the KB 𝑅13 : ¬P2,2 and 𝑅14 : ¬P1,3
Show that there is a pit in [3, 1]:
 Applying biconditional elimination to 𝑅3 , followed by -

elimination, followed by Modus Ponens with 𝑅5 to obtain


𝑅15 : (P1,1  P2,2  P3,1)
Wumpus world example
P1,1  P2,2P3,1 ,P2,2
𝑅13
𝑅15
by resolution
𝑅16 P1,1 P3,1

𝑅16 P1,1  P3,1, P1,1 𝑅 1

by resolution
P3,1
Note

 Clauses are disjunction of literals, e.g., A  B


 The resolution rule applies only to conjunction of
clauses, e.g., (A  B)  (B  C  D),
which is known as Conjunctive Normal Form
(CNF).
Example
 Find by resolution that KB ⊨𝛼:
 KB is (F⇒ P) ∨ (D ⇒ P)
 𝛼 is (F ∧ D) ⇒ P
Example (cont.)
 Convert KB to CNF
 KB is (F⇒ P) ∨ (D ⇒ P)
KB in CNF (¬F ∨ P) ∨ (¬D ∨ P) ≡ ¬ F ∨ P ∨ ¬D
 𝛼 is (F ∧ D) ⇒ P

𝛼 in CNF ¬(F∧D) ∨ P
¬ F ∨ ¬D ∨ P
Example (cont.)
KB and ¬𝛼 ≡ (¬ (¬ F ∨ ¬D ∨ P) ≡ F ∧ D ∧ ¬ P)

¬F ∨ P ∨ ¬D F D ¬P

P ∨ ¬D


Then KB ⊨𝛼
Definite clauses
 Disjunction () of literals with exactly one positive,
e.g., ¬ A  ¬B  C (≡ A  B  C )
 ¬ A  B  C is not definite clause (has 2 positive
literals).
Horn clauses
 Disjunction of literals with at most one positive literal,
◼ proposition symbol (e.g. C); or
◼ (conjunction of symbols)  symbol
◼ E.g., C  (B  A)  (C  D  B)
◼ ¬ A  ¬B  C
◼ ¬ A , goal
 All definite clauses are Horn clauses.
 Clauses with No positive literals are called goal
clauses.
Note
P ∨ Q ⇒ R is definite clause:
¬ (P ∨ Q) ∨ R ≡ (¬ P ∧ ¬Q) ∨ R
≡ (¬ P ∨ R ) ∧ (¬Q ∨ R)
Inference with Horn clauses
 Inference with Horn clauses can be done through
the forward chaining and backward-chaining
algorithms.
 This type of inference is the basis for logic
programming.
Forward and backward chaining
 Horn Form (restricted)
KB = conjunction of Horn clauses

 Modus Ponens (for Horn Form): complete for Horn KBs

α1, … ,αn, α1  …  α n  β

β
 Can be used with forward chaining or backward chaining.
 These algorithms are very natural and run-in linear time.
Forward Chaining (FC)
 Idea: fire any rule whose premises are satisfied in the KB,
 add its conclusion to the KB, until query is found
KB is
• It is desired to prove the goal Q
• Begin from the facts A and B
A B A B  L Modus Pones
• From
𝐿
• Add L to the KB
B L B LM
• From 𝑀

• Add M to the KB
L M L MP
• From 𝑃
• Add P to the KB
P P Q
• From 𝑄
Backward Chaining (BC)
Idea: 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 stack
Avoid repeated work: check if new subgoal
1. has already been proved true, or
2. has already failed
Backward chaining example
• Begin from the query Q
• For Q to be true, P must be true
• For P to be true, L and M should
be true
• To prove L, A and B should be
true; A and B are true then L is
true
• Since L is true and B is true then
M is true.
Forward vs. backward chaining
FC BC
Start with facts Start with goal
Test all rules Test some rules
Data-driven Goal-driven
May do lots of work that is irrelevant Searches only the part of the KB that
to the goal-it infer every statement is relevant to the goal
possible from the KB

Top-down reasoning Bottom-up reasoning

 Complexity of BC can be much less than linear in size of KB

You might also like