logic agents

79
logic agents Notes adapted from lecture notes for CMSC 421 by B.J. Dor

description

Notes adapted from lecture notes for CMSC 421 by B.J. Dorr. logic agents. “Thinking Rationally”. Computational models of human “thought” processes Computational models of human behavior Computational systems that “think” rationally Computational systems that behave rationally. - PowerPoint PPT Presentation

Transcript of logic agents

Page 1: logic agents

logic agents

Notes adapted from lecture notes for CMSC 421 by B.J. Dorr

Page 2: logic agents

2

“Thinking Rationally”• Computational models of human “thought”

processes

• Computational models of human behavior

• Computational systems that “think” rationally

• Computational systems that behave rationally

Page 3: logic agents

3

Logical Agents• Reflex agents find their way from Arad to

Bucharest by dumb luck

• Chess program calculates legal moves of its king, but doesn’t know that no piece can be on 2 different squares at the same time

• Logic (Knowledge-Based) agents combine general knowledge with current percepts to infer hidden aspects of current state prior to selecting actions– Crucial in partially observable environments

Page 4: logic agents

4

Outline• Knowledge-based agents

• Wumpus world

• Logic in general

• Propositional and first-order logic– Inference, validity, equivalence and satifiability– Reasoning patterns

• Resolution• Forward/backward chaining

Page 5: logic agents

5

Knowledge Base

Knowledge Base : set of sentences represented in a knowledge representation language and represents assertions about the world.

Inference rule: when one ASKs questions of the KB, the answer should follow from what has been TELLed to the KB previously.

telltell askask

Page 6: logic agents

6

Generic KB-Based Agent

Page 7: logic agents

7

Generic KB-Based Agent

• Takes percent as input and returns an action

• agent maintains a knowledge base KB

• Each time agent program is called it does – TELLs knowledge base what It perceives – ASKs knowledge base what action it should

perform – Agent records its choice with TELL an executes

action

Page 8: logic agents

8

Abilities KB agent• Agent must be able to:

– Represent states and actions,– Incorporate new percepts– Update internal representation of the world– Deduce hidden properties of the world– Deduce appropriate actions

Page 9: logic agents

9

Desription level• The KB agent is similar to agents with internal

state

• Agents can be described at different levels– Knowledge level

• What they know, regardless of the actual implementation. (Declarative description)

– Implementation level• Data structures in KB and algorithms that manipulate

them e.g propositional logic and resolution.

Page 10: logic agents

10

A Typical Wumpus World

WumpusWumpus

Page 11: logic agents

11

A Typical Wumpus World

• Cave consisting of rooms connected by passageways

• Agent has only one arrow

• Some of rooms contain bottomless pit that will trap anyone who wanders into these rooms – except for wumpus

Page 12: logic agents

12

Wumpus World PEAS Description

Page 13: logic agents

13

Wumpus World Characterization

• Observable?

• Deterministic?

• Episodic?

• Static?

• Discrete?

• Single-agent?

Page 14: logic agents

14

Wumpus World Characterization

• Observable? No, only local perception

• Deterministic?

• Episodic?

• Static?

• Discrete?

• Single-agent?

Page 15: logic agents

15

Wumpus World Characterization

• Observable? No, only local perception

• Deterministic? Yes, outcome exactly specified

• Episodic?

• Static?

• Discrete?

• Single-agent?

Page 16: logic agents

16

Wumpus World Characterization

• Observable? No, only local perception

• Deterministic? Yes, outcome exactly specified

• Episodic? No, sequential at the level of actions

• Static?

• Discrete?

• Single-agent?

Page 17: logic agents

17

Wumpus World Characterization

• Observable? No, only local perception

• Deterministic? Yes, outcome exactly specified

• Episodic? No, sequential at the level of actions

• Static? Yes, Wumpus and pits do not move

• Discrete?

• Single-agent?

Page 18: logic agents

18

Wumpus World Characterization

• Observable? No, only local perception

• Deterministic? Yes, outcome exactly specified

• Episodic? No, sequential at the level of actions

• Static? Yes, Wumpus and pits do not move

• Discrete? Yes

• Single-agent?

Page 19: logic agents

19

Wumpus World Characterization

• Observable? No, only local perception

• Deterministic? Yes, outcome 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.

Page 20: logic agents

20

Exploring the Wumpus World

[1,1] The KB initially contains the rules of the environment. The first percept is [none, none,none,none,none], move to safe cell e.g. 2,1 [none, none,none,none,none] – no stench, no breeze, no glitter, no wall in front, no scream

[2,1] breeze which indicates that there is a pit in [2,2] or [3,1], return to [1,1] to try next safe cell

Page 21: logic agents

21

Exploring the Wumpus World

