Logical AgentsCE417: Introduction to Artificial IntelligenceSharif University of TechnologySpring 2016
βArtificial Intelligence: A Modern Approachβ, 3rd Edition, Chapter 7
Soleymani
Knowledge-based agents
2
Knowledge-based agents
Reasoning operates on internal representation of
knowledge
Logic as a general class of representation
Propositional logic
First-order logic
A generic knowledge-based agent
3
function KB_AGENT(πππππππ‘) returns an πππ‘πππ
persistent:πΎπ΅, a knowledge base
π‘, π counter for time, initially 0
TELL(πΎπ΅,MAKE_PERCEPT_SENTENCE(πππππππ‘, π‘))
πππ‘πππ β ASK(πΎπ΅,MAKE_ACTION_QUERY(π‘))
TELL(πΎπ΅,MAKE_ACTION_SENTENCE(πππ‘πππ, π‘))
π‘ β π‘ + 1
return πππ‘πππ
The agent must be able to: Represent states, actions, percepts,β¦.
Incorporate new percepts
Update internal representations of the world
Deduce hidden properties of the world
Deduce appropriate actions
β’ Makes a sentence asserting that the agent
perceived the given percept at the given time.
β’ Makes a sentence that asks what action
should be done at the current time.
β’ Makes a sentence asserting that the chosen
action was executed.
Knowledge Base (KB)
4
KB = a set of sentences expressed in a knowledge
representation language
TELL: adds new sentences to the knowledge base
ASK: asks a question of KB
the answer follows from previously TELLed sentences to the KB
Inference: derives new sentences from old ones
Basis of TELL and ASK operations
Wumpus world
5
Wumpus
Wumpus
Pitts
Gold
Agent
Wumpus world PEAS description
Performance measure
+1000 for garbing gold
-1000 for death
-1 for each action
-10 for using up the arrow
Game ends when the agent dies or climbs out of the cave
Environment
4Γ4 grid
Agent starts in [1,1] while facing to the right
Gold and wumpus are located randomly in the squares except to [1,1]
Each square other than [1,1] can be a pit with probability 0.2
6
Wumpus world PEAS description
7
Sensors: Stench, Breeze, Glitter, Bump, Scream
In the squares adjacent to wumpus, agent perceives a Stench
It the squares adjacent to a pit, agent perceives a Breeze
In the gold square, agent perceives a Glitter
When walking into a wall, agent perceives a Bump
When Wumpus is killed, agent perceives a Scream
Actuators: Forward, TurnLeft, TurnRight, Shoot, Grab, Climb
Forward,TurnLeft,TurnRight: moving and rotating face actions.
Moving to a square containing a pit or a live wumpus causes death.
If an agent tries to move forward and bumps into a wall then it does not move.
Shoot: to fire an arrow in a straight line in the facing direction of the agent
Shooting kills wumpus if the agent is facing it (o.w. the arrow hits a wall)
The first shoot action has any effect (the agent has only one arrow)
Grab: to pick up the gold if it is in the same square as the agent.
Climb: climb out of the cave but only from [1,1]
Wumpus world characterization
Fully Observable? No β many aspects are not directly
perceivable
Episodic? No β sequential at the level of actions
Static? Yes
Discrete?Yes
Single-agent?Yes
8
Wumpus world example
A: agent
OK: safe square
9
Wumpus world example
A: agent
OK: safe square
A: agent
OK: safe square
B: Breeze
10
Wumpus world example
A: agent
OK: safe square
B: Breeze
P: Pit
11
Wumpus world example
A: agent
OK: safe square
B: Breeze
P: Pit
S: Stench
12
Wumpus world example
A: agent
OK: safe square
B: Breeze
P: Pit
S: Stench
13
Wumpus world example
A: agent
OK: safe square
B: Breeze
P: Pit
S: Stench
14
Wumpus world example
A: agent
OK: safe square
B: Breeze
P: Pit
S: Stench
15
Wumpus world example
A: agent
OK: safe square
B: Breeze
P: Pit
S: Stench
16
Logic & Language
Logic includes formal languages for representing informationsuch that conclusions can be drawn
Syntax defines the well-formed sentences in a language
Semantics define the "meaning" of sentences i.e., define truth of a sentence with respect to each possible world
We will introduce Propositional Logic in this lecture It talks about facts
Propositions can be true, false, or unknown
17
Propositional logic: Syntax
Propositional logic is the simplest logic
To illustrate basic ideas about logic & reasoning
The proposition symbols π,π,β¦ are sentences.
If π is a sentence,π is a sentence (negation)
If π and π are sentences,ππ is a sentence (conjunction)
If π and π are sentences,ππ is a sentence (disjunction)
If π and π are sentences,π π is a sentence (implication)
If π and π are sentences,π π is a sentence (biconditional)
18
Propositional logic (Grammar)
19
ππππ‘ππππ β π΄π‘ππππππππ‘ππππ | πΆππππππ₯ππππ‘πππππ΄π‘ππππππππ‘ππππ β πππ’π πΉπππ π π π π | β¦πΆππππππ₯ππππ‘ππππ β (ππππ‘ππππ)
| Β¬ππππ‘ππππ| ππππ‘ππππ β§ ππππ‘ππππ| ππππ‘ππππ β¨ ππππ‘ππππ| ππππ‘ππππ βΉ ππππ‘ππππ| ππππ‘ππππ β ππππ‘ππππ
ππππππππππ: Β¬, β§, β¨, βΉ, β
Truth tables for connectives
20
Propositional logic: Semantics
21
Each model specifies true/false of each proposition symbol
e.g., Propistion symbols:π1,2, π2,2, π3,1
8 possible models
π1 = {π1,2 = ππππ π, π2,2 = ππππ π, π3,1 = π‘ππ’π}
Semantics of a complex sentence in any model m:
π is true iff π is false in π.
ππ is true iff π is true and π is true in π.
π β¨ π is true iff π is true or π is true in π.
π βΉ π is true unless π is true and π is false in π.
π βΊ π is true iff π and π are both true or both false in π.
Wumpus world sentences
ππ,π is true if there is a pit in [π, π].
ππ,π is true if there is a wumpus in [π, π], dead or alive.
π΅π,π is true if the agent perceives a breeze in [π, π].
ππ,π is true if the agent perceives a stench in [π, π].
General rules (only related ones to the current agent position) π 1: π1,1 (no pit in [1,1])
π 2: π΅1,1 β π1,2 β¨ π2,1 (Pits cause breezes in adjacent squares)
π 3: π΅2,1 β π1,1 β¨ π2,2 β¨ π3,1 (Pits cause breezes in adjacent squares)
Perception
π 4: π΅1,1 π 5: π΅2,1
22
Models
Models are mathematical abstraction of possible worlds Each model fixes the truth or falsehood of every relevant sentence.
We can consider a set for each logical sentence that specifies
all possible worlds in which πΌ is true
π(πΌ): set of all models of the sentence πΌ (all satisfying πΌ) π β π(πΌ) if πΌ is true in model π E.g., For πΌ: π₯ + π¦ = 4, all possible assignments of values to π₯, π¦ are
models.π(πΌ) contains a subset of models satisfying π₯ + π¦ = 4.
23
Wumpus models
24
Agents starts at [1,1] with no active sensor,
then moves to [2,1] and senses Breeze in it
Possible models for pits in [1,2], [2,2], [3,1]:
Logical reasoning: entailment
25
πΌ β¨ π½ (entailment):πΌ entails π½ πΌ β π½ is a tautology or valid
A sentence is valid or tautology if it is Tππ’π in all models (e.g., ππ,
(π(ππ))π)
π½ logically follows from πΌ or πΌ is stronger than π½
πΌ β¨ π½ iff π πΌ π π½ πΌ entails π½ iff π½ is true in all worlds where πΌ is true
Entailment
πΎπ΅ β¨ πΌ iff π(πΎπ΅) β π(πΌ)
Example: KB=βA is redβ & βB is blueβ
Ξ± = βA is redβ
26
Entailment in the wumpus world
πΎπ΅ (before perception): rules of the
wumpus world
Perception: After detecting nothing in
[1,1], moving right, a breeze in [1,2]
27
Wumpus models
28
Possible models for pits in [1,2], [2,2], [3,1]
Consider possible models for only
pits in neighboring squares 23 = 8possible models
Wumpus models
πΎπ΅ = wumpus-world rules + perceptions
29
Wumpus models
πΎπ΅ = wumpus-world rules + perceptions
πΌ1 = "[1,2] is safe"
π(πΎπ΅) β π(πΌ1) β πΎπ΅ β¨ πΌ1
30
Wumpus models
KB = wumpus-world rules + perceptions
31
Wumpus models
πΎπ΅ = wumpus-world rules + perceptions
πΌ2 = "[2,2] is safe",πΎπ΅ β πΌ2
32
Inference
πΎπ΅βππΌ:πΌ can be derived from πΎπ΅ by an inference algorithm π
An inference algorithm π is: sound if whenever πΎπ΅βππΌ, it is also true that πΎπ΅ β¨ πΌ
all provable statements by this algorithm are true
complete if whenever πΎπ΅ β¨ πΌ, it is also true that πΎπ΅βππΌ all true statements are provable by this algorithm
33
Inference methods
Inference methods can be categorized into: Model checking (enumerating models)
truth table enumeration (always exponential in π)
improved backtracking, e.g., Davis-Putnam-Logemann-Loveland (DPLL)
heuristic search in model space (sound but incomplete)
e.g., min-conflicts-like hill-climbing algorithms
Theorem proving (searching proofs by applying inference rules)
Applying a sequence of inference rules on KB to find the desired sentence
Legitimate (sound) generation of new sentences from old ones.
34
Truth tables for inference
35
πΎπ΅ β¨ πΌ?
Enumerate the models
βIs πΌ true in every model in which KB is true?β
π 1: π1,1π 2: π΅1,1 β π1,2 β¨ π2,1π 3: π΅2,1 β π1,1 β¨ π2,2 β¨ π3,1π 4: π΅1,1π 5: π΅2,1
KB
Entailment by enumeration (model checking)
36
function ππ_πΈπππ΄πΌπΏπ? (πΎπ΅, πΌ) returns π‘ππ’π or ππππ π
inputs: πΎπ΅, the knowledge base, a sentence in propositional logic
πΌ, the query, a sentence in propositional logic
π π¦πππππ β a list of the proposition symbols in πΎπ΅ and πΌ
return ππ_πΆπ»πΈπΆπΎ_π΄πΏπΏ(πΎπ΅, πΌ , π π¦πππππ , {})
function ππ_πΆπ»πΈπΆπΎ_π΄πΏπΏ(πΎπ΅, πΌ, π π¦πππππ , πππππ) returns π‘ππ’π or ππππ π
if πΈππππ? ( π π¦πππππ ) then
if ππΏ_ππ ππΈ? (πΎπ΅,πππππ) then return ππΏ_ππ ππΈ? (πΌ,πππππ)
else return π‘ππ’π
else
π β πΉπΌπ ππ(π π¦πππππ )
πππ π‘ β π πΈππ(π π¦πππππ )
return ππ_πΆπ»πΈπΆπΎ_π΄πΏπΏ(πΎπ΅, πΌ, πππ π‘,πππππβ{π = π‘ππ’π}) and
ππ_πΆπ»πΈπΆπΎ_π΄πΏπΏ(πΎπ΅, πΌ, πππ π‘,πππππβ{π = ππππ π})
Entailment by enumeration: properties
37
Depth-first enumeration of all models is sound and complete
(when finite models).
For π symbols, time complexity is π(2π), space complexity is
π(π).
Satisfiability
38
A sentence is satisfiable if it is true in some models
e.g.,ππ
Determining the satisfiability of sentences in propositional
logic (SAT problem) is NP-complete
Satisfiability and validity are connected.
πΌ is valid or tautology iff Β¬πΌ is unsatisfiable.
πΌ is satisfiable iff Β¬πΌ is not valid.
πΌ β¨ π½ iff (πΌ β§ Β¬π½) is unsatisfiable
Proof by contradiction
The DPLL (Davis, Putnam, Logemann,
Loveland) algorithm
39
Improvements over truth table enumeration (simple DFS): Early termination
A clause is true if any literal is true.
A sentence is false if any clause is false.
Pure symbol heuristic
Pure symbol: always appears with the same "sign" in all clauses.
e.g., In (π΄π΅) β§ (Β¬π΅ πΆ) β§ (πΆπ΄),π΄ and π΅ are pure,πΆ is impure.
Make a pure symbol literal true.
In determining purity, we can ignore clauses already known to be true
Unit clause heuristic
Unit clause: all literals except to one already assigned false by model.
The only literal in a unit clause will be assigned true.
Many tricks can be used to enable SAT solvers to scale up.
The DPLL algorithm
40
Determining satisfiability of an input propositional logic sentence (in CNF)
Entailment by using inference rule
41
Propositional theorem proving
Searching proofs can be more efficient than model
checking.
Can ignore irrelevant propositions.
When the number of models is large but the length of proof is
short, entailment by theorem proving is useful.
Inference rules
42
Some samples:
Modus Ponens:Ξ±βΞ², Ξ±
Ξ²
And elimination:πΌβ§π½
π½
Biconditional elimination:πΌβπ½
(πΌβπ½)β§(π½βπΌ)
Valid implication
43
Deduction theorem: πΌ β¨ π½ iff πΌ β π½ is valid.
Every valid implication describes a legitimate inference.
Modus Ponens:Ξ±βΞ², Ξ±
Ξ²is equivalent to valid implication
Ξ± β Ξ² β§ Ξ± β Ξ² is valid
All logical entailments can be used as inference rules.
Logical equivalence
Using logical equivalence we can find many inference rules
Two sentences are logically equivalent iff they are true in the
same models
πΌ β‘ π½ iff πΌ β¨ π½ and π½ β¨ πΌ
44
Example of inference
45
π 1: π1,1
π 2: π΅1,1 β π1,2 β¨ π2,1
π 3: π΅2,1 β π1,1 β¨ π2,2 β¨ π3,1 π 4: π΅1,1 π 5: π΅2,1
Can we infer from π 1 through π 5 to prove π1,2?
π 6: π΅1,1 β π1,2 β¨ π2,1 β§ π1,2 β¨ π2,1 β π΅1,1 [biconditional elim. to π 2]
π 7: π1,2 β¨ π2,1 β π΅1,1 [and elim. to π 6]
π 8: Β¬π΅1,1 β Β¬ π1,2 β¨ π2,1 [(π β π) β‘ (Β¬π β Β¬π)]
π 9: Β¬ π1,2 β¨ π2,1 [Modus Ponens with π 8 and π 4]
π 10: Β¬π1,2 β§ Β¬π2,1 [De Morganβ rule]
Finding a proof as a search problem
46
πΌππΌππΌπ΄πΏ_πππ΄ππΈ: the initial knowledge base
π΄πΆππΌπππ: all inference rules applied to all the sentences
that match their top line
π πΈπππΏπ: add the sentence in the bottom line of the used
inference rule to the current ones
πΊππ΄πΏ: a state containing the sentence we are trying to
prove.
Monotonicity property
47
The set of entailed sentences can only grow as
information is added to πΎπ΅.
Inference rules can be applied where premises are found in KB
(regardless of what else is in KB)
If πΎπ΅ β¨ πΌ then πΎπ΅ β§ π½ β¨ πΌ (for any πΌ and π½)
The assertion π½ can not invalidate any conclusion πΌ already
inferred.
Resolution
48
Inference rules are sound.
Is a set of rules complete?
Are the available inference rules adequate?
Resolution: a single inference rule that yields a complete
inference algorithm (when couples with any complete
search algorithm).
Resolution
49
Unit resolution: ππ and π are complementary literals
π1 β¨ π2 β¨ β―β¨ ππ , π
π1 β¨ β¦β¨ ππβ1 β¨ ππ+1 β¨ β―β¨ ππ
Full resolution rule: ππ and ππ are complementary literals
π1 β¨ π2 β¨ β―β¨ ππ , π1β¨π2 β¨ β―β¨ππ
π1 β¨ β¦β¨ ππβ1 β¨ ππ+1 β¨ β―β¨ ππ β¨ π1 β¨ β¦β¨ππβ1 β¨ ππ+1 β¨ β―β¨ππ
It resolution rule sound.Why?
A resolution-based theorem prover can (for any sentences πΌ and π½ in
propositional logic) decide whether πΌ β¨ π½ Is πΌ β§ Β¬π½ unsatisfiable?
CNF (Conjunctive Normal Form)
CNF Grammar
50
πΆππΉππππ‘ππππ β πΆπππ’π π1 β§ β―β§ πΆπππ’π ππ
πΆπππ’π π β πΏππ‘ππππ1 β¨ β―β¨ πΏππ‘πππππ
πΏππ‘ππππ β ππ¦ππππ | Β¬ππ¦ππππ
ππ¦ππππ β π π π | β¦
Example of resolution
51
π1,1 β¨ π3,1, Β¬π1,1β¨ Β¬π2,2π3,1 β¨ Β¬π2,2
Resolution algorithm
To show πΎπ΅ β¨ πΌ, we show πΎπ΅ β§ Β¬πΌ is unsatisfiable
52
function PL-RESOLUTION(KB,Ξ±) returns π‘ππ’π or ππππ πinput: πΎπ΅, the knowledge base (a sentence in propositional logic)
πΌ, the query (a sentence in propositional logic)
ππππ’π ππ β the set of clauses in the CNF representation of πΎπ΅Β¬πΌπππ€ β {}
loop do
for each pair of clauses πΆπ , πΆπ in ππππ’π ππ do
πππ πππ£πππ‘π β ππΏ_π πΈπππΏππΈ (πΆπ, πΆπ)
if πππ πππ£πππ‘π contains the empty clause then return π‘ππ’π
πππ€ β πππ€ βͺ πππ πππ£πππ‘π
if πππ€ β πΆπππ’π ππ then return ππππ π
ππππ’π ππ β ππππ’π ππ βͺ πππ€
Resolution algorithm
To show πΎπ΅ β¨ πΌ, we show πΎπ΅ β§ Β¬πΌ is unsatisfiable
53
function PL-RESOLUTION(KB,Ξ±) returns π‘ππ’π or ππππ πinput: πΎπ΅, the knowledge base (a sentence in propositional logic)
πΌ, the query (a sentence in propositional logic)
ππππ’π ππ β the set of clauses in the CNF representation of πΎπ΅Β¬πΌπππ€ β {}
loop do
for each pair of clauses πΆπ , πΆπ in ππππ’π ππ do
πππ πππ£πππ‘π β ππΏ_π πΈπππΏππΈ (πΆπ, πΆπ)
if πππ πππ£πππ‘π contains the empty clause then return π‘ππ’π
πππ€ β πππ€ βͺ πππ πππ£πππ‘π
if πππ€ β πΆπππ’π ππ then return ππππ π
ππππ’π ππ β ππππ’π ππ βͺ πππ€
Each pair containing complementary literals is resolved and the
resulted clause is added to the set if it is not already present.
The process continues until one of these happens:
- No new clauses (πΎπ΅ β πΌ)
- Two clauses resolve to yield the empty clause (πΎπ΅ β¨ πΌ)
Sound and complete inference algorithm
Resolution example
πΎπ΅ = π΅1,1 π1,2 π2,1 π΅1,1
πΌ = Β¬π1,2
Convert πΎπ΅ β§ Β¬πΌ into CNF(π΅1,1 π1,2 π2,1) (π1,2 π΅1,1) (π2,1 π΅1,1)(π΅1,1)π1,2
54
Contradiction
OK?
Completeness of resolution
55
Resolution closure π πΆ(π) : a set of clauses derivable by
repeated application of resolution rule to clauses in π or their
derivatives.
π πΆ(π) is finite. Thus ππΏ_onstructed out of π1, π2, β¦ , ππ that appear in
π. π πΈπππΏπππΌππ terminates.
Finite distinct clauses can be c
Ground resolution theorem: If a set of clauses π is unsatisfiable
then π πΆ(π) contains the empty clause.
If π πΆ(π) does not contain the empty clause, we can find an assignment of
values to the symbols that satisfies π πΆ(π) and thus π
Horn clauses & definite clauses
56
Some real-world KBs satisfy restrictions on the form of
sentences
We can use more restricted and efficient inference algorithms
Definite clause: a disjunction of literals of which exactly one is
positive literal.
Β¬π1 β¨ Β¬π2 β¨β¦β¨ Β¬ππ β¨ ππ+1 β (π1 β§ π2 β§β¦ β§ ππ) β ππ+1
Horn clause: a disjunction of literals of which at most one is
positive literal.
Closed under resolution
Grammar
57
ππ¦ππππ β π π π | β¦
π»ππππΆπππ’π π β π·ππππππ‘ππΆπππ’π π | πΊππππΆπππ’π π
π·ππππππ‘ππΆπππ’π π β ππ¦ππππ1 β§ β―β§ ππ¦πππππ β ππ¦ππππ
πΊππππΆπππ’π π β (ππ¦ππππ1 β§β―β§ ππ¦πππππ) β πΉπππ π
Forward and backward chaining
Inference with Horn clauses can be done through forward
chaining or backward chaining.
These algorithms are very natural and run in time that is linear
in the size of KB.
They are bases of logic programming Prolog: backward chaining
58
Forward chaining
Repeated application of Modus Ponens until reaching goal
Fire any rule whose premises are satisfied in the KB
add its conclusion to the KB, until query is found
59
Arcs show
conjunctions
Forward chaining algorithm
60
function ππΏ_πΉπΆ_πΈπππ΄πΌπΏπ? (πΎπ΅, π) returns π‘ππ’π or ππππ π
inputs:πΎπ΅, the knowledge base (a set of propositional definite clauses)
π, the query (a propositional symbol)
πππ’ππ‘ β a table where πππ’ππ‘[π] is the number of symbols in πβs premise
ππππππππ β a table, where ππππππππ[π ] is initially false for all symbols
ππππππ β a queue of symbols, initially symbols known to be true in πΎπ΅
while ππππππ β {} doπ β πππ(ππππππ)
if π = π then return π‘ππ’π
if ππππππππ[π] = ππππ π thenππππππππ[π] β π‘ππ’π
for each clause π in πΎπ΅ where π is in π. ππ πΈππΌππΈ do
πππ’ππ‘[π]= πππ’ππ‘[π]-1
if πππ’ππ‘[π] = 0 then add π. πΆπππΆπΏπππΌππ to ππππππ
return false
Begin with known
facts (positive
literals) in KB
The process continues
until q is added or no
further inference
Forward chaining: example
61
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
62
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
63
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
64
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
65
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
66
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
67
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining: example
68
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward chaining
69
Assumption: KB of definite clauses
Sound?
Every inference is an application of Modus Ponens.
Complete?
Every entailed atomic sentence will be derived?
Humans keep forward chaining under careful control (to
avoid irrelevant consequences)
Proof of completeness
1. Consider the final state of the inferred table (fixed point
where no new atomic sentences are derived)
2. Consider the final state as a model π, assigning π‘ππ’π to
inferred symbols and ππππ π to others
3. Every definite clause in the original KB is true in πIf βπ1 β§ β―β§ ππ β πβ is false then π1 β§ β―β§ ππ is true and π is false. But it
contradicts with reaching a fixed point.
4. Thus,π is a model of KB
5. If πΎπ΅ β¨ π, atomic sentence π is true in every model of KB,
including π
70
Backward chaining
71
Idea: work backwards from the query π:
To prove π:if π is known to be true then
no work is needed
else
Find those rule concluding π
If all premises of one of them can be proved (by backward chaining)then π is true
Avoid loops: check if new subgoal is already on the goal stack
Avoid repeated work: check if new subgoal has already been
proved true or failed
Backward chaining example
72
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
73
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
74
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
75
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
76
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
77
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
78
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
79
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
80
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Backward chaining example
81
π β π
πΏ β§ π β π
π΅ β§ πΏ β π
π΄ β§ π β πΏ
π΄ β§ π΅ β πΏ
π΄
π΅
Forward vs. backward chaining
Forward chaining is data-driven, automatic, unconsciousprocessing
May do lots of work that is irrelevant to the goal
Backward chaining is goal-driven, appropriate for problem-solving
Only relevant facts are considered.
Complexity of BC can be much less than linear in size of KB
82
Efficient propositional inference
Efficient algorithms for general propositional inference:
We introduced DPLL algorithm as a systematic search method
Backtracking search algorithms
Now, we will introduceWalkSAT algorithm as a local search algorithm
83
WalkSAT
It tests entailment πΎπ΅ β¨ πΌ by testing unsatisfiability of πΎπ΅β§ Β¬πΌ.
Local search algorithms (hill climbing, SA,β¦)
state space: complete assignments
evaluation function: number of unsatisfied clauses
WalkSat
Simple & effective
Balance between greediness and randomness
To escape from local minima
84
WalkSAT
85
If max _πππππ = β and π > 0, WalkSAT will find a model (if any exists)
If max _πππππ = β, it never terminates for unsatisfiable sentences.
WalkSAT
86
Failure may mean:
1) The sentence is unsatisfiable.
2) The algorithm needs more time.
It can not detect unsatisfiability.
Cannot be reliably used for deciding entailment.
Random SAT problems
Under-constrained problems: relatively few clauses
A large portion of possible assignments are solutions
e.g., (π·π΅πΆ) β§ (π΅π΄πΆ) β§ (Β¬πΆπ΅πΈ) β§ (πΈπ·π΅) β§ (π΅πΈπΆ)
Over-constrained problems: many clauses relative to the
number of variables
Likely to have no solution
87
Hard satisfiability problems
88
π = number of clauses
π = number of symbols
m/n = 4.3 (critical point)
3 β πΆππΉπ = 50
Hard problems around
critical point
Wumpus world example:
propositional symbols
89
Atemporal variables that may be needed
ππ,π is true if there is a pit in [π, π].
ππ,π is true if there is a wumpus in [π, π], dead or alive.
π΅π,π is true if the agent perceives a breeze in [π, π].
ππ,π is true if the agent perceives a stench in [π, π].
Some of the temporal variables that may be needed Variables for percepts and actions
πΏπ,ππ‘ , πΉππππππΈππ π‘π‘ , πΉππππππππ π‘π‘ , πΉπππππππππ‘βπ‘ , πΉππππππππ’π‘βπ‘
π»ππ£ππ΄ππππ€π‘
ππ’πππ’π π΄πππ£ππ‘
Wumpus world example:
axioms on atemporal aspect of the world
90
Axioms: general knowledge about how the world works
General rules on atemporal variables
π1,1
π1,1
π΅π₯,π¦ (ππ₯,π¦+1 ππ₯,π¦β1 ππ₯+1,π¦ ππ₯β1,π¦)
ππ₯,π¦ (ππ₯,π¦+1 ππ₯,π¦β1 ππ₯+1,π¦ ππ₯β1,π¦)
π1,1 π1,2 β¦ π4,4
π1,1 π1,2
π1,1 π1,3
β¦
A distinct rule is needed
for each square
Wumpus world example:
perceptions & actions
91
Perceptions are converted to facts and are Telled to KB
ππ΄πΎπΈ_ππΈπ πΆπΈππ_ππΈπππΈππΆπΈ π΅ππππ§π, ππ‘πππβ, ππππ, ππππ, ππππ , π‘
π΅ππππ§ππ‘, ππ‘πππβπ‘ are added to KB
Selected action is converted to a fact and is Telled to KB
ππ΄πΎπΈ_π΄πΆππΌππ_ππΈπππΈππΆπΈ πΉπππ€πππ, π‘
πΉπππ€ππππ‘ is added to KB
Wumpus world example: transition model
92
Changing aspect of world (Temporal variables)
They are also called as fluents or state variables
Initial KB includes the initial status of some of the temporal variables:
πΏ1,10 , πΉππππππΈππ π‘0, π»ππ£ππ΄ππππ€0,ππ’πππ’π π΄πππ£π0
Percepts are connected to fluents describing the properties of
squares
πΏπ₯,π¦π‘ β (π΅ππππ§ππ‘ β π΅π₯,π¦)
πΏπ₯,π¦π‘ β ππ‘πππβπ‘ β ππ₯,π¦
β¦
Transition model: fluents can change as the results of agentβs actions
Wumpus world example: transition model
Frame problem
93
Transition model:
πΏ1,10 β§ πΉππππππΈππ π‘0 β§ πΉπππ€πππ0 β (πΏ2,1
1 β§ Β¬πΏ1,11 )
πΏ1,10 β§ πΉππππππΈππ π‘0 β§ ππ’πππΏπππ‘0 β (πΏ1,1
1 β§ πΉπππππππππ‘β1 β§ Β¬πΉππππππΈππ π‘1)
β¦
After doing πΉπππ€πππ action at time 0:
π΄ππΎ πΎπ΅,π»ππ£ππ΄ππππ€1 = ππππ π
Why?
Frame problem: representing the effects of actions without having to
represent explicitly a large number of intuitively obvious non-effects
Wumpus world exampleSolution to frame problem: successor-state axioms
94
A Solution to frame problem: Successor-state axioms
Instead of writing axioms about actions, we write axioms
about fluents
πΉπ‘+1 β π΄ππ‘ππππΆππ’π ππ πΉπ‘ β¨ (πΉπ‘ β§ Β¬π΄ππ‘ππππΆππ’π ππ πππ‘πΉπ‘)
π»ππ£ππ΄ππππ€π‘+1 β π»ππ£ππ΄ππππ€π‘ β§ Β¬πβπππ‘π‘
πΏ1,1π‘+1 β (πΏ1,1
π‘ β§ (Β¬πΉπππ€ππππ‘ β¨ π΅π’πππ‘+1))
β¨ (πΏ1,2π‘ β§ (πΉππππππππ’π‘βπ‘ β§ πΉπππ€ππππ‘))
β¨ (πΏ2,1π‘ β§ (πΉππππππππ π‘π‘ β§ πΉπππ€ππππ‘))
A hybrid agent for the wumpus world
95
Basic concepts:
Safeness of [π₯, π¦] square:
ππΎπ₯,π¦π‘ β Β¬ππ₯,π¦ β§ Β¬(ππ₯,π¦ β§ ππ’πππ’π π΄πππ£ππ‘)
Set of safe squares:
π πππ β π₯, π¦ : π΄ππΎ πΎπ΅, ππΎπ₯,π¦π‘ = π‘ππ’π
Set of unvisited squares:
π’ππ£ππ ππ‘ππ β π₯, π¦ : π΄ππΎ πΎπ΅, πΏπ₯,π¦π‘β² = ππππ π πππ πππ π‘β² β€ π‘
96
function π»ππ΅π πΌπ·_ππππππ_π΄πΊπΈππ(πππππππ‘) returns an action sequence
inputs: πππππππ‘, a list, πππππ§π, π π‘πππβ, ππππ‘π‘ππ, ππ’ππ, π πππππ
persistent: πΎπ΅, initially the atemporal βwumpus physicsβ
π‘, a time counter, initially 0
ππππ, an action sequence, initially empty
TELL(πΎπ΅,ππ΄πΎπΈ_ππΈπ πΆπΈππ_ππΈπππΈππΆπΈ πππππππ‘, π‘ )
TELL the πΎπ΅ the temporal βphysicsβ sentences for time π‘
π πππ β π₯, π¦ : π΄ππΎ πΎπ΅,ππΎπ₯,π¦π‘ = π‘ππ’π
if π΄ππΎ(πΎπ΅, πΊπππ‘π‘πππ‘) = π‘ππ’π then
ππππ β πΊπππ + ππΏπ΄π_π ππππΈ ππ’πππππ‘, 1,1 , π πππ + [πΆππππ]
if ππππ = {} then
π’ππ£ππ ππ‘ππ β π₯, π¦ : π΄ππΎ πΎπ΅, πΏπ₯,π¦π‘β² = ππππ π πππ πππ π‘β² β€ π‘
ππππ β ππΏπ΄π_π ππππΈ(ππ’πππππ‘, π’ππ£ππ ππ‘ππ β© π πππ, π πππ)
if ππππ = {} and π΄ππΎ πΎπ΅,π»ππ£ππ΄ππππ€π‘ = π‘ππ’π then
πππ π ππππ_π€π’πππ’π β π₯, π¦ : π΄ππΎ πΎπ΅,Β¬ππ₯,π¦ = ππππ π
ππππ β ππΏπ΄π_ππ»ππ(ππ’πππππ‘, πππ π ππππ_π€π’πππ’π , π πππ)
if ππππ = {} then
πππ‘_π’ππ πππ β π₯, π¦ : π΄ππΎ πΎπ΅,Β¬ππΎπ₯,π¦π‘ = ππππ π
ππππ β ππΏπ΄π_π ππππΈ(ππ’πππππ‘, π’ππ£ππ ππ‘ππ β© πππ‘_π’ππ πππ, π πππ)
if ππππ = {} then
ππππ β ππΏπ΄π_π ππππΈ ππ’πππππ‘, 1,1 , π πππ + [πΆππππ]
action β πππ(ππππ)
ππΈπΏπΏ(πΎπ΅,ππ΄πΎπΈ_π΄πΆππΌππ_ππΈπππΈππΆπΈ(πππ‘πππ, π‘))
π‘ β π‘ + 1
return πππ‘πππ
Hybrid agent (phases listed based on priority)
97
Simple-reflex (condition-action rule):
if π΄ππΎ(πΎπ΅, πΊπππ‘π‘πππ‘) = π‘ππ’π then
ππππ β [πΊπππ] + ππΏπ΄π_π ππππΈ ππ’πππππ‘, 1,1 , π πππ + [πΆππππ]
Goal-based & reasoning:
if ππππ = {} then
π’ππ£ππ ππ‘ππ β π₯, π¦ : π΄ππΎ πΎπ΅, πΏπ₯,π¦π‘β² = ππππ π πππ πππ π‘β² β€ π‘
ππππ β ππΏπ΄π_π ππππΈ(ππ’πππππ‘, π’ππ£ππ ππ‘ππ β© π πππ, π πππ)
if ππππ = {} and π΄ππΎ πΎπ΅,π»ππ£ππ΄ππππ€π‘ = π‘ππ’π then
πππ π ππππ_π€π’πππ’π β π₯, π¦ : π΄ππΎ πΎπ΅,Β¬ππ₯,π¦ = ππππ π
ππππ β ππΏπ΄π_ππ»ππ(ππ’πππππ‘, πππ π ππππ_π€π’πππ’π , π πππ)
Hybrid agent (phases listed based on priority)
98
Exploration & reasoning
if ππππ = {} then
πππ‘_π’ππ πππ β π₯, π¦ : π΄ππΎ πΎπ΅,Β¬ππΎπ₯,π¦π‘ = ππππ π
ππππ β ππΏπ΄π_π ππππΈ(ππ’πππππ‘, π’ππ£ππ ππ‘ππ β© πππ‘_π’ππ πππ, π πππ)
Goal-based:
if ππππ = {} thenππππ β ππΏπ΄π_π ππππΈ ππ’πππππ‘, 1,1 , π πππ + [πΆππππ]
Hybrid agent: Using A* to make a plan
99
function ππΏπ΄π_π ππππΈ(ππ’πππππ‘, ππππ, πππππ€ππ) returns an action sequence
inputs: ππ’πππππ‘, the agentβs current position
πππππ , a set of squares; try to plan a route to one of them
πππππ€ππ, a set of squares that can form part of the route
πππππππ β π ππππΈ_ππ ππ΅πΏπΈπ(ππ’πππππ‘, πππππ , πππππ€ππ)return π΄β_πΊπ π΄ππ»_ππΈπ΄π πΆπ»(πππππππ)
Logical state estimation
100
Problem: Computational time of ASK grows by the length ofthe agentβs life We must save the results of inference to use them in the next time steps
Solution: Belief state instead of the whole past history ofpercepts and actions
Belief state: the agentβs knowledge about the state of the world
(the set of all possible current states of the world)
π΅1,2 β§ (π1,3 β¨ π2,2)
β§ πΏ1,21 β§ πΉπππππππππ‘β1 β§ π»ππ£ππ΄ππππ€1 β§ππ’πππ’π π΄πππ£π1
State estimation: process of updating the belief state when doingactions and receiving new percepts
Approximate state estimation
101
Problem: The exact belief state estimation may require logical
formulas whose size is exponential in the number of symbols.
π fluent symbols, 2π possible physical states, 22π
possible belief states
Solution: approximation
e.g., 1-CNF belief state: given the belief state at π‘ β 1, agent tries to
prove ππ‘ or Β¬ππ‘ for each ππ‘)
Approximate belief state estimation?
compact representation (open area for research)
Relation to model-based agents
Wumpus world
Distinct rules βfor each square [x,y]β and βfor each time tβ
πΏπ₯,π¦π‘ β§ πΉππππππΈππ π‘π‘ β§ ππ’πππΏπππ‘π‘ β (πΏπ₯,π¦
π‘+1 β§ πΉππππππππ‘+1 β§ Β¬πΉππππππΈππ π‘π‘+1)
Many rules are needed to describe the world axioms
Rather impractical for bigger world
We can not express general knowledge about βphysicsβ of the worlddirectly in propositional language
More expressive language is needed.
Qualification problem: specifying all the exceptions
Probability theory allows us to summarize all the exceptions (withoutexplicitly naming them)
Limitations of propositional logic
102