0% found this document useful (0 votes)
66 views97 pages

Logical Agents: AIMA 3rd Ed. - Chapter 7

Logical agents can represent knowledge about the world in a knowledge base and make inferences over this knowledge. Knowledge-based agents can: 1) Represent states, actions, and other concepts; 2) Incorporate new perceptions to update their internal representations; and 3) Deduce hidden properties and appropriate actions by making logical inferences based on the knowledge base. The document provides examples of how logical reasoning can be applied in the Wumpus World environment to deduce whether moves are safe based on perceptions like breeze and stench. Formal logic provides a framework for agents to represent information and draw valid conclusions even without knowing the meaning or interpretation of the concepts.

Uploaded by

Ulaş Tura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views97 pages

Logical Agents: AIMA 3rd Ed. - Chapter 7

Logical agents can represent knowledge about the world in a knowledge base and make inferences over this knowledge. Knowledge-based agents can: 1) Represent states, actions, and other concepts; 2) Incorporate new perceptions to update their internal representations; and 3) Deduce hidden properties and appropriate actions by making logical inferences based on the knowledge base. The document provides examples of how logical reasoning can be applied in the Wumpus World environment to deduce whether moves are safe based on perceptions like breeze and stench. Formal logic provides a framework for agents to represent information and draw valid conclusions even without knowing the meaning or interpretation of the concepts.

Uploaded by

Ulaş Tura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

Logical agents

AIMA 3rd ed. - Chapter 7

AIMA 3rd ed. - Chapter 7 1


AIMA 3 - Part II
The problem solving agents approached the problems with limited knowledge:
♦ what their possible actions are
♦ resulting states
♦ goal state
But they did not know for instance that UP means the reverse of Down (in
the N-puzzle problem).
Knowledge-based agents represent the world knowledge (related to their
task) in a knowledge base and can make inference over this knowl-
edge base.

AIMA 3rd ed. - Chapter 7 2


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 3rd ed. - Chapter 7 3


Wumpus World PEAS description
Performance measure
gold +1000, death -1000
-1 per step, -10 for using the arrow
Breeze
Environment 4 Stench
PIT

Squares adjacent to wumpus are smelly Breeze


Breeze
3
Squares adjacent to pit are breezy Stench

Gold
PIT

Glitter iff gold is in the same square Stench Breeze


2
Shooting kills wumpus if you are facing it
Shooting uses up the only arrow 1
Breeze
PIT
Breeze

Grabbing picks up gold if in same square START

Releasing drops the gold in same square 1 2 3 4

Actions Left turn, Right turn,


Forward, Grab, Release, Shoot
Sensors Breeze, Glitter, Smell
Goals Get gold back to start without entering pit or wumpus square
AIMA 3rd ed. - Chapter 7 4
Wumpus world characterization
Fully Observable?? No—only local perception
Deterministic?? Yes—outcomes 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

AIMA 3rd ed. - Chapter 7 5


Exploring a wumpus world

OK

OK OK
A

AIMA 3rd ed. - Chapter 7 6


Exploring a wumpus world

B OK
A

OK OK
A

AIMA 3rd ed. - Chapter 7 7


Exploring a wumpus world

P?

B OK P?
A

OK OK
A

AIMA 3rd ed. - Chapter 7 8


Exploring a wumpus world

P?

B OK P?
A

OK S OK
A A

AIMA 3rd ed. - Chapter 7 9


Exploring a wumpus world

P?

P
B OK P?
OK
A

OK S OK
A A W
AIMA 3rd ed. - Chapter 7 10
Exploring a wumpus world

P?

P
B OK P?
OK
A A

OK S OK
A A W
AIMA 3rd ed. - Chapter 7 11
Exploring a wumpus world

P? OK

P
B OK P? OK
OK
A A

OK S OK
A A W
AIMA 3rd ed. - Chapter 7 12
Exploring a wumpus world

P? OK

P
B OK P? BGS OK
OK
A A A