[1,2] Stench in cell which means that wumpus is in [1,3] or [2,2]YET … not in [1,1]YET … not in [2,2] or stench would have been detected in [2,1]THUS … wumpus is in [1,3]THUS [2,2] is safe because of lack of breeze in [1,2]THUS pit in [1,3]move to next safe cell [2,2]

Page 22: logic agents

22

Exploring the Wumpus World

[2,2] move to [2,3][2,3] detect glitter , smell, breeze

THUS pick up goldTHUS pit in [3,3] or [2,4]

Page 23: logic agents

23

What is a logic?• A formal language

– Syntax – what expressions are legal (well-formed)– Semantics – what legal expressions mean

• in logic the truth of each sentence with respect to each possible world.

• E.g 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 and y =1– X+2 >= y is false in a world where x=0 and y =6

Page 24: logic agents

24

Entailment

• One thing follows from anotherKB | =

• KB entails sentence if and only if is true in worlds where KB is true.

g. x+y=4 entails 4=x+y• Entailment is a relationship between

sentences that is based on semantics.

Page 25: logic agents

25

Models

• Logicians typically think in terms of models, which are formally structured worlds with respect to which truth can be evaluated.

• m is a model of a sentence if is true in m

• M() is the set of all models of

Page 26: logic agents

26

Wumpus world model

Page 27: logic agents

27

Wumpus world model

Page 28: logic agents

28

Wumpus world model

Page 29: logic agents

29

Wumpus world model

Page 30: logic agents

30

Wumpus world model

Page 31: logic agents

31

Wumpus world model

Page 32: logic agents

32

Logical inference• The notion of entailment can be used for logic

inference.– Model checking (see wumpus example): enumerate all

possible models and check whether is true.

• If an inference algorithm i can derive from KB– KB |-i – Sentence is derived from KB by i or i derives from KB

• 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

Page 33: logic agents

33

Schematic perspective

If KB is true in the real world, then any sentence derivedFrom KB by a sound inference procedure is also true in the

real world.

Page 34: logic agents

34

Logical inference

• Preview: we will define a 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.

Page 35: logic agents

35

Propositional logic: Syntax• Propositional logic is the simplest logic - illustrates

basic ideas• The proposition symbols P1, P2 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)

Page 36: logic agents

36

Truth tables for connectives

P Q P PQ PQ PQ PQfalse 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

Page 37: logic agents

37

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

true 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 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 S2 is true and S2 S1 is true• Simple recursive process evaluates an arbitrary sentence,

e.g., P1,2 (P2,2 P3,1) = true (false true)=true true=true

Page 38: logic agents

38

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"

Page 39: logic agents

39

Truth tables for inference

Page 40: logic agents

40

Logical equivalence

Page 41: logic agents

41

Validity and satisability

Page 42: logic agents

42

Inference RulesModus ponens,

And elimination

Logical equivalences on pg 40 applyEg biconditional elimination yields 2 inference

rules and () ()

() ()

Page 43: logic agents

43

Inference Rules

• All inferences may not apply in the opposite direction

Page 44: logic agents

44

Inference Rules

• Wumpus world KB

There is no pit in [1,1] etc

R1: P1,1

R2: B1,1 (P1,2 P2,1)

R3: B2,1 (P1,1 P2,2 P3,1)

R4: B1,1

R5: B2,1

Page 45: logic agents

45

Inference Rules• Show there is no pit in [1,2]• biconditional elimination to R2

R6: (B1,1(P1,2 P2,1)) ((P1,2 P2,1 ) B1,1)And elimination to R6R7: ((P1,2 P2,1 ) B1,1)logical equivalence for contrapositivesR8: ( B1,1 (P1,2 P2,1))Apply modus ponens with R8 with precept R4R9: (P1,2 P2,1))De MorganR10: P1,2 P2,1)

Neither [1,2] nor [2,1] contains a pit

Page 46: logic agents

46

Searching for Proofs• Finding proofs is exactly like finding solutions to

search problems.• Can search forward (forward chaining) to derive

goal or search backward (backward chaining) from the goal.

• Searching for proofs is not more efficient than enumerating models, but in many practical cases, it’s more efficient because we can ignore irrelevant propositions

Page 47: logic agents

47

Resolution

• So far – soundness of inference algorithm– soundness – truth preserving – inference

algorithm derives only entailed sentences – Unsound – makes things up as it goes along

• Completeness of inference algorithms?– Completeness – algorithm can derive any

sentence that is entailed

Page 48: logic agents

48

Resolution

• Wumpus world– agent returns from [2,1] to [1,1], then goes to

[1,2] – stench, but no breeze– Add facts to knowledge base

• R11: B1,2

• R12: B1,2 (P1,1 P2,2 P1,3)

