1 CS 461 ARTIFICIAL INTELLIGENCE Computer Science Department

  • Slides: 71
Download presentation
1 CS 461: ARTIFICIAL INTELLIGENCE Computer Science Department Lecture 7: Logical Agent

1 CS 461: ARTIFICIAL INTELLIGENCE Computer Science Department Lecture 7: Logical Agent

Outline Knowledge-based agents Wumpus world Logic in general - models and entailment Propositional (Boolean)

Outline Knowledge-based agents Wumpus world Logic in general - models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem proving � forward chaining � backward chaining � resolution 14 -Dec-21 Computer Science Department

Knowledge bases § § § Knowledge base: set of sentences. Each sentence is expressed

Knowledge bases § § § Knowledge base: set of sentences. Each sentence is expressed in a language called a knowledge representation language. Sentence: a sentence represents some assertion about the world. Inference: Process of deriving new sentences from old ones. 14 -Dec-21 Computer Science Department

Declarative vs. procedural approach üDeclarative approach is an approach to system building that consists

Declarative vs. procedural approach üDeclarative approach is an approach to system building that consists in expressing the knowledge of the environment in the form of sentences using a representation language. ü Procedural approach encodes desired behaviors directly as a program code. 14 -Dec-21 Computer Science Department

Wumpus World PEAS description Performance measure � gold +1000, death -1000 � -1 per

Wumpus World PEAS description Performance measure � gold +1000, death -1000 � -1 per step, -10 for using the 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: Stench, Breeze (for movement), Glitter (gold), Bump (hit), Scream Actuators: Left turn, Right turn, Forward, Grab, Release, Shoot 14 -Dec-21 Computer Science Department

Wumpus world characterization Fully Observable No – only local perception Deterministic Yes – outcomes

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 14 -Dec-21 Computer Science Department

Exploring Wumpus World 7 A 14 -Dec-21 Computer Science Department

Exploring Wumpus World 7 A 14 -Dec-21 Computer Science Department

8 Exploring Wumpus World Ok because: Haven’t fallen into a pit. Haven’t been eaten

8 Exploring Wumpus World Ok because: Haven’t fallen into a pit. Haven’t been eaten by a Wumpus. ok 14 -Dec-21 A Computer Science Department

9 Exploring Wumpus World OK since no Stench, no. OK Breeze, neighbors are safe

9 Exploring Wumpus World OK since no Stench, no. OK Breeze, neighbors are safe (OK). OK OK A 14 -Dec-21 Computer Science Department

10 Exploring Wumpus World We move and smell a stench. OK stench OK 14

10 Exploring Wumpus World We move and smell a stench. OK stench OK 14 -Dec-21 A OK Computer Science Department

Knoweldge Representation & Reasoning 11 Exploring Wumpus World We can W? infer the following.

Knoweldge Representation & Reasoning 11 Exploring Wumpus World We can W? infer the following. Note: square (1, 1) remains OK. OK stench W? OK OK 14 -Dec-21 A Computer Science Department

12 Exploring Wumpus World Move and feel a breeze W? What can we conclude?

12 Exploring Wumpus World Move and feel a breeze W? What can we conclude? OK stench W? OK OK breeze 14 -Dec-21 A Computer Science Department

13 Exploring Wumpus World W? can the 2, 2 square But, really have either

13 Exploring Wumpus World W? can the 2, 2 square But, really have either a Wumpus or a pit? OK stench P? W? OK OK NO! breeze P? A And what about the other P? and W? squares

14 Exploring Wumpus World W OK stench P? W? OK OK breeze 14 -Dec-21

14 Exploring Wumpus World W OK stench P? W? OK OK breeze 14 -Dec-21 P A Computer Science Department

15 Exploring Wumpus World W OK OK stench OK OK P A breeze 14

15 Exploring Wumpus World W OK OK stench OK OK P A breeze 14 -Dec-21 Computer Science Department

16 Exploring Wumpus World … W And the exploration continues onward until the gold

16 Exploring Wumpus World … W And the exploration continues onward until the gold is found. OK … OK A A Breeze OK OK P Stench 14 -Dec-21 Computer Science Department

A tight spot 17 Breeze in (1, 2) and (2, 1) no safe actions.