OK S OK
A A W
AIMA 3rd ed. - Chapter 7 13
Uncertainty
There may be situations where there are no guaranteed safe moves. In that
case, we will consider the likelihoods of events (e.g. if we have no
choice but go to one of the squares that may have a pit, decide on which
one has a higher probability of having a pit; we will see this later).
But for now, we assume that we are dealing with problems that can be solved
with logical thinking and the safety of squares will not be left to chance.

AIMA 3rd ed. - Chapter 7 14


P?

B OK P?
A
P? Breeze in (1,2) and (2,1)
⇒ no safe actions
OK B OK
P?
A A

S
A Smell in (1,1)
⇒ cannot move
Can use a strategy of coercion:
shoot straight ahead
wumpus was there ⇒ dead ⇒ safe
wumpus wasn’t there ⇒ safe
AIMA 3rd ed. - Chapter 7 15
Inference in computers
♦ How can we get an agent do what we did?
That is, how can it know whether it is OK to move to [2,1]?
♦ Notice that an exhaustive search is not an option here because the agent
doesnt want to die.
♦ The agent needs a formal inference mechanism to derive valid conclusions
even when it does not know what the interpretation is (the computer does
not need to know the meaning of OK, or Wumpus or Pit... )
We will use a formal logic to extend the capacity of our agents by endowing
them with the capacity for logical reasoning.

AIMA 3rd ed. - Chapter 7 16


Logic in general
Syntax defines the sentences in the language
Semantics define the “meaning” of sentences;
i.e., define truth of each sentence in each possible world
Logic is the framework where a formal language represents information
such that conclusions can be drawn.
E.g., 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 y
x + 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 3rd ed. - Chapter 7 17


Knowledge bases - 6.1
Knowledge base is a set of representations of facts (sentences)
about the world, expressed in knowledge representation language.**

Inference engine domain−independent algorithms

Knowledge base domain−specific content

AIMA 3rd ed. - Chapter 7 18


Knowledge bases
Declarative approach to building an agent (or other system):
Tell it what it needs to know
Ask it what to do—answers should follow from the KB

function KB-Agent( percept) returns an action


static: KB, a knowledge base
t, a counter, initially 0, indicating time
Tell(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

AIMA 3rd ed. - Chapter 7 19


Knowledge bases
Agents can be viewed at the knowledge level
i.e., what they know, regardless of how implemented
The implementation level may represent the knowledge in various forms. For
instance available flights of an airline can be represented as:
♦ as a list of strings (Sefer(Ist-Ankara))
♦ as TRUE in a 2D table indexed by location pairs etc.

AIMA 3rd ed. - Chapter 7 20


Entailment
We are interested in new sentences that we can infer from the knowledge
base and the new percepts. E.g. seeing that there is no breeze in [1,1], can
we infer that there is no wumpus in [1,2]?
We will use the word entailment which means that one thing follows from
another:
E.g., x = 0 entails xy = 0
E.g., x + y = 4 entails 4 = x + y
E.g. “P11” entails “B12”

KB-Rulesiai.FI#aasLwetaromeb

tas II.
AIMA 3rd ed. - Chapter 7 21
ana
Entailment
Entailment is a relationship between sentences (i.e., syntax)
that is based on semantics

-4worlds
Knowledge base KB entails sentence α (KB |= α)
possible

¥¥¥±
if and only if
α is true in all worlds where KB is true

Wetrounds -3 madde
KB : Rain ⇒
for the
Rain Nett Raminde

T AIMA 3rd ed. - Chapter 7 22


Models
We will consider possible worlds or models that define the truth settings of
all sentences.
KB Wetfriandş ¥
We say m is a model of a sentence α if α is true in m
KB Nett
M (α) is the set of all models of α ¥ .

÷i÷±¥÷÷
Difference between world and model:
”model” is used interchangeably with ”world” in the general sense;
but a ”model” of a particular sentence is a ”world” in which that sentence
is True)

KB ani
:@ ⇒ WETG ) 1 Rami
KB

WETG Rami WAG KB


Rami
:

AIMA 3rd ed. - Chapter 7 23


Entailment in the wumpus world
Situation after detecting nothing in [1,1], moving right and detecting breeze
in [2,1].
Consider possible models for ?s assuming only pits:

? ?
B
A A
?

AIMA 3rd ed. - Chapter 7 24


