Você está na página 1de 99

Chapter 7

Logical Agents

Computer Science & Engineering Discipline


Khulna University

SarfaRaaaZ NewaZ
Knowledge-Based Agent

sensors

?
environment
agent

actuators Domain-specific content

Knowledge base
Inference Engine
Domain-independent algorithms 2
The Wumpus World
 The Wumpus computer game
 The agent explores a cave consisting of rooms
connected by passageways.
 Lurking somewhere in the cave is the
Wumpus, a beast that eats any agent that
enters its room.
 Some rooms contain bottomless pits that trap
any agent that wanders into the room.
 Occasionally, there is a heap of gold in a
room.
 The goal is to collect the gold and exit the
3
world without being eaten
Wumpus PEAS description
 Performance measure:
gold +1000, death -1000,
-1 per step, -10 use arrow
 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
 Sensors: Breeze, Glitter, Smell, Bump, Scream
 Actuators: Left turn, Right turn, Forward, Grab, Release,
Shoot 4
A typical Wumpus world

 The agent
always starts in
the field [1,1].
 The task of the
agent is to find
the gold, return
to the field
[1,1] and climb
out of the cave.

5
Wumpus World Characteristics
 Observable?
 Deterministic?
 Static?
 Discrete?

6
Wumpus world characterization
 Fully Observable No
– only local perception
 Deterministic Yes
– outcomes exactly specified
 Static Yes
– Wumpus and Pits do not move
 Discrete Yes
– none of the percepts and actions is continuous
with respect to time
7
The Wumpus agent’s first step

8
Later

9
Logics

Logics are formal languages for representing


information such that conclusions can be drawn
 Syntax: defines the sentences in the language
 Semantics: define the “meaning” of sentences: i.e.,
define truth of a sentence in a world

 Example: the language of arithmetic


 x+2 > y is a sentence; x2+y > is not a sentence.
 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
10
Entailment
 Entailment means that one thing follows
from another, written KB 
 A knowledge base KB entails sentence  if
and only if  is true in all worlds where KB is
true.
 E.g., the KB containing “the Giants won” and
“the Reds won” entails “Either the Giants won or
the Reds won”
 E.g., x+y = 4 entails x+y >= 4
11
Models
 Models are formal definitions of possible
states of the world
 We say m is a model of a sentence  if  is
true in m
 M() is the set of all models of 
 Then KB  if and only if M(KB)  M()

M()

M(KB) 12
Entailment in the wumpus world
Situation after detecting
nothing in [1,1],
moving right, breeze in
[2,1]
Consider possible models
for KB assuming only
pits
3 Boolean choices  8
possible models
13
Wumpus models

14
Wumpus models

 KB = wumpus-world rules + observations


15
Wumpus models

 KB = wumpus-world rules + observations


 α1 = "[1,2] is safe", KB ╞ α1, proved by model16
checking
Wumpus models

 KB = wumpus-world rules + observations


 α2 = "[2,2] is safe", KB ╞ α2 17
Inference
 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

18
Propositional logic: Syntax
 Propositional logic is the simplest logic.
 Connectives:
  (negation)
  (conjunction)
  (disjunction)
  (implication)
  (biconditional)
 Propositional symbols, e.g., P, Q, R, …
 Each Propositional symbols represent a
proposition. It’s value is either true or false. E.g.,
W1,3 stands for “wumpus in square [1,3]”.
 True, False 19
Syntax of PL
 sentence  atomic sentence | complex sentence
 atomic sentence  Propositional symbol | True
\ False
 complex sentence  sentence
| (sentence  sentence)
| (sentence  sentence)
| (sentence  sentence)
| (sentence  sentence)

20
Syntax of PL
 Examples:
 ((P  Q)  R)
 (A  B)  (C)

 Order of Precedence :     
Examples:
  A  B  C is equivalent to ((A)B)C

 Literal: An atomic sentence (positive literal) or


a negated atomic sentence (negative literal).

21
Models in Propositional Logic
 Each model specifies truth value
(true/false) for each proposition symbol.
 Examples:
 Let A, B, C, and D be the propositional symbols
 is m = {A=true, B=false, C=false, D=true} a model?
 is m’ = {A=true, B=false, C=false} a model?
 How many models can be defined over n