A tight spot 17 Breeze in (1, 2) and (2, 1) no safe actions. Assuming pits uniformly distributed, (2, 2) is most likely to have a pit. 14 -Dec-21 Computer Science Department

Logic in general Logics are formal languages for representing information such that conclusions can

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 possible world. E. g. , the language of arithmetic � � x+2 ≥ y is a sentence; x 2+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 14 -Dec-21 Computer Science Department

Entailment means that one thing follows from another: KB ╞ α Knowledge base KB

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 14 -Dec-21 Computer Science Department

Models Logicians typically think in terms of models, which are formally structured worlds with

Models Logicians typically think in terms of models, which are formally structured worlds with respect to which truth can be evaluated We will use the term model in place of possible word We say m is a model of a sentence α if α is true in 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 14 -Dec-21 Computer Science Department

Entailment in the wumpus world Situation after detecting nothing in [1, 1], moving right,

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 14 -Dec-21 Computer Science Department

Wumpus models 14 -Dec-21 Computer Science Department

Wumpus models 14 -Dec-21 Computer Science Department

Wumpus models KB = wumpus-world rules + observations 14 -Dec-21 Computer Science Department

Wumpus models KB = wumpus-world rules + observations 14 -Dec-21 Computer Science Department

Wumpus models KB = wumpus-world rules + observations α 1 = "[1, 2] is

Wumpus models KB = wumpus-world rules + observations α 1 = "[1, 2] is safe", KB ╞ α 1, proved by model checking 14 -Dec-21 Computer Science Department

Wumpus models KB = wumpus-world rules + observations 14 -Dec-21 Computer Science Department

Wumpus models KB = wumpus-world rules + observations 14 -Dec-21 Computer Science Department

Wumpus models KB = wumpus-world rules + observations α 2 = "[2, 2] is

Wumpus models KB = wumpus-world rules + observations α 2 = "[2, 2] is safe", KB ╞ α 2 14 -Dec-21 Computer Science Department

Propositional logic: Syntax Propositional logic is the simplest logic – illustrates basic ideas The

Propositional logic: Syntax Propositional logic is the simplest logic – illustrates basic ideas The proposition symbols P 1, P 2 etc are sentences � If S is a sentence, S is a sentence (negation) � If S 1 and S 2 are sentences, S 1 S 2 is a sentence (conjunction) � If S 1 and S 2 are sentences, S 1 S 2 is a sentence (disjunction) � If S 1 and S 2 are sentences, S 1 S 2 is a sentence (implication) � If S 1 and S 2 are sentences, S 1 S 2 is a sentence (biconditional) 14 -Dec-21 Computer Science Department

Propositional logic: Semantics Each model specifies true/false for each proposition symbol E. g. P

Propositional logic: Semantics Each model specifies true/false for each proposition symbol E. g. P 1, 2 false P 2, 2 true P 3, 1 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 1 S 2 is false iff S 1 S 2 is true iff S is false S 1 is true and S 2 is true S 1 is true or S 2 is true S 1 is false or S 2 is true S 1 is true and S 2 is false S 1 S 2 is true and S 2 S 1 is true Simple recursive process evaluates an arbitrary sentence, e. g. , P 1, 2 (P 2, 2 P 3, 1) = true (true false) = true 14 -Dec-21 Computer Science Department

Truth tables for connectives 14 -Dec-21 Computer Science Department

Truth tables for connectives 14 -Dec-21 Computer Science Department

Wumpus world sentences Let Pi, j be true if there is a pit in

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]. R 1: P 1, 1 R 4: B 1, 1 R 5: B 2, 1 "Pits cause breezes in adjacent squares" R 2: B 1, 1 (P 1, 2 P 2, 1) R 3: B 2, 1 (P 1, 1 P 2, 2 P 3, 1) KB : R 1 R 2 R 3 R 4 R 5 14 -Dec-21 Computer Science Department

Truth tables for inference True occur just in 3 rows of the 128 rows.

Truth tables for inference True occur just in 3 rows of the 128 rows. In all 3, P 1, 2 is false => there is no pits in [1, 2] 14 -Dec-21 Computer Science Department

Logical equivalence Two sentences are logically equivalent iff true in same models: α ≡

Logical equivalence Two sentences are logically equivalent iff true in same models: α ≡ ß iff α╞ β and β╞ α 14 -Dec-21 Computer Science Department

Validity and satisfiability A sentence is valid if it is true in all models,