Wumpus models
3 Boolean choices ⇒ 8 possible models (considering only pits):

2 PIT
2

Breeze
1
Breeze
1 PIT
1 2 3
1 2 3

2 PIT
2 PIT
2
Breeze
Breeze
1 PIT
1
Breeze
1
1 2 3
1 2 3
1 2 3

2 PIT PIT
2 PIT

Breeze
1
Breeze
1 PIT 2 PIT PIT
1 2 3
1 2 3
Breeze
1 PIT

1 2 3

AIMA 3rd ed. - Chapter 7 25


Models of KB
KB = wumpus-world rules + observations
KB = (No Breeze in 11) and (Breeze in 12) and (Pits cause breeze in
adjacent squares)...

2 PIT
2

Breeze
1
Breeze
1 PIT
1 2 3

KB
1 2 3

2 PIT
2 PIT
2
Breeze
Breeze
1 PIT
1
Breeze
1
1 2 3
1 2 3
1 2 3

2 PIT PIT
2 PIT

Breeze
1
Breeze
1 PIT 2 PIT PIT
1 2 3
1 2 3
Breeze
1 PIT

1 2 3

AIMA 3rd ed. - Chapter 7 26


Wumpus models

2 PIT
2

Breeze
1
Breeze
1 PIT
1 2 3

KB
1 2 3

1
2 PIT
2 PIT
2
Breeze
Breeze
1 PIT
1
Breeze
1
1 2 3
1 2 3
1 2 3

2 PIT PIT
2 PIT

Breeze
1
Breeze
1 PIT 2 PIT PIT
1 2 3
1 2 3
Breeze
1 PIT

1 2 3

KB = wumpus-world rules + observations


α1 = “[1,2] is safe”, KB |= α1, proved by model checking
AIMA 3rd ed. - Chapter 7 27
Wumpus models

2 PIT
2

Breeze
1
Breeze
1 PIT
1 2 3

KB
1 2 3

2 PIT
2 PIT
2
Breeze
Breeze
1 PIT
1
Breeze
1
1 2 3
1 2 3
1 2 3

2 PIT PIT
2 PIT

Breeze
1
Breeze
1 PIT 2 PIT PIT
1 2 3
1 2 3
Breeze
1 PIT

1 2 3

KB = wumpus-world rules + observations

AIMA 3rd ed. - Chapter 7 28


Wumpus models

2 PIT
2

Breeze
1

2
Breeze
1 PIT
1 2 3

KB
1 2 3

2 PIT
2 PIT
2
Breeze
Breeze
1 PIT
1
Breeze
1
1 2 3
1 2 3
1 2 3

2 PIT PIT
2 PIT

Breeze
1
Breeze
1 PIT 2 PIT PIT
1 2 3
1 2 3
Breeze
1 PIT

1 2 3

KB = wumpus-world rules + observations


α2 = “[2,2] is safe”, KB %|= α2

AIMA 3rd ed. - Chapter 7 29


Models
What we saw is summarized as: KB |= α if and only if M (KB) ⊆ M (α)
Equivalently; KB |= α if and only if α is true everywhere KB is true.
x
x x x
E.g. KB = ”Fever” and ”BodyAche” x
x
x x
x

M( ) x
α = ”Fever” x x
x
x
x x
x
x x
x x x x
x x
x x x x
xx x xx
x
x x
x x x

M(KB) x
x x x
x x
x

AIMA 3rd ed. - Chapter 7 30


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 if
whenever KB 'i α, it is also true that KB |= α
Completeness: i is complete if
whenever KB |= α, it is also true that KB 'i α
Preview: We will use a formal 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 3rd ed. - Chapter 7 31


Representation
We know of some knowledge representation languages:
♦ Programming languages
In a programming language you can say that ”there is a pit in [2,2]” by
world[2,2] = pit, but you cannot say ”either there is a pit in [2,1] or in [3,2]”
or that ”there is a wumpus in some square”
♦ Propositional logic
♦ First-order logic
♦ Natural languages are very expressive, but also too flexible/ambiguous

AIMA 3rd ed. - Chapter 7 32


