0% found this document useful (0 votes)
104 views

Logical Agent

This document discusses logical agents and knowledge-based reasoning. It introduces the concept of a knowledge base that represents an agent's knowledge through a set of sentences. The document then presents a toy problem called the Wumpus World, where an agent must navigate a grid to find gold while avoiding hazards. The agent uses logical reasoning over its knowledge base and percepts to deduce properties of the world and determine safe actions. Propositional logic is also introduced as a simple formal language for representing information to perform logical inference.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Logical Agent

This document discusses logical agents and knowledge-based reasoning. It introduces the concept of a knowledge base that represents an agent's knowledge through a set of sentences. The document then presents a toy problem called the Wumpus World, where an agent must navigate a grid to find gold while avoiding hazards. The agent uses logical reasoning over its knowledge base and percepts to deduce properties of the world and determine safe actions. Propositional logic is also introduced as a simple formal language for representing information to perform logical inference.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 80

Chapter 7: Logical Agents

Dr. Daisy Tang

In which we design agents that can form representations of the world,


use a process of inference to derive new representations about the
world, and use these new representations to deduce what to do.
Motivation of Knowledge-Based Agents
 Humans know things and do reasoning, which are important
for artificial agents

 Knowledge-based agents benefit from knowledge expressed in


general forms, combining information to suit various purposes

 Knowledge and reasoning is important when dealing with


partially observable environments

 Understanding natural language requires inferring hidden


states

 Flexibility for accepting new tasks

CS 420: Artificial Intelligence 2


Knowledge Bases

 Knowledge base = set of sentences in a formal language

 Declarative approach to building an agent (or other system):


 Tell it what it needs to know
 Then it can Ask itself what to do - answers should follow from the
KB

 Both “Tell” and “Ask” involve inference – deriving new


sentences from old

CS 420: Artificial Intelligence 3


A Simple Knowledge-Based Agent

 Details hidden in three functions


 Similar to agent with internal state in Chapter 2

 Agents can be viewed at the knowledge level


i.e., what they know, regardless of how they are implemented

 Or at the implementation level


 i.e., data structures in KB and algorithms that manipulate them

CS 420: Artificial Intelligence 4


Agent’s Requirements
 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

CS 420: Artificial Intelligence 5


Our New Toy Problem –
Wumpus World

CS 420: Artificial Intelligence 6


Wumpus World PEAS
 Performance measure
 gold +1000, death -1000
 -1 per step, -10 for using the arrow

 Environment
 4x4 grid of rooms
 Agent always starts at [1,1]
 Wumpus and gold randomly chosen
 Each square other than start is pit
with 0.2 probability

CS 420: Artificial Intelligence 7


Wumpus World PEAS
 Actuators: Left turn, Right turn,
Forward, Grab, Release, Shoot, Climb
 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
 Climbing out of the cave from [1, 1]

 Sensors (percepts): Stench, Breeze,


Glitter, Bump, Scream
 Squares adjacent to wumpus are smelly
 Squares adjacent to pit are breezy
 Glitter iff gold is in the same square
 Bump when agent walks into a wall
 When wumpus is killed, it screams

CS 420: Artificial Intelligence 8


Wumpus World Properties
 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

CS 420: Artificial Intelligence 9


Exploring a Wumpus World
Agent’s initial knowledge base contains the rules of the environment: it knows that
it’s at [1,1] and it’s safe at [1,1]

percept: [none, none, none, none, none]

CS 420: Artificial Intelligence 10


Exploring a Wumpus World

percept: [none, breeze, none, none, none]

CS 420: Artificial Intelligence 11


Exploring a Wumpus World

percept: [none, breeze, none, none, none]

CS 420: Artificial Intelligence 12


Exploring a Wumpus World

our cautious agent will go back and to [2,1]

CS 420: Artificial Intelligence 13


Exploring a Wumpus World

percept: [stench, none, none, none, none]

CS 420: Artificial Intelligence 14


Exploring a Wumpus World

percept: [none, none, none, none, none]

CS 420: Artificial Intelligence 15


Exploring a Wumpus World

CS 420: Artificial Intelligence 16


Exploring a Wumpus World

percept: [stench, breeze, glitter, none, none]

CS 420: Artificial Intelligence 17


Logical Reasoning
 In each case where the agent draws a
conclusion from the available information,
that conclusion is guaranteed to be correct
if the available information is correct

 The above is the fundamental of logical


reasoning

CS 420: Artificial Intelligence 18


Logic in General
 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

 E.g., the 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

CS 420: Artificial Intelligence 19


