Intelligent agents Knowledge and reasoning Reflex agent Problem-solving agent Knowledege-based agent xy TDDC65 Artificial intelligence and Lisp Percept Action If I see… …then I do: Peter Dalenius petda@ida.liu.se Department of Computer and Information Science Linköping University A B C D 2 1 1 3 Percept Problem Search knowledge base 2, 3, 1, 4, 4, 1 Action A sequence of actions The Wumpus world The Wumpus world Sensors PIT Actions Stench: Wumpus in this or adjacent square Breeze: Pit in adjacent square Glitter: Gold in this square Bump: Walked into a wall Scream: Wumpus dies PIT Move forward Turn right or left 90° Grab an object Shoot arrow Points PIT Reasoning in the Wumpus world Percept in (1,1) [None, None, None, None, None] 4 Percept in (2,1) [None, Breeze, None, None, None] 3 Reasoning in the Wumpus world Percept in (1,1) [None, None, None, None, None] 4 Percept in (2,1) [None, Breeze, None, None, None] Percept in (1,2) [Stench, None, None, None, None] 3 Pit? 2 1 1 2 Pit? 2 1 Pit? 3 4 awarded Grab gold: +1000 Die: -1000 Shoot arrow: -10 Each action: -1 PIT Pit? 1 2 3 4 1 Reasoning in the Wumpus world Percept in (1,1) [None, None, None, None, None] 4 Percept in (2,1) [None, Breeze, None, None, None] Percept in (1,2) [Stench, None, None, None, None] 3 Percept in (2,2) [None, None, None, None, None] Pit? 2 1 Percept in (2,3) [Stench, Breeze, Glitter, None, None] Knowledge-based agents What do we need? – A way of representing the agents’ (increasing) knowledge about the world. – A way of drawing conclusions from what we already know. We also need to implement this in a computer language at some point! PIT Pit? 1 2 3 4 Logic Logic represents knowledge Logic rules of deduction Knowledge base: R R⇒S Representation deduce or infer entails semantics Formalize, deduce and reinterpret Sentences Representation Sentences semantics a set ∆ of true facts about the world and a statement A, is there a good argument for believing that A is true based on the facts in ∆? Logic is about making distinctions between good arguments and bad arguments, and the different criteria that may be used in making this distinction. Given World Aspects of the real world follows Aspects of the real world Components of a logic Syntax Conclusion: S – Rules that specify what a well formed sentence (or formula) looks like – Tells us how to build a knowledge base Semantics World formalize Description of the world: • It is raining. • After rain comes sunshine. reinterpret Conclusion: • There will be sunshine. – Rules that specify what a sentence (or formula) really means in the world – Tells us what the knowledge base means 2 Syntax of propositional logic The meaning of symbols What does a well formed sentence (or formula) look like? Sentence Atom Symbol ComplexSentence → Atom | ComplexSentence → True | False | Symbol →P|Q|R|… → ¬Sentence | (Sentence ∧ Sentence) | (Sentence ∨ Sentence) | (Sentence ⇒ Sentence) | (Sentence ⇔ Sentence) The symbols denote atomic statements that says something about the world. ¬P negation (means the opposite of P) P ∧ Q conjunction (means both P and Q) P ∨ Q disjunction (means P or Q, or possibly both) P ⇒ Q implication (means ”if P then Q”) P ⇔ Q biconditional (means “P if and only if Q”) Interpretations in propositional logic Semantics of propositional logic An What is the value of a sentence, given an interpretation m? interpretation assigns a truth value (true or false) to each propositional symbol in the knowledge base. Examples: m1 = {P = false, Q = false, R = true} m2 = {S1 = true, S2 = true} Example • The constans True and False have their obvious values. • Any symbol has the value indicated by the interpretation. • The value of complex sentences are computed according to the following truth table: P Q ¬P P∧Q F F T T F T F T T T F F F F F T F T T T T T F T T F F T Example Is ((¬P ∨ Q) ⇒ P) a well formed sentence? What is the value of ((¬P ∨ Q) ⇒ P)? ((¬P ∨ Q) ⇒ P) SENTENCE ⇒ SENTENCE (¬P ∨ Q) SENTENCE ∨ SENTENCE ¬P Q ¬ SENTENCE ATOM P P∨Q P⇒Q P⇔Q P ATOM There are four possible interpretations, corresponding to the rows in the truth table. P Q ¬P ∨ Q ¬P ∨ Q ⇒ P F F T T F T F T T T F T F F T T The value of the sentence (or formula) in each of the four interpretations can be found in the rightmost column of the truth table. ATOM 3 Properties of sentences A sentence is satisfiable if it is true in some interpretation. An interpretation where a sentence is true is called a model for that sentence. A sentence is valid if it is true in all possible models. Such a sentence is called a tautology. A contradiction is a sentence with no models. Two sentences A and B are logically equivalent (A ≡ B) if they have the same values in all possible interpretations. All of these properties can be verified with a truth table. Some standard equivalences (P ∧ Q) ≡ (Q ∧ P) (P ∨ Q) ≡ (Q ∨ P) ((P ∧ Q) ∧ R) ≡ (P ∧ (Q ∧ R)) ((P ∨ Q) ∨ R) ≡ (P ∨ (Q ∨ R)) ¬(¬P) ≡ P (P ⇒ Q) ≡ (¬P ∨ Q) ¬(P ∧ Q) ≡ (¬Q ∨ ¬P) ¬(P ∨ Q) ≡ (¬Q ∧ ¬P) commutativity commutativity associativity associativity double negation implication elimination de Morgans’s law de Morgans’s law Equivalence can be proved by rewriting the sentence using these rules. Entailment Example If If we know that P ∧ Q is true, a logical consequence is that P must be true, so P ∧ Q╞ P. Is this a correct way of reasoning? a sentence B is a logical consequence of a sentence A, we say that A entails B (A╞ B). The formal definition of entailment is that all models of A should also be models of B. (I.e. in all interpretations where A is true, B is also true.) Informally we can say that the truth of B lies hidden in A. P Q F F T T F T F T P∧Q F F F T We can generalize this and let P and Q be arbitrary sentences, not just symbols. We have found an inference rule (a reasoning pattern) called and-elimination. Some inference rules Proof systems It can be quite boring to prove entailment using truth tables, so instead we try to use reasoning patterns called inference rules. A Modus ponens A A⇒B B And-elimination A∧B A∧B A B And-introduction A B A∧B In all interpretations where P ∧ Q is true (only the last row) P is also true, so we have proven that P ∧ Q╞ P. proof system PS is a set of inference rules. A proof is a sequence of sentences where each sentence can be inferred from a previous sentence using one of the inference rules. A ├PS B means that there exists a proof starting with A (which might be a set of sentences), ending with B, using the proof system PS. 4 Example So, what is a good argument? Prove that {P ∧ Q, Q ⇒ R} ├PS P ∧ R where PS is a proof system containing the inference rules from the previous slide. (1) P ∧ Q (2) Q ⇒ R (3) Q (4) R (5) P (6) P ∧ R We can try to prove base: KB╞ S,Knowledge which involves R truth tables or reasoning R ⇒ Swith interpretations, models, etc. Representation (Semantic proof) And-elimination on (1) Logic rules of deduction deduce or infer Or we can try to find a proof KB├ S, using some well known Conclusion: Sproof system. (Syntactic proof) Modus ponens on (2) and (3) World And-elimination on (1) And-introduction on (5) and (4) Properties of proof systems for any set of sentences ∆ and any sentence A, ∆├PS A implies that ∆╞ A, then the proof system PS is sound. If – You can only prove things that are logical consequences. for any set of sentences ∆ and any sentence A, whenever ∆╞ A there exists a proof of A from ∆ using PS, then PS is complete. If formalize reinterpret Description of the world: • It is raining. • After rain comes sunshine. Conclusion: • There will be sunshine. Resolution Proof system with only one rule Sound and complete Fairly easy to implement in a computer Drawback is you have to rewrite all formulas to fit the rule – All logical consequences can be proven. The resolution rule Rewriting to CNF p 1∨ p 2 ∨ … ∨ p n ∨ r p1∨ p2 ∨ … ∨ pn ∨ q1∨ q2 ∨ … ∨ qn pi and qi are literals, i.e. symbols or negated symbols. This type of formulas are called clauses. We have to rewrite all formulas to conjunctions of clauses. This format is called CNF (conjunctive normal form). Rewrite ⇔ and ⇒ (P ⇔ Q) ≡ (P ⇒ Q) ∧ (Q ⇒ P) (P ⇒ Q) ≡ ¬P ∨ Q q1∨ q2 ∨ … ∨ qn ∨ ¬r Move ¬ inwards ¬ ¬P ≡ P ¬(P ∨ Q) ≡ (¬P ∧ ¬Q) ¬(P ∧ Q) ≡ (¬P ∨ ¬Q) double negation De Morgan De Morgan Distributivity laws P ∧ (Q ∨ R) ≡ (P ∧ Q) ∨ (P ∧ R) P ∨ (Q ∧ R) ≡ (P ∨ Q) ∧ (P ∨ R) 5 How to use resolution Example 1 The starting point is that we want to prove ∆├ A using resolution. This is the same as ∆ ∪ {¬A}├ ⊥ Convert all formulas to CNF and separate the clauses. Using the resolution rule, try to prove ⊥ (contradiction). We Example 1 Example 2 want to prove (P ⇔ Q) ⇒ P ├ P ∨ Q This is the same as proving {(P ⇔ Q) ⇒ P, ¬(P ∨ Q)} ├ ⊥ We now convert formulas to CNF: {P ∨ Q, ¬P, ¬Q} ├ ⊥ We P∨Q ¬P ¬Q Q Example 2 (¬A ∧ B) ⇔ C ≡ ((¬A ∧ B) ⇒ C) ∧ (C ⇒ (¬A ∧ B)) ≡ (¬(¬A ∧ B) ∨ C) ∧ (¬C ∨ (¬A ∧ B)) ≡ ((¬¬A ∨ ¬B) ∨ C) ∧ (¬C ∨ (¬A ∧ B)) ≡ ((A ∨ ¬B) ∨ C) ∧ (¬C ∨ ¬A) ∧ (¬C ∨ B) want to prove (¬A ∧ B) ⇔ C ├ ¬C This is the same as proving {(¬A ∧ B) ⇔ C, C} ├ ⊥ We now convert the formulas to CNF… Example 2 A ∨ ¬B ∨ C ¬C ∨ ¬A ¬C ∨ B C ¬B ¬C The same formula in clause form: {A ∨ ¬B ∨ C, ¬C ∨ ¬A, ¬C ∨ B} 6