Propositional logic: Syntax
Propositional logic is the simplest logic—illustrates basic ideas
Each proposition symbol P1, P2 stands for a proposition that can be true or
false and they form the atomic 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)

AIMA 3rd ed. - Chapter 7 33


Propositional logic: Semantics
Each model specifies true/false for each proposition symbol:
E.g. P1,2 P2,2 P3,1
true true f alse

AIMA 3rd ed. - Chapter 7 34


Propositional logic: Semantics
The semantics must specify how to compute the 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 S1 ⇒ S2 is true and S2 ⇒ S1 is true

AIMA 3rd ed. - Chapter 7 35


Truth tables for connectives
P Q ¬P P ∧Q P ∨Q P ⇒Q P ⇔Q
false false true false false true true
false true true false true true false
true false false false true false false
true true false true true true true
♦ True is true in every model
♦ False is false in every model
♦ Simple recursive process evaluates an arbitrarily complex sentence, e.g.,
¬P1,2 ∧ (P2,2 ∨ P3,1) = f alse ∧ (f alse ∨ true) = f alse ∧ true = f alse

AIMA 3rd ed. - Chapter 7 36


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”

AIMA 3rd ed. - Chapter 7 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”

AIMA 3rd ed. - Chapter 7 38


Truth tables for inference
B1,1 B2,1 P1,1 P1,2 P2,1 P2,2 P3,1 R1 R2 R3 R4 R5 KB
false false false false false false false true true true true false false
false false false false false false true true true false true false false
... ... ... ... ... ... ... ... ... ... ... ... ...
false true false false false false false true true false true true false
false true false false false false true true true true true true true
false true false false false true false true true true true true true
false true false false false true true true true true true true true
false true false false true false false true false false true true false
... ... ... ... ... ... ... ... ... ... ... ... ...
true true true true true true true false true true false true false

Enumerate rows (different assignments to symbols),


if KB is true in row, check that α is true too

AIMA 3rd ed. - Chapter 7 39


Inference by enumeration
Depth-first enumeration of all models is sound and complete

function TT-Entails?(KB, α) returns true or false

ğµF
inputs: KB, the knowledge base, a sentence in propositional logic Rain
α, the query, a sentence in propositional logic


symbols ← a list of the proposition symbols in KB and α
return TT-Check-All(KB, α, symbols, [ ])
IX.

¥ ! were

function TT-Check-All(KB, α, symbols, model) returns true or false


if Empty?(symbols) then
if PL-True?(KB, model) then return PL-True?(α, model)
else return true
else do
P ← 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))

O(2n) for n symbols; problem is co-NP-complete


AIMA 3rd ed. - Chapter 7 40
Proof methods
Proof methods divide into (roughly) two kinds:

Model checking
a) truth table enumeration (always exponential in n)
b) heuristic search in model space (sound but incomplete)
e.g., min-conflicts-like hill-climbing algorithms
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

AIMA 3rd ed. - Chapter 7 41


Logical equivalence
Two sentences are logically equivalent iff true in same models:
α ≡ β if and only if α |= β and β |= α

(α ∧ β) ≡ (β ∧ α) commutativity of ∧ and
(α ∨ β) ≡ (β ∨ α) commutativity of ∨ or
((α ∧ β) ∧ γ) ≡ (α ∧ (β ∧ γ)) associativity of ∧
((α ∨ β) ∨ γ) ≡ (α ∨ (β ∨ γ)) associativity of ∨
¬(¬α) ≡ α double-negation elimination
(α ⇒ β) ≡ (¬β ⇒ ¬α) contraposition Rain ⇒ wetGE.TW
⇒ Rain
Talip ← (α ⇒ β) ≡ (¬α ∨ β) implication elimination
(α ⇔ β) ≡ ((α ⇒ β) ∧ (β ⇒ α)) biconditional elimination
¬(α ∧ β) ≡ (¬α ∨ ¬β) De Morgan
⇐ VTR )
¬(α ∨ β) ≡ (¬α ∧ ¬β) De Morgan Tan TB
(α ∧ (β ∨ γ)) ≡ ((α ∧ β) ∨ (α ∧ γ)) distributivity of ∧ over ∨
(α ∨ (β ∧ γ)) ≡ ((α ∨ β) ∧ (α ∨ γ)) distributivity of ∨ over ∧

