Logical Agents & Propositional Logic PDF

Document Details

FirstRateEternity2873

Uploaded by FirstRateEternity2873

Alexandria University

2024

Tags

logical agents propositional logic inference rules knowledge representation

Summary

This document provides an overview of logical agents, propositional logic, and their application to a world scenario model, including methods like forward and backward chaining. The summary covers basic concepts like syntax, semantics, and inference.

Full Transcript

Logical Agents 27 February 2024 Outline 2 Knowledge-based agents Logic in general—models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem proving – forw...

Logical Agents 27 February 2024 Outline 2 Knowledge-based agents Logic in general—models and entailment Propositional (Boolean) logic Equivalence, validity, satisfiability Inference rules and theorem proving – forward chaining – backward chaining – resolution 3 knowledge-based agents Knowledge-Based Agent 4 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 Agents can be viewed at the knowledge level i.e., what they know, regardless of how implemented Or at the implementation level i.e., data structures in KB and algorithms that manipulate them A Simple Knowledge-Based Agent 5 function KB-AGENT( percept) returns an action static: KB, a knowledge base t, a counter, initially 0, indicating time TELL(KB, MAKE-PERCEPT- SENTENCE( percept, t)) action ← ASK(KB, MAKE-ACTION-QUERY(t)) TELL(KB, MAKE- ACTION-SENTENCE(action, t)) t←t+ 1 return action 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 6 exampl e Hunt the Wumpus 7 Computer game from 1972 Wumpus World PEAS Description 8 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 Actuators Left turn, Right turn, Forward, Grab, Wumpus World Characterization 9 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 Exploring a Wumpus World 1 0 Exploring a Wumpus World 1 1 Exploring a Wumpus World 1 2 Exploring a Wumpus World 1 3 Exploring a Wumpus World 1 4 Exploring a Wumpus World 1 5 Exploring a Wumpus World 1 6 2 0 logic in general Logic in General 2 1 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 Entailment 2 2 Entailment means that one thing follows from another: KB s α Knowledge base K B entails sentence α if and only if α is true in all worlds where K B is true E.g., the KB containing “the Ravens won” and “the Jays won” entails “the Ravens won or the Jays won” E.g., x + y = 4 entails 4 = x + y Entailment is a relationship between sentences (i.e., syntax) that is based on semantics Note: brains process syntax (of some sort) Models 2 3 Logicians typically think in terms of models, which are formally structured worlds with respect to which truth can be evaluated We say m is a model of a sentence α if α is true in m M (α) is the set of all models of α. K B s α if and only if M ( K B ) ⊆ M (α) E.g. K B = Ravens won and Jays won α = Ravens won Entailment in the Wumpus World 2 4 Situation after detecting nothing in [1,1], moving right, breeze in [2,1] Consider possible models for all ?, assuming only pits 3 Boolean choices =. 8 possible models Possible Wumpus Models 2 5 Valid Wumpus Models 2 6 K B = wumpus-world rules + observations Entailment 2 7 K B = wumpus-world rules + observations α 1 = “[1,2] is safe”, K B s α 1 , proved by model checking Valid Wumpus Models 2 8 K B = wumpus-world rules + observations Not Entailed 2 9 K B = wumpus-world rules + observations α 2 = “[2,2] is safe”, K B s/ α2 Inference 3 0 K B › i α = sentence α can be derived from K B by procedure i Consequences of K B are a haystack; α is a needle. Entailment = needle in haystack; inference = finding it Soundness: i is sound if whenever K B › i α, it is also true that KB s α Completeness: i is complete if whenever K B s α, it is also true that K B ›i α 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 K B. 3 1 propositional logic Propositional Logic: Syntax 3 2 Propositional logic is the simplest logic—illustrates basic ideas The proposition symbols P 1 , P 2 etc are sentences If P is a sentence, ¬P is a sentence (negation) If P 1 and P 2 are sentences, P 1 ∧ P 2 is a sentence (conjunction) If P 1 and P 2 are sentences, P 1 ∨ P 2 is a sentence (disjunction) If P 1 and P 2 are sentences, P 1 =. P 2 is a sentence (implication) If P 1 and P 2 are sentences, P 1 ⇔ P 2 is a sentence (biconditional) Propositional Logic: Semantics 3 3 Each model specifies true/false for each proposition P symbol P 2,2 P 3,1 E. 1,2 g. fals tru fals e e e (with these symbols, 8 possible models, can be enumerated automatically) Rules for evaluating truth with respect to a model m: ¬P is true iff P is false P 1 ∧ P 2 is true iff P1 is true and P2 is true P 1 ∨ P 2 is true iff P1 is true or P2 is true P 1 =. is true iff P1 is false or P2 is P2 true Simple i.e., recursiveis false iff process P1 evaluatesisan true and arbitrary P2 is sentence, e.g., false P ⇔ P is true iff P 1 =. P 2 is true and P 2 =. P1 is ¬P1,21 ∧ (P 22, 2 ∨ P 3 , 1 ) = true ∧ (false ∨ true) = true ∧ true true = true Truth Tables for Connectives 3 4 P Q ¬P P ∧Q P ∨Q P. Q P ⇔Q false 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 Wumpus World Sentences 3 5 Let P i , j be true if there is a pit in [i, j] – observation R 1 ∶ ¬P1,1 Let B i , j be true if there is a breeze in [i, j]. “Pits – rulecause R 3 ∶ breezes in adjacent squares” ⇔ (P 1,1 ∨ P 2,2 ∨ – observation B2 , 1 R 4 ∶ ¬B1,1 – rule R 2 ∶ B 1 , 1PR3,1 – observation ) 2,1 5⇔∶ B(P 1 , 2 ∨ P 2 , 1 ) What can we infer about P 1 , 2 , P 2 , 1 , P 2 , 2 , etc.? Truth Tables for Inference 3 6 B 1,1 B 2,1 P 1,1 P 1,2 P 2,1 P 2,2 P 3,1 R1 R2 R3 R4 R5 KB fals fals fals fals fals fals fals tru tru tru tru fals fals e e e e e e e e e e e e e fals fals fals fals fals fals tru tru tru fals tru fals fals e e e e e e e e e e e e e ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ false true false false false false false true true false true true false fals tru fals fals fals fals tru tru tru tru tru tru tru e e e e e e e e e e e e e fals tru fals fals fals tru fals tru tru tru tru tru tru e e e e e e e e e e e e e fals tru fals fals fals tru tru tru tru tru tru tru tru e Enumerate e e rows e (different e e assignments e e toe e e e e false true P symbols false i,j) false true false false true false false true true false ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ Check true trueif true rules true are satisfied true true(R i )true false true true false true false Valid model ( K B ) if all rules satisfied Inference by Enumeration 3 7 Depth-first enumeration of all models is sound and complete function TT-ENTAILS?(KB, α) returns true or false inputs: KB, the knowledge base, a sentence in propositional logic α, the query, a sentence in propositional logic symbols ← a list of the proposition symbols in KB and α return TT-CHECK-ALL(KB, α, symbols, [ ]) function TT-CHECK-ALL(KB, α, symbols, model) returns true or false if EMPTY?(symbols) then if PL-TRUE?(KB, model) then return PL-T RUE ?(α, model) else return true else do P ← FIRST(symbols); rest ← REST(symbols) return TT-CHECK-ALL(KB, α, rest, EXTEND(P, true, model )) and TT-CHECK-ALL(KB, α, rest, EXTEND(P, false, model )) 3 8 equivalence, validity, satisfiability Logical Equivalence 3 9 Two sentences are logically equivalent iff true in same models: α ≡ β if and only if α s β and β s α (α ∧ β) ≡ (β ∧ α) commutativity of ∧ (α ∨ β) ≡ (β ∨ α) commutativity of ∨ ((α ∧ β) ∧ ≡ (α ∧ (β ∧ γ)) associativity of ∧ γ) ((α ∨ β) ∨ ≡ (α ∨ (β ∨ γ)) associativity of ∨ γ) ¬(¬α) ≡α double-negation elimination (α =. β) ≡(¬β =. ¬α) contraposition (α =. β) ≡(¬α ∨ β) implication elimination (α ⇔ β) ≡((α =. β) ∧ (β =. α)) biconditional elimination ¬(α ∧ β) ≡ (¬α ∨ ¬β) De Morgan ¬(α ∨ β) ≡ (¬α ∧ ¬β) De Morgan (α ∧ (β ∨ ≡ ((α ∧ β) ∨ (α ∧ γ)) distributivity of ∧ over ∨ γ)) (α ∨ (β ∧ ≡ ((α ∨ β) ∧ (α ∨ γ)) distributivity of ∨ over ∧ Validity and Satisfi ability 4 0 A sentence is valid if it is true in all models, e.g., True, A ∨ ¬A, A =. A, ( A ∧ ( A =. B)) =. B 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: K B s α if and only if ( K B ∧ ¬α) is unsatisfiable i.e., prove α by reductio ad absurdum 4 1 inferen ce Proof Methods 4 2 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 alg. – Typically require translation of sentences into a normal form Model checking – truth table enumeration (always exponential in n) – improved backtracking – heuristic search in model space (sound but incomplete) e.g., min-conflicts-like hill- climbing algorithms Forward and Backward Chaining 4 3 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 Example 4 4 Idea: fire any rule whose premises are satisfied in the K B , add its conclusion to the K B , until query is found P =. Q L B ∧ ∧ M=.=M. P L A ∧ =. L P A ∧ =. L B A B 4 5 forward chaining Forward Chaining 4 6 Start with given proposition symbols (atomic sentence) e.g., A and B Iteratively try to infer truth of additional proposition symbols e.g., A ∧ B =. C , therefor we establish C is true Continue until – no more inference can be carried out, or – goal is reached Forward Chaining Example 4 7 Given P =. Q L B ∧ ∧ M=. M L. P = A ∧ =. L P A ∧ =. L B A B Agenda: A, B Annotate horn clauses with number of premises Forward Chaining Example 4 8 Process agenda item A Decrease count for horn clauses in which A is premise Forward Chaining Example 4 9 Process agenda item B Decrease count for horn clauses in which B is premise A ∧ B =. L has now fulfilled premise Add L to agenda Forward Chaining Example 5 0 Process agenda item L Decrease count for horn clauses in which L is premise B ∧ L =. M has now fulfilled premise Add M to agenda Forward Chaining Example 5 1 Process agenda item M Decrease count for horn clauses in which M is premise L ∧ M =. P has now fulfilled premise Add P to agenda Forward Chaining Example 5 2 Process agenda item P Decrease count for horn clauses in which P is premise P =. Q has now fulfilled premise Add Q to agenda A ∧ P =. L has now fulfilled premise Forward Chaining Example 5 3 Process agenda item P Decrease count for horn clauses in which P is premise P =. Q has now fulfilled premise Add Q to agenda A ∧ P =. L has now fulfilled premise But L is already inferred Forward Chaining Example 5 4 Process agenda item Q Q is inferred Done Forward Chaining Algorithm 5 5 function PL-FC-ENTAILS?(KB, q) returns true or false inputs: KB, the knowledge base, a set of propositional Horn clauses q, the query, a proposition symbol local variables: count, a table, indexed by clause, init. number of premises inferred, a table, indexed by symbol, each entry initially false agenda, a list of symbols, initially the symbols known in KB while agenda is not empty do p ← POP(agenda) unless inferred[p] do inferred[p] ← true for each Horn clause c in whose premise p appears do decrement count[c] if count[c] = 0 then do if HEAD[c] = q then return true PUSH(HEAD[c], agenda) return false 5 6 backward chaining Backward Chaining 5 7 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 Backward Chaining Example 5 8 A and B are known to be true Q needs to be proven Backward Chaining Example 5 9 Current goal: Q Q can be inferred by P =. Q P needs to be proven Backward Chaining Example 6 0 Current goal: P P can be inferred by L ∧ M =. P L and M need to be proven Backward Chaining Example 6 1 Current goal: L L can be inferred by A ∧ P =. L A is already true P is already a goal. repeated subgoal Backward Chaining Example 6 2 Current goal: L Backward Chaining Example 6 3 Current goal: L L can be inferred by A ∧ B =. L Both are true Backward Chaining Example 6 4 Current goal: L L can be inferred by A ∧ B =. L Both are true. L is true Backward Chaining Example 6 5 Current goal: M Backward Chaining Example 6 6 Current goal: M M can be inferred by B ∧ L =. M Backward Chaining Example 6 7 Current goal: M M can be inferred by B ∧ L =. M Both are true. M is true Backward Chaining Example 6 8 Current goal: P P can be inferred by L ∧ M =. P Both are true. P is true Backward Chaining Example 6 9 Current goal: Q Q can be inferred by P =. Q P is true. Q is true Forward vs. Backward Chaining 7 0 FC is data-driven, cf. 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 7 1 resolutio n Resolution 7 2 Conjunctive Normal Form (CNF— universal) conjunction of−\−−−−−−−−−−−−−−−−−−− disjunctions −−−− of −−−−−−− literals clauses −−−−−−−−−−−−−−−−−−−−− −−−−−−−−−− E.g., ( A −∨ −−−¬B) −−−−−−−∧ −−−(−v− B−−− ∨ −−−¬C −−−−−−∨−−¬D) −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− Resolution inference −−−−−−−−−−−−rule −−−−−−− (for −−−−−−− CNF): −−−− complete for propositional −logic - l1 ∨ ⋯ ∨ lk , m1 ∨ ⋯ ∨ m n l 1 ∨ ⋯ ∨ l i−1 ∨ l i+1 ∨ ⋯ ∨ l k ∨ m 1 ∨ ⋯ ∨ m j−1 ∨ m j+1 ∨ ⋯ ∨ mn where l i and m j are complementary literals. E.g., P 1,3 ∨ P 2,2 , ¬P2,2 P 1,3 Resolution is sound and complete for propositional logic Wampus World 7 3 Rules such as: “If breeze, then a pit adjacent.“ B 1 ,1 ⇔ (P 1,2 ∨ P 2,1 ) Conversion to CNF 7 4 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 ∨ P2 , 1 ) =. B1 , 1 ) 2. Eliminate. , replacing α. β with ¬α ∨ β. (¬B1,1 ∨ P 1 , 2 ∨ P 2 , 1 ) ∧ (¬(P1,2 ∨ P 2 , 1 ) ∨ B 1 , 1 ) 3. Move ¬ inwards using de Morgan’s rules and double- negation: (¬B1,1 ∨ P 1 , 2 ∨ P 2 , 1 ) ∧ ((¬P1,2 ∧ ¬P2,1) ∨ B 1 , 1 ) 4. Apply distributivity law (∨ over ∧) and flatten: (¬B1,1 ∨ P 1 , 2 ∨ P 2 , 1 ) ∧ (¬P1,2 ∨ B 1 , 1 ) ∧ (¬P2,1 ∨ B1 , 1 ) Resolution Example 7 5 K B = (B 1 , 1 ⇔ (P 1 , 2 ∨ P 2 , 1 )) reformulated as: (¬B1,1 ∨ P 1 , 2 ∨ P 2 , 1 ) ∧ (¬P1,2 ∨ B 1 , 1 ) ∧ (¬P2,1 ∨ B1 , 1 ) Observation: ¬B1,1 Goal: disprove: α = ¬P1,2 (we add P 1 , 2 to the KB and check for contraction) ¬P1,2 ∨ ¬B1,1 Resolution B 1,1 ¬P1,2 Resolutio n ¬P1,2 P 1,2 fals e Resolution Example 7 7 In practice: all resolvable pairs of clauses are combined Resolution Algorithm 7 8 Proof by contradiction, i.e., show K B ∧ ¬α unsatisfiable function PL-RESOLUTION(KB, α) returns true or false inputs: KB, the knowledge base, a sentence in propositional logic α, the query, a sentence in propositional logic clauses ← the set of clauses in the C N F representation of K B ∧ ¬α new ← { } loop do for each C i , C j in clauses do resolvents ← PL-R ESOLVE (C i , C j ) if resolvents contains the empty clause then return true new ← new ∪ resolvents if new ⊆ clauses then return false clauses ← clauses ∪ new Logical Agent 7 9 Logical agent for Wumpus world explores actions – observe glitter → done – unexplored safe spot → plan route to it – if Wampus in possible spot → shoot arrow – take a risk to go possibly risky spot Propositional logic to infer state of the world Heuristic search to decide which action to take Summary 8 0 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 – soundess: derivations produce only entailed sentences – completeness: derivations can produce all entailed sentences Wumpus world requires the ability to represent partial and negated information, inference to determine state of the world, etc. Forward, backward chaining are linear-time, complete for Horn

Use Quizgecko on...
Browser
Browser