Validity and satisfiability A sentence is valid if it is true in all models, e. g. , True, 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 14 -Dec-21 Computer Science Department

Proof methods divide into (roughly) two kinds: � Application of inference rules Legitimate (sound)

Proof methods divide into (roughly) two kinds: � 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 � 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 14 -Dec-21 Computer Science Department

Seven Inference Rules for Propositional Logic 1. Modus-Ponens or Implication elimination (From an implication

Seven Inference Rules for Propositional Logic 1. Modus-Ponens or Implication elimination (From an implication and the premise of the implication, you can infer the conclusion) { => , } 2. And-Elimination (From a conjunction, you can infer any of the conjuncts ) 1 2 3… n i 3. And-Introduction (From a list of sentences, you can infer their conjunction) 1 , 2 , 3… n 1 2 3. . n 4. Or-Introduction (From a sentence, you can infer its disjunction) 14 -Dec-21 i 1 2 3. . n Computer Science Department

Seven Inference Rules for Propositional Logic 5. Double-Negation Elimination 6. Unit Resolution (From disjunction,

Seven Inference Rules for Propositional Logic 5. Double-Negation Elimination 6. Unit Resolution (From disjunction, if one is false, then you can infer the other one is true ) , 7. Resolution (Because cannot be true and false in the same time ) , 14 -Dec-21 Computer Science Department

Inference in Wumpus World 37 ØLet Si, j be true if there is a

Inference in Wumpus World 37 ØLet Si, j be true if there is a stench in cell i, j ØLet Bi, j be true if there is a breeze in cell i, j ØLet Wi, j be true if there is a Wumpus in cell i, j Given: 1. ¬B 1, 1 2. B 1, 1 ⇔ (P 1, 2 P 2, 1) Let’s make some inferences: 1. (B 1, 1 ⇒ (P 1, 2 P 2, 1)) ((P 1, 2 P 2, 1) ⇒ B 1, 1 ) (By definition of the biconditional) 2. (P 1, 2 P 2, 1) ⇒ B 1, 1 (And-elimination) 3. ¬B 1, 1 ⇒ ¬(P 1, 2 P 2, 1) (equivalence with contrapositive) 4. ¬(P 1, 2 P 2, 1) (modus ponens) 5. ¬P 1, 2 ¬P 2, 1 (De. Morgan’s rule) 6. ¬P 1, 2 (And Elimination) 14 -Dec-21 Computer Science Department

Inference in Wumpus World 38 Initial KB Percept Sentences S 1, 1 S 2,

Inference in Wumpus World 38 Initial KB Percept Sentences S 1, 1 S 2, 1 S 1, 2 B 1, 1 B 2, 1 B 1, 2 … Some inferences: Apply Modus Ponens to R 1 Add to KB W 1, 1 W W 2, 1 1, 2 Environment Knowledge R 1: S 1, 1 W 2, 1 W 1, 2 R 2: S 2, 1 W 1, 1 W 2, 2 W 3, 1 R 3: B 1, 1 P 2, 1 P 1, 2 R 5: B 1, 2 P 1, 1 P 1, 2 P 2, 2 P 1, 3. . . 14 -Dec-21 Apply to this AND-Elimination Add to KB W 1, 1 W 2, 1 W 1, 2 Computer Science Department

39 Recall that when we were at (2, 1) we could not decide on

39 Recall that when we were at (2, 1) we could not decide on a safe move, so we backtracked, and explored (1, 2), which yielded ¬B 1, 2 ⇔ ¬P 1, 1 ¬P 1, 3 ¬P 2, 2 this yields to ¬P 1, 1 ¬P 1, 3 ¬P 2, 2 and consequently ¬P 1, 1 , ¬P 1, 3 , ¬P 2, 2 • Now we can consider the implications of B 2, 1. 14 -Dec-21 Computer Science Department

40 1. 2. 3. 4. 5. B 2, 1 ⇔ (P 1, 1 P

40 1. 2. 3. 4. 5. B 2, 1 ⇔ (P 1, 1 P 2, 2 P 3, 1) B 2, 1 ⇒ (P 1, 1 P 2, 2 P 3, 1) (biconditional Elimination) P 1, 1 P 2, 2 P 3, 1 (modus ponens) P 1, 1 P 3, 1 (resolution rule because no pit in (2, 2)) P 3, 1 (resolution rule because no pit in (1, 1)) 14 -Dec-21 Computer Science Department

Resolution algorithm Proof by contradiction, i. e. , show KB α unsatisfiabl 14 -Dec-21

Resolution algorithm Proof by contradiction, i. e. , show KB α unsatisfiabl 14 -Dec-21 Computer Science Department

Normal Forms 14 -Dec-21 Computer Science Department

Normal Forms 14 -Dec-21 Computer Science Department

Conversion to CNF B 1, 1 (P 1, 2 P 2, 1) 1. Eliminate

Conversion to CNF B 1, 1 (P 1, 2 P 2, 1) 1. Eliminate , replacing α β with (α β) (β α). (B 1, 1 (P 1, 2 P 2, 1)) ((P 1, 2 P 2, 1) B 1, 1) 2. Eliminate , replacing α β with α β. ( B 1, 1 P 1, 2 P 2, 1) ( (P 1, 2 P 2, 1) B 1, 1) 3. Move inwards using de Morgan's rules and double-negation: ( B 1, 1 P 1, 2 P 2, 1) (( P 1, 2 P 2, 1) B 1, 1) 4. Apply distributivity law ( over ) and flatten: ( B 1, 1 P 1, 2 P 2, 1) ( P 1, 2 B 1, 1) ( P 2, 1 B 1, 1) 14 -Dec-21 Computer Science Department

Resolution example KB = (B 1, 1 (P 1, 2 P 2, 1)) B

Resolution example KB = (B 1, 1 (P 1, 2 P 2, 1)) B 1, 1 α = P 1, 2 14 -Dec-21 Computer Science Department

Forward and backward chaining Horn Form (restricted) KB = conjunction of Horn clauses �

Forward and backward chaining Horn Form (restricted) KB = conjunction of Horn clauses � Horn clause = � proposition symbol; or (conjunction of symbols) symbol E. g. , C (B A) (C D B) Modus Ponens (for Horn Form): complete for Horn KBs α 1 … αn β α 1, … , αn, β Can be used with forward chaining or backward chaining. These algorithms are very natural and run in linear time 14 -Dec-21 Computer Science Department

Forward chaining Idea: fire any rule whose premises are satisfied in the KB, �

Forward chaining Idea: fire any rule whose premises are satisfied in the KB, � add its conclusion to the KB, until query is found 14 -Dec-21 Computer Science Department

Forward chaining algorithm Forward chaining is sound and complete for Horn KB 14 -Dec-21

Forward chaining algorithm Forward chaining is sound and complete for Horn KB 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example 14 -Dec-21 Computer Science Department

Forward chaining example Rule Proved R 2 A, B R 1 A, B, D

Forward chaining example Rule Proved R 2 A, B R 1 A, B, D R 3 A, B, D, C R 5 A, B, D, C, F R 4 A, B, D, C, F, E, G 14 -Dec-21 Computer Science Department

Proof of completeness FC derives every atomic sentence that is entailed by KB 1.

Proof of completeness FC derives every atomic sentence that is entailed by KB 1. 2. 3. FC reaches a fixed point where no new atomic sentences are derived Consider the final state as a model m, assigning true/false to symbols Every clause in the original KB is true in m a 1 … ak b 4. 5. 14 -Dec-21 Hence m is a model of KB If KB╞ q, q is true in every model of KB, including m Computer Science Department

Backward chaining Idea: work backwards from the query q: to prove q by BC,

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 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example 14 -Dec-21 Computer Science Department

Backward chaining example Proved Goal Rule A, B G R 4 A, B F,

Backward chaining example Proved Goal Rule A, B G R 4 A, B F, E, G R 3 A, B D, C, F, E, G R 2 A, B, D C, F, E , G R 1 A, B, D, C F, E, G R 3 A, B, D, C, F E, G R 5 A, B, D, C, F, E G R 4 A, B, D, C, F, E, G 14 -Dec-21 Computer Science Department

Forward vs. backward chaining FC is data-driven, automatic, unconscious processing, � e. g. ,

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 Ph. D program? Complexity of BC can be much less than linear in size of KB 14 -Dec-21 Computer Science Department

Summary Logical agents apply inference to a knowledge base to derive new information and

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 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 14 -Dec-21 Computer Science Department