⇒ AACSNW
AAERAÜ
) WAG GIDI
:L
Raif

KB
. -
.

AIMA 3rd ed. - Chapter 7 42


-
-
Validity
A sentence is valid if it is true in all models,
e.g., T rue, A ∨ ¬A, A ⇒ A, (A ∧ (A ⇒ B)) ⇒ B
Validity is connected to inference via the Deduction Theorem:
KB |= α if and only if (KB ⇒ α) is valid v
-

ET
[email protected]
weansty

A⇒A] TAVA isaüstüne


AMA

AIMA 3rd ed. - Chapter 7 43


Satisfiability
A sentence is satisfiable if it is true in some model
e.g., 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

IBM )
AVB
KBtdifdonly.it is

C

[email protected]
)
harrison AIMA 3rd ed. - Chapter 7 44

.
Propositional inference: Enumeration method
Let KB = (A ∨ C) ∧ (B ∨ ¬C) and α = A ∨ B
Is it the case that KB |= α?
Check all possible models—α must be true wherever KB is true
O
A
F alse
B
OF
F alse
C
F alse
A ∨ C B ∨ ¬C KB α

E
F alse F alse T rue .
.
.

F alse T rue F alse


F alse T rue T rue T
T rue F alse F alse T

T rue F alse T rue T


T rue T rue F alse T
T rue T rue T rue T

AIMA 3rd ed. - Chapter 7 45


Propositional inference: Enumeration method
Let α = A ∨ B and KB = (A ∨ C) ∧ (B ∨ ¬C)
Is it the case that KB |= α?
Check all possible models: α must be true wherever KB is true
A B C A ∨ C B ∨ ¬C KB α
F alse F alse F alse F alse T rue F alse F alse
F alse F alse T rue T rue F alse F alse F alse
F alse T rue F alse F alse T rue F alse T rue
F alse T rue T rue T rue T rue T rue T rue
ar
T rue F alse F alse T rue T rue T rue T rue
T rue F alse T rue T rue F alse F alse T rue 4 modeli
T rue T rue F alse T rue T rue T rue T rue
T rue T rue T rue T rue T rue T rue T rue
Note that this is equivalent to proving that the sentence
((A ∨ C) ∧ (B ∨ ¬C) ⇒ A ∨ B is a valid sentence.

AIMA 3rd ed. - Chapter 7 46


Complexity of inference

Truth table method: 2N rows of table for any proof involving N symbols
Linear space requirements though using Depth-First search.

AIMA 3rd ed. - Chapter 7 47


Inference
An inference procedure that generates only entailed sentences is called sound
An inference procedure that generates all sentences that can be entailed is
called complete

Formally:

KB 'i α = sentence α can be derived from KB by procedure i


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 α

AIMA 3rd ed. - Chapter 7 48


Rules of Inference
Instead of enumeration method, we can also use inference rules that we know
are sound. We list these rules as:
α
β

Notation: Whenever a sentence in the KB matches the pattern above the


line (α), the inference rule can conclude the sentence below the line (β).

AIMA 3rd ed. - Chapter 7 49


Application: Propositional inference in the Wumpus world
Assume the following situation:
1,4 2,4 3,4 4,4 A = Agent
B = Breeze
G = Glitter, Gold
OK = Safe square
1,3 2,3 3,3 4,3 P = Pit
W! S = Stench
V = Visited
W = Wumpus
1,2 2,2 3,2 4,2
A
S
OK OK
1,1 2,1 3,1 4,1
B P!
V V
OK OK

How does the agent know where to go?

AIMA 3rd ed. - Chapter 7 50


Complete Set of Inference rules for propositional logic
Modus Ponens (or implication elimination)
ex
O1, . . . , αn, O top
iki
:
α α1 ∧ · · · ∧ αn ⇒ β

bottomwd-G.pwpam-D9.TK
β

And-elimination (from a conjunction, you can infer any of the conjuncts)


α1 ∧ · · · ∧ αn

-
aldan .
- -

αi
Dayer.Traff.ie -

di
Unit resolution (from a disjunction, if one of the disjuncts are false, you can
infer the other one is true)
α∨β ¬β
α

Resolution (for CNF): complete for propositional logic


α ∨ β,
0110
¬β ∨ γ
α∨γ
AIMA 3rd ed. - Chapter 7 51
Normal forms
Approaches to inference use syntactic operations on sentences which are
often expressed in standardized forms:
Disjunctive Normal Form (DNF—universal*)
disjunction of conjunctions
! "#
of literals$
terms
E.g., (A ∧ B) ∨ (A ∧ ¬C) ∨ (A ∧ ¬D) ∨ (¬B ∧ ¬C) ∨ (¬B ∧ ¬D)

Conjunctive Normal Form (CNF—universal*))