propositional symbols?

22
Semantics of PL
 It specifies how to determine the truth
value of any sentence in a model m
 The truth value of True is True
 The truth value of False is False
 The truth value of each atomic sentence
is given by m
 The truth value of every other sentence is
obtained recursively by using truth tables

23
Truth Tables

24
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].
R1: P1,1
R2: B1,1
R3: B2,1
 "Pits cause breezes in adjacent squares"
R4: B1,1  (P1,2  P2,1)
R5: B2,1  (P1,1  P2,2  P3,1)

Knowledge Base, KB = R1  R2  R3  R4  R5

25
Be Careful to Write Rules
 Why we write the rule as follows ?
R4: B1,1  (P1,2  P2,1)
 Why we don’t write the rule as follows ?
R4: B1,1  (P1,2  P2,1)
 Read A  B as:
“If A IS True, then I claim that B is True,
otherwise I make no claim.”

26
Example

Model: A=True, B=False, C=False, D=True

(A  B  C)  D  A
F
F
T T

27
Model of a KB
 Let KB be a set of sentences
KB = R1  R2  R3  R4  R5
 A model m is a model of KB iff it is a
model of all sentences in KB, that is,
all sentences in KB are true in m
 There are 7 proposition variables, so total
number of possible models are 27 = 128.
Among these 128 possible models, KB is
true only in 3 models.
28
Truth tables for inference

29
Inference by enumeration
 Depth-first enumeration of all models is
sound and complete.
 For n symbols, time complexity is O(2n),
space complexity is O(n).
 Can we conclude KB entails P1,2 ?
 Can we conclude KB entails P2,2 ?

30
Logical equivalence
 Two sentences are logically equivalent iff true in same
models: α ≡ β iff α╞ β and β╞ α

31
Validity and satisfiability
 A sentence is valid if it is true in all models,
 e.g., True, A A, A  A
 Validity is connected to inference via the Deduction
Theorem: for any  and β,  |= β iff (  β) is valid
 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
 Remember proof by contradiction.
32
Validity and satisfiability
 Relations between validity and
satisfiablity:
  is valid iff  is unsatisfiable.
  is satisfiable iff  is not valid.

33
Inference rules in PL
 Modus Ponens    ,

 And-elimination: from a conjuction any
conjunction can be inferred:   
 
 All logical equivalences can be used as
inference rules.  
(   ) (   )
 34
Example
 Assume R1 through R5 on slide # 25.

 How can we prove P1,2?


R6 :B1,1  P1, 2  P2,1   P1, 2  P2,1   B1,1  Biconditional elim.

R7 :P1, 2  P2,1   B1,1 And elim.

R8 :B1,1  P1, 2  P2,1  Contraposition


R9 :P1, 2  P2,1  Modus ponens
R10 :P1, 2  P2,1
Morgan’s rule
R11 :P1, 2
And elim. 35
Searching for proofs
 Searching for proofs is not more efficient
than enumerating models, but in many
practical cases, it is more efficient
because we can ignore irrelevant
properties.
 Monotonicity: the set of entailed
sentences can only increase as
information is added to the knowledge
base.
forany sentence and  : if KB |  then KB  | 
36
Proof Methods
 Enumeration of models (model checking)
 Truth table enumeration (exponential in n)
 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
 Construction of a proof (inference rules)
 Legitimate (sound) generation of new sentences from old
 Proof = a sequence of inference rule applications
 Typically requires translation of sentences into a normal
form
37
Enumeration of Models
 KB : set of sentences
  : arbitrary sentence
 KB entails  iff every model of KB is also
a model of 
 Alternatively, KB  iff
 {KB,}is unsatisfiable
 KB   is valid

38
Enumeration of Models
P: Set of propositional symbols in {KB,}
n: Size of P

ENTAILS(KB,)
For each of the 2n models on P do
If it is a model of {KB,} then
Return no
Return yes

39
Construction of a Proof

 What do we need?
 A complete set of sound inference
rules
 A complete search algorithm to
decide which rule to apply next and
to which sentences