Entailment
 Now, we have a notion of truth, we are ready to
study logical reasoning, which involves logical
entailment between sentences
 Entailment means that one thing follows from
another:
KB ╞ α

 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 4 = x+y
 Entailment is a relationship between sentences (i.e.,
syntax) that is based on semantics

CS 420: Artificial Intelligence 20


Models
 We use the term “model” to represent “possible world”
 We say m is a model of a sentence α if α is true in model m

 M(α) is the set of all models of α

 Then KB ╞ α iff M(KB)  M(α)


 E.g. KB = Giants won and Reds
won, α = Giants won

CS 420: Artificial Intelligence 21


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

CS 420: Artificial Intelligence 22


Wumpus Models

CS 420: Artificial Intelligence 23


Wumpus Models

 KB = wumpus-world rules + observations

CS 420: Artificial Intelligence 24


Wumpus Models

 KB = wumpus-world rules + observations


 α1 = "[1,2] is safe", KB ╞ α1, proved by model checking
 enumerate all possible models to check that α is true in all models in
which KB is true
CS 420: Artificial Intelligence 25
Wumpus Models

 KB = wumpus-world rules + observations

CS 420: Artificial Intelligence 26


Wumpus Models

 KB = wumpus-world rules + observations


 α2 = "[2,2] is safe“, KB ╞ α2?
CS 420: Artificial Intelligence 27
Wumpus Models

 KB = wumpus-world rules + observations


 α2 = "[2,2] is safe", KB ╞ α2
CS 420: Artificial Intelligence 28
Soundness and Completeness
 An inference algorithm that derives only
entailed sentences is called sound

 An inference algorithm is complete if it can


derive any sentence that is entailed

 If KB is true in the real world, then any


sentence derived from KB by a sound
inference procedure is also true in the real
world

CS 420: Artificial Intelligence 29


Grounding Problem
 Grounding: how do we know that KB is true
in the real world?
 Agent’s sensors create the connection
 Meaning and truth of percept sentences through
sensing and sentence construction
 General rules
 From perceptual experience

CS 420: Artificial Intelligence 30


Propositional Logic: Syntax
 Propositional logic is the simplest logic – illustrates
basic ideas

 The proposition symbols S1, S2 etc are 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)

CS 420: Artificial Intelligence 31


Propositional Logic: Semantics
Each model specifies true/false for each proposition symbol

E.g. P1,2 P2,2 P3,1


false true false

With these symbols, 8 possible models, can be enumerated automatically.

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 S1is 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
S 1  S2 is true iff S1S2 is true and S2S1 is true

Simple recursive process evaluates an arbitrary sentence, e.g.,

P1,2  (P2,2  P3,1) = true  (true  false) = true  true = true

CS 420: Artificial Intelligence 32


Truth Tables for Connectives

CS 420: Artificial Intelligence 33


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)

 The KB consists of the above 5 sentences. It can


also be considered as a single sentence – the
conjunction R1  R2  R3  R4  R5

CS 420: Artificial Intelligence 34


Inference
 The aim of logical inference is to decide
whether KB╞ Ɑ for some sentence Ɑ.

 Is P2,2 entailed?

 Our first algorithm for inference will be a


direct implementation of the definition of
entailment:
 Enumerate the models, and check that Ɑ is true in
every model in which KB is true

CS 420: Artificial Intelligence 35


Truth Tables for Inference

P1,2 is false, cannot decide on p2,2


α1 = "[1,2] is safe"

CS 420: Artificial Intelligence 36


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)


PL-TRUE? returns true if a sentence holds within a model.

CS 420: Artificial Intelligence 37