conjunction of disjunctions
! "#
of literals$
clauses
E.g., (A ∨ ¬B) ∧ (B ∨ ¬C ∨ ¬D)

* Every sentence can be written in CNF or DNF.

AIMA 3rd ed. - Chapter 7 52


Resolution
♦ Resolution requires the Conjunctive Normal Form.
♦ Resolution is sound and complete for propositional logic
♦ Resolution inference rule (for CNF): complete for propositional logic
$1 ∨ · · · ∨ $k , m1 ∨ · · · ∨ mn
$1 ∨ · · · ∨ $i−1 ∨ $i+1 ∨ · · · ∨ $k ∨ m1 ∨ · · · ∨ mj−1 ∨ mj+1 ∨ · · · ∨ mn
where $i and mj are complementary literals.

P?
♦ E.g., P
00
P1,3 ∨ P2,2, ¬P2,2 B OK P?
OK

O
A A
P1,3

Raü
OK S OK
A A W

O
7RJInvwewta.GR/ain-
AIMA 3rd ed. - Chapter 7 53
Resolution algorithm
Proof 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 do
resolvents ← PL-Resolve(Ci, Cj )
if resolvents contains the empty clause then return true
new ← new ∪ resolvents
if new ⊆ clauses then return false
clauses ← clauses ∪ new

AIMA 3rd ed. - Chapter 7 54


Resolution example
KB = (B1,1 ⇔ (P1,2 ∨ P2,1)) ∧ ¬B1,1 α = ¬P1,2
Add the negation of the sentence you want to prove (e.g. ¬¬P1,2) and see
if you reach a contradiction:

AIMA 3rd ed. - Chapter 7 55


Resolution example
KB = (B1,1 ⇔ (P1,2 ∨ P2,1)) ∧ ¬B1,1 α = ¬P1,2

AIMA 3rd ed. - Chapter 7 56


Conversion to CNF
B1,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 and double-negation:
(¬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)

AIMA 3rd ed. - Chapter 7 57


Resolution example
KB = (B1,1 ⇔ (P1,2 ∨ P2,1)) ∧ ¬B1,1 α = ¬P1,2

P2,1 B1,1 B1,1 P1,2 P2,1 P1,2 B1,1 B1,1 P1,2

B1,1 P1,2 B1,1 P P P1,2 B1,1 P2,1 B1,1 P P P2,1 P2,1 P1,2
1,2 2,1 1,2 2,1

AIMA 3rd ed. - Chapter 7 58


Restricted Forms: Horn Form
The completeness of resolution makes it a very important inference method.
In many practical situations however, the full power of resolution is not
needed.
Horn Form (restricted): A KB is said to be in Horn Form if it is a conjunction
of Horn clauses.
Horn clause =
♦ proposition symbol; or
♦ (conjunction of symbols) ⇒ symbol

Alternative definition: a Horn clause is a disjunction of literals of which at


most one is positive

Example KB: C ∧ (B ⇒ A) ∧ (C ∧ D ∧ E ⇒ B)
Terminology: premise ⇒ conclusion

AIMA 3rd ed. - Chapter 7 59


Restricted Forms: Horn Form
The Horn form seems very restricted, but many real databases can be written
in Horn form. How?
♦ Definite clauses: with exactly one positive literal
e.g.: (C ∧ D ⇒ B)
♦ Facts: a definite clause with no negative literals simply asserts a given
proposition
e.g.: C
♦ Integrity constraints: a Horn clause with no positive literals
e.g.: ¬(W 11 ∧ W 22)