40
Complementary Literals
 A literal is a either an atomic sentence
(positive literal) or the negated atomic
sentence (negative literal), e.g.:
P or P

 Two literals are complementary if one is


the negation of the other, e.g.:
P and P

41
Unit Resolution Rule
 Given two sentences:
L1  …  Lp and M
where L1,…, Lp and M are all literals,
and M and Li are complementary
literals
 Infer: L1  …  Li-1  Li+1  …  Lp
This is expressed as :

42
Soundness of Unit Resolution
 Let m be a model of:
L1  …  Lp and M
where M and Li are complementary
literals
 Li must be False in m, hence
L1  …  Li-1  Li+1  …  Lp
must be True

43
Shortcoming of Unit Resolution

From:
 E  F  D
 EG
we can infer nothing!

What does this mean?


44
Full Resolution Rule
 Given two sentences:
L1  …  Lp and M1  …  Mq
where L1,…, Lp, M1,…, Mq are all literals,
and Li and Mj are complementary literals

 Infer:
L1 … Li-1Li+1…LpM1 … Mj-1
Mj+1…Mq

in which only one copy of each literal is


retained 45
Resolution
 Full Resolution Rule is expressed as:

Example :
E  F  D
E  G
Infer:
FDG 46
Not All Inferences are Useful!

From:
E F C
E  F

Infer:
F  F  C  True

tautology

47
Resolution
 Uses CNF (Conjunctive normal form)
 Conjunction of disjunctions of literals
(clauses)
 The resolution rule is sound:
 Only entailed sentences are derived
 Resolution is complete in the sense that it
can always be used to either confirm or
refute a sentence (it can not be used to
enumerate true sentences.)
48
Resolution algorithm
 First KB   is converted into CNF
 Then apply resolution rule to resulting
clauses.
 The process continues until:
 There are no new clauses that can be added
 Hence KB does not entail 
 Two clauses resolve to entail the empty clause.
 Hence KB entails 

49
Conversion to CNF
Example: B11  (P12 v P21)
1. Eliminate , replacing    with () ٨ ()
(B11  ((P12 v P21)) ٨((P12 v P21)  B11))
2. Eliminate , replacing  with ¬ v 
(¬B11 v P12 v P21) ٨ (¬(P12 v P21) v B11))
3. Move  in using deMorgan’s rules and double
negation
(¬B11 v P12 v P21) ٨ ( (¬P12 ٨ ¬P21) v B11))
4. Distribute  over 
(¬B11 v P12 v P21) ٨ (¬P12 v B11) ٨ (¬P21 v B11)
Set of clauses:
{(¬B11 v P12 v P21), (¬P12 v B11) ,(¬P21 v B11)} 50
Resolution example
 KB = (B1,1  (P1,2 P2,1))  B1,1 = P1,2

51
Forward and backward chaining

 Applicable only when the KB is


represented as a conjunction of Horn
clauses
 Horn clause:
 Propositional symbol or
 a disjunction of literals of which at most
one is positive, e.g., P  Q  R

52
Forward and backward chaining
 Horn clause can be converted in following
way
 (conjunction of negated symbols) 
(positive symbol)
E.g. ( P  R )  Q

 Modus Ponens complete for Horn KBs


 Deciding entailment can be done in a time
linear size of the knowledge base.
53
Forward and backward chaining
 Definite Clause: Horn clause with exactly
one positive literal.
 Fact: A Definite clause with no negative
literal is called fact. It asserts a given
proposition.
 Integrity Constraint: A Horn clause with
no positive literal can be expressed as an
implication whose conclusion is false.
 E.g. Wumpus can’t be at 2 locations)
W1,1  W1,2
W1,1  W1,2  False 54
Forward chaining
 Idea: fire any rule whose premises are satisfied
in the KB,
 add its conclusion to the KB, until query is found

55
Forward chaining algorithm

 Forward chaining is sound and complete for Horn


KB

56
Forward chaining example

57
Forward chaining example

58
Forward chaining example

59
Forward chaining example

60
Forward chaining example

61
Forward chaining example

62
Forward chaining example

63
Forward chaining example