Proof Methods In General
 Proof methods divide into (roughly) two kinds:
 Model checking
 truth table enumeration (always 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
 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
algorithm
 Typically require transformation of sentences into a normal
form

CS 420: Artificial Intelligence 38


Validity and Satisfiability
 A sentence is valid if it is true in all models, also known as
tautology
 e.g., True, 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

 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
 Thus proof by contradiction

CS 420: Artificial Intelligence 39


Equivalence Rules
 Two sentences are logically equivalent iff they are true in the
same models: α ≡ ß iff α╞ β and β╞ α

CS 420: Artificial Intelligence 40


Inference Rules
From Can Derive Abbreviation for rule
R, R  S S Modus Ponens- mp
R  S, S´ R´ Modus Tollens- mt
R, S RΛS Conjunction-con
RΛS R, S Simplification- sim
R RVS Addition- add

 R1: ¬P1,1
 R2: B1,1  P1,2 V P2,1
 R3: B2,1  P1,1 V P2,2 V P3,1
 R4: ¬B1,1
 R5: B2,1

 Can we prove ¬P1,2?

CS 420: Artificial Intelligence 41


Now, Your Turn
From Can Derive Abbreviation for rule
R, R  S S Modus Ponens- mp
R  S, S´ R´ Modus Tollens- mt
R, S RΛS Conjunction-con
RΛS R, S Simplification- sim
R RVS Addition- add

 Prove ¬P2,1 and P3,1 given:


R1: P1, 1
R 2 : B1, 1  P1, 2  P 2, 1
R3 : B 2, 1  P1, 1  P 2, 2  P 3, 1
R 4 : B1, 1
R5 : B 2, 1
R 6 : P 2, 2
CS 420: Artificial Intelligence 42
Proof Problem as Search Problem
 Initial state: initial knowledge base
 Actions:
 set of actions consists of all the inference rules applied to
all the sentences that match the left half of the inference
rule
 Result: add the sentence in the right half of the inference
rule
 Goal: the goal is a state that contains the sentence we are
trying to prove

 Practically, finding a proof can be more efficient because the


proof can ignore irrelevant propositions, no matter how many
of them there are

CS 420: Artificial Intelligence 43


Resolution
 The inference rules can be applied whenever suitable
premises are found in the KB

 The conclusion of the rule must follow regardless of what else


is in the KB

 The above rules are sound, but if the available inference rules
are inadequate, then it’s not complete

 Now, we introduce a single inference rule, resolution, that


gives a complete inference algorithm when coupled with any
complete search algorithm

CS 420: Artificial Intelligence 44


Example
 Recall our previous proof of P3,1 given:
R1: P1, 1
R 2 : B1, 1  P1, 2  P 2, 1
R3 : B 2, 1  P1, 1  P 2, 2  P 3, 1
R 4 : B1, 1
R5 : B 2, 1
R6 : P 2, 2
... Unit resolution
R9 : P1, 1  P 2, 2  P 3, 1
R10 : P 2, 2  P 3, 1 Resolution rule: ¬P1,1 resolves with P1,1 in R9

R11: P 3, 1 Resolution rule: ¬P2,2 resolves with P2,2 in R10

CS 420: Artificial Intelligence 45


Resolution Rules
 Unit resolution
 li and m are complementary literals
l 1 …  l k, m
l 1  …  l i-1  l i+1  …  lk

 Generalized resolution rule


l 1 …  l k, m1  …  m n
l 1  …  l i-1  l i+1  …  lk  m1  …  m j-1  m j+1 ...  m n

 where li and mj are complementary literals

CS 420: Artificial Intelligence 46


Soundness and Completeness
 Soundness of resolution inference rule:
 if l i is true, then mj is false
 hence (m1  …  m j-1  m j+1 ...  mn) must be true
 if l i is false, then
 (l1  …  l i-1  l i+1  …  lk) must be true
 no matter what
 (l1  …  l i-1  l i+1  …  lk) V (m1  …  m j-1  m j+1 ...  mn) is
true

 Any complete search algorithm, applying only the


resolution rule, can derive any conclusion entailed
by any knowledge base in propositional logic
CS 420: Artificial Intelligence 47
 A resolution-based theorem prover can, for
any sentences α and β in propositional logic,
decide whether α╞ β

CS 420: Artificial Intelligence 48


Resolution and CNF
 Resolution rule applies only to
disjunctions of literals

 Every sentence of propositional


logic is logically equivalent to a
conjunction of disjunctions of
literals

 Conjunctive Normal Form


(CNF): P1,3  P2,2, P2,2

 conjunction of disjunctions of
literals clauses P1,3
 E.g., (A  B)  (B  C  D)

CS 420: Artificial Intelligence 49


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 distributive law ( over ) and flatten:


(B1,1  P1,2  P2,1)  (P1,2  B1,1)  (P2,1  B1,1)

CS 420: Artificial Intelligence 50


Resolution Algorithm
 To show that KB╞ α, we show that (KB ^ α) is unsatisfiable,
proof by contradiction

 First, (KB ^ α) is converted into CNF


 Then the resolution rule is applied to the resulting clauses
 Each pair that contains complementary literals is resolved to
produce a new clause, which is added to the set if it’s not present
 Process continues until:
 no new clauses can be added, thus KB does not entail α
 two clauses resolve to yield {}, thus KB entails α

 {} is a disjunction of no disjuncts is equivalent to False, thus


the contradiction

CS 420: Artificial Intelligence 51


Resolution Algorithm
 Proof by contradiction, i.e., show that KBα is
unsatisfiable

CS 420: Artificial Intelligence 52


Resolution Example
 KB = (B1,1  (P1,2 P2,1))  B1,1 α = P1,2

CS 420: Artificial Intelligence 53


Horn Clauses
 Why horn clauses?
 In many practical situations, however, the full
power of resolution is not needed
 Real-world KBs often contain some restricted
clauses – Horn clauses

 Horn Form (restricted)


 KB = conjunction of Horn clauses
 Horn clause = disjunction of literals of which at
most one is positive
 E.g., (L1,1 V Breeze V B1,1)

CS 420: Artificial Intelligence 54


Properties of Horn Clauses
 Every horn clause can be written as an implication whose
premise is a conjunction of positive literals and whose
conclusion is a single positive literal
 E.g., (L1,1 ^ Breeze)  B1,1

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


α1, … ,αn, α1  …  αn  β
β
 Definite clause: horn clauses with exactly one positive literal
 The positive literal is called the head and the negative literals
form the body
 Definite clauses form the basis for logic programming

 Can be used with forward chaining or backward chaining


 These algorithms are very natural and run in linear time

CS 420: Artificial Intelligence 55


Forward Chaining
 Idea:
 fire any rule whose premises are satisfied in the KB
 add its conclusion to the KB, until query is found

and-or
graph

CS 420: Artificial Intelligence 56


Forward Chaining Algorithm

if p = q then return true


if inferred[p] = false then
inferred[p] = true
for each clause c in KB where p is in c.Premise do
decrement count[c]
if count[c] = 0 then add c.Conclusion to agenda

 Forward chaining is sound and complete for Horn KB

CS 420: Artificial Intelligence 57


Forward Chaining Example

CS 420: Artificial Intelligence 58


Forward Chaining Example

CS 420: Artificial Intelligence 59


Forward Chaining Example

CS 420: Artificial Intelligence 60


Forward Chaining Example

CS 420: Artificial Intelligence 61


Forward Chaining Example

CS 420: Artificial Intelligence 62


Forward Chaining Example

CS 420: Artificial Intelligence 63


Proof of Soundness
 Every inference is an application of Modus
Ponens

CS 420: Artificial Intelligence 64


Proof of Completeness
 FC derives every atomic sentence that is entailed
by KB
1. FC reaches a fixed point where no new atomic sentences
are derived
2. Consider the final state as a model m, assigning
true/false to symbols; true for each symbol inferred,
false otherwise
3. Every definite clause in the original KB is true in m
a1  …  ak  b, assuming the opposite, then it contradicts
“fixed point”
4. Hence, every entailed atomic sentence will be derived

CS 420: Artificial Intelligence 65


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


 has already been proved true, or

 has already failed

CS 420: Artificial Intelligence 66


Backward Chaining Example

CS 420: Artificial Intelligence 67


Backward Chaining Example

CS 420: Artificial Intelligence 68


Backward Chaining Example

CS 420: Artificial Intelligence 69


Backward Chaining Example

CS 420: Artificial Intelligence 70


Backward Chaining Example

CS 420: Artificial Intelligence 71


Backward Chaining Example

CS 420: Artificial Intelligence 72


Backward Chaining Example

CS 420: Artificial Intelligence 73


Backward Chaining Example

CS 420: Artificial Intelligence 74


Backward Chaining Example

CS 420: Artificial Intelligence 75


Backward Chaining Example

CS 420: Artificial Intelligence 76


Forward vs. Backward Chaining
 FC is data-driven, 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

CS 420: Artificial Intelligence 77


Inference-Based Agents in the Wumpus World

 A wumpus-world agent using propositional logic:

P1,1
W1,1
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)
W1,1  W1,2  …  W4,4
W1,1  W1,2
W1,1  W1,3

 64 distinct proposition symbols, 155 sentences


CS 420: Artificial Intelligence 78
Expressiveness Limitation
 KB contains "physics" sentences for every single
square

 For every time t and every location [x,y],


Lx,y  FacingRight t  Forward t  Lx+1,y

 Rapid proliferation of clauses

CS 420: Artificial Intelligence 79


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 based on models
 entailment: necessary truth of one sentence given another
 inference: deriving sentences from other sentences
 soundness: derivations produce only entailed sentences
 completeness: derivations can produce all entailed sentences

 Wumpus world requires the ability to represent partial and negated


information, reason by cases, etc.

 Resolution is complete for propositional logic


Forward, backward chaining are linear-time, complete for Horn
clauses

 Propositional logic lacks expressive power


CS 420: Artificial Intelligence 80

You might also like