AIMA 3rd ed. - Chapter 7 60


Restricted Forms: Horn Form
How can you convert each sentence into an implication?
(W 11 ∧ W 22) ⇒ F alse

AIMA 3rd ed. - Chapter 7 61


Restricted Forms: Horn Form
Modus Ponens is complete for Horn KBs (not in general)
α1, . . . , αn, α1 ∧ · · · ∧ αn ⇒ β
β
Can be used with forward chaining or backward chaining.
These algorithms are very natural and run in linear time in the size of the
KB.
They are also sound (easy to show) and complete (slightly more complicated
argument).

AIMA 3rd ed. - Chapter 7 62


Forward chaining
Idea: fire any rule whose premises are satisfied in the KB,
add its conclusion to the KB, until query is found
Q
P ⇒ Q
L∧M ⇒ P P
B∧L ⇒ M
A∧P ⇒ L M

A∧B ⇒ L L
A
B
A B

AIMA 3rd ed. - Chapter 7 63


Forward chaining algorithm

function PL-FC-Entails?(KB, q) returns true or false


local variables: count, a table, indexed by clause, initially the number of premises
inferred, a table, indexed by symbol, each entry initially false
agenda, a list of symbols, initially the symbols known to be true
while agenda is not empty do
p ← Pop(agenda)
unless inferred[p] do
inferred[p] ← true
for 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 true
Push(Head[c], agenda)
return false

AIMA 3rd ed. - Chapter 7 64


Forward chaining example

1
P
2
M
2
L

2 2

A B

AIMA 3rd ed. - Chapter 7 65


Forward chaining example

1
P
2
M
2
L

1 1

A B

AIMA 3rd ed. - Chapter 7 66


Forward chaining example

1
P
2
M
1
L

1 0

A B

AIMA 3rd ed. - Chapter 7 67


Forward chaining example

1
P
1
M
0
L

1 0

A B

AIMA 3rd ed. - Chapter 7 68


Forward chaining example

1
P
0
M
0
L

1 0

A B

AIMA 3rd ed. - Chapter 7 69


Forward chaining example

0
P
0
M
0
L

0 0

A B

AIMA 3rd ed. - Chapter 7 70


Forward chaining example

0
P
0
M
0
L

0 0

A B

AIMA 3rd ed. - Chapter 7 71


Forward chaining example

0
P
0
M
0
L

0 0

A B

AIMA 3rd ed. - Chapter 7 72


Backward chaining
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
The algorithm is essentially identical to the AND-OR-GRAPH search given
in Fig. 4.11.

AIMA 3rd ed. - Chapter 7 73


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 74


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 75


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 76


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 77


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 78


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 79


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 80


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 81


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 82


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 83


Backward chaining example

A B

AIMA 3rd ed. - Chapter 7 84


Forward vs. backward chaining
FC is data-driven, cf. automatic, unconscious processing,
e.g., object recognition, routine decisions
May do lots of work that is irrelevant to the goal
BC 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

AIMA 3rd ed. - Chapter 7 85


Satisfiability Problem
Satisfiability problem (is this sentence satisfiable?) is at the core of many
general problems (e.g. constraint satisfaction problems).
Satisfiability can be checked by enumerating the possible models until one
is found that satisfies the sentence using depth-first enumeration of possible
models.
However, determining the satisfiability of sentences in propositional logic was
the first problem proved to be NP-complete.

AIMA 3rd ed. - Chapter 7 86


Davis-Putnam (or DPLL) Algorithm
♦ Recursive, depth-first enumeration of possible models,
♦ similar to TT-ENTAILS but with important improvements such as early
termination...
DPLL algorithm is one of the fastest satisfiability algorithms, despite the fact
that it is very old. It can solve hardware verification problems with a million
variables.

AIMA 3rd ed. - Chapter 7 87


Local search algorithms
We can use local search algorithms for satisfiability problems, as well.

Since each clause in the sentence needs to be satisfied, evaluation function


should count the number of unsatisfied clauses (e.g. min-conflict heuristic).