64
Backward chaining
 Idea: work backward from the query q
to prove q using 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
 Has already been proved true, OR
 Has already failed
65
Backward chaining example

66
Backward chaining example

67
Backward chaining example

68
Backward chaining example

69
Backward chaining example

70
Backward chaining example

71
Backward chaining example

72
Backward chaining example

73
Backward chaining example

74
Backward chaining example

75
Forward vs. backward chaining
 FC is data-driven
 Automatic, unconscious processing
 May do lots of work that is irrelevant to the goal

 BC is goal-driven, appropriate for


problem-solving
 Complexity of BC can be much less than linear in
the size of the KB

 Complexity of BC can be much less than


linear in size of KB
76
Effective propositional inference
 Two families of efficient algorithms for
propositional inference based on model checking:
 Are used for checking satisfiability
 Complete backtracking search algorithms
 DPLL algorithm (Davis, Putnam, Logemann, Loveland)
 Incomplete local search algorithms
 WalkSAT algorithm

77
The DPLL algorithm
 Determine if an input propositional logic sentence
(in CNF) is satisfiable.
 Improvements over truth table enumeration:
1. Early termination
 A clause is true if any literal is true.
 A sentence is false if any clause is false.
 E.g., clause (ABC) is true whenever A=True
regardless of the value of B and C.
 E.g., a sentence in CNF (P  Q  R). Here search
space can be pruned when P=False, or P=True,
Q=True. 78
The DPLL algorithm
2. Pure symbol heuristic
 Pure symbol: always appears with the same "sign"
in all clauses. E.g., In the four clauses (A  B),
(B  C), (C  D), (D  A) A and B are pure, C
and D are impure.
 Make a pure symbol literal true, because that
assignment would make more than one clauses to
be true.
 Assigning truth value to a pure symbol might
make a previous impure symbol to pure. E.g., if
B is assigned True, first and second clauses
become True. So C becomes a pure symbol. 79
The DPLL algorithm
3. Unit clause heuristic
 Unit clause: A clause in which all literals except
one are already assigned to ‘False’ or there is only
one literal in the clause.
 The only literal in a unit clause must be true to
make the clause True. E.g., in (A  D) if A is
assigned False, D must be assigned False.
 Assigning one unit clause can create another unit
clause. It’s called Unit Propagation. E.g., after
assigning D=False, (C  D) becomes a unit clause,
because to make it true C must be assigned True. 80
3-SAT Problem

 n propositional symbols P1,…,Pn


 KB consists of p sentences of the form
Q i  Qj  Qk
where:
 i  j  k are indices in {1,…,n}
 Qi = Pi or Pi
 3-SAT: Is KB satisifiable?
 3-SAT is NP-complete
The WalkSAT algorithm
 Incomplete, local search algorithm.
 Here, goal is to find an assignment that satisfies
every clause.
 Evaluation function: The min-conflict heuristic of
minimizing the number of unsatisfied clauses.
 On every iteration WalkSAT picks an unsatisfied
clause and picks a symbol in that clause to flip the
truth value of it. To choose the symbol, either of
following 2 strategies are taken -
 Just random
 Use Min-Conflict Heuristic 82
Inference-based agents in the
wumpus world
A wumpus-world agent using propositional logic:
a) P1,1
W1,1
b) Bx,y  (Px,y+1  Px,y-1  Px+1,y  Px-1,y)
Sx,y  (Wx,y+1  Wx,y-1  Wx+1,y  Wx-1,y  Wx,y)

c) W1,1  W1,2  …  W4,4 [At least 1 wumpus]


W1,1  W1,2 [At most 1 wumpus: 16C2]
W1,1  W1,3

 64 distinct proposition symbols, 155 sentences
83
Expressiveness limitation of
propositional logic
 KB contains sentences for every single
square
 With all consequences for large KB
 Better would be to have just two sentences for
breezes and stenches for all squares.
 Keeping track of location & orientation:
L1,1  FacingRight  Forward  L2,1
 Initially L1,1 is entailed, and now L2,1 is also
entailed. It is contradicting.
84
Expressiveness limitation of
propositional logic
 How could we solve it?
 It needs to keep time stamp to track