– Same process that led to R10 – we can derive• R13: P2,2

• R14: P1,3

Page 49: logic agents

49

Resolution– Biconditional elimination to R3, …

• R15: P1,1 P2,2 P3,1– Resolution

• P2,2 in R13 resolves with P2,2 in R15 to give R16: P1,1 P3,1

In other words, if there Is a pit in one of [1,1],[2,2],[3,1], and it is not in [2,2] then it is in [1,1] or [3,1]

• Similarly, P1,1 in R1 resolves with P1,1 in R16 to give R17: P3,1

In other words, if there Is a pit of [1,1] or [3,1], and it is not in [1,1] then it is in [3,1]

Page 50: logic agents

50

Resolution

• We used the unit resolution inference rule above

li and m are complimentary literals (one is the negative of the other)

• A Full Resolution Rule is a generalization of this rule

Page 51: logic agents

51

Resolution

• For clauses of length two:

Page 52: logic agents

52

Conjunctive normal form

• Conjunctive Normal Form is a disjunction of literals

• Example: literals

(A B C) (B D) ( A) (B C)

clause - Disjunction of literals

Page 53: logic agents

53

Conjunctive normal form• Convert R2 (B1,1 (P1,2 P2,1) into CNF

• Steps– Eliminate , replacing with ()()

(B1,1 (P1,2 P2,1 )) ((P1,2 P2,1 )) B1,1)

– Eliminate , replacing with (B1,1 P1,2 P2,1) ((P1,2 P2,1 )) B1,1)

must appear only in literals

(B1,1 P1,2 P2,1) ((P1,2 P2,1 ) B1,1)

– Distribute

(B1,1P1,2 P2,1) (P1,2 B1,1) ( P2,1B1,1)

• Conjunction of three clauses – CNF form

Page 54: logic agents

54

Resolution Algorithm• To show KB |= , we show (KB ) is

unsatisfiable.• This is a proof by contradiction.

• First convert (KB ) into CNF.

• Then apply resolution rule to resulting clauses.• The process continues until:

– there are no new clauses that can be added (KB does not entail )

– two clauses resolve to yield empty clause (KB entails )

Page 55: logic agents

55

Simple Inference in Wumpus World

• Agent is in [1,1]– KB = R2 R4 = (B1,1 (P1,2 P2,1)) B1,1

– Wish to prove which is, say, P1,2

– Convert KB P1,2 to CNF

Page 56: logic agents

56

Horn Clauses• A disjunction of literals where at most one is

positive( L1,1 Breeze B1,1) is a horn clause( B1,1 P1,2 P2,1)) is not a horn clause

• 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( L1,1 Breeze B1,1) can be written as

(L1,1 Breeze) B1,1

( W1,1 W1,2 ) can be written as ( W1,1 W1,2) False

Page 57: logic agents

57

Horn Clauses

• Inference with Horn clauses can be done through the forward chaining and backward chaining algorithms

Page 58: logic agents

58

Forward Chaining

• Fire any rule whose premises are satisfied in the KB.

• Add its conclusion to the KB until query is found.

Page 59: logic agents

59

Forward Chaining Example

P QL M P

B L M

A P L

A B L

A

B

Page 60: logic agents

60

Forward Chaining Example

Page 61: logic agents

61

Forward Chaining Example

Page 62: logic agents

62

Forward Chaining Example

Page 63: logic agents

63

Forward Chaining Example

Page 64: logic agents

64

Forward Chaining Example

Page 65: logic agents

65

Forward Chaining Example

Page 66: logic agents

66

Forward Chaining Example

Page 67: logic agents

67

Backward Chaining

• Motivation: Need goal-directed reasoning in order to keep from getting overwhelmed with irrelevant consequences

• Main idea:– Work backwards from query q – To prove q:

• Check if q is known already• Prove by backward chaining all premises of some rule

concluding q

Page 68: logic agents

68

Backward Chaining Example

P QL M P

B L M

A P L

A B L

A

B

Page 69: logic agents

69

Backward Chaining Example

Page 70: logic agents

70

Backward Chaining Example

Page 71: logic agents

71

Backward Chaining Example

Page 72: logic agents

72

Backward Chaining Example

Page 73: logic agents

73

Backward Chaining Example

Page 74: logic agents

74

Backward Chaining Example

Page 75: logic agents

75

Backward Chaining Example

Page 76: logic agents

76

Backward Chaining Example

Page 77: logic agents

77

Backward Chaining Example

Page 78: logic agents

78

Backward Chaining Example

Page 79: logic agents

79

Forward Chaining vs. Backward Chaining

• FC is data-driven—it may do lots of work irrelevant to the goal

• BC is goal-driven—appropriate for problem-solving