WALKSAT algorithm is one of the most effective algorithms.

AIMA 3rd ed. - Chapter 7 88


WALKSAT algorithm
♦ In each iteration, pick an unsatisfied clause and picks a symbol to flip.
♦ Two ways to decide how to choose the symbol:
- randomly select the symbol from the clause and flip it with probability p
- pick one that minimizes the number of unsatisfied clauses in the new state

AIMA 3rd ed. - Chapter 7 89


Local search algorithms
In underconstrained situations, solutions are easy to find.
Ex. A 3-CNF with 5 symbols and 5 clauses:
(¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E)
∧ (E ∨ ¬D ∨ B) ∧ (B ∨ E ∨ ¬C)
How many solutions?
What makes it a hard satisfiability problem?
What if WALKSAT returns failure?

AIMA 3rd ed. - Chapter 7 90


Local search algorithms
In underconstrained situations, solutions are easy to find.
Ex. A 3-CNF with 5 symbols and 5 clauses:
(¬D ∨ ¬B ∨ C) ∧ (B ∨ ¬A ∨ ¬C) ∧ (¬C ∨ ¬B ∨ E)
∧ (E ∨ ¬D ∨ B) ∧ (B ∨ E ∨ ¬C)
How many solutions?
16 of the 32 possible worlds are models for this sentence!
What makes it a hard satisfiability problem?
How constrained it is (w.r.t number of constraints and number of variables)
What if WALKSAT returns failure?
We cannot tell if the sentence is unsatisfiable or just WALKSAT needs more
time.

AIMA 3rd ed. - Chapter 7 91


Shortcomings of PL
Lack of Expressive Efficiency:
We need to state the general rule about breeze and pits as a separate sentence
for each square:
B11 ⇒ P 21 ∨ P 12
B21 ⇒ P 11 ∨ P 22 ∨ P 31

AIMA 3rd ed. - Chapter 7 92


Shortcomings of PL - Fluents
Fluents: changing aspects of the world
E.g. Keeping track of location and orientation.
Let Lij be a location proposition. Then we would like to say something like:
L11 ∧ F acingRight ∧ F orward ⇒ L21
But then we will have both L11 and L21 in the database!

AIMA 3rd ed. - Chapter 7 93


Shortcomings of PL - Fluents
Solution: Associate propositions with time steps:
L011, F acingEast0, HaveArrow 0...

After associating time with prepositions, we need axioms to keep track of


these fluents:
Ltxy ⇒ (Breezet ⇔ Breezexy )
Effect axioms:
L011 ∧ F acingEast0 ∧ F orward0 ⇒ (L121 ∧ ¬L111)
We would need one such sentence for each possible time step and each
possible square and orientation!

AIMA 3rd ed. - Chapter 7 94


Shortcomings of PL - Frame Problem
We still have other problems. The effect axioms fail to state what is un-
changed as the result of an action.
E.g. That the agent still has an arrow.
One possible solution is to add frame axioms explicitly asserting all the
propositions that remain the same:
F orwardt ⇒ (HaveArrow t ⇔ HaveArrow t+1)

F orwardt ⇒ (W umpusAlivet ⇔ W umpusAlivet+1)


...

AIMA 3rd ed. - Chapter 7 95


rtcomings of Propositional Logic - Representational Frame Prob
The proliferation of frame axioms is very inefficient and this problem was a
significant problem for AI researchers.
Solution: Focusing from writing axioms about actions to writing axioms
about fluents (successor-state axioms): HaveArrow t+1 ⇔ (HaveArrow t∧
¬Shoott)
Lt+1 t t
11 ⇔ (L11 ∧ (¬F orward ∨ Bump
t+1
))
∨(Lt12 ∧ (¬F acingSoutht ∧ F orwardt))
∨(Lt21 ∧ (¬F acingW estt ∧ F orwardt)).

AIMA 3rd ed. - Chapter 7 96


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
Truth table method is sound and complete for propositional logic.
Forward, backward chaining are linear-time, complete for Horn clauses
Resolution is complete for propositional logic.
Propositional logic lacks expressive power

AIMA 3rd ed. - Chapter 7 97

You might also like