location:
Lt1,1  FacingRightt  Forwardt  Lt+12,1
 Similarly, to keep track of orientation it
needs to consider time:
FacingRightt  TurnLeftt  FacingUpt+1
 Huge proliferation of clauses.
85
Expressiveness limitation of
propositional logic
 Let upper limit of time stamps = 100.
 For each square for every time t, 4
sentences are needed to track location and
8 sentences are needed to track orientation.
 Total # sentences = 100 x 16 x (4+8)
= 19200

86
Circuit Based Agent
 A model based reflex agent where an agent keeps
track of internal states.
 Circuit is composed of –
 Gates: Each gate represents a logical connective.
 Registers: Each register stores truth value of a
propositional symbol.
 The inputs of the circuit are registers
corresponding to perceptions.
 The outputs of the circuit are registers
corresponding to actions.
87
Example of circuit based agent

Alivet  Screamt 
Alivet-1
Initially, Alive set to true. 88
Tracking Location
 For each square [x,y] register Lx,y should
be true if the agent is at [x,y] at a
particular time stamp.
 E.g., agent is at [1,1] at time t which can
be expresses as:

89
Circuit for Tracking Location

90
Problem with circuit based agent
 In case of Alivet or Lt1,1the circuit based
inference is correct, because we can initialize
the truth values when the game starts. Say, L1,1 =
True, other Lx,y = False
 However the value of B4,4 (whether there is a
breeze at[4,4]) can’t be initialized until the agent
visits [4,4] or discover a pit at some adjacent
blocks.
 Here three value needs to be stored: known true,
known false, unknown (initial value).
91
In the Circuit, use K(B4,4) where
needs B4,4 and use K(B4,4)
where needs B4,4.
 Solution to this problem requires 2
K(B4,4) and K(B4,4) are called
registers to represent B4,4 namely
Knowledge K(B4,4)
Proposition.
and K(B4,4):
K(B4,4) K(B4,4) meaning
0 0 Unknown
0 1 Not Breezy
1 0 Breezy
1 1 Bug in the KB
92
Determining Truth value of
Knowledge Proposition
 K(B4,4) starts out false and then becomes true as
soon as B4,4 can be determined to be true—that
is, when the agent is in [4,4] and detects a
breeze. It stays true thereafter.

 A Square contains a pit when there is a


breeze in an adjacent square that cannot
be accounted for by another pit.

93
Properties of circuit-based agent
design
 Locality: An environment exhibits locality
if the truth of each proposition can be
determined looking only at constant
number of other propositions.
 E.g., environment of Mine Sweeper game
is non-local.

94
Properties of circuit-based agent
design
 Acyclicity: A circuit is acyclic if every
path that connects the output of a register
back to its input has an intervening delay
element. E.g.,
K(B4,4)t  K(B4,4)t-1  (Lt4,4  Breezet) 
K(P3,4)t  K(P4,3)t
To determine K(P3,4)t or K(P4,3)t it needs to
determine the value of K(B4,4)t .So the
circuit contains a cycle.
95
Limitations of Circuit based agent
 Accyclicity restriction attributes to the
incompleteness of circuit-based agent.
 To avoid accyclicity the truth value of
K(P3,4)t and K(P4,3)t can’t be included in the
rule for determining truth value K(B4,4)t.
Which implies the agent can’t infer some
sentences which are logically entailed by the
knowledge base.
 Accyclicity can be avoided by introducing
96
more hardware, but it becomes too complex.
Inference-based agent (IBA) VS
Circuit-based agent (CBA)
 Conciseness:
 CBA, unlike IBA, doesn’t need to have
separate copies of its knowledge for
every time step. It requires only
knowledge of only previous and current
time stamp.
 However, both agents have to store the
knowledge for every square.
97
Inference-based agent (IBA) VS
Circuit-based agent (CBA)
 Computational efficiency:
 IBA can take time exponential in number of
symbols.
 CBA takes time linear in the size of circuit.
 Ease of construction:
 IBA is much easier to construct.
 Completeness:
 CBA can be incomplete due to acyclicity
restriction.
98
99

Você também pode gostar